elm-test icon indicating copy to clipboard operation
elm-test copied to clipboard

Helpers for Maybe and Result

Open avh4 opened this issue 8 years ago • 8 comments

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?

avh4 avatar Mar 08 '17 17:03 avh4

Yes, see #67.

mgold avatar Mar 09 '17 01:03 mgold

Sample:

        [ test "Can decode a category with a learning path" <|
            \() ->
                Json.Decode.decodeString Decoder.category categoryJson
                    |> Expect.isOk

rtfeldman avatar Mar 13 '17 05:03 rtfeldman

JSON decoding tests seems like a strong enough motivation for me!

@mgold do I recall correctly that you'd already implemented these somewhere?

rtfeldman avatar Mar 13 '17 05:03 rtfeldman

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.)

mgold avatar Mar 13 '17 05:03 mgold

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.

rtfeldman avatar Mar 13 '17 05:03 rtfeldman

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?

eeue56 avatar Mar 13 '17 09:03 eeue56

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.

mgold avatar Mar 13 '17 15:03 mgold

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.

lpil avatar Sep 27 '17 15:09 lpil