Skip to content

Commit 251e52b

Browse files
Provide slice tests.
1 parent 8abe3ef commit 251e52b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

test/test.expressions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,36 @@ describe('Twig.js Expressions ->', function () {
467467
output.should.equal('23');
468468
});
469469

470+
it('should support slice shorthand (full form) with negative start', function () {
471+
const testTemplate = twig({data: '{{ "12345"[-2:1] }}'});
472+
const output = testTemplate.render();
473+
output.should.equal('4');
474+
});
475+
476+
it('should support slice shorthand (full form) with negative lenght', function () {
477+
const testTemplate = twig({data: '{{ "12345"[2:-1] }}'});
478+
const output = testTemplate.render();
479+
output.should.equal('34');
480+
});
481+
482+
it('should support slice shorthand (full form) with variables as arguments', function () {
483+
const testTemplate = twig({data: '{{ "12345"[start:length] }}'});
484+
const output = testTemplate.render({start: 2, length: 3});
485+
output.should.equal('345');
486+
});
487+
488+
it('should support slice shorthand (full form) with variable as argument (omit first)', function () {
489+
const testTemplate = twig({data: '{{ "12345"[:length] }}'});
490+
const output = testTemplate.render({length: 3});
491+
output.should.equal('123');
492+
});
493+
494+
it('should support slice shorthand (full form) variable as argument (omit last)', function () {
495+
const testTemplate = twig({data: '{{ "12345"[start:] }}'});
496+
const output = testTemplate.render({start: 2});
497+
output.should.equal('345');
498+
});
499+
470500
it('should support slice shorthand (omit first)', function () {
471501
const testTemplate = twig({data: '{{ "12345"[:2] }}'});
472502
const output = testTemplate.render();

test/test.filters.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ describe('Twig.js Filters ->', function () {
640640
const testTemplate = twig({data: '{{ \'12345\'|slice(1, 2) }}'});
641641
testTemplate.render().should.equal('23');
642642
});
643+
it('should slice a string with variables as arguments', function () {
644+
const testTemplate = twig({data: '{{ \'12345\'|slice(start, length) }}'});
645+
testTemplate.render({start: 2, length: 3}).should.equal('345');
646+
});
643647
it('should slice a string to the end', function () {
644648
const testTemplate = twig({data: '{{ \'12345\'|slice(2) }}'});
645649
testTemplate.render().should.equal('345');

0 commit comments

Comments
 (0)