Skip to content

Commit

Permalink
Fix the rule of naming and identifing a macro to keep the same with a…
Browse files Browse the repository at this point in the history
… reference's.
  • Loading branch information
fool2fish committed Nov 18, 2014
1 parent 2e4e3fc commit a848b67
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/engine/velocity.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/engine/velocity.l
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

%x rp rw r b bpm bp pm p m

A [a-zA-Z]
A [a-zA-Z0-9-_]
ID [a-zA-Z_][a-zA-Z0-9-_]*
LP (?=[ \t]*\()

Expand Down Expand Up @@ -76,8 +76,8 @@ LP (?=[ \t]*\()
"#"("{evaluate}"|"evaluate"){LP} { this.pushState('bp'); return 'EVALUATE'; }
"#"("{define}"|"define"){LP} { this.pushState('bp'); return 'DEFINE'; }
"#"("{macro}"|"macro"){LP} { this.pushState('bpm'); return 'MACRO'; }
"#"(\{{A}+\}|{A}+){LP} { this.pushState('bpm'); return 'MACROCALL'; }
"#@"(\{{A}+\}|{A}+){LP} { this.pushState('bpm'); return 'BMACROCALL'; }
"#"(\{{ID}\}|{ID}){LP} { this.pushState('bpm'); return 'MACROCALL'; }
"#@"(\{{ID}\}|{ID}){LP} { this.pushState('bpm'); return 'BMACROCALL'; }
<bp>[ \t]*"(" { this.popState();
this.pushState('p'); return '('; }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "velocity",
"version": "0.7.0",
"version": "0.7.1",
"description": "A node velocity template engine.",
"homepage": "https://github.com/fool2fish/velocity",
"keywords": [
Expand Down
7 changes: 7 additions & 0 deletions test/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,12 @@ describe('engine.test.js', function () {
favorites: ['food', 'travel', 'comic', '...']
}).should.equal(utils.string('macro-result.txt'))
})

it('should throw an error when calling an undefined macro', function() {
var engine = new Engine({
template: '#a()'
})
engine.render.bind().should.throw()
})
})
})
58 changes: 58 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,62 @@ describe('parser.test.js', function () {
alternate.consequent.body.should.length(3)
})
})

describe('Macro', function() {
it('should define a macro', function() {
var ast = parser.parse('#macro(a)#end')
ast.body[0].type.should.equal('Macro')
ast.body[0].name.should.equal('a')

ast = parser.parse('#macro(_a)#end')
ast.body[0].type.should.equal('Macro')
ast.body[0].name.should.equal('_a')

ast = parser.parse('#macro(a-)#end')
ast.body[0].type.should.equal('Macro')
ast.body[0].name.should.equal('a-')

ast = parser.parse('#macro(a1)#end')
ast.body[0].type.should.equal('Macro')
ast.body[0].name.should.equal('a1')

parser.parse.bind('#macro(a*)#end').should.throw()

parser.parse.bind('#macro(1a)#end').should.throw()
})
})

describe('MacroCall', function() {
it('should call a macro', function() {
var ast = parser.parse('#a()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('a')

ast = parser.parse('#1a()')
ast.body[0].type.should.equal('Text')

ast = parser.parse('#a*()')
ast.body[0].type.should.equal('Text')

ast = parser.parse('#_a()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('_a')

ast = parser.parse('#a-()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('a-')

ast = parser.parse('#a1()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('a1')

ast = parser.parse('#_if()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('_if')

ast = parser.parse('#define1()')
ast.body[0].type.should.equal('MacroCall')
ast.body[0].name.should.equal('define1')
})
})
})

0 comments on commit a848b67

Please sign in to comment.