question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Better support comparing lists / iterable objects

See original GitHub issue

There is new feature in 3.4.0 version: assertThat(...).isEqualToComparingFieldByFieldRecursively(...)

but comparing lists is still poor, because we have to use equals(…) method when comparing two objects - it is used by default?

We would like to have methods like:

assertThat(<some iterable>).containsExactlyElementsOf(<some iterable>) - already provided.
assertThat(<some iterable>).containsExactlyElementsOfComparingFieldByField(<some iterable>) - really missing
assertThat(<some iterable>).containsExactlyElementsOfComparingFieldByFieldRecursively(<some iterable>) - missing too 

As suggested by @PascalSchumacher we can use:

assertThat((Object) Arrays.asList("a", "c")).isEqualToComparingFieldByFieldRecursively(Arrays.asList("a", "c"));
assertThat((Object) Arrays.asList("a", "c")).isEqualToComparingFieldByField(Arrays.asList("a", "c"));

But this approach works only for identical Lists - internal state of lists such as private fields other than compared objects will be compared too.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PascalSchumachercommented, Apr 3, 2016

You a welcome to give it a try. 😃

The feature should added to AbstractObjectArrayAssert as well as to AbstractIterableAssert I think.

If you any questions @joel-costigliola and I are there to help. 😃

1reaction
PascalSchumachercommented, Apr 3, 2016

Just noticed that a field/property by field/property comparison of elements can already be done like this:

public static class Bean {
  int i;

  public Bean(int i) {
    this.i = i;
  }
}

@Test
public void test() {
  assertThat(Arrays.asList(new Bean(1), new Bean(2))).usingFieldByFieldElementComparator().containsExactly(new Bean(1), new Bean(2));
}

so we are just missing a usingFieldByFieldElementRecursivelyComparator() method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better support comparing lists / iterable objects #46 - GitHub
method when comparing two objects - it is used by default? We would like to have methods like: assertThat(<some iterable>).
Read more >
How to Compare Two Lists in Python | DigitalOcean
We can use the Python map() function along with functools.reduce() function to compare the data items of two lists. The map() method accepts...
Read more >
Comparing iterables for same content, but not regarding order
Actually, I found this answer, that is just slightly more memory efficient for iterables with duplicates. Nevertheless it should already help ...
Read more >
Field Data Types - Iterable Support Center
This article explains different values and data types for fields and events in Iterable and examples of what those types of data would...
Read more >
Python List Comparison - Scaler Topics
To compare two lists, we can utilize both approaches simultaneously. The map() method would apply the function to each iterable object, and the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found