Adding templates to the DSL#587
Conversation
|
Some examples of what is currently possible: Usage of object templatetranslations:
animal:
en: "animals"
nl: "dieren"
tabs:
- tab: '{{translations.animal}}'
templates:
default_template:
expression:
javascript: "{{translations.animal}}_javascript({{ templates.input }})"
typescript: "{{translations.animal}}_typescript({{ templates.input }})"
java: "Submission.{{translations.animal}}_java({{ templates.input }})"
python: !natural_language
en: "{{translations.animal}}_python_en({{ templates.input }})"
nl: "{{translations.animal}}_python_nl({{ templates.input }})"
return: "{{ templates.output }}"
testcases:
- placeholder:
name: "default_template"
data:
input: !natural_language
en: "Sardines"
nl: "Sardienen"
output: !natural_language
en: "fish"
nl: "vis"
- placeholder:
name: "default_template"
data:
input: !natural_language
en: "Tiger"
nl: "Tijger"
output: !natural_language
en: "mammal"
nl: "zoogdier"Usage of list template"units":
- "unit": "echo"
"translations":
"monkeys":
en: "monkeys"
nl: "apen"
"templates":
"drinks":
- "expression": "drinks({{ templates.input1 }})"
"return": "{{ templates.ok }}"
- "expression": "drinks({{ templates.input2 }})"
"return": "{{ templates.ok }}"
- "expression": "drinks({{ templates.input3 }})"
"return": "{{ templates.ok }}"
"scripts":
"placeholder":
"name": "drinks"
"data":
"ok": "OK!"
"input1": "Beer"
"input2": "Soda"
"input3": "Water"Usage of scalar template"units":
- "unit": "IO"
"translations":
"monkeys":
en: "monkeys"
nl: "apen"
"templates":
"heartbeats": !natural_language
"en": "{{templates.animals}} have {{templates.billion_heartbeats_per_lifetime}} billion heartbeats"
"nl": "{{templates.animals}} hebben {{templates.billion_heartbeats_per_lifetime}} miljard hartslagen"
"scripts":
- "stdin": |-
horses
44
40
"stdout": "horses have 0.93 billion heartbeats"
- "stdin": |-
{{translations.monkeys}}
190
40
15
"stdout":
"placeholder":
"name": "heartbeats"
"data":
"animals": "{{translations.monkeys}}"
"billion_heartbeats_per_lifetime": 1.50 |
New suggestion for templatesUsage of object templateBasic usagetabs:
- tab: 'animals'
templates:
default_animal:
expression:
java: "Submission.animals({{ input }})"
python: "animals({{ input }})"
return: "{{ output }}"
testcases:
- template:
name: "default_animal"
parameters:
- input: "Sardines"
output: "fish"
- input: "Tiger"
output: "mammal"
- template:
name: "default_animal"
parameters: # Parameters kan bijvoorbeeld ook een object zijn.
input: "Whale"
output: "mammal"A In the test-suite, you can then define a With repeatstabs:
- tab: 'random'
templates:
default_random:
expression: "random()"
return: "{{ output }}"
testcases:
- template:
name: "default_random"
parameters:
- output: 0
repeat: 100
- output: 9
repeat: 100Default templateFor a default template, you should define it globally. default_template:
expression: "calculator({{ expr }})"
return: "{{ result }}"
templates:
print_calc:
expression: "print_steps({{ expr }})"
return: "{{ result }}"
tabs:
- tab: 'calculator'
testcases:
- parameters:
- expr: "2 + 10"
result: 12
- expr: "9 + 5"
result: 14
- expr: "99 + 3"
result: 102
- expr: "4 * 13"
result: 52
- template:
name: "print_calc"
parameters:
- expr: "2 + 10"
result: "2 + 10 = 12"
- expr: "9 + 5"
result: "9 + 5 = 14"
- expr: "99 + 3"
result: "99 + 3 = 102"
- expr: "4 * 13"
result: "4 * 13 = 4 * 10 + 4 * 3 = 40 + 12 = 52"To access the default template, you just have to specify the Using translationstranslations:
animals:
en: "animals"
nl: "dieren"
tabs:
- tab: '{{ animals }}'
templates:
default_animal:
expression:
java: "Submission.{{ translations.animals }}({{ templates.animals }})"
python: "{{translations.animals}}({{ templates.animals }})"
return: "{{ output }}"
testcases:
- template:
name: "default_animal"
parameters:
- input: "Sardines"
output: "fish"
- input: "Tiger"
output: "mammal"This is a very rare case in which the Usage of list template"units":
- "unit": "drinks"
"templates":
"default_drinks":
- "expression": "drinks({{ input1 }})"
"return": "{{ ok }}"
- "expression": "drinks({{ input2 }})"
"return": "{{ ok }}"
- "expression": "drinks({{ input3 }})"
"return": "{{ ok }}"
"scripts":
"placeholder":
"name": "default_drinks"
"data":
"ok": "OK!"
"input1": "Beer"
"input2": "Soda"
"input3": "Water"Usage of scalar templateNot sure if usage in "units":
- "unit": "IO"
"translations":
"monkeys":
en: "monkeys"
nl: "apen"
"templates":
"default_heartbeats": !natural_language
"en": "{{animals}} have {{billion_heartbeats_per_lifetime}} billion heartbeats"
"nl": "{{animals}} hebben {{billion_heartbeats_per_lifetime}} miljard hartslagen"
"scripts":
- "stdin": |-
horses
44
40
"stdout": "horses have 0.93 billion heartbeats"
- "stdin": |-
{{monkeys}}
190
40
15
"stdout":
"placeholder":
"name": "default_heartbeats"
"data":
"animals": "{{monkeys}}"
"billion_heartbeats_per_lifetime": 1.50 |
Suggestion 3 for templatesBasic usagetabs:
- tab: 'animals'
templates:
animal_IO:
expression:
java: "Submission.animals({{ input }})"
python: "animals({{ input }})"
return: "{{ output }}"
testcases:
- template: "animal_IO"
parameters:
input: "Sardines"
output: "fish"
- template: "animal_IO"
parameters:
input: "Whale"
output: "mammal"
return: "mammal (not a fish)"A After that you can define a With a In the second testcase a return is specified. This will overwrite the return in the template. A template inside a template is not allowed. With repeatstemplates:
one_name:
stdin: |-
1
{{ name }}
stdout: "{{ username }}"
units:
- unit: IO
scripts:
- template: one_name
parameters:
name: Graham Chapman
username: gchap
- repeat:
template: one_name
parameters:
- name: John Cleese
username: jclee
- name: Terry Gilliam
username: tgill
- name: Eric Idle
username: eidle
- name: Terry Jones
username: tjone
- name: Michael Palin
username: mpaliA Using translationstranslations:
animals:
en: "animals"
nl: "dieren"
tabs:
- tab: '{{ animals }}'
templates:
animal_IO:
expression:
java: "Submission.{{ animals }}({{ animals }})"
python: "{{animals}}({{ animals }})"
return: "{{ output }}"
testcases:
- template: "animal_IO"
parameters:
animals: "Sardines"
output: "fish"This is a very rare case in which the Non-string parametersUp until now, all examples have used string parameters, but what if you'd like to specify an integer or a list? templates:
calculator_IO:
expression: "calculator('{{ expr }}')"
return: !parameter "result"
temp2:
expression: "calculator('{{ expr }}')"
return: !oracle
value: !parameter "result"
oracle: "builtin"
temp3:
arguments: !parameter "list"
stdout: !parameter "res"
exit_code: !parameter "code"
temp4:
expression: "calculator('{{ expr }}')"
return: !natural_language
en: !parameter "test"
nl: !parameter "test"
temp5:
expression: "calculator('{{ expr }}')"
return: !oracle
value: !parameter "result"
oracle: "custom_check"
file: "file.txt"
temp6:
arguments:
- !parameter "list1"
- "second"
- "third"
stdout: !parameter "res"
exit_code: !parameter "code"
tabs:
- tab: 'calculator'
testcases:
- template: "calculator_IO"
parameters:
expr: "2 + 10"
result: 12
- template: "calculator_IO"
parameters:
expr: "4 * 13"
result: 52In this case, Jinja is not used, and a |
Templates are handled in the same file as the translations. So using to following will now also proces templates: