Skip to content

Commit

Permalink
Run tests twice to ensure full testing
Browse files Browse the repository at this point in the history
  • Loading branch information
matthemsteger committed Jul 4, 2020
1 parent 57c9c99 commit 8fd429f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 78 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
"precoverage": "npm run test",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pretest": "npm run lint",
"test": "nyc --reporter=lcov --reporter=text-summary npm run test:mocha",
"test": "nyc --reporter=lcov --reporter=text-summary npm run test:mocha:all",
"test:mocha": "mocha --reporter dot",
"test:mocha:legacy": "NO_SET=1 mocha --reporter dot",
"test:mocha:all": "npm run test:mocha && npm run test:mocha:legacy",
"test:watch": "mocha --reporter min --watch",
"test:chrome": "karma start"
}
Expand Down
83 changes: 35 additions & 48 deletions test/core/seq.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
"use strict";

testSetUsage(function setUsage(api) {
it(api.setTestDescription("Parsimmon.seq"), function() {
var parser = Parsimmon.seq(
Parsimmon.string("("),
Parsimmon.regexp(/[^)]/)
.many()
.map(function(xs) {
return xs.join("");
}),
Parsimmon.string(")")
);
it("Parsimmon.seq", function() {
var parser = Parsimmon.seq(
Parsimmon.string("("),
Parsimmon.regexp(/[^)]/)
.many()
.map(function(xs) {
return xs.join("");
}),
Parsimmon.string(")")
);

assert.deepEqual(
api.scopeSet(function scopeSet() {
return parser.parse("(string between parens)");
}).value,
["(", "string between parens", ")"]
);
assert.deepEqual(parser.parse("(string between parens)").value, [
"(",
"string between parens",
")"
]);

assert.deepEqual(
api.scopeSet(function scopeSet() {
return parser.parse("(string");
}),
{
status: false,
index: {
offset: 7,
line: 1,
column: 8
},
expected: ["')'", "/[^)]/"]
}
);
assert.deepEqual(parser.parse("(string"), {
status: false,
index: {
offset: 7,
line: 1,
column: 8
},
expected: ["')'", "/[^)]/"]
});

assert.deepEqual(
api.scopeSet(function scopeSet() {
return parser.parse("starts wrong (string between parens)");
}),
{
status: false,
index: {
offset: 0,
line: 1,
column: 1
},
expected: ["'('"]
}
);
assert.deepEqual(parser.parse("starts wrong (string between parens)"), {
status: false,
index: {
offset: 0,
line: 1,
column: 1
},
expected: ["'('"]
});

assert.throws(function() {
Parsimmon.seq("not a parser");
});
assert.throws(function() {
Parsimmon.seq("not a parser");
});
});
35 changes: 6 additions & 29 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
delete Object.prototype.__magic__;
})();

if (typeof process !== "undefined") {
if (process.env.NO_SET) {
delete globalThis.Set;
}
}

// Karma loads these for us
if (typeof require !== "undefined") {
globalThis.assert = require("chai").assert;
Expand All @@ -28,32 +34,3 @@ globalThis.equivalentParsers = function equivalentParsers(p1, p2, inputs) {
assert.deepEqual(p1.parse(inputs[i]), p2.parse(inputs[i]));
}
};

// In order to test a scenario where "Set" is not available, we need
// to scope out a execution without Set only for code under test. The
// chai libraries will error if Set doesnt exist when assertions run.
globalThis.testSetUsage = function testSetUsage(innerFn) {
var origSet = globalThis.Set;
var setAvailableScenarios = [true, false];
for (var i = 0; i < setAvailableScenarios.length; i++) {
var api = {
setTestDescription: function setTestDescription(desc) {
if (!setAvailableScenarios[i]) {
return desc + " (no Set)";
}
return desc;
},
scopeSet: function scopeSet(fn) {
if (!setAvailableScenarios[i]) {
delete globalThis.Set;
}
var result = fn();
if (!setAvailableScenarios[i]) {
globalThis.Set = origSet;
}
return result;
}
};
innerFn(api);
}
};

0 comments on commit 8fd429f

Please sign in to comment.