Skip to content

Commit

Permalink
test: restructure tests, add more test from handlebars-php
Browse files Browse the repository at this point in the history
  • Loading branch information
simontaurus committed May 19, 2024
1 parent f1fdb35 commit 353d8bf
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 50 deletions.
10 changes: 5 additions & 5 deletions tests/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from handlebars import Handlebars


def _run_test(template, data, expected, helpers={}):
def _run_test(template, data, expected_result, helpers={}):
for helper_key, helper_func in helpers.items():
Handlebars.registerHelper(helper_key, helper_func)
template = Handlebars.compile(template)
actual = template(data)
assert actual == expected
assert actual == expected_result

def _test_helper():
return "test"
Expand All @@ -37,9 +37,9 @@ def test_cases():
for case in cases:
#print(case[0])
_run_test(
case[0],
case[1],
case[2]
case["template"],
case["data"],
case["result"]
)
continue
else:
Expand Down
30 changes: 15 additions & 15 deletions tests/test_cases/basic_tags.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[
[
"{{! This is comment}}",
[],
""
],
[
"{{data}}",
{
{
"template": "{{! This is comment}}",
"data":[],
"result":""
},
{
"template": "{{data}}",
"data":{
"data": "result"
},
"result"
],
[
"{{data.key}}",
{
"result":"result"
},
{
"template": "{{data.key}}",
"data":{
"data": {
"key": "result"
}
},
"result"
]
"result":"result"
}
]
276 changes: 276 additions & 0 deletions tests/test_cases/internal_data_variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
[
{
"template": "{{#each data}}{{@index}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},
"result": "0123"
},
{
"template": "{{#each data}}{{@key}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},

"result": "0123"
},
{
"template": "{{#each data}}{{#each this}}outer: {{@../key}},inner: {{@key}};{{/each}}{{/each}}",
"data": {
"data": [
[
"apple",
"banana"
],
[
"carrot",
"zucchini"
]
]
},
"result": "outer: 0,inner: 0;outer: 0,inner: 1;outer: 1,inner: 0;outer: 1,inner: 1;"
},
{
"template": "{{#each data}}{{#if @first}}true{{else}}false{{/if}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},
"result": "truefalsefalsefalse"
},
{
"template": "{{#each data}}{{@first}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},

"result": "truefalsefalsefalse"
},
{
"template": "{{#each data}}{{#each this}}outer: {{@../first}},inner: {{@first}};{{/each}}{{/each}}",
"data": {
"data": [
[
"apple",
"banana"
],
[
"carrot",
"zucchini"
]
]
},
"result": "outer: true,inner: true;outer: true,inner: false;outer: false,inner: true;outer: false,inner: false;"
},
{
"template": "{{#each data}}{{#if @last}}true{{else}}false{{/if}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},
"result": "falsefalsefalsetrue"
},
{
"template": "{{#each data}}{{@last}}{{/each}}",
"data": {
"data": [
"apple",
"banana",
"carrot",
"zucchini"
]
},

"result": "falsefalsefalsetrue"
},
{
"template": "{{#each data}}{{#each this}}outer: {{@../last}},inner: {{@last}};{{/each}}{{/each}}",
"data": {
"data": [
[
"apple",
"banana"
],
[
"carrot",
"zucchini"
]
]
},
"result": "outer: false,inner: false;outer: false,inner: true;outer: true,inner: false;outer: true,inner: true;"
},
{
"template": "{{#each data}}{{@index}}{{/each}}",
"data": {
"data": [
{
"@index": "apple"
},
{
"@index": "banana"
},
{
"@index": "carrot"
},
{
"@index": "zucchini"
}
]
},
"result": "0123"
},
{
"template": "{{#each data}}{{@index}}{{/each}}",
"data": {
"data": [{
"@index": "apple",
"@first": "apple",
"@last": "apple",
"@unknown": "apple"
},
{
"@index": "banana",
"@first": "banana",
"@last": "banana",
"@unknown": "banana"
},
{
"@index": "carrot",
"@first": "carrot",
"@last": "carrot",
"@unknown": "carrot"
},
{
"@index": "zucchini",
"@first": "zucchini",
"@last": "zucchini",
"@unknown": "zucchini"
}]
},
"result": "0123"
},
{
"template": "{{#each data}}{{@first}}{{/each}}",
"data": {
"data": [{
"@index": "apple",
"@first": "apple",
"@last": "apple",
"@unknown": "apple"
},
{
"@index": "banana",
"@first": "banana",
"@last": "banana",
"@unknown": "banana"
},
{
"@index": "carrot",
"@first": "carrot",
"@last": "carrot",
"@unknown": "carrot"
},
{
"@index": "zucchini",
"@first": "zucchini",
"@last": "zucchini",
"@unknown": "zucchini"
}]
},
"result": "truefalsefalsefalse"
},
{
"template": "{{#each data}}{{@last}}{{/each}}",
"data": {
"data": [{
"@index": "apple",
"@first": "apple",
"@last": "apple",
"@unknown": "apple"
},
{
"@index": "banana",
"@first": "banana",
"@last": "banana",
"@unknown": "banana"
},
{
"@index": "carrot",
"@first": "carrot",
"@last": "carrot",
"@unknown": "carrot"
},
{
"@index": "zucchini",
"@first": "zucchini",
"@last": "zucchini",
"@unknown": "zucchini"
}]
},
"result": "falsefalsefalsetrue"
},
{
"template": "{{#each data}}{{@unknown}}{{/each}}",
"data": {
"data": [{
"@index": "apple",
"@first": "apple",
"@last": "apple",
"@unknown": "apple"
},
{
"@index": "banana",
"@first": "banana",
"@last": "banana",
"@unknown": "banana"
},
{
"@index": "carrot",
"@first": "carrot",
"@last": "carrot",
"@unknown": "carrot"
},
{
"@index": "zucchini",
"@first": "zucchini",
"@last": "zucchini",
"@unknown": "zucchini"
}]
},
"result": ""
},
{
"template": "{{#each data}}{{@key}}{{/each}}",
"data": {
"data": {
"fruit": "apple",
"19": "banana",
"true": "carrot"
}
},
"result": "fruit19true"
}
]
Loading

0 comments on commit 353d8bf

Please sign in to comment.