Skip to content

Commit 88e1c05

Browse files
author
Jeff Escalante
committed
tests for loop error conditions
1 parent 0572008 commit 88e1c05

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function walk (opts, nodes) {
140140
throw new Error('You must provide an array or object to loop through')
141141
}
142142

143-
if (loopParams.length < 1) {
143+
if (loopParams.keys.length < 1 || loopParams.keys[0] === '') {
144144
throw new Error('You must provide at least one loop argument')
145145
}
146146

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>x</p>
2+
<each loop="item in #@$%#$">
3+
<p>{{index}}: {{item}}</p>
4+
</each>
5+
<p>x</p>

test/fixtures/loop_no_args.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>x</p>
2+
<each loop=" in [1,2,3]">
3+
<p>{{index}}: {{item}}</p>
4+
</each>
5+
<p>x</p>

test/fixtures/loop_no_attr.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>x</p>
2+
<each>
3+
<p>{{index}}: {{item}}</p>
4+
</each>
5+
<p>x</p>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>x</p>
2+
<each loop="item in 5">
3+
<p>{{index}}: {{item}}</p>
4+
</each>
5+
<p>x</p>

test/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ test('nested loops', (t) => {
9595
})
9696
})
9797

98+
test('loop - no loop attribute', (t) => {
99+
return expectError('loop_no_attr', (err) => {
100+
t.truthy(err.toString() === 'Error: the "elseif" tag must have a "loop" attribute')
101+
})
102+
})
103+
104+
test('loop - no array or object passed', (t) => {
105+
return expectError('loop_no_collection', (err) => {
106+
t.truthy(err.toString() === 'Error: You must provide an array or object to loop through')
107+
})
108+
})
109+
110+
test('loop - no loop arguments', (t) => {
111+
return expectError('loop_no_args', (err) => {
112+
t.truthy(err.toString() === 'Error: You must provide at least one loop argument')
113+
})
114+
})
115+
116+
test('loop - expression error', (t) => {
117+
return expectError('loop_expression_error', (err) => {
118+
t.truthy(err.toString() === 'SyntaxError: Unexpected token ILLEGAL')
119+
})
120+
})
121+
98122
//
99123
// Utility
100124
//

0 commit comments

Comments
 (0)