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

Implement #98: Record supports DSL #101

Merged
merged 1 commit into from Apr 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -279,7 +279,7 @@ The DSL is based on a subset of language recognized by [typify-parser](https://g

Randomly uses one of the given arbitraries.

- `record(spec: { key: arbitrary a... }): arbitrary { key: a... }`
- `record(spec: { key: arbitrary a... }, userenv: env?): arbitrary { key: a... }`

Generates a javascript object with given record spec.

Expand Down
9 changes: 2 additions & 7 deletions lib/arbitrary.js
Expand Up @@ -5,10 +5,10 @@ var arbitraryAssert = require("./arbitraryAssert.js");
var arbitraryBless = require("./arbitraryBless.js");
var array = require("./array.js");
var assert = require("assert");
var generator = require("./generator.js");
var pair = require("./pair.js");
var dict = require("./dict.js");
var generator = require("./generator.js");
var json = require("./json.js");
var pair = require("./pair.js");
var show = require("./show.js");
var shrink = require("./shrink.js");
var utils = require("./utils.js");
Expand Down Expand Up @@ -139,11 +139,6 @@ function oneof() {
});
}

/**
- `record(spec: { key: arbitrary a... }): arbitrary { key: a... }`

Generates a javascript object with given record spec.
*/
function record(spec) {
var forcedSpec = {};
// TODO: use mapValues
Expand Down
3 changes: 3 additions & 0 deletions lib/jsverify.js
Expand Up @@ -107,6 +107,7 @@ var random = require("./random.js");
var show = require("./show.js");
var shrink = require("./shrink.js");
var string = require("./string.js");
var record = require("./record.js");
var suchthat = require("./suchthat.js");
var typify = require("./typify.js");
var utils = require("./utils.js");
Expand Down Expand Up @@ -416,6 +417,7 @@ function sampler(arb, size) {
/// include ./bless.js
/// include ./primitive.js
/// include ./arbitrary.js
/// include ./record.js
/// include ./string.js
/// include ./fn.js
/// include ./generator.js
Expand Down Expand Up @@ -468,6 +470,7 @@ for (k in primitive) {
for (k in arbitrary) {
jsc[k] = arbitrary[k];
}
jsc.record = record; // to override arbitrary.record
for (k in string) {
jsc[k] = string[k];
}
Expand Down
25 changes: 25 additions & 0 deletions lib/record.js
@@ -0,0 +1,25 @@
"use strict";

var arbitrary = require("./arbitrary.js");
var environment = require("./environment.js");
var typify = require("./typify.js");
var utils = require("./utils.js");

/**
- `record(spec: { key: arbitrary a... }, userenv: env?): arbitrary { key: a... }`

Generates a javascript object with given record spec.
*/
function record(spec, userenv) {
var env = userenv ? utils.merge(environment, userenv) : environment;

var parsedSpec = {};
Object.keys(spec).forEach(function (k) {
var arb = spec[k];
parsedSpec[k] = typeof arb === "string" ? typify.parseTypify(env, arb) : arb;
});

return arbitrary.record(parsedSpec);
}

module.exports = record;
2 changes: 1 addition & 1 deletion lib/suchthat.js
Expand Up @@ -8,7 +8,7 @@ var generator = require("./generator.js");
var shrink = require("./shrink.js");

/**
- `suchthat(arb: arbitrary a, p : a -> bool): arbitrary a`
- `suchthat(arb: arbitrary a, userenv: env?, p : a -> bool): arbitrary a`
Arbitrary of values that satisfy `p` predicate. It's advised that `p`'s accept rate is high.
*/
function suchthat(arb, userenv, predicate) {
Expand Down
34 changes: 34 additions & 0 deletions test/record.js
Expand Up @@ -21,6 +21,40 @@ describe("record", function () {
}));
});

it("supports environment", function () {
var spec = {
a: "string",
b: "nat",
c: "unit -> unit"
};

jsc.assert(jsc.forall(jsc.record(spec), function (obj) {
return _.isObject(obj) &&
_.isString(obj.a) &&
_.isNumber(obj.b) &&
_.isFunction(obj.c);
}));
});

it("supports environment", function () {
var userenv = {
"str": jsc.string,
};

var spec = {
a: "str",
b: "nat",
c: "unit -> unit"
};

jsc.assert(jsc.forall(jsc.record(spec, userenv), function (obj) {
return _.isObject(obj) &&
_.isString(obj.a) &&
_.isNumber(obj.b) &&
_.isFunction(obj.c);
}));
});

it("generates objects according to the dsl spec", function () {
jsc.assert(jsc.forall("{ a: string; b: nat; c: (* -> bool) }", function (obj) {
return _.isObject(obj) &&
Expand Down