问题
I try to write a FsCheck generator that generates strings with length in a given interval.
My attempt is the following:
let genString minLength maxLength =
let isValidLength (s : string) =
s.Length >= minLength && s.Length <= maxLength
Arb.generate
|> Gen.suchThat isValidLength
|> Arb.fromGen
...and I get the error:
"System.Exception : No instances of class FsCheck.Arbitrary`1[a] for type System.String with arguments set []"
What am I doing wrong?
thanks!
UPDATE 1:
I managed to write the generator like this:
let genStrings minLength maxLength =
gen {
let! length = Gen.choose (minLength, maxLength)
let! chars = Gen.arrayOfLength length Arb.generate<char>
return new String(chars)
}
Is there a better way?
UPDATE 2: I wanted to add this as a separate question but it's pretty much the same issue as my original one.
So I refactored the above code to the following structure in order to reuse the sequence generator:
let seqOfLength lengthInterval generator =
gen {
let! length = Gen.choose lengthInterval
let! items = Gen.arrayOfLength length generator
return items |> Seq.ofArray
}
let sizedString lengthInterval =
seqOfLength lengthInterval Arb.generate<char>
|> Gen.map Strings.ofCharSeq
Now I'm getting the runtime error:
System.Exception : No instances of class FsCheck.Arbitrary`1[a] for type System.Char with arguments set []
...which brings me back to my original issue: Why do can't it find any instance of Arbitrary for System.Char? I thought the arbitrary for the basic types are registered by default. What am I doing wrong?
Thanks!
来源:https://stackoverflow.com/questions/26255550/fscheck-generating-string-with-size-between-min-max