-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlooping.js
212 lines (164 loc) · 9.04 KB
/
looping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* eslint-env mocha */
if (typeof process === 'object') {
var chai = require('chai')
var assert = chai.assert
var chaiString = require('chai-string')
var makeModel = require('./models/model')
var teddy = require('../')
var model
chai.use(chaiString)
}
describe('Looping', function () {
before(function () {
teddy.setTemplateRoot('test/templates')
model = makeModel()
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0)
} else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3)
}
}
})
this.timeout(5000)
it('should loop through {letters} correctly (looping/loopVal.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopVal.html', model), '<p>a</p><p>b</p><p>c</p>')
done()
})
it('should loop through {names} correctly (looping/loopKeyVal.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopKeyVal.html', model), '<p>jack</p> <p>guy</p><p>jill</p> <p>girl</p><p>hill</p> <p>landscape</p>')
done()
})
// TODO: https://github.com/rooseveltframework/teddy/issues/404
it.skip('should loop through {arrays} correctly (looping/loopArrayOfArrays.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopArrayOfArrays.html', model), '<p>0</p><p>a</p><p>b</p><p>c</p><p>1</p><p>d</p><p>e</p><p>f</p><p>2</p><p>g</p><p>h</p><p>i</p>')
done()
})
it('should loop through {objects} correctly (looping/loopArrayOfObjects.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopArrayOfObjects.html', model), '<p>0</p> <p>1</p> <p>2</p> <p>3</p><p>1</p> <p>4</p> <p>5</p> <p>6</p><p>2</p> <p>7</p> <p>8</p> <p>9</p>')
done()
})
it('should loop through a {nested.object} correctly (looping/nestedObjectLoop.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedObjectLoop.html', model), '<p>a: 4</p><p>b: 5</p><p>c: 6</p>')
done()
})
it('should parse nested loops correctly (looping/nestedLoops.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedLoops.html', model), '<p>1</p> <ul> <li>0: one</li><li>1: two</li><li>2: three</li> </ul><p>2</p> <ul> <li>0: four</li><li>1: five</li><li>2: six</li> </ul><p>3</p> <ul> <li>0: seven</li><li>1: eight</li><li>2: nine</li> </ul>')
done()
})
it('should parse nested nested loops correctly (looping/nestedNestedLoops.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedNestedLoops.html', model), '<p>1</p><ul><li>1</li><ul><li>0: one</li><li>1: two</li><li>2: three</li></ul><li>2</li><ul><li>0: four</li><li>1: five</li><li>2: six</li></ul><li>3</li><ul><li>0: seven</li><li>1: eight</li><li>2: nine</li></ul></ul><p>2</p><ul><li>1</li><ul><li>0: one</li><li>1: two</li><li>2: three</li></ul><li>2</li><ul><li>0: four</li><li>1: five</li><li>2: six</li></ul><li>3</li><ul><li>0: seven</li><li>1: eight</li><li>2: nine</li></ul></ul><p>3</p><ul><li>1</li><ul><li>0: one</li><li>1: two</li><li>2: three</li></ul><li>2</li><ul><li>0: four</li><li>1: five</li><li>2: six</li></ul><li>3</li><ul><li>0: seven</li><li>1: eight</li><li>2: nine</li></ul></ul>')
done()
})
// #47 and #39
it('should loop through a nested arrays correctly (looping/nestedArrays.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedArrays.html', model), '<p>one</p><p>two</p><p>three</p><p>four</p><p>five</p><p>six</p><p>seven</p><p>eight</p><p>nine</p>')
done()
})
it('should loop through an array of 5000 elements in < 5000ms (looping/largeDataSet.html)', function (done) {
teddy.cacheRenders(true)
var start, end, time
start = new Date().getTime()
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
assert.isAtMost(time, 5000)
done()
})
it('should loop through same array of 5000 elements in < 1200ms during second attempt due to caching (looping/largeDataSet.html)', function (done) {
var start, end, time
start = new Date().getTime()
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
assert.isAtMost(time, 1200)
done()
})
it('should loop through an array of 5000 elements in < 5000ms doing a fresh render via partial cache invalidation (looping/largeDataSet.html)', function (done) {
var start, end, time
start = new Date().getTime()
teddy.flushCache('looping/largeDataSet.html', model)
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
assert.isAtMost(time, 5000)
done()
})
it('should loop through same array of 5000 elements in < 1200ms during second attempt due to caching (looping/largeDataSet.html)', function (done) {
var start, end, time
start = new Date().getTime()
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
assert.isAtMost(time, 1200)
done()
})
it('should loop through an array of 5000 elements in < 5000ms doing a fresh render via full cache invalidation (looping/largeDataSet.html)', function (done) {
var start, end, time
start = new Date().getTime()
teddy.flushCache('looping/largeDataSet.html')
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
assert.isAtMost(time, 5000)
done()
})
it('should loop through same array of 5000 elements in < 1200ms during second attempt due to caching (looping/largeDataSet.html)', function (done) {
var start, end, time
start = new Date().getTime()
teddy.render('looping/largeDataSet.html', model)
end = new Date().getTime()
time = end - start
teddy.cacheRenders(false)
assert.isAtMost(time, 1200)
done()
})
it('should ignore loop with invalid through attribute (looping/undefinedObjectLoop.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/undefinedObjectLoop.html', model), '<div></div>')
done()
})
it('should ignore loop with no contents (looping/emptyMarkupLoop.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/emptyMarkupLoop.html', model), '<div></div>')
done()
})
it('should loop without nested markup (looping/noMarkupLoop.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/noMarkupLoop.html', model), '<div>abc</div>')
done()
})
it('should loop through {letters} correctly with numeric val (looping/numericalVal.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/numericalVal.html', model), '<p>a</p><p>b</p><p>c</p>')
done()
})
it('should loop through {letters} correctly with camelCase val (looping/camelCaseLoopVal.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/camelCaseLoopVal.html', model), '<p>a</p><p>b</p><p>c</p>')
done()
})
it('should ignore loops with missing attributes (looping/loopInvalidAttributes.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopInvalidAttributes.html', model), '<div></div>')
done()
})
it('should ignore undefined members of objects and arrays (looping/loopUndefinedMember.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopUndefinedMember.html', model), '<p>a</p><p>{letter}</p><p>c</p><p>{item.a}</p><p>{item.b}</p><p>{item.c}</p><p>4</p><p>5</p><p>6</p><p>7</p><p>8</p><p>9</p>')
done()
})
it('should evaluate evaluate include, if, and unless statements inside of loop (conditionals/loopIncludesIfUnless.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopIncludesIfUnless.html', model), '<p>a</p><p>Some content</p><p>Hello</p><p>b</p><p>Some content</p><p>Hello</p><p>c</p><p>Some content</p><p>Hello</p>')
done()
})
it('should render deeply nested vars with teddy code (looping/nestedObjectWithTeddyContent.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedObjectWithTeddyContent.html', model), '<p>1</p><p>Something Exists</p><p>2</p><p>Something Exists</p>')
done()
})
it('should render deeply nested vars with teddy code and respect noparse flag (looping/nestedObjectWithTeddyContentNoParse.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedObjectWithTeddyContentNoParse.html', model), '<p>1</p><p><if something>Something Exists</if></p><p>2</p><p><if something>Something Exists</if></p>')
done()
})
it('should not crash if attempting to set a <loop> val that matches the name of something else in the model (looping/loopValNameCollision.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopValNameCollision.html', model), '<p>2</p><p>5</p><p>8</p>')
done()
})
it('should print an empty string for array member set to an empty string (looping/loopValEmptyString.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopValEmptyString.html', model), '<p>one</p><p>two</p><p></p><p>three</p>')
done()
})
})