Skip to content

Shrinking from a choose() block ignores min/max parameters #12

Closed
@aseigo

Description

@aseigo

A function exists which takes a list or an int, and contains a bug:

defmodule Demo do
  def list_or_int(input) when is_list(input), do: Enum.count input
  def list_or_int(input) when is_integer(input) and input > 0, do: -1
end

To test, we may do this:

  test "returns a positive integer value" do
    ptest input: choose(from: [list(of: positive_int(), min: 1), positive_int()]) do
      result = Demo.list_or_int input
      assert is_integer(result)
      assert result > 0
    end
  end

When the value 1 is passed to list_or_int, it will return a negative number. quixir will then attempt to shrink the value, but will try 0 as a possible value despite the generator being declared as a positive_int().

If the test drops the choose clause like this:

  test "returns a positive integer value" do
    ptest input: list(of: positive_int(), min: 1) do
      result = Demo.list_or_int input
      assert is_integer(result)
      assert result > 0
    end

    ptest input: positive_int() do
      result = Demo.list_or_int input
      assert is_integer(result)
      assert result > 0
    end
  end

Then it works as expected.

It therefore appears to be the choose(..) clause causing the min/max boundaries for the generator to be lost.

Code to reproduce attached.

QuixirBugDemo.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions