Fscheck generation string with size between min and max

I am trying to write an FsCheck generator that generates strings with a length in a given interval.

My attempt is as follows:

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 message:

 "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 a generator as follows:

 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 issue, but this is pretty much the same problem as my original one.

Therefore, I reprocessed the above code with the following structure 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 get a 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 problem: why can't he find any arbitrary instance for System.Char? I thought that arbitrary for base types are registered by default. What am I doing wrong?

Thanks!

+5
source share

Source: https://habr.com/ru/post/1204248/


All Articles