forked from jtwig/jtwig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As part of jtwig#88 this tech task will allow to create jtwig functio…
…ns using type safe Java methods.
- Loading branch information
1 parent
17bf9b4
commit e361cd7
Showing
144 changed files
with
3,368 additions
and
3,443 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/DateFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.exception.RenderException; | ||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import java.util.Date; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.given; | ||
import static com.lyncode.jtwig.SyntacticSugar.when; | ||
|
||
public class DateFunctionsTest extends AbstractJtwigTest { | ||
@Test | ||
public void dateFormatWithDate() throws Exception { | ||
given(theContext().withModelAttribute("time", new Date())); | ||
when(jtwigRenders(template("{{ date_format(time, 'yyyy') }}"))); | ||
} | ||
|
||
@Test | ||
public void dateFormat() throws Exception { | ||
given(theContext().withModelAttribute("time", new Date())); | ||
when(jtwigRenders(template("{{ date_format(time) }}"))); | ||
} | ||
|
||
@Test | ||
public void dateModify() throws Exception { | ||
given(theContext().withModelAttribute("time", new Date())); | ||
when(jtwigRenders(template("{{ date_format(time, '+1 day') }}"))); | ||
} | ||
|
||
@Test(expected = RenderException.class) | ||
public void dateModifyWithWrongFormat() throws Exception { | ||
given(theContext().withModelAttribute("time", new Date())); | ||
when(jtwigRenders(template("{{ date_modify(time, '+1 unknown') }}"))); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/ListFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.then; | ||
import static com.lyncode.jtwig.SyntacticSugar.when; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class ListFunctionsTest extends AbstractJtwigTest { | ||
@Test | ||
public void batch() throws Exception { | ||
when(jtwigRenders(template("{{ batch([1,2,3,4], 3) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[[1, 2, 3], [4]]"))); | ||
} | ||
|
||
@Test | ||
public void batchWithPadding() throws Exception { | ||
when(jtwigRenders(template("{{ batch([1,2,3,4], 3, 1) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[[1, 2, 3], [4, 1, 1]]"))); | ||
} | ||
|
||
@Test | ||
public void concatenate() throws Exception { | ||
when(jtwigRenders(template("{{ concatenate(1, 2, 3) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("123"))); | ||
} | ||
|
||
@Test | ||
public void join() throws Exception { | ||
when(jtwigRenders(template("{{ join([1, 2, 3]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("123"))); | ||
} | ||
|
||
@Test | ||
public void joinWithCustomSeparator() throws Exception { | ||
when(jtwigRenders(template("{{ join([1, 2, 3], ' - ') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1 - 2 - 3"))); | ||
} | ||
|
||
@Test | ||
public void merge() throws Exception { | ||
when(jtwigRenders(template("{{ merge([1, 2, 3], [4]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[1, 2, 3, 4]"))); | ||
} | ||
@Test | ||
public void mergeMultipleArgs() throws Exception { | ||
when(jtwigRenders(template("{{ merge([1, 2, 3], [4], [5, 6]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[1, 2, 3, 4, 5, 6]"))); | ||
} | ||
@Test | ||
public void slice() throws Exception { | ||
when(jtwigRenders(template("{{ slice('abc', 1, 2) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("bc"))); | ||
} | ||
@Test | ||
public void sort() throws Exception { | ||
when(jtwigRenders(template("{{ sort([2, 1, 3]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[1, 2, 3]"))); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/MapFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.then; | ||
import static com.lyncode.jtwig.SyntacticSugar.when; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class MapFunctionsTest extends AbstractJtwigTest { | ||
@Test | ||
public void map() throws Exception { | ||
when(jtwigRenders(template("{{ keys({ one:1, two:2 }) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[one, two]"))); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/MathFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.then; | ||
import static com.lyncode.jtwig.SyntacticSugar.when; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class MathFunctionsTest extends AbstractJtwigTest { | ||
@Test | ||
public void absTest() throws Exception { | ||
when(jtwigRenders(template("{{ abs(-1) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
|
||
@Test | ||
public void roundTest() throws Exception { | ||
when(jtwigRenders(template("{{ round(1.12) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
@Test | ||
public void roundCeilTest() throws Exception { | ||
when(jtwigRenders(template("{{ round(1.12, 'ceil') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("2"))); | ||
} | ||
@Test | ||
public void roundFloorTest() throws Exception { | ||
when(jtwigRenders(template("{{ round(1.62, 'floor') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/NumberFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.then; | ||
import static com.lyncode.jtwig.SyntacticSugar.when; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class NumberFunctionsTest extends AbstractJtwigTest { | ||
@Test | ||
public void testDefaultBehaviorWithoutSeparators() throws Exception { | ||
when(jtwigRenders(template("{{ number_format(100000.1) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("100000.1"))); | ||
} | ||
|
||
@Test | ||
public void testWithTwoDecimalDigits() throws Exception { | ||
when(jtwigRenders(template("{{ number_format(100000.1, 2) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("100000.10"))); | ||
} | ||
@Test | ||
public void testWithCommaSeparatingDecimal() throws Exception { | ||
when(jtwigRenders(template("{{ number_format(100000.1, 2, ',') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("100000,10"))); | ||
} | ||
@Test | ||
public void testWithThousandsSeparator() throws Exception { | ||
when(jtwigRenders(template("{{ number_format(100000.1, 2, ',', ' ') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("100 000,10"))); | ||
} | ||
@Test | ||
public void rangeTest() throws Exception { | ||
when(jtwigRenders(template("{{ range(1,3) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[1, 2, 3]"))); | ||
} | ||
@Test | ||
public void rangeStepTest() throws Exception { | ||
when(jtwigRenders(template("{{ range(1, 3, 2) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[1, 3]"))); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
jtwig-core-test/src/test/java/com/lyncode/jtwig/test/functions/ObjectFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.lyncode.jtwig.test.functions; | ||
|
||
import com.lyncode.jtwig.test.AbstractJtwigTest; | ||
import org.junit.Test; | ||
|
||
import static com.lyncode.jtwig.SyntacticSugar.*; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class ObjectFunctionsTest extends AbstractJtwigTest { | ||
|
||
@Test | ||
public void defaultFunctionTest() throws Exception { | ||
when(jtwigRenders(template("{{ default(null, 1) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
|
||
@Test | ||
public void defaultUndefinedFunctionTest() throws Exception { | ||
when(jtwigRenders(template("{{ default(a.call, 1) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
|
||
@Test | ||
public void jsonEncode() throws Exception { | ||
when(jtwigRenders(template("{{ json_encode({one: 'hello'}) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("{\"one\":\"hello\"}"))); | ||
} | ||
|
||
@Test | ||
public void mapLength() throws Exception { | ||
when(jtwigRenders(template("{{ length({one: 'hello'}) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
|
||
@Test | ||
public void listLength() throws Exception { | ||
when(jtwigRenders(template("{{ length([1,2]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("2"))); | ||
} | ||
|
||
@Test | ||
public void stringLength() throws Exception { | ||
when(jtwigRenders(template("{{ length('Hello') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("5"))); | ||
} | ||
|
||
@Test | ||
public void mapFirst() throws Exception { | ||
when(jtwigRenders(template("{{ first({one: 'hello'}) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("hello"))); | ||
} | ||
|
||
@Test | ||
public void listFirst() throws Exception { | ||
when(jtwigRenders(template("{{ first([1,2]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("1"))); | ||
} | ||
|
||
@Test | ||
public void stringFirst() throws Exception { | ||
when(jtwigRenders(template("{{ first('Hello') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("H"))); | ||
} | ||
|
||
@Test | ||
public void mapLast() throws Exception { | ||
when(jtwigRenders(template("{{ last({one: 'hello'}) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("hello"))); | ||
} | ||
|
||
@Test | ||
public void listLast() throws Exception { | ||
when(jtwigRenders(template("{{ last([1,2]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("2"))); | ||
} | ||
|
||
@Test | ||
public void arrayLast() throws Exception { | ||
given(theContext().withModelAttribute("array", new int[]{1,2})); | ||
when(jtwigRenders(template("{{ last(array) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("2"))); | ||
} | ||
|
||
@Test | ||
public void stringLast() throws Exception { | ||
when(jtwigRenders(template("{{ last('Hello') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("o"))); | ||
} | ||
|
||
@Test | ||
public void reserveTest() throws Exception { | ||
when(jtwigRenders(template("{{ reverse('Hello') }}"))); | ||
then(theRenderedTemplate(), is(equalTo("olleH"))); | ||
} | ||
@Test | ||
public void reserveListTest() throws Exception { | ||
when(jtwigRenders(template("{{ reverse([1,2]) }}"))); | ||
then(theRenderedTemplate(), is(equalTo("[2, 1]"))); | ||
} | ||
} |
Oops, something went wrong.