Search code examples
haskellhspec

How can I easily express that I don't care about a value of a particular data field?


I was writing tests for my parser, using a method which might not be the best, but has been working for me so far. The tests assumed perfectly defined AST representation for every code block, like so:

(parse "x = 5") `shouldBe` (Block [Assignment [LVar "x"] [Number 5.0]])

However, when I moved to more complex cases, a need for more "fuzzy" verification arised:

(parse "t.x = 5") `shouldBe` (Block [Assignment [LFieldRef (Var "t") (StringLiteral undefined "x")] [Number 5.0]])

I put in undefined in this example to showcase the field I don't want to be compared to the result of parse (It's a source position of a string literal). Right now the only way of fixing that I see is rewriting the code to make use of shouldSatisfy instead of shouldBe, which I'll have to do if I don't find any other solution.


Solution

  • You can write a normalizePosition function which replaces all the position data in your AST with some fixed dummyPosition value, and then use shouldBe against a pattern built from the same dummy value.

    If the AST is very involved, consider writing this normalization using Scrap-Your-Boilerplate.