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

Built-in shrinker for "json" type #122

Closed
joshuawarner32 opened this Issue Jul 21, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@joshuawarner32

joshuawarner32 commented Jul 21, 2015

I'm experimenting with a simple alternative json encoding, using jsverify to check that a simple round-trip works, across a wide range of inputs. Specifically, I'm using this:

jsc.assert(jsc.forall("json", (tree) => 
    JSON.stringify(deminify(minify(tree))) == JSON.stringify(tree)));

Here, minify and deminify are the encoding and decoding routines, respectively. The details are immaterial, I think.

That works great, and seems to be finding bugs - but it's not shrinking. I'm consistently seeing Error: Failed after <n> tests and 0 shrinks..

Questions:

  • Is this user error? Do I have to enable shrinking somehow? Is there something I could be doing in deminify and minify that's causing shrinking to fail?
  • If it's not user error, is it something should be (in your view) included in jsverify, or should I look elsewhere, or write a purpose-built one?
  • Writing a shrinker in the general sense is obviously not difficult; I've done it before in other contexts. However, the documentation doesn't make it clear (to me) how it needs to be integrated with jsverify. Any pointers?
@phadej

This comment has been minimized.

Show comment
Hide comment
@phadej

phadej Jul 22, 2015

Member

There is a long standing issue to implement shrinking of json values. Sorry that not being reflected in the documentation.

If you can implement the shrink it will be wonderful, you can experiment by:

var jsonShrink = function (json) { /* ... * / };
var betterJsonArb = jsc.bless({
  generator: jsc.json.generator,
  show: jsc.json.show,
  shrink: betterJsonArb
});

Also you can use user environment to override json in DSL approach:

var userEnv = { json: betterJsonArb };

jsc.property("deminify . minify = id", "json", userEnv, function (json) {
  /* ... */
});
Member

phadej commented Jul 22, 2015

There is a long standing issue to implement shrinking of json values. Sorry that not being reflected in the documentation.

If you can implement the shrink it will be wonderful, you can experiment by:

var jsonShrink = function (json) { /* ... * / };
var betterJsonArb = jsc.bless({
  generator: jsc.json.generator,
  show: jsc.json.show,
  shrink: betterJsonArb
});

Also you can use user environment to override json in DSL approach:

var userEnv = { json: betterJsonArb };

jsc.property("deminify . minify = id", "json", userEnv, function (json) {
  /* ... */
});
@joshuawarner32

This comment has been minimized.

Show comment
Hide comment
@joshuawarner32

joshuawarner32 Jul 24, 2015

Cool. I wasn't able to figure out how to use jsc.property (the VM complained of an undefined global it, which is mentioned in the documentation - but I can't make heads or tails of the how or why there). However, jsc.assert(jsc.foreach(...)) continued to work fine for me.

I have a passable json simplifier implemented here (forgive the typescript type annotations): https://gist.github.com/joshuawarner32/14988fb48cf1a452fb3e.

There are two problems:

  • I'm unsure of how to delegate to jsverify's shrinkers for numbers, strings, booleans, etc.
  • Right now, I'm returning an array of possible shrinks, which can grow quite large and be highly redundant between successive shrinking steps. It looks like there's some support for lazyseqs, which I think is what I want - except, it seems to me using that will turn the code into a awful mess of closures. Suggestions welcome.

joshuawarner32 commented Jul 24, 2015

Cool. I wasn't able to figure out how to use jsc.property (the VM complained of an undefined global it, which is mentioned in the documentation - but I can't make heads or tails of the how or why there). However, jsc.assert(jsc.foreach(...)) continued to work fine for me.

I have a passable json simplifier implemented here (forgive the typescript type annotations): https://gist.github.com/joshuawarner32/14988fb48cf1a452fb3e.

There are two problems:

  • I'm unsure of how to delegate to jsverify's shrinkers for numbers, strings, booleans, etc.
  • Right now, I'm returning an array of possible shrinks, which can grow quite large and be highly redundant between successive shrinking steps. It looks like there's some support for lazyseqs, which I think is what I want - except, it seems to me using that will turn the code into a awful mess of closures. Suggestions welcome.
@phadej

This comment has been minimized.

Show comment
Hide comment
@phadej

phadej Jul 24, 2015

Member

I can continue from here. This could be a good topic for a tutorial too. Ping me at the end of the next week if I forget about this. Hopefully this isn't blocking you too much.

And you can shrink number with jsc.number.shrink. And there is jsc.shrink.array(elementsShrink) IIRC. some (most?) shrinks return lazyseq though..

Member

phadej commented Jul 24, 2015

I can continue from here. This could be a good topic for a tutorial too. Ping me at the end of the next week if I forget about this. Hopefully this isn't blocking you too much.

And you can shrink number with jsc.number.shrink. And there is jsc.shrink.array(elementsShrink) IIRC. some (most?) shrinks return lazyseq though..

@phadej

This comment has been minimized.

Show comment
Hide comment
@phadej

phadej Jul 28, 2015

Member

Sorry for the delay, I try to assemble 0.7.0 as soon as possible.

Member

phadej commented Jul 28, 2015

Sorry for the delay, I try to assemble 0.7.0 as soon as possible.

@phadej phadej added this to the 0.7.0 milestone Jul 28, 2015

@phadej phadej closed this in 32c4377 Aug 22, 2015

phadej added a commit that referenced this issue Aug 22, 2015

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