elm-explorations / test

Write unit and fuzz tests for Elm code.
https://package.elm-lang.org/packages/elm-explorations/test/latest
BSD 3-Clause "New" or "Revised" License
234 stars 40 forks source link

Idea: Better `Char` and `String` Fuzzers #90

Open jonathanjouty opened 5 years ago

jonathanjouty commented 5 years ago

As mentioned in #89, I'm a fan of hedgehog.

hedgehog comes with a useful set of generators for characters and for strings too, such as (as would be written in Elm):

These are great building blocks for larger fuzzers, as the generator for strings has the form (ignoring Range, discussed separately in #89):

string : Fuzzer Char -> Fuzzer String

Thus allowing the user to choose what class of characters to use when generating a ~list~ string. In many cases all printable Unicode characters are invalid input.

Even if you represented valid strings safely using type MyAlphaString = MyAlphaString String. Writing a fuzzer for MyAlphaString is not straightforward.

Would you accept a PR in this direction? or prefer it as a separate package?

drathier commented 5 years ago

I'd like the default to be unicode strings, so you have to make a conscious effort to not support all of unicode. Same argument applies for all fuzzers that generate a small, specifically chosen subset of all possible values. What if we put these in a new package for now, and see how people use them? I bet similar functions for Floats would be useful to some people.

jonathanjouty commented 5 years ago

I'd like the default to be unicode strings, so you have to make a conscious effort to not support all of unicode. Same argument applies for all fuzzers that generate a small, specifically chosen subset of all possible values.

Makes sense, and then to use subsets as as a conscious choice.

What if we put these in a new package for now, and see how people use them?

I've seen this mentioned elsewhere, how do you "see how people use them"? I'm not trying to be unhelpful or contrarian, just genuinely curious as to how that process works. Is it based on number of downloads for a package? Are those stats even available anywhere?

I bet similar functions for Floats would be useful to some people.

I don't understand, and can't imagine, what kind of functions you mean by this?

drathier commented 5 years ago

There are no centralized statistics atm. It's mostly based on what people talk about with each other, mainly on slack. The package author can see the number of clones for their package on github; that's it.

For floats, someone might want floats in the range 0 to 1, with a step length of 0.05. Or someone wants positive floats, etc.

miniBill commented 4 years ago

The current fuzzer only generates ASCII, which is definitely NOT enough. Generating even a single multi-byte character would have detected https://github.com/MartinSStewart/elm-codec-bytes/issues/1 while fuzz testing

drathier commented 4 years ago

I thought I had fixed that already, but the PR hasn't been merged! https://github.com/elm-explorations/test/pull/92

sdeframond commented 3 years ago

I support this idea. Is it the right place to suggest a use case ? I am new to the Elm community

My current use case would be to test strings that do not start with "=". For example, assuming some helper like

-- Exclude a specific character from being generated.
excluding : Char -> Fuzzer Char -> Fuzzer Char

One could write something like this

fuzz2 "non empty string" (unicode |> excluding "=") (string unicode) =
  \head tail -> Expect.equal someValue (myFunction <| String.cons head tail)