-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define spec/tests for rules.js (Rule and Sym classes)
- Loading branch information
Showing
5 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*global describe, it, expect, beforeAll*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/*global describe, it, expect, beforeAll*/ | ||
|
||
const Sym = require('../lib/rules').Sym; | ||
const Rule = require('../lib/rules').Rule; | ||
|
||
var sum, prod, r, f; | ||
|
||
describe('Sym', () => { | ||
|
||
describe('constructor()', () => { | ||
|
||
it('takes a name parameter and assigns it to `name`', () => { | ||
sum = new Sym('sum'); | ||
expect(sum.name).toBe('sum'); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('Rule', () => { | ||
|
||
beforeAll(() => { | ||
sum = new Sym('sum'); | ||
prod = new Sym('prod'); | ||
}); | ||
|
||
describe('constructor()', () => { | ||
|
||
it('extends Array', () => { | ||
r = new Rule(sum, ['+']); | ||
expect(r instanceof Array).toBe(true); | ||
}); | ||
|
||
it('takes a LHS parameter and assigns it to `lhs`', () => { | ||
r = new Rule(sum, ['+']); | ||
expect(r.lhs).toBe(sum); | ||
}); | ||
|
||
it('throws if no RHS is given', () => { | ||
f = () => r = new Rule(sum); | ||
expect(f).toThrowError(); | ||
}); | ||
|
||
it('throws if an empty RHS is given', () => { | ||
f = () => r = new Rule(sum, []); | ||
expect(f).toThrowError(); | ||
}); | ||
|
||
it('accepts a valuator function as third optional param', () => { | ||
r = new Rule(sum, [sum, '+', prod], (x, _, y) => x + y); | ||
expect(r.evaluate instanceof Function).toBe(true); | ||
}); | ||
|
||
}); | ||
|
||
describe('evaluate()', () => { | ||
|
||
it('evaluates an array of values using the given function', () => { | ||
r = new Rule(sum, [sum, '+', prod], (x, _, y) => x + y); | ||
expect(r.evaluate([1, '+', 2])).toBe(3); | ||
}); | ||
|
||
it('fails if the first argument is not an array', () => { | ||
f = () => new Rule(sum, [sum, '+', prod], (x, _, y) => x + y).evaluate(1); | ||
expect(f).toThrowError(); | ||
}); | ||
|
||
}); | ||
|
||
it('\'s RHS is accessible via array indices', () => { | ||
r = new Rule(sum, ['+']); | ||
expect(r[0]).toBe('+'); | ||
}); | ||
|
||
it('\'s `lhs` property is non-enumerable', () => { | ||
r = new Rule(sum, ['+']); | ||
expect(Object.propertyIsEnumerable(r, 'lhs')).toBe(false); | ||
}); | ||
|
||
it('\'s `rhs` can be iterated with forEach', () => { | ||
let items = [sum, '+', prod]; | ||
r = new Rule(sum, items); | ||
let filteredRhs = r.filter((item, index) => item === items[index]); | ||
expect(filteredRhs.length).toBe(3); | ||
}); | ||
|
||
}); | ||
|