From 9bf5328da79187b561f75373e96023a9ec57286a Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 21 Dec 2014 12:05:55 -0500 Subject: [PATCH] Adding specs for Subexp nodes --- spec/parser/javascript/subexp_spec.js | 102 ++++++++++++++++++++++++++ src/js/parser/javascript/subexp.js | 8 +- 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 spec/parser/javascript/subexp_spec.js diff --git a/spec/parser/javascript/subexp_spec.js b/spec/parser/javascript/subexp_spec.js new file mode 100644 index 0000000..a0bd550 --- /dev/null +++ b/spec/parser/javascript/subexp_spec.js @@ -0,0 +1,102 @@ +import javascript from 'src/js/parser/javascript/parser.js'; +import Node from 'src/js/parser/javascript/node.js'; +import _ from 'lodash'; +import Snap from 'snapsvg'; +import Q from 'q'; + +describe('parser/javascript/subexp.js', function() { + + beforeEach(function() { + Node.state = { groupCounter: 1 }; + }); + + _.forIn({ + '(test)': { + label: 'group #1', + regexp: jasmine.objectContaining({ textValue: 'test' }), + proxy: undefined, + state: { groupCounter: 2 } + }, + '(?=test)': { + label: 'positive lookahead', + regexp: jasmine.objectContaining({ textValue: 'test' }), + proxy: undefined, + state: { groupCounter: 1 } + }, + '(?!test)': { + label: 'negative lookahead', + regexp: jasmine.objectContaining({ textValue: 'test' }), + proxy: undefined, + state: { groupCounter: 1 } + }, + '(?:test)': { + label: '', + regexp: jasmine.objectContaining({ textValue: 'test' }), + proxy: jasmine.objectContaining({ textValue: 'test' }), + state: { groupCounter: 1 } + } + }, (content, str) => { + it(`parses "${str}" as a Subexp`, function() { + var parser = new javascript.Parser(str); + expect(parser.__consume__subexp()).toEqual(jasmine.objectContaining(content)); + }); + }); + + describe('_anchor property', function() { + + it('applies the local transform matrix to the anchor from the regexp', function() { + var node = new javascript.Parser('(test)').__consume__subexp(); + + node.regexp = { + anchor: { + ax: 10, + ax2: 15, + ay: 20 + } + }; + + spyOn(node, 'transform').and.returnValue({ + localMatrix: Snap.matrix().translate(3, 8) + }); + + expect(node._anchor).toEqual({ + ax: 13, + ax2: 18, + ay: 28 + }); + }); + + }); + + describe('#_render', function() { + + beforeEach(function() { + this.renderDeferred = Q.defer(); + + this.node = new javascript.Parser('(test)').__consume__subexp(); + this.node.label = 'example label'; + this.node.regexp = jasmine.createSpyObj('regexp', ['render']); + this.node.container = jasmine.createSpyObj('container', ['addClass', 'group']); + + this.node.regexp.render.and.returnValue(this.renderDeferred.promise); + }); + + it('renders the regexp', function() { + this.node._render(); + expect(this.node.regexp.render).toHaveBeenCalled(); + }); + + it('renders a labeled box', function(done) { + spyOn(this.node, 'renderLabeledBox'); + this.renderDeferred.resolve(); + this.node._render() + .then(() => { + expect(this.node.renderLabeledBox).toHaveBeenCalledWith('example label', this.node.regexp, { padding: 10 }); + }) + .finally(done) + .done(); + }); + + }); + +}); diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 00f4692..8e4a56a 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -26,9 +26,11 @@ export default { _render() { return this.regexp.render(this.container.group()) - .then(this.renderLabeledBox.bind(this, this.label, this.regexp, { - padding: 10 - })); + .then(() => { + return this.renderLabeledBox(this.label, this.regexp, { + padding: 10 + }); + }); }, setup() {