jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • FEST
  • FEST-64

Assertions should be allowed to compare objects using a custom comparator instead of equals()

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: FEST-Assert 1.1
  • Fix Version/s: FEST-Assert 2.0M1
  • Component/s: Assert
  • Labels:
    None

Description

Sometimes you can be interested in comparing objects using a custom comparator instead of using the default equals() method definitions.
For example, let's say you have a list of invoices and want to make sure there isn't more than one invoice with the same date.

You can't use the doesNotHaveDuplicates() method since that uses the equals() method on the objects.

The solution is to specify the comparator to use and then makes assertions (the comparator being used for all chained assertions).

assertThat(firstInvoiceList).usingComparator(DATE_COMPARATOR).doesNotHaveDuplicates();

where DATE_COMPARATOR compares the two invoices just by the date on each.

assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList).contains(someInvoice);

that would make sure that the two lists are equal and it contains someInvoice, but only paying attention to the Payee property for each.

The assertions based a custom comparator only last for the chained assertions, if you write :

assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList).contains(someInvoice);
assertThat(firstInvoiceList).isEqualTo(secondInvoiceList);

then the second line does not use a PAYEE_COMPARATOR comparison strategy (by default new assertions use equals() comparison strategy).

— initial request From Ted Young: ----

Sometimes I'm interested in comparing objects using a custom comparator instead of using the default equals() method definitions. For example, I have a list of invoices and I want to make sure there isn't more than one invoice with the same date.

I can't use the doesNotHaveDuplicates() method since that uses the equals() method on the objects. I'd like to use something like

assertThat(firstInvoiceList).doesNotHaveDuplicates(DATE_COMPARATOR);

where DATE_COMPARATOR compares the two invoices just by the date on each.

This would also apply to methods such as isEqualTo(), so that I can say:

assertThat(firstInvoiceList).isEqualTo(PAYEE_COMPARATOR, secondInvoiceList);

that would make sure that the two lists are equal, but only paying attention to the Payee property for each. An alternative would be:

assertThat(firstInvoiceList).using(PAYEE_COMPARATOR)
                            .isEqualTo(secondInvoiceList);

But that would make it difficult to combine my prior duplicate assert with this one, e.g.:

assertThat(firstInvoiceList).doesNotHaveDuplicates(DATE_COMPARATOR)
                            .isEqualTo(PAYEE_COMPARATOR, secondInvoiceList);

Original report: Issue 148 (Google Code)

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. Hide
    Zip Archive
    mylyn-context.zip
    28/Mar/09 3:17 AM
    13 kB
    Alex Ruiz
    1. XML File
      http%3A%2F%2Fjira.codehaus.org-83013.xml 335 kB
    Download Zip
    Show
    Zip Archive
    mylyn-context.zip
    28/Mar/09 3:17 AM
    13 kB
    Alex Ruiz

Issue Links

relates to

New Feature - A new feature of the product, which has yet to be developed. FEST-252 ignoringCase() feature for String assert

  • Major - Major loss of function.
  • Resolved - A resolution has been taken, and it is awaiting verification by reporter. From here issues are either reopened, or are closed.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Alex Ruiz added a comment - 28/Mar/09 3:17 AM

As much as I think this is a very useful feature, I'm unable to implement it due to generics. Comparator uses generics for type safety, by "generifying" the type of objects a comparator can take. The problem is that ObjectAssert (for example) does not use generics. It works at the Object level. I don't see the point of adding generics to ObjectAssert either. Please see the attached context (Mylyn) to see the compilation error I got. Since I don't see a viable solution for this issue, I'm marking this issue as "Won't Fix".

Show
Alex Ruiz added a comment - 28/Mar/09 3:17 AM As much as I think this is a very useful feature, I'm unable to implement it due to generics. Comparator uses generics for type safety, by "generifying" the type of objects a comparator can take. The problem is that ObjectAssert (for example) does not use generics. It works at the Object level. I don't see the point of adding generics to ObjectAssert either. Please see the attached context (Mylyn) to see the compilation error I got. Since I don't see a viable solution for this issue, I'm marking this issue as "Won't Fix".
Hide
Permalink
Ansgar Konermann added a comment - 23/Jul/09 6:51 PM

Very interesting feature request. I'd like to look into this. Maybe we can find a solution which is at least somewhat satisfactory.

Show
Ansgar Konermann added a comment - 23/Jul/09 6:51 PM Very interesting feature request. I'd like to look into this. Maybe we can find a solution which is at least somewhat satisfactory.
Hide
Permalink
Ansgar Konermann added a comment - 12/Apr/10 1:11 PM - edited

What do you think of an API like this one:

assertThat(firstInvoideList).compareUsing(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .compareUsing(DATE_COMPARATOR).doesNotHaveDuplicates()

The first assertion call isEqualTo would be made using PAYEE_COMPARATOR, the second one using DATE_COMPARATOR.

We could probably achieve this by storing the comparator object inside the Assertion instance returned by assertThat. Or put another way: the idea is to modify the behaviour of the same Assertion instance by storing not only the "actual" value inside the instance, but also a description of how it should perform certain actions (strategy pattern). A Java comparator is basically a strategy to compare objects.

See also comments on FEST-252.

Show
Ansgar Konermann added a comment - 12/Apr/10 1:11 PM - edited What do you think of an API like this one:
assertThat(firstInvoideList).compareUsing(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .compareUsing(DATE_COMPARATOR).doesNotHaveDuplicates()
The first assertion call isEqualTo would be made using PAYEE_COMPARATOR, the second one using DATE_COMPARATOR. We could probably achieve this by storing the comparator object inside the Assertion instance returned by assertThat. Or put another way: the idea is to modify the behaviour of the same Assertion instance by storing not only the "actual" value inside the instance, but also a description of how it should perform certain actions (strategy pattern). A Java comparator is basically a strategy to compare objects. See also comments on FEST-252.
Hide
Permalink
Joel Costigliola added a comment - 13/Nov/11 12:22 PM - edited

I would like to give a try implementing it if Ansgar does not plan to work on it soon (Ansgar ?).

My preferred API could be very similar to Ansgar's, only changing 'compareUsing' to 'usingComparator' or even 'using' to be lighter

assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .usingComparator(DATE_COMPARATOR).doesNotHaveDuplicates()

assertThat(firstInvoiceList).using(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .using(DATE_COMPARATOR).doesNotHaveDuplicates()
Show
Joel Costigliola added a comment - 13/Nov/11 12:22 PM - edited I would like to give a try implementing it if Ansgar does not plan to work on it soon (Ansgar ?). My preferred API could be very similar to Ansgar's, only changing 'compareUsing' to 'usingComparator' or even 'using' to be lighter
assertThat(firstInvoiceList).usingComparator(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .usingComparator(DATE_COMPARATOR).doesNotHaveDuplicates()

assertThat(firstInvoiceList).using(PAYEE_COMPARATOR).isEqualTo(secondInvoiceList)
                            .using(DATE_COMPARATOR).doesNotHaveDuplicates()
Hide
Permalink
Ansgar Konermann added a comment - 13/Nov/11 12:30 PM

Hi Joel,

feel free to go ahead. I'm heads down with my maven drools plugin, so I'd actually be quite happy if you take on this subject.

Best

Ansgar

Show
Ansgar Konermann added a comment - 13/Nov/11 12:30 PM Hi Joel, feel free to go ahead. I'm heads down with my maven drools plugin, so I'd actually be quite happy if you take on this subject. Best Ansgar

People

  • Assignee:
    Joel Costigliola
    Reporter:
    Alex Ruiz
Vote (0)
Watch (0)

Dates

  • Created:
    05/Mar/09 4:38 PM
    Updated:
    07/Mar/12 4:51 PM
    Resolved:
    21/Jan/12 10:44 AM

Time Tracking

Estimated:
2d
Original Estimate - 2 days
Remaining:
2d
Remaining Estimate - 2 days
Logged:
Not Specified
Time Spent - Not Specified
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.