proper-testing / proper

PropEr: a QuickCheck-inspired property-based testing tool for Erlang
http://proper-testing.github.io
GNU General Public License v3.0
882 stars 167 forks source link

Generate a binary of variable size #267

Closed ankhers closed 3 years ago

ankhers commented 3 years ago

I was wondering how I am supposed to generate a binary with a min and max size? I have tried using binary(range(0,10)), but I am getting the following error.

{'EXIT',
 {badarith,
  [{proper_gen,vector_gen_tr,3,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,479}]},
   {proper_gen,generate,3,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,187}]},
   {proper_gen,generate,1,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,127}]},
   {proper_gen,generate,3,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,175}]},
   {proper_gen,generate,1,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,127}]},
   {proper_gen,generate,3,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,189}]},
   {proper_gen,generate,1,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,127}]},
   {proper_gen,generate,3,
    [{file,
      "PROJECT_PATH/_build/test/lib/proper/src/proper_gen.erl"},
     {line,175}]}]}}

If you have any insight, it would be greatly appreciated.

kostis commented 3 years ago

As the fine manual says, the binary(N) generator takes an expression that evaluates to an integer as argument, not a generator as you have specified.

The PropEr way(TM) to do what you want is with a ?LET. Something like:

my_binary(Min, Max) ->
  ?LET(N, range(Min, Max), binary(N)).
ankhers commented 3 years ago

That makes sense. Thank you for the clarification!