elm-test
elm-test copied to clipboard
Helpers for Maybe and Result
At NoRedInk, we've found these functions to be useful in testing:
Expect.isJust : (a -> Expectation) -> Maybe a -> Expectation
Expect.isOk : (a -> Expectation) -> Result x a -> Expectation
Expect.isErr : (x -> Expectation) -> Result x a -> Expectation
Thoughts about adding these to elm-test?
Yes, see #67.
Sample:
[ test "Can decode a category with a learning path" <|
\() ->
Json.Decode.decodeString Decoder.category categoryJson
|> Expect.isOk
JSON decoding tests seems like a strong enough motivation for me!
@mgold do I recall correctly that you'd already implemented these somewhere?
I don't think I did but they're trivial to implement. But what about something more specialized?
[ test "Can decode a category with a learning path" <|
\() ->
categoryJson
|> Expect.decodableBy Decoder.category
This gets rid of the call to Json.Decode.decodeString, although we'd either want two versions, for strings and values, with reasonable names. (Or make a strong argument why only one should be supported.)
That's interesting, although I'm not sure if it saves enough to warrant its own function.
The ones in OP save you a whole case-expression, whereas decodableBy only saves you an argument or two.
IMO the originally proposed functions are more useful than one that special cases for JSON. Decoding isn't the only situation where you'd want to check a result. Since these are trivial functions to implement, if you're opposed to having them in elm-test core, maybe we could make elm-test-extra where all the strangely shaped functions live?
Richard, decodableBy would fail if the JSON decoder fails, it's strictly less test code... but Noah's point about wanting a more general function is valid.
This is a reversal of our previous position and I want to make sure that we've thought this through.
We also added Expect.err which does not care about the error value, just that it's not Ok. I think this is useful even if we add the version proposed about. I think the best names -- since we're doing a major version bump -- are to move the existing function to isErr and implement the new one as err, alongside just and ok.
Hello! I'm using isOk and friends a lot in my tests and think they'd make good additions to Expect.
Ignoring the discussion around the more specialised decodableBy, could these get added? I'm happy to make a PR providing the implementation if it will be accepted.