From 24e21834f6c4c2356268dc72a772a68e466ba81c Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 19 Dec 2014 21:49:11 -0500 Subject: [PATCH] Adding tests for Escape nodes --- spec/parser/javascript/escape_spec.js | 76 +++++++++++++++++++++++++++ src/js/parser/javascript/escape.js | 13 ++--- 2 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 spec/parser/javascript/escape_spec.js diff --git a/spec/parser/javascript/escape_spec.js b/spec/parser/javascript/escape_spec.js new file mode 100644 index 0000000..c1d54db --- /dev/null +++ b/spec/parser/javascript/escape_spec.js @@ -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(); + }); + + }); + +}); diff --git a/src/js/parser/javascript/escape.js b/src/js/parser/javascript/escape.js index 047ebed..6f74c5f 100644 --- a/src/js/parser/javascript/escape.js +++ b/src/js/parser/javascript/escape.js @@ -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()}`; } };