|
1 |
| -import { Liquid } from '../../../../src/liquid' |
| 1 | +import { Drop, Liquid } from '../../../../src/liquid' |
2 | 2 | import { expect, use } from 'chai'
|
3 | 3 | import * as chaiAsPromised from 'chai-as-promised'
|
4 | 4 | import { Scope } from '../../../../src/context/scope'
|
@@ -304,4 +304,66 @@ describe('tags/for', function () {
|
304 | 304 | return expect(html).to.equal('b')
|
305 | 305 | })
|
306 | 306 | })
|
| 307 | + |
| 308 | + describe('iterables', function () { |
| 309 | + class MockIterable { |
| 310 | + * [Symbol.iterator] () { |
| 311 | + yield 'a' |
| 312 | + yield 'b' |
| 313 | + yield 'c' |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + class MockEmptyIterable { |
| 318 | + * [Symbol.iterator] () {} |
| 319 | + } |
| 320 | + |
| 321 | + class MockIterableDrop extends Drop { |
| 322 | + * [Symbol.iterator] () { |
| 323 | + yield 'a' |
| 324 | + yield 'b' |
| 325 | + yield 'c' |
| 326 | + } |
| 327 | + |
| 328 | + public valueOf (): string { |
| 329 | + return 'MockIterableDrop' |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + it('should loop over iterable objects', function () { |
| 334 | + const src = '{% for i in someIterable %}{{i}}{%endfor%}' |
| 335 | + const html = liquid.parseAndRenderSync(src, { someIterable: new MockIterable() }) |
| 336 | + return expect(html).to.equal('abc') |
| 337 | + }) |
| 338 | + it('should loop over iterable drops', function () { |
| 339 | + const src = '{{ someDrop }}: {% for i in someDrop %}{{i}}{%endfor%}' |
| 340 | + const html = liquid.parseAndRenderSync(src, { someDrop: new MockIterableDrop() }) |
| 341 | + return expect(html).to.equal('MockIterableDrop: abc') |
| 342 | + }) |
| 343 | + it('should loop over iterable objects with a limit', function () { |
| 344 | + const src = '{% for i in someIterable limit:2 %}{{i}}{%endfor%}' |
| 345 | + const html = liquid.parseAndRenderSync(src, { someIterable: new MockIterable() }) |
| 346 | + return expect(html).to.equal('ab') |
| 347 | + }) |
| 348 | + it('should loop over iterable objects with an offset', function () { |
| 349 | + const src = '{% for i in someIterable offset:1 %}{{i}}{%endfor%}' |
| 350 | + const html = liquid.parseAndRenderSync(src, { someIterable: new MockIterable() }) |
| 351 | + return expect(html).to.equal('bc') |
| 352 | + }) |
| 353 | + it('should loop over iterable objects in reverse', function () { |
| 354 | + const src = '{% for i in someIterable reversed %}{{i}}{%endfor%}' |
| 355 | + const html = liquid.parseAndRenderSync(src, { someIterable: new MockIterable() }) |
| 356 | + return expect(html).to.equal('cba') |
| 357 | + }) |
| 358 | + it('should go to else for an empty iterable', function () { |
| 359 | + const src = '{% for i in emptyIterable reversed %}{{i}}{%else%}EMPTY{%endfor%}' |
| 360 | + const html = liquid.parseAndRenderSync(src, { emptyIterable: new MockEmptyIterable() }) |
| 361 | + return expect(html).to.equal('EMPTY') |
| 362 | + }) |
| 363 | + it('should support iterable names', function () { |
| 364 | + const src = '{% for i in someDrop %}{{forloop.name}} {%else%}EMPTY{%endfor%}' |
| 365 | + const html = liquid.parseAndRenderSync(src, { someDrop: new MockIterableDrop() }) |
| 366 | + return expect(html).to.equal('i-someDrop i-someDrop i-someDrop ') |
| 367 | + }) |
| 368 | + }) |
307 | 369 | })
|
0 commit comments