diff --git a/lib/atword.js b/lib/atword.js index 3d787c2..32196b7 100644 --- a/lib/atword.js +++ b/lib/atword.js @@ -7,6 +7,18 @@ class AtWord extends Container { super(opts); this.type = 'atword'; } + + toString () { + let quote = this.quoted ? this.raws.quote : ''; + return [ + this.raws.before, + '@', + // we can't use String() here because it'll try using itself + // as the constructor + String.prototype.toString.call(this.value), + this.raws.after + ].join(''); + } } Container.registerWalker(AtWord); diff --git a/test/atrule.js b/test/atrule.js new file mode 100644 index 0000000..5c4a125 --- /dev/null +++ b/test/atrule.js @@ -0,0 +1,53 @@ +'use strict'; + +const chai = require('chai'); +const shallowDeepEqual = require('chai-shallow-deep-equal'); +const Parser = require('../lib/parser'); + +let expect = chai.expect; + +describe('Parser → Atrule', () => { + + chai.use(shallowDeepEqual); + + let fixtures, + failures; + + fixtures = [ + { + it: 'should parse atword', + test: ' @word ', + expected: [ + { type: 'atword', value: 'word', raws: { before: ' ', after: ' ' } } + ] + }, + { + it: 'should not parse escaped @ after @ (@\\@)', + test: ' @\\@word ', + expected: [ + { type: 'atword', value: '\\', raws: { before: ' ', after: '' } } + ] + } + ]; + + fixtures.forEach((fixture) => { + it(fixture.it, () => { + let ast = new Parser(fixture.test).parse(); + + ast.first.walk((node, index) => { + let expected = fixture.expected[index]; + + if (expected) { + expect(node).to.shallowDeepEqual(expected); + } + }); + }); + }); + + failures && failures.forEach((fixture) => { + it(fixture.it, () => { + expect(() => new Parser(fixture.test).parse()).to.throw(Error); + }); + }); + +});