Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test shrinking #160

Open
Qqwy opened this issue Dec 30, 2021 · 2 comments
Open

Test shrinking #160

Qqwy opened this issue Dec 30, 2021 · 2 comments

Comments

@Qqwy
Copy link
Contributor

Qqwy commented Dec 30, 2021

While working on the PR for #129, I encountered # TODO: test shrinking at the top of the StreamDataTest module.

Since there is no open issue for it, it seemed like a good idea to open it so it is not forgotten, we can discuss how this might be approached, and maybe someone can contribute a PR.


One approach might be to add a function StreamData.shrink([max_shrinking_steps: 100, initial_seed: :os.timestamp()]).
Its implementation might look like follows:

def shrink(options) when is_list(options) do
  max_shrinking_steps = Keyword.get(options, :max_shrinking_steps, 100)
  initial_seed = Keyword.get(options, :initial_seed, :os.timestamp()) # Or obtained from ExUnitProperties if set during a test run.

    {:error, %{shrunk_value: value}} = 
      check_all(StreamData.integer(), [max_shrinking_steps: max_shrinking_steps, initial_seed: initial_seed], &{:error, &1})
  value
end

(Side note: If desired, it might be optimized somewhat by instead hooking into the internals of StreamData.check_all/3 rather than calling it from the outside.)

This could then be used in a higher-order property test (a property test which builds and tests a generator).
An example:

  describe "integer/1" do
    property "shrinks towards smallest value still in range" do
      check all bound1 <- integer(),
                bound2 <- integer() do
        generator = integer(bound1..bound2)
        assert shrink(generator) == min(bound1, bound2)
      end
    end
end
@CoderDennis
Copy link

@whatyouhide should this issue be closed? I took a look at the source and can see that there is code to do shrinking.

Oh! Maybe I misread it. Is this issue for testing the shrinking that has already been implemented? I thought it was for implementing shrinking itself.

@whatyouhide
Copy link
Owner

@CoderDennis this is to add tests to code that already does shrinking. Right now, we do shrinking, but we don't really have automated tests that check that the shrinking behavior is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants