regexper-static/spec/parser/javascript/charset_range_spec.js
Jeff Avallone b35dece228 Fixing character set ranges
Certain escape sequences are not considered in ranges, and lead to the
hyphen to be part of the matched set of characters
2014-12-22 16:09:27 -05:00

83 lines
2.2 KiB
JavaScript

import javascript from 'src/js/parser/javascript/parser.js';
import util from 'src/js/util.js';
import Q from 'q';
import _ from 'lodash';
describe('parser/javascript/charset_range.js', function() {
it('parses "a-z" as a CharsetRange', function() {
var parser = new javascript.Parser('a-z');
expect(parser.__consume__charset_range()).toEqual(jasmine.objectContaining({
type: 'charset-range',
first: jasmine.objectContaining({
textValue: 'a'
}),
last: jasmine.objectContaining({
textValue: 'z'
})
}));
});
_.each([
'\\d-a',
'\\D-a',
'\\s-a',
'\\S-a',
'\\w-a',
'\\W-a'
], str => {
it(`does not parse "${str}" as a CharsetRange`, function() {
var parser = new javascript.Parser(str);
expect(parser.__consume__charset_range()).toEqual(null);
});
});
it('throws an exception when the range is out of order', function() {
var parser = new javascript.Parser('z-a');
expect(() => {
parser.__consume__charset_range();
}).toThrow('Range out of order in character class: z-a');
});
describe('#_render', function() {
beforeEach(function() {
var parser = new javascript.Parser('a-z');
this.node = parser.__consume__charset_range();
this.node.container = jasmine.createSpyObj('cotnainer', ['addClass', 'text', 'group']);
this.node.container.text.and.returnValue('hyphen');
this.firstDeferred = Q.defer();
this.lastDeferred = Q.defer();
spyOn(this.node.first, 'render').and.returnValue(this.firstDeferred.promise);
spyOn(this.node.last, 'render').and.returnValue(this.lastDeferred.promise);
spyOn(util, 'spaceHorizontally');
});
it('renders a hyphen', function() {
this.node._render();
expect(this.node.container.text).toHaveBeenCalledWith(0, 0, '-');
});
it('spaces the items horizontally', function(done) {
this.firstDeferred.resolve();
this.lastDeferred.resolve();
this.node._render()
.then(() => {
expect(util.spaceHorizontally).toHaveBeenCalledWith([
this.node.first,
'hyphen',
this.node.last
], { padding: 5 });
})
.finally(done)
.done();
});
});
});