Skip to content

Commit

Permalink
update to latest spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Pullara committed Jun 26, 2024
1 parent b2676e4 commit 9cc19a4
Show file tree
Hide file tree
Showing 14 changed files with 1,011 additions and 19 deletions.
5 changes: 4 additions & 1 deletion compiler/src/test/java/com/github/mustachejava/SpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -101,7 +102,9 @@ public String apply(String input) {
StringWriter writer = new StringWriter();
String json = data.toString();
if (json.startsWith("{")) {
compile.execute(writer, new Object[]{new ObjectMapper().readValue(json, Map.class), functionMap.get(file)});
compile.execute(writer, new Object[]{functionMap.get(file), new ObjectMapper().readValue(json, Map.class)});
} else if (json.startsWith("[")) {
compile.execute(writer, new Object[]{functionMap.get(file), new ObjectMapper().readValue(json, List.class)});
} else {
String s = new ObjectMapper().readValue(json, String.class);
compile.execute(writer, new Object[]{functionMap.get(file), s});
Expand Down
12 changes: 12 additions & 0 deletions compiler/src/test/resources/spec/specs/comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@
},
"template": "12345 {{! Comment Block! }} 67890",
"expected": "12345 67890"
},
{
"name": "Variable Name Collision",
"desc": "Comments must never render, even if variable with same name exists.",
"data": {
"! comment": 1,
"! comment ": 2,
"!comment": 3,
"comment": 4
},
"template": "comments never show: >{{! comment }}<",
"expected": "comments never show: ><"
}
]
}
6 changes: 6 additions & 0 deletions compiler/src/test/resources/spec/specs/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,9 @@ tests:
data: { }
template: '12345 {{! Comment Block! }} 67890'
expected: '12345 67890'

- name: Variable Name Collision
desc: Comments must never render, even if variable with same name exists.
data: { '! comment': 1, '! comment ': 2, '!comment': 3, 'comment': 4}
template: 'comments never show: >{{! comment }}<'
expected: 'comments never show: ><'
6 changes: 6 additions & 0 deletions compiler/src/test/resources/spec/specs/interpolation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ tests:
expected: |
Hello, world!
- name: No Re-interpolation
desc: Interpolated tag output should not be re-interpolated.
data: { template: '{{planet}}', planet: 'Earth' }
template: '{{template}}: {{planet}}'
expected: '{{planet}}: Earth'

- name: HTML Escaping
desc: Basic interpolation should be HTML escaped.
data: { forbidden: '& " < >' }
Expand Down
14 changes: 14 additions & 0 deletions compiler/src/test/resources/spec/specs/partials.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@
},
"expected": "X<Y<>>"
},
{
"name": "Nested",
"desc": "The greater-than operator should work from within partials.",
"data": {
"a": "hello",
"b": "world"
},
"template": "{{>outer}}",
"partials": {
"outer": "*{{a}} {{>inner}}*",
"inner": "{{b}}!"
},
"expected": "*hello world!*"
},
{
"name": "Surrounding Whitespace",
"desc": "The greater-than operator should not alter surrounding whitespace.",
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/test/resources/spec/specs/partials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ tests:
partials: { node: '{{content}}<{{#nodes}}{{>node}}{{/nodes}}>' }
expected: 'X<Y<>>'

- name: Nested
desc: The greater-than operator should work from within partials.
data: { a: "hello", b: "world" }
template: '{{>outer}}'
partials: { outer: '*{{a}} {{>inner}}*', inner: '{{b}}!' }
expected: '*hello world!*'

# Whitespace Sensitivity

- name: Surrounding Whitespace
Expand Down
58 changes: 57 additions & 1 deletion compiler/src/test/resources/spec/specs/sections.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
"overview": "Section tags and End Section tags are used in combination to wrap a section\nof the template for iteration\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Section tag MUST be followed\nby an End Section tag with the same content within the same section.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\n 1) Split the name on periods; the first part is the name to resolve, any\n remaining parts should be retained.\n 2) Walk the context stack from top to bottom, finding the first context\n that is a) a hash containing the name as a key OR b) an object responding\n to a method with the given name.\n 3) If the context is a hash, the data is the value associated with the\n name.\n 4) If the context is an object and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 5) Otherwise, the data is the value returned by calling the method with\n the given name.\n 6) If any name parts were retained in step 1, each should be resolved\n against a context stack containing only the result from the former\n resolution. If any part fails resolution, the result should be considered\n falsey, and should interpolate as the empty string.\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nFor each element in the data list, the element MUST be pushed onto the\ncontext stack, the section MUST be rendered, and the element MUST be popped\noff the context stack.\n\nSection and End Section tags SHOULD be treated as standalone when\nappropriate.\n",
"overview": "Section tags and End Section tags are used in combination to wrap a section\nof the template for iteration.\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Section tag MUST be followed\nby an End Section tag with the same content within the same section.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\n 1) If the name is a single period (.), the data is the item currently\n sitting atop the context stack. Skip the rest of these steps.\n 2) Split the name on periods; the first part is the name to resolve, any\n remaining parts should be retained.\n 3) Walk the context stack from top to bottom, finding the first context\n that is a) a hash containing the name as a key OR b) an object responding\n to a method with the given name.\n 4) If the context is a hash, the data is the value associated with the\n name.\n 5) If the context is an object and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 6) Otherwise, the data is the value returned by calling the method with\n the given name.\n 7) If any name parts were retained in step 1, each should be resolved\n against a context stack containing only the result from the former\n resolution. If any part fails resolution, the result should be considered\n falsey, and should interpolate as the empty string.\n\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nFor each element in the data list, the element MUST be pushed onto the\ncontext stack, the section MUST be rendered, and the element MUST be popped\noff the context stack.\n\nSection and End Section tags SHOULD be treated as standalone when\nappropriate.\n",
"tests": [
{
"name": "Truthy",
Expand Down Expand Up @@ -246,6 +246,62 @@
"template": "\"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}\"",
"expected": "\"(123)(abc)\""
},
{
"name": "Implicit Iterator - HTML Escaping",
"desc": "Implicit iterators with basic interpolation should be HTML escaped.",
"data": {
"list": [
"&",
"\"",
"<",
">"
]
},
"template": "\"{{#list}}({{.}}){{/list}}\"",
"expected": "\"(&amp;)(&quot;)(&lt;)(&gt;)\""
},
{
"name": "Implicit Iterator - Triple mustache",
"desc": "Implicit iterators in triple mustache should interpolate without HTML escaping.",
"data": {
"list": [
"&",
"\"",
"<",
">"
]
},
"template": "\"{{#list}}({{{.}}}){{/list}}\"",
"expected": "\"(&)(\")(<)(>)\""
},
{
"name": "Implicit Iterator - Ampersand",
"desc": "Implicit iterators in an Ampersand tag should interpolate without HTML escaping.",
"data": {
"list": [
"&",
"\"",
"<",
">"
]
},
"template": "\"{{#list}}({{&.}}){{/list}}\"",
"expected": "\"(&)(\")(<)(>)\""
},
{
"name": "Implicit Iterator - Root-level",
"desc": "Implicit iterators should work on root-level lists.",
"data": [
{
"value": "a"
},
{
"value": "b"
}
],
"template": "\"{{#.}}({{value}}){{/.}}\"",
"expected": "\"(a)(b)\""
},
{
"name": "Dotted Names - Truthy",
"desc": "Dotted names should be valid for Section tags.",
Expand Down
44 changes: 37 additions & 7 deletions compiler/src/test/resources/spec/specs/sections.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
overview: |
Section tags and End Section tags are used in combination to wrap a section
of the template for iteration
of the template for iteration.
These tags' content MUST be a non-whitespace character sequence NOT
containing the current closing delimiter; each Section tag MUST be followed
by an End Section tag with the same content within the same section.
This tag's content names the data to replace the tag. Name resolution is as
follows:
1) Split the name on periods; the first part is the name to resolve, any
1) If the name is a single period (.), the data is the item currently
sitting atop the context stack. Skip the rest of these steps.
2) Split the name on periods; the first part is the name to resolve, any
remaining parts should be retained.
2) Walk the context stack from top to bottom, finding the first context
3) Walk the context stack from top to bottom, finding the first context
that is a) a hash containing the name as a key OR b) an object responding
to a method with the given name.
3) If the context is a hash, the data is the value associated with the
4) If the context is a hash, the data is the value associated with the
name.
4) If the context is an object and the method with the given name has an
5) If the context is an object and the method with the given name has an
arity of 1, the method SHOULD be called with a String containing the
unprocessed contents of the sections; the data is the value returned.
5) Otherwise, the data is the value returned by calling the method with
6) Otherwise, the data is the value returned by calling the method with
the given name.
6) If any name parts were retained in step 1, each should be resolved
7) If any name parts were retained in step 1, each should be resolved
against a context stack containing only the result from the former
resolution. If any part fails resolution, the result should be considered
falsey, and should interpolate as the empty string.
If the data is not of a list type, it is coerced into a list as follows: if
the data is truthy (e.g. `!!data == true`), use a single-element list
containing the data, otherwise use an empty list.
Expand Down Expand Up @@ -206,6 +209,33 @@ tests:
template: '"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}"'
expected: '"(123)(abc)"'

- name: Implicit Iterator - HTML Escaping
desc: Implicit iterators with basic interpolation should be HTML escaped.
data:
list: [ '&', '"', '<', '>' ]
template: '"{{#list}}({{.}}){{/list}}"'
expected: '"(&amp;)(&quot;)(&lt;)(&gt;)"'

- name: Implicit Iterator - Triple mustache
desc: Implicit iterators in triple mustache should interpolate without HTML escaping.
data:
list: [ '&', '"', '<', '>' ]
template: '"{{#list}}({{{.}}}){{/list}}"'
expected: '"(&)(")(<)(>)"'

- name: Implicit Iterator - Ampersand
desc: Implicit iterators in an Ampersand tag should interpolate without HTML escaping.
data:
list: [ '&', '"', '<', '>' ]
template: '"{{#list}}({{&.}}){{/list}}"'
expected: '"(&)(")(<)(>)"'

- name: Implicit Iterator - Root-level
desc: Implicit iterators should work on root-level lists.
data: [ { value: 'a' }, { value: 'b' } ]
template: '"{{#.}}({{value}}){{/.}}"'
expected: '"(a)(b)"'

# Dotted Names

- name: Dotted Names - Truthy
Expand Down
Loading

0 comments on commit 9cc19a4

Please sign in to comment.