From 32a28ede90e0340ad185e69a2998de0ebbc6f1a3 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 19 Dec 2014 21:35:03 -0500 Subject: [PATCH] Adding tests for Literal nodes --- spec/parser/javascript/literal_spec.js | 77 ++++++++++++++++++++++++++ src/js/parser/javascript/literal.js | 9 ++- 2 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 spec/parser/javascript/literal_spec.js diff --git a/spec/parser/javascript/literal_spec.js b/spec/parser/javascript/literal_spec.js new file mode 100644 index 0000000..24cac2c --- /dev/null +++ b/spec/parser/javascript/literal_spec.js @@ -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'); + }); + + }); + +}); diff --git a/src/js/parser/javascript/literal.js b/src/js/parser/javascript/literal.js index f75707a..95b10ca 100644 --- a/src/js/parser/javascript/literal.js +++ b/src/js/parser/javascript/literal.js @@ -4,8 +4,8 @@ export default { type: 'literal', _render() { - return this.renderLabel(['"', this.literal.textValue, '"']) - .then(label => { + return this.renderLabel(['"', this.literal, '"']) + .tap(label => { var spans = label.selectAll('tspan'); spans[0].addClass('quote'); @@ -19,11 +19,10 @@ export default { }, merge(other) { - this.textValue += other.textValue; - this.literal.textValue += other.literal.textValue; + this.literal += other.literal; }, setup() { - this.literal = this.properties.literal; + this.literal = this.properties.literal.textValue; } };