Adding tests for Literal nodes

This commit is contained in:
Jeff Avallone 2014-12-19 21:35:03 -05:00
parent 94ff093824
commit 32a28ede90
2 changed files with 81 additions and 5 deletions

View File

@ -0,0 +1,77 @@
import javascript from 'src/js/parser/javascript/parser.js';
import Snap from 'snapsvg';
describe('parser/javascript/any_character.js', function() {
it('parses "x" as a Literal', function() {
var parser = new javascript.Parser('x');
expect(parser.__consume__terminal()).toEqual(jasmine.objectContaining({
type: 'literal',
literal: 'x'
}));
});
it('parses "\\x" as a Literal', function() {
var parser = new javascript.Parser('\\x');
expect(parser.__consume__terminal()).toEqual(jasmine.objectContaining({
type: 'literal',
literal: 'x'
}));
});
describe('#_render', function() {
beforeEach(function() {
var parser = new javascript.Parser('a');
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(['"', 'a', '"']);
});
it('sets the class of the first and third tspan to "quote"', function(done) {
this.node._render()
.then(label => {
expect(label.selectAll('tspan')[0].hasClass('quote')).toBeTruthy();
expect(label.selectAll('tspan')[2].hasClass('quote')).toBeTruthy();
})
.finally(done)
.done();
});
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();
});
});
describe('#merge', function() {
beforeEach(function() {
var parser = new javascript.Parser('a');
this.node = parser.__consume__terminal();
});
it('appends to the literal value', function() {
this.node.merge({ literal: 'b' });
expect(this.node.literal).toEqual('ab');
});
});
});

View File

@ -4,8 +4,8 @@ export default {
type: 'literal', type: 'literal',
_render() { _render() {
return this.renderLabel(['"', this.literal.textValue, '"']) return this.renderLabel(['"', this.literal, '"'])
.then(label => { .tap(label => {
var spans = label.selectAll('tspan'); var spans = label.selectAll('tspan');
spans[0].addClass('quote'); spans[0].addClass('quote');
@ -19,11 +19,10 @@ export default {
}, },
merge(other) { merge(other) {
this.textValue += other.textValue; this.literal += other.literal;
this.literal.textValue += other.literal.textValue;
}, },
setup() { setup() {
this.literal = this.properties.literal; this.literal = this.properties.literal.textValue;
} }
}; };