Add tests for combineGroups
This commit is contained in:
parent
f1b73fdaad
commit
965d322405
159
test/test.js
159
test/test.js
@ -56,13 +56,6 @@ describe('types.js', () => {
|
||||
});
|
||||
});
|
||||
describe('tokenizer.js', () => {
|
||||
it('Has a tokenize child', () => {
|
||||
assert.equal(tokenizer.hasOwnProperty('tokenize'), true);
|
||||
});
|
||||
it('Has a util child', () => {
|
||||
assert.equal(tokenizer.hasOwnProperty('util'), true);
|
||||
});
|
||||
describe('util', () => {
|
||||
it('combineEscapedChars works', () => {
|
||||
assert.equal(tokenizer.util.combineEscapedChars(`let x = 'test\\nnewline';`.split('')).join(''), `let x = 'test\\nnewline';`);
|
||||
});
|
||||
@ -97,7 +90,8 @@ describe('tokenizer.js', () => {
|
||||
else if (t.value != correct[i].value)
|
||||
throw new Error('Changed value: Expected \'' + correct[i].value + '\' but got \'' + t.value + '\'')
|
||||
});
|
||||
}); it('getDelimiterToken works', () => {
|
||||
});
|
||||
it('getDelimiterToken works', () => {
|
||||
let token = tokenizer.util.getDelimiterToken(')');
|
||||
if (token.type != 'delimiter')
|
||||
throw new Error('Incorrect type: Expected \'delimiter\' but got \'' + token.type + '\'')
|
||||
@ -105,27 +99,23 @@ describe('tokenizer.js', () => {
|
||||
throw new Error('Incorrect subtype: Expected \'right\' but got \'' + token.subtype + '\'')
|
||||
else if (token.value != 'parenthesis')
|
||||
throw new Error('Incorrect value: Expected \'parenthesis\' but got \'' + token.value + '\'')
|
||||
}); it('operatorType works', () => {
|
||||
});
|
||||
it('operatorType works', () => {
|
||||
assert.equal(tokenizer.util.operatorType('++'), 'left');
|
||||
assert.equal(tokenizer.util.operatorType(';'), 'none');
|
||||
assert.equal(tokenizer.util.operatorType('+'), 'dual');
|
||||
}); it('determineCharType works', () => {
|
||||
});
|
||||
it('determineCharType works', () => {
|
||||
assert.equal(tokenizer.util.determineCharType('+'), 'operator');
|
||||
assert.equal(tokenizer.util.determineCharType('"'), 'string delimiter');
|
||||
assert.equal(tokenizer.util.determineCharType('4'), 'digit');
|
||||
}); it('determineType works', () => {
|
||||
});
|
||||
it('determineType works', () => {
|
||||
assert.equal(tokenizer.util.determineType('let'), 'keyword');
|
||||
assert.equal(tokenizer.util.determineType('dog'), 'unknown');
|
||||
});
|
||||
});
|
||||
}); describe('parser.js', () => {
|
||||
it('Has a parse child', () => {
|
||||
assert.equal(parser.hasOwnProperty('parse'), true);
|
||||
});
|
||||
it('Has a util child', () => {
|
||||
assert.equal(parser.hasOwnProperty('util'), true);
|
||||
});
|
||||
describe('util', () => {
|
||||
describe('parser.js', () => {
|
||||
it('addIndexes works', () => {
|
||||
let tokens = parser.util.addIndexes([{
|
||||
type: 'name',
|
||||
@ -280,29 +270,136 @@ describe('tokenizer.js', () => {
|
||||
value: '+',
|
||||
index: 5,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'left',
|
||||
value: 'parenthesis',
|
||||
index: 6,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'number',
|
||||
subtype: 'n/a',
|
||||
value: '3',
|
||||
index: 6,
|
||||
level: 1
|
||||
},
|
||||
{
|
||||
value: '6',
|
||||
index: 7,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'dual',
|
||||
value: '*',
|
||||
index: 8,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'number',
|
||||
subtype: 'n/a',
|
||||
value: '2',
|
||||
index: 9,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'right',
|
||||
value: 'parenthesis',
|
||||
index: 7,
|
||||
index: 10,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'right',
|
||||
value: 'parenthesis',
|
||||
index: 11,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'none',
|
||||
value: ';',
|
||||
index: 8,
|
||||
index: 12,
|
||||
level: 0
|
||||
}
|
||||
]);
|
||||
if (deepestLevel != 1)
|
||||
throw new Error('Incorrect deepestLevel. Expected \'1\' but got \'' + deepestLevel + '\'');
|
||||
});
|
||||
}]);
|
||||
if (deepestLevel != 2)
|
||||
throw new Error('Incorrect deepestLevel. Expected \'2\' but got \'' + deepestLevel + '\'');
|
||||
});
|
||||
it('combineGroups works', () => {
|
||||
let ast = parser.util.combineGroups([{
|
||||
type: 'name',
|
||||
subtype: 'keyword',
|
||||
value: 'let',
|
||||
index: 0,
|
||||
level: 0
|
||||
}, {
|
||||
type: 'name',
|
||||
subtype: 'variable',
|
||||
value: 'x',
|
||||
index: 1,
|
||||
level: 0
|
||||
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'dual',
|
||||
value: '=',
|
||||
index: 2,
|
||||
level: 0
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'left',
|
||||
value: 'parenthesis',
|
||||
index: 3,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'number',
|
||||
subtype: 'n/a',
|
||||
value: '5',
|
||||
index: 4,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'dual',
|
||||
value: '+',
|
||||
index: 5,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'left',
|
||||
value: 'parenthesis',
|
||||
index: 6,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'number',
|
||||
subtype: 'n/a',
|
||||
value: '6',
|
||||
index: 7,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'dual',
|
||||
value: '*',
|
||||
index: 8,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'number',
|
||||
subtype: 'n/a',
|
||||
value: '2',
|
||||
index: 9,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'right',
|
||||
value: 'parenthesis',
|
||||
index: 10,
|
||||
level: 2
|
||||
}, {
|
||||
type: 'delimiter',
|
||||
subtype: 'right',
|
||||
value: 'parenthesis',
|
||||
index: 11,
|
||||
level: 1
|
||||
}, {
|
||||
type: 'operator',
|
||||
subtype: 'none',
|
||||
value: ';',
|
||||
index: 12,
|
||||
level: 0
|
||||
}]);
|
||||
if (ast[3].type != 'group')
|
||||
throw new Error('Incorrectly combined group.');
|
||||
if (ast[3].tokens[3].type != 'group')
|
||||
throw new Error('Incorrectly combined group.');
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user