Adding tests for Escape nodes

This commit is contained in:
Jeff Avallone 2014-12-19 21:49:11 -05:00
parent 32a28ede90
commit 24e21834f6
2 changed files with 83 additions and 6 deletions

View File

@ -0,0 +1,76 @@
import javascript from 'src/js/parser/javascript/parser.js';
import _ from 'lodash';
import Snap from 'snapsvg';
describe('parser/javascript/any_character.js', function() {
_.forIn({
'\\b': 'word boundary',
'\\B': 'non-word boundary',
'\\d': 'digit',
'\\D': 'non-digit',
'\\f': 'form feed',
'\\n': 'line feed',
'\\r': 'carriage return',
'\\s': 'white space',
'\\S': 'non-white space',
'\\t': 'tab',
'\\v': 'vertical tab',
'\\w': 'word',
'\\W': 'non-word',
'\\0': 'null',
'\\1': 'Back reference (group = 1)',
'\\2': 'Back reference (group = 2)',
'\\3': 'Back reference (group = 3)',
'\\4': 'Back reference (group = 4)',
'\\5': 'Back reference (group = 5)',
'\\6': 'Back reference (group = 6)',
'\\7': 'Back reference (group = 7)',
'\\8': 'Back reference (group = 8)',
'\\9': 'Back reference (group = 9)',
'\\012': 'octal: 12',
'\\cx': 'ctrl-x',
'\\xab': '0xAB',
'\\uabcd': 'U+ABCD'
}, (label, str) => {
it(`parses "${str}" as an Escape`, function() {
var parser = new javascript.Parser(str);
expect(parser.__consume__terminal()).toEqual(jasmine.objectContaining({
type: 'escape',
label
}));
});
});
describe('#_render', function() {
beforeEach(function() {
var parser = new javascript.Parser('\\b');
this.node = parser.__consume__terminal();
this.node.state = {};
this.svg = Snap(document.createElement('svg'));
this.node.container = this.svg.group();
spyOn(this.node, 'renderLabel').and.callThrough();
});
it('renders a label', function() {
this.node._render();
expect(this.node.renderLabel).toHaveBeenCalledWith('word boundary');
});
it('sets the edge radius of the rect', function(done) {
this.node._render()
.then(label => {
expect(label.select('rect').attr()).toEqual(jasmine.objectContaining({
rx: '3',
ry: '3'
}));
})
.finally(done)
.done();
});
});
});

View File

@ -4,8 +4,8 @@ export default {
type: 'escape',
_render() {
return this.renderLabel(_.result(this, this.code))
.then(label => {
return this.renderLabel(this.label)
.tap(label => {
label.select('rect').attr({
rx: 3,
ry: 3
@ -16,6 +16,7 @@ export default {
setup() {
this.code = this.properties.esc.properties.code.textValue;
this.arg = this.properties.esc.properties.arg.textValue;
this.label = _.result(this, this.code);
},
// Escape code mappings
@ -43,18 +44,18 @@ export default {
9: 'Back reference (group = 9)',
0() {
if (this.arg) {
return 'octal: ' + this.arg;
return `octal: ${this.arg}`;
} else {
return 'null';
}
},
c() {
return 'ctrl-' + this.arg;
return `ctrl-${this.arg}`;
},
x() {
return '0x' + this.arg.toUpperCase();
return `0x${this.arg.toUpperCase()}`;
},
u() {
return 'U+' + this.arg.toUpperCase();
return `U+${this.arg.toUpperCase()}`;
}
};