approvals / ApprovalTests.Java

ApprovalTest verification library for Java
Apache License 2.0
337 stars 74 forks source link

Scrubbers not enabled for verifyXml #132

Open emilybache opened 3 years ago

emilybache commented 3 years ago

I have this exercise where I'm using verifyXml:

github.com/emilybache/Product-Export-Refactoring-Kata

If you look at the 'with_tests' branch in the java version the fourth test 'exportHistory' works and passes with this code:

@Test
    public void exportHistory() throws Exception {
        Collection<Order> orders = Arrays.asList(RecentOrder, OldOrder);
        String xml = XMLExporter.exportHistory(orders);
        String regex = "createdAt='[^']+'";
        xml = xml.replaceFirst(regex, "createdAt='2018-09-20T00:00Z'");
        Approvals.verifyXml(xml);
    }

I would like to be able to use a scrubber like this:

    Approvals.verifyXml(xml, new Options(new RegExScrubber("createdAt='[^']+'", "createdAt='2018-09-20T00:00Z'")));

Unfortunately that code does not work.

LarsEckart commented 3 years ago

I investigated a bit. The problem is that we transform&pretty-print the input string before applying the scrubber and this step changes the ' to ". So by the time we scrub, the pattern doesn't find any matches anymore.

I wrote a quick fix and will discuss it with @isidore on Monday night. :)

emilybache commented 3 years ago

Thankyou! I hadn't realized the regex could be adjusted to make this actually work. I guess what I'd really like is for an xml aware scrubber that you could give an xpath and it would scrub the piece of xml the xpath refers to. Since we're parsing the xml in order to pretty print it, I would have thought this should be possible?