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.

Support matching elements in arrays ignoring fields

See original GitHub issue

Hello Kotest developers!

I was wondering if you were planning on developing a feature for assertions to match list elements by ignoring fields. Here is a sample of that kind of assertings with assertJ

assertThat(underTest.findAll())
    .usingElementComparatorIgnoringFields(ConfiguredTask::id.name)
    .containsExactlyInAnyOrderElementsOf(expectedTasks)

Maybe something similar could be implemented in Kotest?

Thank you!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
sksamuelcommented, Jan 12, 2022

I think this is an excellent idea. And we can provide a few comparators out of the box.

1reaction
pelletier197commented, Jan 3, 2022

I have an idea of how it could work. Right now, Kotest has a few matchers with long names. So to follow library naming, the function should be something like

fun <T, C : Collection<T>> C?.shouldContainExactlyInAnyOrderIgnoringFields(expected: C, ignorePrivateFields: Boolean,    property: KProperty<*>, vararg others: KProperty<*>)

That said this becomes really hard to read, and you need to provide overloads for the different configurations (one without ignorePrivateFiels for instance.

What could be great is to pass an optional object comparor. The default comparator would simply do an object equality, which is the current behaviour (using ==).

Something like this:

fun <T, C : Collection<T>> C?.shouldContainExactlyInAnyOrder(expected: C, comparator: ObjectComparator = ObjectComparator.equality()) 

You could therefore, if you want to verify the same thing ignoring fields, do something like this:

actual.shouldContainExactlyInAnyOrder(expected, comparator = ObjectComparator.ignoringFields(ConfiguredTask::id))
// All other equality configuration could be done in chain, making this easy to configure and super readable.
// Example: ObjectComparator.ignoringFields(ConfiguredTask::id, ConfiguredTask::name).ignoringPrivateFields(true)

I understand this is a rather complex concept to implement, and that it should probably not be part of a single merge request to implement this feature, but this is the best way I personally see this could be implemented.

Also, this approach could maybe help reduce the number of functions in the library and allow for more powerful matching.

Let me know what you think about the idea 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing content of list ignoring some of the fields - java
I have a scenario where I receive a list from a method call and I would like to assert that the list contains...
Read more >
How to ignore empty fields, objects, and arrays using ...
Using the below dataweave function, you can ignore any empty values, objects or arrays that you want! You can update the case v...
Read more >
Array methods - The Modern JavaScript Tutorial
sort() sorts the array in place, changing its element order. It also returns the sorted array, but the returned value is usually ignored,...
Read more >
array_intersect - Manual - PHP
array_intersect handles duplicate items in arrays differently. If there are duplicates in the first array, all matching duplicates will be returned.
Read more >
Work with arrays | BigQuery - Google Cloud
You can also flatten ARRAY type fields of STRUCT values. Querying STRUCT elements in an ARRAY. The following example uses UNNEST with CROSS...
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