Skip to content

Commit

Permalink
Provide slice tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineveldhoven committed Nov 8, 2023
1 parent 8abe3ef commit 251e52b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/test.expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,36 @@ describe('Twig.js Expressions ->', function () {
output.should.equal('23');
});

it('should support slice shorthand (full form) with negative start', function () {
const testTemplate = twig({data: '{{ "12345"[-2:1] }}'});
const output = testTemplate.render();
output.should.equal('4');
});

it('should support slice shorthand (full form) with negative lenght', function () {
const testTemplate = twig({data: '{{ "12345"[2:-1] }}'});
const output = testTemplate.render();
output.should.equal('34');
});

it('should support slice shorthand (full form) with variables as arguments', function () {
const testTemplate = twig({data: '{{ "12345"[start:length] }}'});
const output = testTemplate.render({start: 2, length: 3});
output.should.equal('345');
});

it('should support slice shorthand (full form) with variable as argument (omit first)', function () {
const testTemplate = twig({data: '{{ "12345"[:length] }}'});
const output = testTemplate.render({length: 3});
output.should.equal('123');
});

it('should support slice shorthand (full form) variable as argument (omit last)', function () {
const testTemplate = twig({data: '{{ "12345"[start:] }}'});
const output = testTemplate.render({start: 2});
output.should.equal('345');
});

it('should support slice shorthand (omit first)', function () {
const testTemplate = twig({data: '{{ "12345"[:2] }}'});
const output = testTemplate.render();
Expand Down
4 changes: 4 additions & 0 deletions test/test.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ describe('Twig.js Filters ->', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(1, 2) }}'});
testTemplate.render().should.equal('23');
});
it('should slice a string with variables as arguments', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(start, length) }}'});
testTemplate.render({start: 2, length: 3}).should.equal('345');
});
it('should slice a string to the end', function () {
const testTemplate = twig({data: '{{ \'12345\'|slice(2) }}'});
testTemplate.render().should.equal('345');
Expand Down

0 comments on commit 251e52b

Please sign in to comment.