1
2
3
4 package de.powerstat.phplib.templateengine;
5
6
7 import static org.junit.jupiter.api.Assertions.assertEquals;
8
9 import java.io.File;
10 import java.io.IOException;
11
12 import io.cucumber.java.en.Given;
13 import io.cucumber.java.en.Then;
14 import io.cucumber.java.en.When;
15
16
17
18
19
20 @SuppressWarnings({"PMD.MethodNamingConventions", "checkstyle:MethodName"})
21 public class StepDefinitions
22 {
23
24
25
26 private static final String TEMPLATE1_TMPL = "target/test-classes/templates/template1.tmpl";
27
28
29
30
31 private static final String TEMPLATE2_TMPL = "target/test-classes/templates/template2.tmpl";
32
33
34
35
36 private static final String BLK1_BLK = "BLK1_BLK";
37
38
39
40
41 private static final String BLK1 = "BLK1";
42
43
44
45
46 private static final String TEMPLATE = "template";
47
48
49
50
51 private static final String VARIABLE1 = "variable1";
52
53
54
55
56 private static final String HELLO_WORLD = "hello world";
57
58
59
60
61 private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected";
62
63
64
65
66
67
68 private TemplateEngine engine;
69
70
71
72
73 private String resultVar = "";
74
75
76
77
78 private String resultBlock = "";
79
80
81
82
83
84 public StepDefinitions()
85 {
86 super();
87 }
88
89
90
91
92
93
94
95 @Given("A template with a variable")
96 public void a_template_with_a_variable() throws IOException
97 {
98 this.engine = TemplateEngine.newInstance(new File(StepDefinitions.TEMPLATE1_TMPL));
99 }
100
101
102
103
104
105
106
107 @When("The template is processed to replace the variable")
108 public void the_template_is_processed_to_replace_the_variable() throws IOException
109 {
110 this.engine.setVar(StepDefinitions.VARIABLE1, StepDefinitions.HELLO_WORLD);
111 this.resultVar = this.engine.subst(StepDefinitions.TEMPLATE);
112 }
113
114
115
116
117
118 @Then("The variable is replaced with the value hello world")
119 public void the_variable_is_replaced_with_the_value_hello_world()
120 {
121 assertEquals("123\nhello world\n456\n", this.resultVar, StepDefinitions.RESULT_NOT_AS_EXPECTED);
122 }
123
124
125
126
127
128
129
130 @Given("A template with a block")
131 public void a_template_with_a_block() throws IOException
132 {
133 this.engine = TemplateEngine.newInstance(new File(StepDefinitions.TEMPLATE2_TMPL));
134 }
135
136
137
138
139
140
141
142 @When("The template is processed to replace the block")
143 public void the_template_is_processed_to_replace_the_block() throws IOException
144 {
145 this.engine.setBlock(StepDefinitions.TEMPLATE, StepDefinitions.BLK1, StepDefinitions.BLK1_BLK);
146 this.engine.setVar(StepDefinitions.BLK1, StepDefinitions.HELLO_WORLD);
147 this.engine.setVar(StepDefinitions.VARIABLE1, "");
148 this.engine.setVar("variable3", "");
149 this.engine.parse(StepDefinitions.BLK1_BLK, StepDefinitions.BLK1, false);
150 this.resultBlock = this.engine.subst(StepDefinitions.TEMPLATE);
151 }
152
153
154
155
156
157 @Then("The block is replaced with the value hello world")
158 public void the_block_is_replaced_with_the_value_hello_world()
159 {
160 assertEquals("123\n\n456\nhello world\ndef\n\nghi\n", this.resultBlock, StepDefinitions.RESULT_NOT_AS_EXPECTED);
161 }
162
163 }