Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mrijk committed Apr 10, 2017
2 parents 3eb5688 + 32c8eaf commit ffa7e9c
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 43 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js

node_js:
- stable

install:
- npm install

script:
- npm run cover

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
# nodespec
Speculaas [![Build Status](https://travis-ci.org/mrijk/nodespec.svg)](https://travis-ci.org/mrijk/nodespec)
[![Coverage Status](https://coveralls.io/repos/github/mrijk/nodespec/badge.svg?branch=master)](https://coveralls.io/github/mrijk/nodespec?branch=master)
======

NodeJS version of [clojure.spec](http://clojure.org/about/spec)

Disclaimer: for now mostly experimental, incomplete and potentially harmful ;)
## Installation

If you want to contribute, you are more than welcome!
`npm install speculaas`

## Usage

For now, see the examples folder.

## Tests

Unit tests:

`npm test`

Test coverage:

`npm run cover`

If you want to test:
Run [ESlint](http://eslint.org):

```bash
$ npm test
```
`npm run lint`

For an alternative look at [js.spec](http://js-spec.online)
## FAQ

Q: what kind of a silly name is speculaas?

A: [nodespec](https://www.npmjs.com/package/nodespec) and [node-spec](https://www.npmjs.com/package/node-spec) where already taken. [Speculaas](https://en.wikipedia.org/wiki/Speculaas) is a kind of a Dutch/Belgium biscuit.

Q: any alternatives for your code?

A: for an alternative look at [js.spec](http://js-spec.online)

## Contributing

If you want to contribute, you are more than welcome!
7 changes: 5 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ Still to implement:
- ...
- 'Real' parsing

Instrumentation hints:

- falafel, rewire?

Other TODO:

- create an npm out of this lib
- add documentation

- Use nodespec to test nodespec!
- Use speculaas to test speculaas!
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// Empty
module.exports = require('./lib/spec');
2 changes: 1 addition & 1 deletion lib/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {conform} = require('./conform');
const {sampleFromSpec} = require('./gen');

function exercise(spec, n = 10) {
const samples = sampleFromSpec(spec, n);
const samples = [...sampleFromSpec(spec, n)];

return _.map(samples, sample => [sample, conform(spec, sample)]);
}
Expand Down
46 changes: 26 additions & 20 deletions lib/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,38 @@ function gen(p) {
}

function generate(generator) {
return sample(generator, 1)[0];
return sample(generator, 1).next().value;
}

function sampleFromSpec(spec, n = 10, depth = 0) {
if (depth > 10) {
return [];
function* sampleFromSpec(spec, n = 10) {
for (let s of sampleInf(gen(spec))) {
if (n === 0) {
break;
} else if (isValid(spec, s)) {
n--;
yield s;
} else {
// Count samples that didn't fullfil predicate
}
}

const samples = sample(gen(spec));

const validSamples = _.filter(samples, sample => isValid(spec, sample));
const todo = n - validSamples.length;

if (todo < 0) {
return _.slice(validSamples, 0, n);
} else if (todo === 0) {
return validSamples;
} else {
return _.concat(validSamples, sampleFromSpec(spec, todo, ++depth));
}

function* sample(generator, n = 10) {
for (let s of sampleInf(generator)) {
if (n === 0) {
break;
} else {
n--;
yield s;
}
}
}

function sample(generator, n = 10) {
const sample = testcheck.sample(generator);

return n < 10 ? _.slice(sample, 0, n) : sample;
function* sampleInf(generator) {
for (const sample of testcheck.sample(generator)) {
yield sample;
}
yield* sampleInf(generator);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/isValid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function isValid(spec, value) {
if (_.isUndefined(def)) {
throw new Error(`Unable to resolve spec ${spec}`);
}
predicate = def.conform ? def.conform : def;
predicate = def.conform;
}
const result = predicate(value);

Expand Down
1 change: 1 addition & 0 deletions lib/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ module.exports = {
plus: require('./plus'),
question: require('./question'),
star: require('./star'),
test: require('./test'),
tuple: require('./tuple')
};
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
{
"name": "spec",
"version": "1.0.0",
"name": "speculaas",
"version": "0.0.3",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --recursive -R spec",
"cover": "istanbul cover _mocha -- -R spec",
"lint": "eslint lib samples"
},
"author": "",
"license": "ISC",
"author": {
"name": "Maurits Rijk",
"email": "maurits.rijk@gmail.com"
},
"bugs": {
"url": "https://github.com/mrijk/nodespec/issues"
},
"engines": {
"node": ">=6.0.0"
},
"license": "MIT",
"dependencies": {
"lodash": "^4.17.4",
"testcheck": "^0.1.4"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.17.0",
"coveralls": "^2.13.0",
"eslint": "^3.19.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0"
}
}
2 changes: 1 addition & 1 deletion test/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ describe('Test node.spec.gen functions', () => {
});

it('should generate 10 integers', () => {
expect(gen.sample(s.gen(isInt))).to.have.length(10);
expect([...gen.sample(s.gen(isInt))]).to.have.length(10);
});
});
2 changes: 1 addition & 1 deletion test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Test the keys function', () => {
});

it('should implement a generator', () => {
// console.log(s.exercise('::person'));
// console.log(s.exercise('::person'));
// expect(s.exercise(s.question(isString))).to.have.length(10)
// .to.satisfy(sample => _.every(sample, ([[v]]) => _.isUndefined(v) || isString(v)));
});
Expand Down
5 changes: 2 additions & 3 deletions test/mapOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ describe('Test the mapOf function', () => {
});

it('should implement a generator', () => {
// console.log(s.exercise('::scores'));
expect(s.exercise('::scores')).to.have.length(10);
// .to.satisfy(sample => _.every(sample, ([v]) => _.isArray(v) && v.length === 2));
expect(s.exercise('::scores')).to.have.length(10)
.to.satisfy(sample => _.every(sample, (v) => _.isArray(v) && v.length === 2));
});
});

0 comments on commit ffa7e9c

Please sign in to comment.