summaryrefslogtreecommitdiff
path: root/packages/fcl-mustache/tests
diff options
context:
space:
mode:
authornickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2>2021-04-27 23:11:09 +0000
committernickysn <nickysn@3ad0048d-3df7-0310-abae-a5850022a9f2>2021-04-27 23:11:09 +0000
commita491c935588745154b576226b73833bac78fcb6e (patch)
tree973289073fb5d21573a6be2b5cfeba9abd2a9472 /packages/fcl-mustache/tests
parent38b5e0606069cc5985e995e1da5b6855db67f507 (diff)
parentae5b0de491a91321675f73eae5db628d068f4e05 (diff)
downloadfpc-a491c935588745154b576226b73833bac78fcb6e.tar.gz
* synchronized with trunkunicodekvm
git-svn-id: https://svn.freepascal.org/svn/fpc/branches/unicodekvm@49282 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'packages/fcl-mustache/tests')
-rw-r--r--packages/fcl-mustache/tests/spec/comments.json1
-rw-r--r--packages/fcl-mustache/tests/spec/delimiters.json1
-rw-r--r--packages/fcl-mustache/tests/spec/interpolation.json1
-rw-r--r--packages/fcl-mustache/tests/spec/inverted.json1
-rw-r--r--packages/fcl-mustache/tests/spec/partials.json1
-rw-r--r--packages/fcl-mustache/tests/spec/sections.json1
-rw-r--r--packages/fcl-mustache/tests/tcbasemustache.pas290
-rw-r--r--packages/fcl-mustache/tests/tcdbmustache.pas149
-rw-r--r--packages/fcl-mustache/tests/tcexmustache.pas199
-rw-r--r--packages/fcl-mustache/tests/tcmustache.pas728
-rw-r--r--packages/fcl-mustache/tests/tcspecs.pas188
-rw-r--r--packages/fcl-mustache/tests/testmustache.lpi88
-rw-r--r--packages/fcl-mustache/tests/testmustache.lpr29
13 files changed, 1677 insertions, 0 deletions
diff --git a/packages/fcl-mustache/tests/spec/comments.json b/packages/fcl-mustache/tests/spec/comments.json
new file mode 100644
index 0000000000..30cb927e62
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/comments.json
@@ -0,0 +1 @@
+{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Comment tags represent content that should never appear in the resulting\noutput.\n\nThe tag's content may contain any substring (including newlines) EXCEPT the\nclosing delimiter.\n\nComment tags SHOULD be treated as standalone when appropriate.\n","tests":[{"name":"Inline","data":{},"expected":"1234567890","template":"12345{{! Comment Block! }}67890","desc":"Comment blocks should be removed from the template."},{"name":"Multiline","data":{},"expected":"1234567890\n","template":"12345{{!\n This is a\n multi-line comment...\n}}67890\n","desc":"Multiline comments should be permitted."},{"name":"Standalone","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n{{! Comment Block! }}\nEnd.\n","desc":"All standalone comment lines should be removed."},{"name":"Indented Standalone","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n {{! Indented Comment Block! }}\nEnd.\n","desc":"All standalone comment lines should be removed."},{"name":"Standalone Line Endings","data":{},"expected":"|\r\n|","template":"|\r\n{{! Standalone Comment }}\r\n|","desc":"\"\\r\\n\" should be considered a newline for standalone tags."},{"name":"Standalone Without Previous Line","data":{},"expected":"!","template":" {{! I'm Still Standalone }}\n!","desc":"Standalone tags should not require a newline to precede them."},{"name":"Standalone Without Newline","data":{},"expected":"!\n","template":"!\n {{! I'm Still Standalone }}","desc":"Standalone tags should not require a newline to follow them."},{"name":"Multiline Standalone","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n{{!\nSomething's going on here...\n}}\nEnd.\n","desc":"All standalone comment lines should be removed."},{"name":"Indented Multiline Standalone","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n {{!\n Something's going on here...\n }}\nEnd.\n","desc":"All standalone comment lines should be removed."},{"name":"Indented Inline","data":{},"expected":" 12 \n","template":" 12 {{! 34 }}\n","desc":"Inline comments should not strip whitespace"},{"name":"Surrounding Whitespace","data":{},"expected":"12345 67890","template":"12345 {{! Comment Block! }} 67890","desc":"Comment removal should preserve surrounding whitespace."}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/spec/delimiters.json b/packages/fcl-mustache/tests/spec/delimiters.json
new file mode 100644
index 0000000000..fcf95888db
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/delimiters.json
@@ -0,0 +1 @@
+{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Set Delimiter tags are used to change the tag delimiters for all content\nfollowing the tag in the current compilation unit.\n\nThe tag's content MUST be any two non-whitespace sequences (separated by\nwhitespace) EXCEPT an equals sign ('=') followed by the current closing\ndelimiter.\n\nSet Delimiter tags SHOULD be treated as standalone when appropriate.\n","tests":[{"name":"Pair Behavior","data":{"text":"Hey!"},"expected":"(Hey!)","template":"{{=<% %>=}}(<%text%>)","desc":"The equals sign (used on both sides) should permit delimiter changes."},{"name":"Special Characters","data":{"text":"It worked!"},"expected":"(It worked!)","template":"({{=[ ]=}}[text])","desc":"Characters with special meaning regexen should be valid delimiters."},{"name":"Sections","data":{"section":true,"data":"I got interpolated."},"expected":"[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n","template":"[\n{{#section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|#section|\n {{data}}\n |data|\n|/section|\n]\n","desc":"Delimiters set outside sections should persist."},{"name":"Inverted Sections","data":{"section":false,"data":"I got interpolated."},"expected":"[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n","template":"[\n{{^section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|^section|\n {{data}}\n |data|\n|/section|\n]\n","desc":"Delimiters set outside inverted sections should persist."},{"name":"Partial Inheritence","data":{"value":"yes"},"expected":"[ .yes. ]\n[ .yes. ]\n","template":"[ {{>include}} ]\n{{= | | =}}\n[ |>include| ]\n","desc":"Delimiters set in a parent template should not affect a partial.","partials":{"include":".{{value}}."}},{"name":"Post-Partial Behavior","data":{"value":"yes"},"expected":"[ .yes. .yes. ]\n[ .yes. .|value|. ]\n","template":"[ {{>include}} ]\n[ .{{value}}. .|value|. ]\n","desc":"Delimiters set in a partial should not affect the parent template.","partials":{"include":".{{value}}. {{= | | =}} .|value|."}},{"name":"Surrounding Whitespace","data":{},"expected":"| |","template":"| {{=@ @=}} |","desc":"Surrounding whitespace should be left untouched."},{"name":"Outlying Whitespace (Inline)","data":{},"expected":" | \n","template":" | {{=@ @=}}\n","desc":"Whitespace should be left untouched."},{"name":"Standalone Tag","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n{{=@ @=}}\nEnd.\n","desc":"Standalone lines should be removed from the template."},{"name":"Indented Standalone Tag","data":{},"expected":"Begin.\nEnd.\n","template":"Begin.\n {{=@ @=}}\nEnd.\n","desc":"Indented standalone lines should be removed from the template."},{"name":"Standalone Line Endings","data":{},"expected":"|\r\n|","template":"|\r\n{{= @ @ =}}\r\n|","desc":"\"\\r\\n\" should be considered a newline for standalone tags."},{"name":"Standalone Without Previous Line","data":{},"expected":"=","template":" {{=@ @=}}\n=","desc":"Standalone tags should not require a newline to precede them."},{"name":"Standalone Without Newline","data":{},"expected":"=\n","template":"=\n {{=@ @=}}","desc":"Standalone tags should not require a newline to follow them."},{"name":"Pair with Padding","data":{},"expected":"||","template":"|{{= @ @ =}}|","desc":"Superfluous in-tag whitespace should be ignored."}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/spec/interpolation.json b/packages/fcl-mustache/tests/spec/interpolation.json
new file mode 100644
index 0000000000..d1a1a32897
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/interpolation.json
@@ -0,0 +1 @@
+{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Interpolation tags are used to integrate dynamic content into the template.\n\nThe tag's content MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter.\n\nThis tag's content names the data to replace the tag. A single period (`.`)\nindicates that the item currently sitting atop the context stack should be\nused; otherwise, name resolution is as follows:\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, the data is the value returned by the\n method with the given name.\n 5) 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.\nData should be coerced into a string (and escaped, if appropriate) before\ninterpolation.\n\nThe Interpolation tags MUST NOT be treated as standalone.\n","tests":[{"name":"No Interpolation","data":{},"expected":"Hello from {Mustache}!\n","template":"Hello from {Mustache}!\n","desc":"Mustache-free templates should render as-is."},{"name":"Basic Interpolation","data":{"subject":"world"},"expected":"Hello, world!\n","template":"Hello, {{subject}}!\n","desc":"Unadorned tags should interpolate content into the template."},{"name":"HTML Escaping","data":{"forbidden":"& \" < >"},"expected":"These characters should be HTML escaped: &amp; &quot; &lt; &gt;\n","template":"These characters should be HTML escaped: {{forbidden}}\n","desc":"Basic interpolation should be HTML escaped."},{"name":"Triple Mustache","data":{"forbidden":"& \" < >"},"expected":"These characters should not be HTML escaped: & \" < >\n","template":"These characters should not be HTML escaped: {{{forbidden}}}\n","desc":"Triple mustaches should interpolate without HTML escaping."},{"name":"Ampersand","data":{"forbidden":"& \" < >"},"expected":"These characters should not be HTML escaped: & \" < >\n","template":"These characters should not be HTML escaped: {{&forbidden}}\n","desc":"Ampersand should interpolate without HTML escaping."},{"name":"Basic Integer Interpolation","data":{"mph":85},"expected":"\"85 miles an hour!\"","template":"\"{{mph}} miles an hour!\"","desc":"Integers should interpolate seamlessly."},{"name":"Triple Mustache Integer Interpolation","data":{"mph":85},"expected":"\"85 miles an hour!\"","template":"\"{{{mph}}} miles an hour!\"","desc":"Integers should interpolate seamlessly."},{"name":"Ampersand Integer Interpolation","data":{"mph":85},"expected":"\"85 miles an hour!\"","template":"\"{{&mph}} miles an hour!\"","desc":"Integers should interpolate seamlessly."},{"name":"Basic Decimal Interpolation","data":{"power":1.21},"expected":"\"1.21 jiggawatts!\"","template":"\"{{power}} jiggawatts!\"","desc":"Decimals should interpolate seamlessly with proper significance."},{"name":"Triple Mustache Decimal Interpolation","data":{"power":1.21},"expected":"\"1.21 jiggawatts!\"","template":"\"{{{power}}} jiggawatts!\"","desc":"Decimals should interpolate seamlessly with proper significance."},{"name":"Ampersand Decimal Interpolation","data":{"power":1.21},"expected":"\"1.21 jiggawatts!\"","template":"\"{{&power}} jiggawatts!\"","desc":"Decimals should interpolate seamlessly with proper significance."},{"name":"Basic Context Miss Interpolation","data":{},"expected":"I () be seen!","template":"I ({{cannot}}) be seen!","desc":"Failed context lookups should default to empty strings."},{"name":"Triple Mustache Context Miss Interpolation","data":{},"expected":"I () be seen!","template":"I ({{{cannot}}}) be seen!","desc":"Failed context lookups should default to empty strings."},{"name":"Ampersand Context Miss Interpolation","data":{},"expected":"I () be seen!","template":"I ({{&cannot}}) be seen!","desc":"Failed context lookups should default to empty strings."},{"name":"Dotted Names - Basic Interpolation","data":{"person":{"name":"Joe"}},"expected":"\"Joe\" == \"Joe\"","template":"\"{{person.name}}\" == \"{{#person}}{{name}}{{/person}}\"","desc":"Dotted names should be considered a form of shorthand for sections."},{"name":"Dotted Names - Triple Mustache Interpolation","data":{"person":{"name":"Joe"}},"expected":"\"Joe\" == \"Joe\"","template":"\"{{{person.name}}}\" == \"{{#person}}{{{name}}}{{/person}}\"","desc":"Dotted names should be considered a form of shorthand for sections."},{"name":"Dotted Names - Ampersand Interpolation","data":{"person":{"name":"Joe"}},"expected":"\"Joe\" == \"Joe\"","template":"\"{{&person.name}}\" == \"{{#person}}{{&name}}{{/person}}\"","desc":"Dotted names should be considered a form of shorthand for sections."},{"name":"Dotted Names - Arbitrary Depth","data":{"a":{"b":{"c":{"d":{"e":{"name":"Phil"}}}}}},"expected":"\"Phil\" == \"Phil\"","template":"\"{{a.b.c.d.e.name}}\" == \"Phil\"","desc":"Dotted names should be functional to any level of nesting."},{"name":"Dotted Names - Broken Chains","data":{"a":{}},"expected":"\"\" == \"\"","template":"\"{{a.b.c}}\" == \"\"","desc":"Any falsey value prior to the last part of the name should yield ''."},{"name":"Dotted Names - Broken Chain Resolution","data":{"a":{"b":{}},"c":{"name":"Jim"}},"expected":"\"\" == \"\"","template":"\"{{a.b.c.name}}\" == \"\"","desc":"Each part of a dotted name should resolve only against its parent."},{"name":"Dotted Names - Initial Resolution","data":{"a":{"b":{"c":{"d":{"e":{"name":"Phil"}}}}},"b":{"c":{"d":{"e":{"name":"Wrong"}}}}},"expected":"\"Phil\" == \"Phil\"","template":"\"{{#a}}{{b.c.d.e.name}}{{/a}}\" == \"Phil\"","desc":"The first part of a dotted name should resolve as any other name."},{"name":"Interpolation - Surrounding Whitespace","data":{"string":"---"},"expected":"| --- |","template":"| {{string}} |","desc":"Interpolation should not alter surrounding whitespace."},{"name":"Triple Mustache - Surrounding Whitespace","data":{"string":"---"},"expected":"| --- |","template":"| {{{string}}} |","desc":"Interpolation should not alter surrounding whitespace."},{"name":"Ampersand - Surrounding Whitespace","data":{"string":"---"},"expected":"| --- |","template":"| {{&string}} |","desc":"Interpolation should not alter surrounding whitespace."},{"name":"Interpolation - Standalone","data":{"string":"---"},"expected":" ---\n","template":" {{string}}\n","desc":"Standalone interpolation should not alter surrounding whitespace."},{"name":"Triple Mustache - Standalone","data":{"string":"---"},"expected":" ---\n","template":" {{{string}}}\n","desc":"Standalone interpolation should not alter surrounding whitespace."},{"name":"Ampersand - Standalone","data":{"string":"---"},"expected":" ---\n","template":" {{&string}}\n","desc":"Standalone interpolation should not alter surrounding whitespace."},{"name":"Interpolation With Padding","data":{"string":"---"},"expected":"|---|","template":"|{{ string }}|","desc":"Superfluous in-tag whitespace should be ignored."},{"name":"Triple Mustache With Padding","data":{"string":"---"},"expected":"|---|","template":"|{{{ string }}}|","desc":"Superfluous in-tag whitespace should be ignored."},{"name":"Ampersand With Padding","data":{"string":"---"},"expected":"|---|","template":"|{{& string }}|","desc":"Superfluous in-tag whitespace should be ignored."}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/spec/inverted.json b/packages/fcl-mustache/tests/spec/inverted.json
new file mode 100644
index 0000000000..c9b550b964
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/inverted.json
@@ -0,0 +1 @@
+{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Inverted Section tags and End Section tags are used in combination to wrap a\nsection of the template.\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Inverted Section tag MUST be\nfollowed by an End Section tag with the same content within the same\nsection.\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\nThis section MUST NOT be rendered unless the data list is empty.\n\nInverted Section and End Section tags SHOULD be treated as standalone when\nappropriate.\n","tests":[{"name":"Falsey","data":{"boolean":false},"expected":"\"This should be rendered.\"","template":"\"{{^boolean}}This should be rendered.{{/boolean}}\"","desc":"Falsey sections should have their contents rendered."},{"name":"Truthy","data":{"boolean":true},"expected":"\"\"","template":"\"{{^boolean}}This should not be rendered.{{/boolean}}\"","desc":"Truthy sections should have their contents omitted."},{"name":"Context","data":{"context":{"name":"Joe"}},"expected":"\"\"","template":"\"{{^context}}Hi {{name}}.{{/context}}\"","desc":"Objects and hashes should behave like truthy values."},{"name":"List","data":{"list":[{"n":1},{"n":2},{"n":3}]},"expected":"\"\"","template":"\"{{^list}}{{n}}{{/list}}\"","desc":"Lists should behave like truthy values."},{"name":"Empty List","data":{"list":[]},"expected":"\"Yay lists!\"","template":"\"{{^list}}Yay lists!{{/list}}\"","desc":"Empty lists should behave like falsey values."},{"name":"Doubled","data":{"two":"second","bool":false},"expected":"* first\n* second\n* third\n","template":"{{^bool}}\n* first\n{{/bool}}\n* {{two}}\n{{^bool}}\n* third\n{{/bool}}\n","desc":"Multiple inverted sections per template should be permitted."},{"name":"Nested (Falsey)","data":{"bool":false},"expected":"| A B C D E |","template":"| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |","desc":"Nested falsey sections should have their contents rendered."},{"name":"Nested (Truthy)","data":{"bool":true},"expected":"| A E |","template":"| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |","desc":"Nested truthy sections should be omitted."},{"name":"Context Misses","data":{},"expected":"[Cannot find key 'missing'!]","template":"[{{^missing}}Cannot find key 'missing'!{{/missing}}]","desc":"Failed context lookups should be considered falsey."},{"name":"Dotted Names - Truthy","data":{"a":{"b":{"c":true}}},"expected":"\"\" == \"\"","template":"\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"\"","desc":"Dotted names should be valid for Inverted Section tags."},{"name":"Dotted Names - Falsey","data":{"a":{"b":{"c":false}}},"expected":"\"Not Here\" == \"Not Here\"","template":"\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\"","desc":"Dotted names should be valid for Inverted Section tags."},{"name":"Dotted Names - Broken Chains","data":{"a":{}},"expected":"\"Not Here\" == \"Not Here\"","template":"\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\"","desc":"Dotted names that cannot be resolved should be considered falsey."},{"name":"Surrounding Whitespace","data":{"boolean":false},"expected":" | \t|\t | \n","template":" | {{^boolean}}\t|\t{{/boolean}} | \n","desc":"Inverted sections should not alter surrounding whitespace."},{"name":"Internal Whitespace","data":{"boolean":false},"expected":" | \n | \n","template":" | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n","desc":"Inverted should not alter internal whitespace."},{"name":"Indented Inline Sections","data":{"boolean":false},"expected":" NO\n WAY\n","template":" {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n","desc":"Single-line sections should not alter surrounding whitespace."},{"name":"Standalone Lines","data":{"boolean":false},"expected":"| This Is\n|\n| A Line\n","template":"| This Is\n{{^boolean}}\n|\n{{/boolean}}\n| A Line\n","desc":"Standalone lines should be removed from the template."},{"name":"Standalone Indented Lines","data":{"boolean":false},"expected":"| This Is\n|\n| A Line\n","template":"| This Is\n {{^boolean}}\n|\n {{/boolean}}\n| A Line\n","desc":"Standalone indented lines should be removed from the template."},{"name":"Standalone Line Endings","data":{"boolean":false},"expected":"|\r\n|","template":"|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|","desc":"\"\\r\\n\" should be considered a newline for standalone tags."},{"name":"Standalone Without Previous Line","data":{"boolean":false},"expected":"^\n/","template":" {{^boolean}}\n^{{/boolean}}\n/","desc":"Standalone tags should not require a newline to precede them."},{"name":"Standalone Without Newline","data":{"boolean":false},"expected":"^\n/\n","template":"^{{^boolean}}\n/\n {{/boolean}}","desc":"Standalone tags should not require a newline to follow them."},{"name":"Padding","data":{"boolean":false},"expected":"|=|","template":"|{{^ boolean }}={{/ boolean }}|","desc":"Superfluous in-tag whitespace should be ignored."}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/spec/partials.json b/packages/fcl-mustache/tests/spec/partials.json
new file mode 100644
index 0000000000..e5f21a2a48
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/partials.json
@@ -0,0 +1 @@
+{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Partial tags are used to expand an external template into the current\ntemplate.\n\nThe tag's content MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter.\n\nThis tag's content names the partial to inject. Set Delimiter tags MUST NOT\naffect the parsing of a partial. The partial MUST be rendered against the\ncontext stack local to the tag. If the named partial cannot be found, the\nempty string SHOULD be used instead, as in interpolations.\n\nPartial tags SHOULD be treated as standalone when appropriate. If this tag\nis used standalone, any whitespace preceding the tag should treated as\nindentation, and prepended to each line of the partial before rendering.\n","tests":[{"name":"Basic Behavior","data":{},"expected":"\"from partial\"","template":"\"{{>text}}\"","desc":"The greater-than operator should expand to the named partial.","partials":{"text":"from partial"}},{"name":"Failed Lookup","data":{},"expected":"\"\"","template":"\"{{>text}}\"","desc":"The empty string should be used when the named partial is not found.","partials":{}},{"name":"Context","data":{"text":"content"},"expected":"\"*content*\"","template":"\"{{>partial}}\"","desc":"The greater-than operator should operate within the current context.","partials":{"partial":"*{{text}}*"}},{"name":"Recursion","data":{"content":"X","nodes":[{"content":"Y","nodes":[]}]},"expected":"X<Y<>>","template":"{{>node}}","desc":"The greater-than operator should properly recurse.","partials":{"node":"{{content}}<{{#nodes}}{{>node}}{{/nodes}}>"}},{"name":"Surrounding Whitespace","data":{},"expected":"| \t|\t |","template":"| {{>partial}} |","desc":"The greater-than operator should not alter surrounding whitespace.","partials":{"partial":"\t|\t"}},{"name":"Inline Indentation","data":{"data":"|"},"expected":" | >\n>\n","template":" {{data}} {{> partial}}\n","desc":"Whitespace should be left untouched.","partials":{"partial":">\n>"}},{"name":"Standalone Line Endings","data":{},"expected":"|\r\n>|","template":"|\r\n{{>partial}}\r\n|","desc":"\"\\r\\n\" should be considered a newline for standalone tags.","partials":{"partial":">"}},{"name":"Standalone Without Previous Line","data":{},"expected":" >\n >>","template":" {{>partial}}\n>","desc":"Standalone tags should not require a newline to precede them.","partials":{"partial":">\n>"}},{"name":"Standalone Without Newline","data":{},"expected":">\n >\n >","template":">\n {{>partial}}","desc":"Standalone tags should not require a newline to follow them.","partials":{"partial":">\n>"}},{"name":"Standalone Indentation","data":{"content":"<\n->"},"expected":"\\\n |\n <\n->\n |\n/\n","template":"\\\n {{>partial}}\n/\n","desc":"Each line of the partial should be indented before rendering.","partials":{"partial":"|\n{{{content}}}\n|\n"}},{"name":"Padding Whitespace","data":{"boolean":true},"expected":"|[]|","template":"|{{> partial }}|","desc":"Superfluous in-tag whitespace should be ignored.","partials":{"partial":"[]"}}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/spec/sections.json b/packages/fcl-mustache/tests/spec/sections.json
new file mode 100644
index 0000000000..795f6acd74
--- /dev/null
+++ b/packages/fcl-mustache/tests/spec/sections.json
@@ -0,0 +1 @@
+{"__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","tests":[{"name":"Truthy","data":{"boolean":true},"expected":"\"This should be rendered.\"","template":"\"{{#boolean}}This should be rendered.{{/boolean}}\"","desc":"Truthy sections should have their contents rendered."},{"name":"Falsey","data":{"boolean":false},"expected":"\"\"","template":"\"{{#boolean}}This should not be rendered.{{/boolean}}\"","desc":"Falsey sections should have their contents omitted."},{"name":"Context","data":{"context":{"name":"Joe"}},"expected":"\"Hi Joe.\"","template":"\"{{#context}}Hi {{name}}.{{/context}}\"","desc":"Objects and hashes should be pushed onto the context stack."},{"name":"Deeply Nested Contexts","data":{"a":{"one":1},"b":{"two":2},"c":{"three":3},"d":{"four":4},"e":{"five":5}},"expected":"1\n121\n12321\n1234321\n123454321\n1234321\n12321\n121\n1\n","template":"{{#a}}\n{{one}}\n{{#b}}\n{{one}}{{two}}{{one}}\n{{#c}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{#d}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{#e}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{/e}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{/d}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{/c}}\n{{one}}{{two}}{{one}}\n{{/b}}\n{{one}}\n{{/a}}\n","desc":"All elements on the context stack should be accessible."},{"name":"List","data":{"list":[{"item":1},{"item":2},{"item":3}]},"expected":"\"123\"","template":"\"{{#list}}{{item}}{{/list}}\"","desc":"Lists should be iterated; list items should visit the context stack."},{"name":"Empty List","data":{"list":[]},"expected":"\"\"","template":"\"{{#list}}Yay lists!{{/list}}\"","desc":"Empty lists should behave like falsey values."},{"name":"Doubled","data":{"two":"second","bool":true},"expected":"* first\n* second\n* third\n","template":"{{#bool}}\n* first\n{{/bool}}\n* {{two}}\n{{#bool}}\n* third\n{{/bool}}\n","desc":"Multiple sections per template should be permitted."},{"name":"Nested (Truthy)","data":{"bool":true},"expected":"| A B C D E |","template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","desc":"Nested truthy sections should have their contents rendered."},{"name":"Nested (Falsey)","data":{"bool":false},"expected":"| A E |","template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","desc":"Nested falsey sections should be omitted."},{"name":"Context Misses","data":{},"expected":"[]","template":"[{{#missing}}Found key 'missing'!{{/missing}}]","desc":"Failed context lookups should be considered falsey."},{"name":"Implicit Iterator - String","data":{"list":["a","b","c","d","e"]},"expected":"\"(a)(b)(c)(d)(e)\"","template":"\"{{#list}}({{.}}){{/list}}\"","desc":"Implicit iterators should directly interpolate strings."},{"name":"Implicit Iterator - Integer","data":{"list":[1,2,3,4,5]},"expected":"\"(1)(2)(3)(4)(5)\"","template":"\"{{#list}}({{.}}){{/list}}\"","desc":"Implicit iterators should cast integers to strings and interpolate."},{"name":"Implicit Iterator - Decimal","data":{"list":[1.1,2.2,3.3,4.4,5.5]},"expected":"\"(1.1)(2.2)(3.3)(4.4)(5.5)\"","template":"\"{{#list}}({{.}}){{/list}}\"","desc":"Implicit iterators should cast decimals to strings and interpolate."},{"name":"Implicit Iterator - Array","desc":"Implicit iterators should allow iterating over nested arrays.","data":{"list":[[1,2,3],["a","b","c"]]},"template":"\"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}\"","expected":"\"(123)(abc)\""},{"name":"Dotted Names - Truthy","data":{"a":{"b":{"c":true}}},"expected":"\"Here\" == \"Here\"","template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"Here\"","desc":"Dotted names should be valid for Section tags."},{"name":"Dotted Names - Falsey","data":{"a":{"b":{"c":false}}},"expected":"\"\" == \"\"","template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","desc":"Dotted names should be valid for Section tags."},{"name":"Dotted Names - Broken Chains","data":{"a":{}},"expected":"\"\" == \"\"","template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","desc":"Dotted names that cannot be resolved should be considered falsey."},{"name":"Surrounding Whitespace","data":{"boolean":true},"expected":" | \t|\t | \n","template":" | {{#boolean}}\t|\t{{/boolean}} | \n","desc":"Sections should not alter surrounding whitespace."},{"name":"Internal Whitespace","data":{"boolean":true},"expected":" | \n | \n","template":" | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n","desc":"Sections should not alter internal whitespace."},{"name":"Indented Inline Sections","data":{"boolean":true},"expected":" YES\n GOOD\n","template":" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n","desc":"Single-line sections should not alter surrounding whitespace."},{"name":"Standalone Lines","data":{"boolean":true},"expected":"| This Is\n|\n| A Line\n","template":"| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n","desc":"Standalone lines should be removed from the template."},{"name":"Indented Standalone Lines","data":{"boolean":true},"expected":"| This Is\n|\n| A Line\n","template":"| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n","desc":"Indented standalone lines should be removed from the template."},{"name":"Standalone Line Endings","data":{"boolean":true},"expected":"|\r\n|","template":"|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|","desc":"\"\\r\\n\" should be considered a newline for standalone tags."},{"name":"Standalone Without Previous Line","data":{"boolean":true},"expected":"#\n/","template":" {{#boolean}}\n#{{/boolean}}\n/","desc":"Standalone tags should not require a newline to precede them."},{"name":"Standalone Without Newline","data":{"boolean":true},"expected":"#\n/\n","template":"#{{#boolean}}\n/\n {{/boolean}}","desc":"Standalone tags should not require a newline to follow them."},{"name":"Padding","data":{"boolean":true},"expected":"|=|","template":"|{{# boolean }}={{/ boolean }}|","desc":"Superfluous in-tag whitespace should be ignored."}]} \ No newline at end of file
diff --git a/packages/fcl-mustache/tests/tcbasemustache.pas b/packages/fcl-mustache/tests/tcbasemustache.pas
new file mode 100644
index 0000000000..80abdf9e37
--- /dev/null
+++ b/packages/fcl-mustache/tests/tcbasemustache.pas
@@ -0,0 +1,290 @@
+{
+ This file is part of the Free Pascal Run time library.
+ Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
+
+ Helper classes for Mustache test cases
+
+ See the File COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+unit tcbasemustache;
+
+{$mode ObjFPC}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils, fpcunit, fpmustache;
+
+type
+
+ { TTestContext }
+
+ (* StringList with following encoding
+ // Null value
+ aName=<null>
+ // false value
+ aName=<null>
+ // plain value
+ aName=AValue
+ // Object value & member. Object value must be present
+ SubObj={}
+ SubObj.aName=aValue
+ // Array and members. Array value must be present
+ SubObj.SubArr=[]
+ SubObj.SubArr[0]={}
+ SubObj.SubArr[0].aName=aValue
+ SubObj.SubArr[1]={}
+ Subobj.SubArr[1].aName=aValue
+ *)
+
+ TTestContext = class (TMustacheContext)
+ Private
+ FValues : TStringList;
+ FPath : String;
+ public
+ Constructor Create(aCallback: TGetTextValueEvent); override;
+ Destructor destroy; override;
+ Function GetTextValue(Const aName : TMustacheString) : TMustacheString; override;
+ Function MoveNextSectionItem(Const aName : TMustacheString) : Boolean; override;
+ Function PushSection(Const aName : TMustacheString) : TMustacheSectionType; override;
+ Procedure PopSection(Const aName : TMustacheString); override;
+ Procedure SetValue(const aPath,aValue : string);
+ Property Values : TStringList read FValues;
+ end;
+
+ TBaseMustacheTest = class(TTestCase)
+ Private
+ FPartials: TStrings;
+ FTemplate: String;
+ FResult: TMustacheElement;
+ FParser: TMustacheParser;
+ Protected
+ Function CreateParser : TMustacheParser; virtual; abstract;
+ Procedure DoGetPartial(const aName: TMustacheString; var aHandled: Boolean; var aValue: TMustacheString);
+ Public
+ Class Procedure AssertEquals(Msg : String; aExpected,aActual : TMustacheElementType); overload;
+ Class Function AssertElement(aParent : TMustacheElement; aIndex: Integer; aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil) : TMustacheElement; overload;
+ Function AssertElement(aIndex: Integer; aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil) : TMustacheElement; overload;
+ Procedure AssertResultCount(aCount : Integer);
+ procedure SetUp; override;
+ procedure TearDown; override;
+ Procedure CallParser;
+ Procedure AddPartial(Const aName,aText: TMustacheString);
+ Property Partials : TStrings Read FPartials;
+ Property Template : String Read FTemplate Write FTemplate;
+ property ParseResult : TMustacheElement Read FResult;
+ property Parser : TMustacheParser Read FParser;
+ end;
+
+
+implementation
+
+uses strutils, typinfo;
+
+{ TTestContext }
+
+constructor TTestContext.Create(aCallback: TGetTextValueEvent);
+begin
+ inherited Create(aCallback);
+ FValues:=TStringList.Create;
+ FValues.OwnsObjects:=True;
+end;
+
+destructor TTestContext.destroy;
+begin
+ FreeAndNil(FValues);
+ inherited destroy;
+end;
+
+function TTestContext.GetTextValue(const aName: TMustacheString
+ ): TMustacheString;
+
+Var
+ aPath,N : String;
+ Done : Boolean;
+begin
+ Result:='';
+ aPath:=FPath;
+ Done:=False;
+ Repeat
+ if aPath<>'' then
+ N:=aPath+'.'+aName
+ else
+ begin
+ N:=aName;
+ Done:=True;
+ end;
+ Result:=FValues.Values[N];
+ if not Done then
+ aPath:=Copy(aPath,1,RPos('.',aPath)-1);
+ until (Result<>'') or Done;
+end;
+
+function TTestContext.MoveNextSectionItem(const aName: TMustacheString
+ ): Boolean;
+
+Var
+ L,P,Idx : Integer;
+ N : String;
+
+begin
+ L:=Length(FPath);
+ if (L>0) and (FPath[L]=']') then
+ begin
+ P:=RPos('[',FPath)+1;
+ Idx:=StrToIntDef(Copy(FPath,P,L-P),-1);
+ N:=Copy(FPath,1,P-1)+IntToStr(Idx+1)+']';
+ Result:=FValues.Values[N]<>''; // We could check for {}
+ if Result then
+ FPath:=N;
+ end;
+
+end;
+
+function TTestContext.PushSection(const aName: TMustacheString): TMustacheSectionType;
+
+Var
+ aPath,S : String;
+
+begin
+ if FPath<>'' then
+ FPath:=FPath+'.';
+ aPath:=FPath+aName;
+ S:=Values.Values[aPath];
+ if S='{}' then
+ begin
+ FPath:=aPath;
+ result:=mstSingle;
+ end;
+ if S='[]' then
+ begin
+ if Values.Values[aPath+'[0]']='' then
+ Result:=mstNone
+ else
+ begin
+ FPath:=aPath+'[-1]';
+ result:=mstList;
+ end;
+ end
+ else if (s='<null>') or (s='<false>') or (s='') then
+ begin
+ Result:=mstNone;
+ end
+ else
+ begin
+ FPath:=aPath;
+ result:=mstSingle;
+ end;
+
+end;
+
+procedure TTestContext.PopSection(const aName: TMustacheString);
+begin
+ FPath:=Copy(FPath,1,RPos('.',FPath)-1);
+end;
+
+procedure TTestContext.SetValue(const aPath, aValue: string);
+begin
+ Values.Values[aPath]:=aValue;
+end;
+
+
+{ TBaseMustacheTest }
+
+procedure TBaseMustacheTest.SetUp;
+
+begin
+ Inherited;
+ FParser:=CreateParser;
+ FParser.Partials:=TMustachePartialList.Create(metRoot,Nil,0);
+ FParser.OnGetPartial:=@DoGetPartial;
+ FPartials:=TStringList.Create;
+ TStringList(FPartials).OwnsObjects:=True;
+end;
+
+procedure TBaseMustacheTest.TearDown;
+
+begin
+ FreeAndNil(FPartials);
+ FreeAndNil(FResult);
+ FParser.Partials.Free;
+ FreeAndNil(FParser);
+ Inherited;
+end;
+
+procedure TBaseMustacheTest.DoGetPartial(const aName: TMustacheString;
+ var aHandled: Boolean; var aValue: TMustacheString);
+begin
+ aValue:=FPartials.Values[aName];
+ aHandled:=FPartials.IndexOfName(aName)<>-1;
+end;
+
+class function TBaseMustacheTest.AssertElement(aParent: TMustacheElement;
+ aIndex: Integer; aType: TMustacheElementType; aData: String;
+ aClass: TMustacheElementClass): TMustacheElement;
+Var
+ El : TMustacheElement;
+ aChild : String;
+begin
+ AssertNotNull('Have parent',aParent);
+ AssertTrue(Format('Index %d in range 0..%d',[aIndex,aParent.ChildCount-1]),(aIndex>=0) and (aIndex<aParent.ChildCount));
+ EL:=aParent.Children[aIndex];
+ aChild:=Format('Child %d',[aIndex]);
+ AssertNotNull('Have result '+aChild,El);
+ AssertEquals(aChild+' has correct type',aType,El.ElementType);
+ AssertEquals(aChild+' has correct data',aData,El.Data);
+ if (aClass<>Nil) then
+ AssertEquals(aChild+' has correct class',aClass,el.Classtype);
+ Result:=El;
+end;
+
+function TBaseMustacheTest.AssertElement(aIndex: Integer;
+ aType: TMustacheElementType; aData: String; aClass : TMustacheElementClass = Nil): TMustacheElement;
+
+begin
+ AssertNotNull('Have result',FResult);
+ Result:=AssertElement(FResult,aIndex,aType,aData,aClass);
+end;
+
+procedure TBaseMustacheTest.AssertResultCount(aCount: Integer);
+begin
+ AssertNotNull('Have result',FResult);
+ AssertEquals('Result count',aCount,FResult.ChildCount);
+end;
+
+
+procedure TBaseMustacheTest.CallParser;
+
+begin
+ Parser.Template:=Template;
+ FResult:=Parser.Parse;
+end;
+
+procedure TBaseMustacheTest.AddPartial(const aName, aText: TMustacheString);
+
+//Var
+// T : TMustacheTextElement;
+
+begin
+// T:=TMustacheTextElement.Create(metText,Nil,0);
+// T.Data:=aText;
+ FPartials.Add(aName+'='+atext);
+end;
+
+class procedure TBaseMustacheTest.AssertEquals(Msg: String; aExpected,
+ aActual: TMustacheElementType);
+
+begin
+ AssertEquals(Msg,GetEnumName(typeInfo(TMustacheElementType),Ord(aExpected)),
+ GetEnumName(typeInfo(TMustacheElementType),Ord(aActual)));
+end;
+
+
+end.
+
diff --git a/packages/fcl-mustache/tests/tcdbmustache.pas b/packages/fcl-mustache/tests/tcdbmustache.pas
new file mode 100644
index 0000000000..07710c9493
--- /dev/null
+++ b/packages/fcl-mustache/tests/tcdbmustache.pas
@@ -0,0 +1,149 @@
+{
+ This file is part of the Free Pascal Run time library.
+ Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
+
+ Test cases for DB Context for Mustache
+
+ See the File COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+unit tcdbmustache;
+
+{$mode ObjFPC}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils, fpcunit, testregistry, fpmustache, db, bufdataset, fpdbmustache;
+
+Type
+
+ { TTestMustacheDBContext }
+
+ TTestMustacheDBContext = Class(TTestCase)
+ private
+ FContext: TMustacheDBContext;
+ FDataset1: TBufDataset;
+ FDataset2: TBufDataset;
+ FMustache: TMustache;
+ Public
+ Procedure Setup; override;
+ Procedure TearDown; override;
+ Procedure CreateDataset1;
+ Procedure CreateDataset2;
+ Property Dataset1 : TBufDataset Read FDataset1;
+ Property Dataset2 : TBufDataset Read FDataset2;
+ Property Context : TMustacheDBContext Read FContext;
+ Property Mustache : TMustache Read FMustache;
+ Published
+ Procedure TestEmpty;
+ Procedure TestSingleSection;
+ Procedure TestTwoSections;
+ end;
+
+implementation
+
+Const
+ Template1 = '{{title}}! {{#Parents}}{{name}} {{age}} - {{/Parents}}';
+ Template2 = '{{title}}! {{#Parents}}{{name}}({{age}}) : {{#Children}}{{name}} {{age}},{{/Children}} - {{/Parents}}';
+
+{ TTestMustacheDBContext }
+
+procedure TTestMustacheDBContext.Setup;
+begin
+ Inherited;
+ FDataset1:=TBufDataset.Create(Nil);
+ FDataset1.Name:='Parents';
+ FDataset2:=TBufDataset.Create(Nil);
+ FDataset2.Name:='Children';
+ FContext:=TMustacheDBContext.Create(Nil);
+ FContext.StaticValues.Values['title']:='Family';
+ FMustache:=TMustache.Create(Nil);
+end;
+
+procedure TTestMustacheDBContext.TearDown;
+begin
+ FreeAndNil(FDataset1);
+ FreeAndNil(FDataset2);
+ FreeAndNil(FContext);
+ FreeAndNil(FMustache);
+end;
+
+procedure TTestMustacheDBContext.CreateDataset1;
+begin
+ FDataset1.FieldDefs.Add('name',ftString,20);
+ FDataset1.FieldDefs.Add('age',ftInteger);
+ FDataset1.CreateDataset;
+ FDataset1.Append;
+ FDataset1.FieldByName('name').AsString:='Father';
+ FDataset1.FieldByName('age').AsInteger:=40;
+ FDataset1.Post;
+ FDataset1.Append;
+ FDataset1.FieldByName('name').AsString:='Mother';
+ FDataset1.FieldByName('age').AsInteger:=39;
+ FDataset1.Post;
+ FDataset1.First;
+end;
+
+procedure TTestMustacheDBContext.CreateDataset2;
+begin
+ FDataset2.FieldDefs.Add('name',ftString,20);
+ FDataset2.FieldDefs.Add('age',ftInteger);
+ FDataset2.CreateDataset;
+ FDataset2.Append;
+ FDataset2.FieldByName('name').AsString:='Child1';
+ FDataset2.FieldByName('age').AsInteger:=4;
+ FDataset2.Post;
+ FDataset2.Append;
+ FDataset2.FieldByName('name').AsString:='Child2';
+ FDataset2.FieldByName('age').AsInteger:=2;
+ FDataset2.Post;
+ FDataset2.First;
+end;
+
+procedure TTestMustacheDBContext.TestEmpty;
+begin
+ AssertNotNull('Mustache',Mustache);
+ AssertNotNull('Dataset1',Dataset1);
+ AssertNotNull('Dataset2',Dataset2);
+ AssertNotNull('Context',Context);
+ AssertEquals('Context static','Family',Context.StaticValues.Values['title']);
+end;
+
+procedure TTestMustacheDBContext.TestSingleSection;
+
+Var
+ S : String;
+
+begin
+ Mustache.Template:=Template1;
+ CreateDataset1;
+ Context.AddDataset(FDataset1);
+ S:=Mustache.Render(Context);
+ AssertEquals('Correct result','Family! Father 40 - Mother 39 - ',S);
+end;
+
+procedure TTestMustacheDBContext.TestTwoSections;
+
+Var
+ S : String;
+
+begin
+ Mustache.Template:=Template2;
+ CreateDataset1;
+ CreateDataset2;
+ Context.AddDataset(FDataset1);
+ Context.AddDataset(FDataset2);
+ S:=Mustache.Render(Context);
+ AssertEquals('Correct result','Family! Father(40) : Child1 4,Child2 2, - Mother(39) : - ',S);
+end;
+
+initialization
+ RegisterTest(TTestMustacheDBContext);
+end.
+
diff --git a/packages/fcl-mustache/tests/tcexmustache.pas b/packages/fcl-mustache/tests/tcexmustache.pas
new file mode 100644
index 0000000000..b88ed9fba0
--- /dev/null
+++ b/packages/fcl-mustache/tests/tcexmustache.pas
@@ -0,0 +1,199 @@
+{
+ This file is part of the Free Pascal Run time library.
+ Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
+
+ Test cases for expression parser support
+
+ See the File COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+unit tcexmustache;
+
+{$mode ObjFPC}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils, fpcunit, fpjson, testregistry, fpmustache, tcbasemustache, fpexmustache, fpexprpars;
+
+Type
+
+ { TTestExMustacheParser }
+
+ TTestExMustacheParser = Class(TBaseMustacheTest)
+ private
+ FExpr: TFPExpressionParser;
+ FOutput: TMustacheStringOutput;
+ FContext : TMustacheJSONContext;
+ FData : TJSONData;
+ procedure GetVar(var Result: TFPExpressionResult; ConstRef
+ AName: ShortString);
+ Public
+ Procedure SetUp; override;
+ Procedure TearDown; override;
+ Function CreateParser: TMustacheParser; override;
+ Property Expr : TFPExpressionParser Read FExpr;
+ Property Output : TMustacheStringOutput Read FOutput;
+ Published
+ Procedure TestSimple;
+ Procedure TestRenderSimple;
+ Procedure TestRenderSection;
+ end;
+
+ { TTestMustacheExpr }
+
+ TTestMustacheExpr = Class(TTestCase)
+ private
+ FJSON: TJSONObject;
+ FMustache: TMustacheExpr;
+ public
+ Procedure SetUp; override;
+ Procedure TearDown; override;
+ Property Mustache : TMustacheExpr Read FMustache;
+ Property JSON : TJSONObject Read FJSON;
+ Published
+ Procedure TestEmpty;
+ Procedure TestRegisterVariables;
+ Procedure TestRenderSection;
+ procedure TestRenderSectionStaticVariables;
+ end;
+
+
+implementation
+
+Const
+ STestJSON = '{ "data" : [ { "name": "me", "age" : 10}, { "name": "you", "age" : 12 }, { "name": "he", "age" : 13 } ] }';
+
+{ TTestMustacheExpr }
+
+procedure TTestMustacheExpr.SetUp;
+begin
+ inherited SetUp;
+ FMustache:=TMustacheExpr.Create(Nil);
+ FJSON:=GetJSON(STestJSON) as TJSONObject;
+end;
+
+procedure TTestMustacheExpr.TearDown;
+begin
+ FreeAndNil(FJSON);
+ FreeAndNil(FMustache);
+ inherited TearDown;
+end;
+
+procedure TTestMustacheExpr.TestEmpty;
+begin
+ AssertNotNull('Have mustache instance',Mustache);
+ AssertNotNull('Have mustache expression engine instance',Mustache.ExpressionParser);
+end;
+
+procedure TTestMustacheExpr.TestRegisterVariables;
+begin
+ Mustache.RegisterVariables(JSON,'data[0]',True);
+ AssertEquals('Variable count',2,Mustache.ExpressionParser.Identifiers.Count);
+ AssertEquals('Variable 0','name',Mustache.ExpressionParser.Identifiers[0].Name);
+ AssertEquals('Variable 1','age',Mustache.ExpressionParser.Identifiers[1].Name);
+ AssertTrue('Variable 0 type',rtString=Mustache.ExpressionParser.Identifiers[0].ResultType);
+ AssertTrue('Variable 1 type',rtInteger=Mustache.ExpressionParser.Identifiers[1].ResultType);
+end;
+
+procedure TTestMustacheExpr.TestRenderSection;
+
+Var
+ S : String;
+
+Const
+ Template = '{{#data}}{{[name]}}:{{[age>11]}} {{/data}}';
+
+begin
+ Mustache.Template:=Template;
+ Mustache.RegisterVariables(JSON,'data[0]',True);
+ S:=Mustache.Render(JSON);
+ AssertEquals('Correct result','me:False you:True he:True ',S);
+end;
+
+procedure TTestMustacheExpr.TestRenderSectionStaticVariables;
+Var
+ S : String;
+
+Const
+ Template = '{{#data}}{{[name]}}:{{[age>11]}} {{/data}}';
+
+begin
+ Mustache.Template:=Template;
+ Mustache.RegisterVariables(JSON,'data[0]',False);
+ S:=Mustache.Render(JSON);
+ AssertEquals('Correct result','me:False me:False me:False ',S);
+end;
+
+
+{ TTestExMustacheParser }
+
+procedure TTestExMustacheParser.SetUp;
+begin
+ FExpr:=TFPExpressionParser.Create(Nil);
+ Foutput:=TMustacheStringOutput.Create;
+ inherited SetUp;
+end;
+
+procedure TTestExMustacheParser.TearDown;
+begin
+ inherited TearDown;
+ FreeAndNil(FExpr);
+ FreeAndNil(Foutput);
+ FreeAndNil(FContext);
+ FreeAndNil(FData);
+end;
+
+function TTestExMustacheParser.CreateParser: TMustacheParser;
+
+Var
+ P : TMustacheExprParser;
+
+begin
+ P:=TMustacheExprParser.Create;
+ P.ExprParser:=FExpr;
+ Result:=P;
+end;
+
+procedure TTestExMustacheParser.TestSimple;
+begin
+ Template:='{{[1+2]}}';
+ CallParser;
+ AssertElement(0,metVariable,'1+2',TMustacheExprElement);
+end;
+
+procedure TTestExMustacheParser.TestRenderSimple;
+begin
+ TestSimple;
+ ParseResult.Children[0].Render(Nil,Output,'',False);
+ AssertEquals('Correct result','3',Output.Data);
+end;
+
+procedure TTestExMustacheParser.GetVar(Var Result : TFPExpressionResult; ConstRef AName : ShortString);
+
+begin
+ Result.ResultType:=rtInteger;
+ Result.ResInteger:=StrToINt(FContext.GetTextValue('age'));
+end;
+
+procedure TTestExMustacheParser.TestRenderSection;
+begin
+ FData:=GetJSON(STestJSON);
+ FContext:=TMustacheJSONContext.Create(FData,Nil);
+ FExpr.Identifiers.AddVariable('age',rtInteger,@GetVar);
+ Template:='{{#data}}{{{name}}}:{{[age>11]}} {{/data}}';
+ CallParser;
+ ParseResult.Render(FContext,Output,'',False);
+ AssertEquals('Correct result','me:False you:True he:True ',Output.Data);
+end;
+
+initialization
+ RegisterTests([TTestExMustacheParser,TTestMustacheExpr]);
+end.
+
diff --git a/packages/fcl-mustache/tests/tcmustache.pas b/packages/fcl-mustache/tests/tcmustache.pas
new file mode 100644
index 0000000000..6175044b0b
--- /dev/null
+++ b/packages/fcl-mustache/tests/tcmustache.pas
@@ -0,0 +1,728 @@
+{
+ This file is part of the Free Pascal Run time library.
+ Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
+
+ Test cases for basic mustache parser support
+
+ See the File COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+
+unit tcmustache;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils, fpcunit, testregistry, fpmustache, tcbasemustache;
+
+type
+
+ { TTestMustacheParser }
+
+
+ TTestMustacheParser= class(TBaseMustacheTest)
+ private
+ protected
+ Function CreateParser : TMustacheParser; override;
+ Public
+ procedure SetUp; override;
+ procedure TearDown; override;
+ published
+ procedure TestEmpty;
+ procedure TestText;
+ procedure TestVariable;
+ procedure TestVariableErrNonClosed;
+ procedure TestVariableAlternateStartStop;
+ procedure TestDottedVariable;
+ procedure TestVariableNoUnescape;
+ procedure TestVariableNoUnescapeErrNonClosed;
+ procedure TestVariableNoUnescapeAlternateStartStop;
+ procedure TestComment;
+ procedure TestCommentSurround;
+ procedure TestCommentStandalone;
+ procedure TestCommentStandaloneSpaced;
+ procedure TestSetDelimiter;
+ procedure TestSetDelimiterErrInvalid;
+ procedure TestSection;
+ procedure TestSectionNested;
+ procedure TestSectionErrNotClosed;
+ procedure TestSectionErrWrongClosed;
+ procedure TestSectionErrNotStarted;
+ procedure TestTextSection;
+ procedure TestPartial;
+ end;
+
+ { TTestMustacheOutput }
+
+ TTestMustacheOutput = class(TTestCase)
+ Published
+ Procedure TestStringOutput;
+ end;
+
+ { TTestMustacheElement }
+
+ TTestMustacheElement = class(TTestCase)
+ private
+ FContext: TTestContext;
+ FEl: TMustacheElement;
+ Foutput: TMustacheStringOutput;
+ procedure DoCallBack(const aName: TMustacheString; var aHandled: Boolean;
+ var aValue: TMustacheString);
+ Public
+ Procedure SetUp; override;
+ Procedure TearDown; override;
+ Property Context : TTestContext Read FContext;
+ Property Output : TMustacheStringOutput Read Foutput;
+ Property El : TMustacheElement Read FEl;
+ Published
+ Procedure TestEmpty;
+ Procedure TestTextElement;
+ Procedure TestTextElementNoEscape;
+ Procedure TestTextElementComment;
+ Procedure TestTextElementPrefix;
+ procedure TestTextElementPrefixNotLast;
+ procedure TestTextElementPrefixLast;
+ Procedure TestVariableElement;
+ Procedure TestVariableElementNoEscape;
+ Procedure TestVariableElementEscape;
+ Procedure TestSectionEmpty;
+ Procedure TestSectionValue;
+ Procedure TestSectionValueFalse;
+ Procedure TestSectionValueNull;
+ Procedure TestSectionValueEmptyArray;
+ Procedure TestSectionValueArray1El;
+ Procedure TestSectionValueArray2El;
+ Procedure TestSectionValueArray2ElValue;
+ Procedure TestSectionValueArray1ElValueSuper;
+ Procedure TestSectionValueArray2ElValueSuper;
+ Procedure TestParentElement;
+ Procedure TestParentElementRender;
+ Procedure TestParentElementRenderPrefix;
+ end;
+
+implementation
+
+uses Typinfo;
+
+Const
+ SNeedsQuoting = '< > & "';
+ SQuotedResult = '&lt; &gt; &amp; &quot;';
+
+
+{ TTestMustacheElement }
+
+procedure TTestMustacheElement.DoCallBack(const aName: TMustacheString;
+ var aHandled: Boolean; var aValue: TMustacheString);
+begin
+ aValue:='';
+end;
+
+procedure TTestMustacheElement.SetUp;
+begin
+ inherited SetUp;
+ FOutput:=TMustacheStringOutput.Create;
+ FContext:=TTestContext.Create(@DoCallBack);
+end;
+
+procedure TTestMustacheElement.TearDown;
+begin
+ FreeAndNil(FContext);
+ FreeAndNil(FOutput);
+ FreeAndNil(FEl);
+ inherited TearDown;
+end;
+
+procedure TTestMustacheElement.TestEmpty;
+begin
+ AssertNotNull('Have output',Output);
+end;
+
+procedure TTestMustacheElement.TestTextElement;
+
+begin
+ Fel:=TMustacheTextElement.Create(metText,Nil,0);
+ El.Render(Nil,Output);
+ AssertEquals('No output','',Output.Data);
+ El.Data:='me';
+ El.Render(Nil,Output);
+ AssertEquals('Correct output','me',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestTextElementNoEscape;
+begin
+ Fel:=TMustacheTextElement.Create(metText,Nil,0);
+ El.Data:=SNeedsQuoting;
+ El.Render(Nil,Output);
+ AssertEquals('Correct output',SNeedsQuoting,Output.Data);
+end;
+
+procedure TTestMustacheElement.TestTextElementComment;
+begin
+ Fel:=TMustacheTextElement.Create(metComment,Nil,0);
+ El.Data:='Something';
+ El.Render(Nil,Output);
+ AssertEquals('Correct output','',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestTextElementPrefix;
+begin
+ Fel:=TMustacheTextElement.Create(metText,Nil,0);
+ El.Data:='me'#10'you';
+ El.Render(Nil,Output,' ');
+ AssertEquals('Correct output 1','me'#10' you',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestTextElementPrefixNotLast;
+begin
+ Fel:=TMustacheTextElement.Create(metText,Nil,0);
+ El.Data:='me'#10'you'#10;
+ El.Render(Nil,Output,' ');
+ AssertEquals('Correct output 2','me'#10' you'#10' ',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestTextElementPrefixLast;
+
+begin
+ Fel:=TMustacheTextElement.Create(metText,Nil,0);
+ El.Data:='me'#10'you'#10;
+ El.Render(Nil,Output,' ',True);
+ AssertEquals('Correct output 2','me'#10' you'#10,Output.Data);
+end;
+
+
+procedure TTestMustacheElement.TestVariableElement;
+begin
+ Fel:=TMustacheVariableElement.Create(metText,Nil,0);
+ Context.Values.Values['name']:='abc';
+ El.Data:='name';
+ El.Render(Context,Output);
+ AssertEquals('Correct output','abc',Output.Data);
+end;
+
+
+procedure TTestMustacheElement.TestVariableElementNoEscape;
+begin
+ Fel:=TMustacheVariableElement.Create(metText,Nil,0);
+ Context.Values.Values['name']:=SNeedsQuoting;
+ El.Data:='{name}';
+ El.Render(Context,Output);
+ AssertEquals('Correct output',SNeedsQuoting,Output.Data);
+end;
+
+procedure TTestMustacheElement.TestVariableElementEscape;
+begin
+ Fel:=TMustacheVariableElement.Create(metText,Nil,0);
+ Context.Values.Values['name']:=SNeedsQuoting;
+ El.Data:='name';
+ El.Render(Context,Output);
+ AssertEquals('Correct output',SQuotedResult,Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionEmpty;
+
+Var
+ T : TMustacheTextElement;
+
+begin
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('No output','',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValue;
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','b');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('Single pass','a',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueFalse;
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','<false>');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('no pass','',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueNull;
+
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','<null>');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('no pass','',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueEmptyArray;
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','[]');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('no pass','',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueArray1El;
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','[]');
+ Context.SetValue('s[0]','toto');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('Single pass','a',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueArray2El;
+
+Var
+ T : TMustacheTextElement;
+
+begin
+ Context.SetValue('s','[]');
+ Context.SetValue('s[0]','toto');
+ Context.SetValue('s[1]','tata');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheTextElement.Create(metText,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('Double pass','aa',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueArray2ElValue;
+
+Var
+ T : TMustacheElement;
+
+begin
+ Context.SetValue('s','[]');
+ Context.SetValue('s[0]','{}');
+ Context.SetValue('s[0].a','1');
+ Context.SetValue('s[1]','{}');
+ Context.SetValue('s[1].a','2');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheVariableElement.Create(metVariable,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('Double pass','12',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueArray1ElValueSuper;
+
+Var
+ T : TMustacheElement;
+
+begin
+ Context.SetValue('s','[]');
+ Context.SetValue('s[0]','{}');
+ Context.SetValue('s[0].b','1');
+ Context.SetValue('a','2');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheVariableElement.Create(metVariable,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ Fel.Render(Context,Output);
+ AssertEquals('Single pass','2',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestSectionValueArray2ElValueSuper;
+Var
+ T : TMustacheElement;
+
+begin
+ Context.SetValue('s','[]');
+ Context.SetValue('s[0]','{}');
+ Context.SetValue('s[0].b','1');
+ Context.SetValue('s[1]','{}');
+ Context.SetValue('s[1].b','2');
+ Context.SetValue('a','.a.');
+ Fel:=TMustacheSectionElement.Create(metSection,Nil,0);
+ Fel.Data:='s';
+ T:=TMustacheVariableElement.Create(metVariable,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='a';
+ T:=TMustacheVariableElement.Create(metVariable,Nil,0);
+ Fel.AddChild(T);
+ T.Data:='b';
+ Fel.Render(Context,Output);
+ AssertEquals('Single pass','.a.1.a.2',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestParentElement;
+
+Var
+ SEl : TMustacheElement;
+
+begin
+ Fel:=TMustacheParentElement.Create(metSection,Nil,0);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ AssertSame('Parent stored',Fel,Sel.Parent);
+ AssertEquals('Not added to parent',0,FEl.ChildCount);
+ Fel.AddChild(Sel);
+ AssertEquals('added to parent - count',1,FEl.ChildCount);
+ AssertSame('added to parent - stored',Sel,FEl.Children[0]);
+end;
+
+procedure TTestMustacheElement.TestParentElementRender;
+
+Var
+ SEl : TMustacheElement;
+
+begin
+ Fel:=TMustacheParentElement.Create(metSection,Nil,0);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='a';
+ Fel.AddChild(Sel);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='b';
+ Fel.AddChild(Sel);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='c';
+ Fel.AddChild(Sel);
+ Fel.Render(Context,Output);
+ AssertEquals('Correct output','abc',Output.Data);
+end;
+
+procedure TTestMustacheElement.TestParentElementRenderPrefix;
+Var
+ SEl : TMustacheElement;
+
+begin
+ Fel:=TMustacheParentElement.Create(metSection,Nil,0);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='a'#10'b';
+ Fel.AddChild(Sel);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='d'#10'e';
+ Fel.AddChild(Sel);
+ Sel:=TMustacheTextElement.Create(metText,Fel,0);
+ Sel.Data:='f'#10;
+ Fel.AddChild(Sel);
+ Fel.Render(Context,Output,' ');
+ AssertEquals('Correct output','a'#10' bd'#10' ef'#10,Output.Data);
+end;
+
+{ TTestMustacheOutput }
+
+procedure TTestMustacheOutput.TestStringOutput;
+
+Var
+ SO : TMustacheStringOutput;
+
+begin
+ SO:=TMustacheStringOutput.Create;
+ try
+ AssertEquals('Empty start','',SO.Data);
+ SO.Output('abc');
+ AssertEquals('Output 1','abc',SO.Data);
+ SO.Output('def');
+ AssertEquals('Output 2','abcdef',SO.Data);
+ finally
+ SO.Free;
+ end;
+
+end;
+
+function TTestMustacheParser.CreateParser: TMustacheParser;
+begin
+ Result:=TMustacheParser.Create;
+end;
+
+procedure TTestMustacheParser.SetUp;
+begin
+ inherited SetUp;
+end;
+
+procedure TTestMustacheParser.TearDown;
+begin
+ inherited TearDown;
+end;
+
+procedure TTestMustacheParser.TestEmpty;
+
+begin
+ AssertNotNull('Have parser',Parser);
+ AssertNull('Have no result',ParseResult);
+ AssertEquals('Have no template','',Template);
+end;
+
+
+procedure TTestMustacheParser.TestText;
+
+begin
+ Template:='a simple text';
+ CallParser;
+ AssertResultCount(1);
+ AssertElement(0,metText,'a simple text');
+end;
+
+procedure TTestMustacheParser.TestVariable;
+
+Var
+ el : TMustacheVariableElement;
+
+begin
+ Template:='{{a}}';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
+ AssertFalse('unescape',El.NoUnescape);
+end;
+
+procedure TTestMustacheParser.TestVariableErrNonClosed;
+
+begin
+ Template:='{{a';
+ AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
+ Template:='{{a}';
+ AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
+end;
+
+procedure TTestMustacheParser.TestVariableAlternateStartStop;
+Var
+ el : TMustacheVariableElement;
+
+begin
+ Parser.StartTag:='<<';
+ Parser.StopTag:='>>';
+ Template:='<<a>>';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
+ AssertFalse('unescape',El.NoUnescape);
+end;
+
+procedure TTestMustacheParser.TestDottedVariable;
+begin
+ Template:='{{a.b}}';
+ CallParser;
+ AssertResultCount(1);
+ AssertElement(0,metVariable,'a.b');
+end;
+
+procedure TTestMustacheParser.TestVariableNoUnescape;
+Var
+ el : TMustacheVariableElement;
+
+begin
+ Template:='{{{a}}}';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
+ AssertTrue('unescape',El.NoUnescape);
+end;
+
+procedure TTestMustacheParser.TestVariableNoUnescapeErrNonClosed;
+begin
+ Template:='{{{a';
+ AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
+ Template:='{{{a}';
+ AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
+ Template:='{{{a}}';
+ AssertException('Have error',EMustache,@CallParser,'Tag {{ opened on position 1 but not closed.');
+end;
+
+procedure TTestMustacheParser.TestVariableNoUnescapeAlternateStartStop;
+
+Var
+ el : TMustacheVariableElement;
+
+begin
+ Parser.StartTag:='<<';
+ Parser.StopTag:='>>';
+ Template:='<<{a}>>';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metVariable,'a',TMustacheVariableElement) as TMustacheVariableElement;
+ AssertTrue('unescape',El.NoUnescape);
+end;
+
+procedure TTestMustacheParser.TestComment;
+
+begin
+ Parser.StartTag:='<<';
+ Parser.StopTag:='>>';
+ Template:='<<! a comment>>';
+ CallParser;
+ AssertResultCount(1);
+ AssertElement(0,metComment,' a comment',TMustacheTextElement);
+end;
+
+procedure TTestMustacheParser.TestCommentSurround;
+begin
+ Template:='ab{{! a comment}}cd';
+ CallParser;
+ AssertResultCount(3);
+ AssertElement(0,metText,'ab',TMustacheTextElement);
+ AssertElement(1,metComment,' a comment',TMustacheTextElement);
+ AssertElement(2,metText,'cd',TMustacheTextElement);
+end;
+
+procedure TTestMustacheParser.TestCommentStandalone;
+begin
+ Template:='a'+sLineBreak+'{{! a comment}}'+sLineBreak+'b';
+ CallParser;
+ AssertResultCount(3);
+ AssertElement(0,metText,'a'+sLineBreak,TMustacheTextElement);
+ AssertElement(1,metComment,' a comment',TMustacheTextElement);
+ AssertElement(2,metText,'b',TMustacheTextElement);
+end;
+
+procedure TTestMustacheParser.TestCommentStandaloneSpaced;
+begin
+ Template:='a'+sLineBreak+' {{! a comment}} '+sLineBreak+'b';
+ CallParser;
+ AssertResultCount(3);
+ AssertElement(0,metText,'a'+sLineBreak,TMustacheTextElement);
+ AssertElement(1,metComment,' a comment',TMustacheTextElement);
+ AssertElement(2,metText,'b',TMustacheTextElement);
+end;
+
+procedure TTestMustacheParser.TestSetDelimiter;
+
+begin
+ Template:='{{=<< >>=}}<<! a comment>>';
+ CallParser;
+ AssertResultCount(1);
+ AssertElement(0,metComment,' a comment',TMustacheTextElement);
+end;
+
+procedure TTestMustacheParser.TestSetDelimiterErrInvalid;
+begin
+ Template:='{{=== ===}}';
+ AssertException('Have error',EMustache,@CallParser,'Invalid set delimiter Stop value: == in "== =="');
+end;
+
+procedure TTestMustacheParser.TestSection;
+
+Var
+ el : TMustacheSectionElement;
+
+begin
+ Template:='{{#a}}{{/a}}';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
+ AssertEquals('No elements in section',0,el.ChildCount);
+end;
+
+procedure TTestMustacheParser.TestSectionNested;
+
+Var
+ el : TMustacheSectionElement;
+
+begin
+ Template:='{{#a}}{{#b}}{{/b}}{{/a}}';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
+ AssertEquals('elements in section',1,el.ChildCount);
+ el:=AssertElement(el,0,metSection,'b',TMustacheSectionElement) as TMustacheSectionElement;
+ AssertEquals('elements in section sub',0,el.ChildCount);
+end;
+
+procedure TTestMustacheParser.TestSectionErrNotClosed;
+
+begin
+ Template:='{{#a}}';
+ AssertException('Have error',EMustache,@CallParser,'Structural error: Section "a" on position 1 is not closed.');
+end;
+
+procedure TTestMustacheParser.TestSectionErrWrongClosed;
+begin
+ Template:='{{#a}}{{#b}}{{/a}}{{/b}}';
+ AssertException('Have error',EMustache,@CallParser,'Structural error: Section "b" on position 7 is closed by tag "a" on position 13.');
+end;
+
+procedure TTestMustacheParser.TestSectionErrNotStarted;
+begin
+ Template:='{{/a}}';
+ AssertException('Have error',EMustache,@CallParser,'Structural error: Section "a" on position 1 was never opened.');
+end;
+
+procedure TTestMustacheParser.TestTextSection;
+
+Var
+ el : TMustacheSectionElement;
+
+begin
+ Template:='{{#a}}bbb{{/a}}';
+ CallParser;
+ AssertResultCount(1);
+ el:=AssertElement(0,metSection,'a',TMustacheSectionElement) as TMustacheSectionElement;
+ AssertEquals('No elements in section',1,el.ChildCount);
+ AssertElement(el,0,metText,'bbb');
+end;
+
+procedure TTestMustacheParser.TestPartial;
+
+Var
+ el : TMustachePartialElement;
+
+begin
+ AddPartial('part','bcd');
+ Template:='a{{>part}}e';
+ CallParser;
+ AssertResultCount(3);
+ AssertElement(0,metText,'a',TMustacheTextElement);
+ el:=AssertElement(1,metPartial,'part',TMustachePartialElement) as TMustachePartialElement;
+ AssertElement(2,metText,'e',TMustacheTextElement);
+ AssertEquals('Correct partial','part',El.Partial.Data);
+ AssertEquals('Correct partial',1,El.Partial.ChildCount);
+ AssertElement(el.Partial,0,metText,'bcd',TMustacheTextElement);
+end;
+
+
+initialization
+ RegisterTests([TTestMustacheParser,TTestMustacheOutput,TTestMustacheElement]);
+end.
+
diff --git a/packages/fcl-mustache/tests/tcspecs.pas b/packages/fcl-mustache/tests/tcspecs.pas
new file mode 100644
index 0000000000..5892733e37
--- /dev/null
+++ b/packages/fcl-mustache/tests/tcspecs.pas
@@ -0,0 +1,188 @@
+{
+ This file is part of the Free Pascal Run time library.
+ Copyright (c) 2021 by Michael Van Canneyt (michael@freepascal.org)
+
+ testcase for official Mustache tests
+
+ See the File COPYING.FPC, included in this distribution,
+ for details about the copyright.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+ **********************************************************************}
+unit tcspecs;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+ Classes, SysUtils, fpcunit, testregistry, fpmustache, fpjson, jsonparser;
+
+Type
+
+ { TTestMustacheSpecs }
+
+ TTestMustacheSpecs = class(TTestCase)
+ private
+ FTests: TJSONArray;
+ procedure RunMustacheTest(aIndex: Integer; aTest: TJSONObject);
+ Public
+ class var BaseDir : string;
+ Public
+ Procedure Setup; override;
+ Procedure TearDown; override;
+ Procedure DoTest(aFileName : string);
+ Property Tests : TJSONArray Read FTests;
+ Published
+ Procedure TestComments;
+ Procedure TestDelimiters;
+ Procedure TestInterpolation;
+ Procedure TestInverted;
+ Procedure TestPartials;
+ Procedure TestSections;
+ end;
+
+
+implementation
+
+{ TTestMustacheSpecs }
+
+procedure TTestMustacheSpecs.RunMustacheTest(aIndex : Integer; aTest : TJSONObject);
+
+Var
+ M : TMustache;
+ aTempl,aErr,aRes,aName : TMustacheString;
+ Parts : TJSONObject;
+ I : Integer;
+ Ok : Boolean;
+
+ Procedure TreeDump;
+
+ begin
+ if not OK then
+ begin
+ Writeln('Tree dump:');
+ Writeln(M.Dump);
+ end;
+ end;
+
+ Procedure InputDump;
+
+ begin
+ Writeln('Test : ',aIndex);
+ writeln(aTempl);
+ writeln(StringReplace(StringReplace(aTempl,#10,' ',[rfReplaceAll]),#13,' ',[rfReplaceAll]));
+ aName:='';
+ While Length(aName)<Length(aTempl) do
+ aName:=AName+'1234567890';
+ Writeln(aName);
+ end;
+
+begin
+ OK:=False;
+ aTempl:=aTest.Get('template','');
+ // InputDump;
+ M:=TMustache.CreateMustache(Nil,aTempl);
+ try
+ // Load partials
+ Parts:=aTest.Get('partials',TJSONObject(Nil));
+ if Assigned(Parts) then
+ for I:=0 to Parts.Count-1 do
+ M.Partials.Add(Parts.Names[i]+'='+Parts.Items[i].AsString);
+ // Set test name and run tests
+ aName:='Test '+IntToStr(aIndex)+': '+aTest.Get('name','');
+ Try
+ aErr:='';
+ aRes:=m.Render(aTest.Get('data',TJSONObject(Nil)));
+ except
+ on e : exception do
+ aErr:=E.ClassName+' '+E.message;
+ end;
+ if aErr<>'' then
+ Fail(aName+': Unexpected error: '+aErr);
+ AssertEquals(aName,aTest.Get('expected',''),aRes);
+ OK:=true;
+ finally
+ // TreeDump;
+ M.Free;
+ end;
+end;
+
+procedure TTestMustacheSpecs.Setup;
+begin
+ inherited Setup;
+end;
+
+procedure TTestMustacheSpecs.TearDown;
+begin
+ inherited TearDown;
+end;
+
+procedure TTestMustacheSpecs.DoTest(aFileName: string);
+
+Var
+ I : Integer;
+ F : TFileStream;
+ D : TJSONData;
+ FN : String;
+
+begin
+ D:=Nil;
+ FN:=IncludeTrailingPathDelimiter(BaseDir)+aFileName+'.json';
+ F:=TFileStream.Create(FN,fmOpenRead or fmShareDenyWrite);
+ try
+ D:=GetJSON(F);
+ if D is TJSONObject then
+ begin
+ Ftests:=(D as TJSONObject).Get('tests',TJSONArray(Nil));
+ if (FTests=Nil) then
+ Fail('Invalid mustache tests in '+FN);
+ end
+ else
+ Fail('Invalid JSON object in '+FN);
+ For I:=0 to Tests.Count-1 do
+ RunMustacheTest(I,Tests.Items[i] as TJSONObject);
+ finally
+ D.Free;
+ F.Free;
+ end;
+end;
+
+procedure TTestMustacheSpecs.TestComments;
+begin
+ DoTest('comments');
+end;
+
+procedure TTestMustacheSpecs.TestDelimiters;
+begin
+ DoTest('delimiters');
+end;
+
+procedure TTestMustacheSpecs.TestInterpolation;
+begin
+ DoTest('interpolation');
+end;
+
+procedure TTestMustacheSpecs.TestInverted;
+begin
+ DoTest('inverted');
+end;
+
+procedure TTestMustacheSpecs.TestPartials;
+begin
+ DoTest('partials');
+end;
+
+procedure TTestMustacheSpecs.TestSections;
+begin
+ DoTest('sections');
+end;
+
+begin
+ TTestMustacheSpecs.BaseDir:='spec/';
+ RegisterTest(TTestMustacheSpecs);
+end.
+
diff --git a/packages/fcl-mustache/tests/testmustache.lpi b/packages/fcl-mustache/tests/testmustache.lpi
new file mode 100644
index 0000000000..6a8b3c1be6
--- /dev/null
+++ b/packages/fcl-mustache/tests/testmustache.lpi
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+ <ProjectOptions>
+ <Version Value="12"/>
+ <General>
+ <Flags>
+ <SaveOnlyProjectUnits Value="True"/>
+ <MainUnitHasCreateFormStatements Value="False"/>
+ <MainUnitHasTitleStatement Value="False"/>
+ <MainUnitHasScaledStatement Value="False"/>
+ </Flags>
+ <SessionStorage Value="InProjectDir"/>
+ <Title Value="testmustache"/>
+ <UseAppBundle Value="False"/>
+ <ResourceType Value="res"/>
+ </General>
+ <BuildModes>
+ <Item Name="Default" Default="True"/>
+ </BuildModes>
+ <PublishOptions>
+ <Version Value="2"/>
+ <UseFileFilters Value="True"/>
+ </PublishOptions>
+ <RunParams>
+ <FormatVersion Value="2"/>
+ </RunParams>
+ <RequiredPackages>
+ <Item>
+ <PackageName Value="FCL"/>
+ </Item>
+ </RequiredPackages>
+ <Units>
+ <Unit>
+ <Filename Value="testmustache.lpr"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ <Unit>
+ <Filename Value="tcmustache.pas"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ <Unit>
+ <Filename Value="tcspecs.pas"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ <Unit>
+ <Filename Value="tcexmustache.pas"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ <Unit>
+ <Filename Value="tcbasemustache.pas"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ <Unit>
+ <Filename Value="tcdbmustache.pas"/>
+ <IsPartOfProject Value="True"/>
+ </Unit>
+ </Units>
+ </ProjectOptions>
+ <CompilerOptions>
+ <Version Value="11"/>
+ <Target>
+ <Filename Value="testmustache"/>
+ </Target>
+ <SearchPaths>
+ <IncludeFiles Value="$(ProjOutDir)"/>
+ <OtherUnitFiles Value="../src"/>
+ <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
+ </SearchPaths>
+ <Linking>
+ <Debugging>
+ <UseHeaptrc Value="True"/>
+ </Debugging>
+ </Linking>
+ </CompilerOptions>
+ <Debugging>
+ <Exceptions>
+ <Item>
+ <Name Value="EAbort"/>
+ </Item>
+ <Item>
+ <Name Value="ECodetoolError"/>
+ </Item>
+ <Item>
+ <Name Value="EFOpenError"/>
+ </Item>
+ </Exceptions>
+ </Debugging>
+</CONFIG>
diff --git a/packages/fcl-mustache/tests/testmustache.lpr b/packages/fcl-mustache/tests/testmustache.lpr
new file mode 100644
index 0000000000..421c0a679c
--- /dev/null
+++ b/packages/fcl-mustache/tests/testmustache.lpr
@@ -0,0 +1,29 @@
+program testmustache;
+
+{$mode objfpc}{$H+}
+
+uses
+ Classes, consoletestrunner, tcmustache, tcspecs,
+ tcexmustache, tcbasemustache, tcdbmustache;
+
+type
+
+ { TMyTestRunner }
+
+ TMyTestRunner = class(TTestRunner)
+ protected
+ // override the protected methods of TTestRunner to customize its behavior
+ end;
+
+var
+ Application: TMyTestRunner;
+
+begin
+ DefaultFormat:=fPlain;
+ DefaultRunAllTests:=True;
+ Application := TMyTestRunner.Create(nil);
+ Application.Initialize;
+ Application.Title := 'FPCUnit Console test runner';
+ Application.Run;
+ Application.Free;
+end.