1
1
package org .openapitools .codegen .templating ;
2
2
3
+ import org .mockito .Mockito ;
4
+ import org .openapitools .codegen .api .TemplatingExecutor ;
3
5
import org .testng .annotations .DataProvider ;
4
6
import org .testng .annotations .Test ;
5
7
6
- import static org .testng .Assert .*;
8
+ import java .io .IOException ;
9
+ import java .util .Map ;
10
+
11
+ import static org .testng .Assert .assertEquals ;
7
12
8
13
public class HandlebarsEngineAdapterTest {
9
14
@ Test (dataProvider = "handlesFileExpectations" )
@@ -33,4 +38,73 @@ public Object[][] handlesFileExpectations() {
33
38
{"README.md" , false , "Should not attempt to handle non-handlebars extensions (other than mustache)" }
34
39
};
35
40
}
41
+
42
+ @ Test (description = "verify https://github.com/jknack/handlebars.java/issues/940#issue-1111612043 is fixed" )
43
+ public void testHandlePartialTemplate () throws IOException {
44
+ // Given
45
+ HandlebarsEngineAdapter adapter = new HandlebarsEngineAdapter ();
46
+ TemplatingExecutor executorMock = Mockito .mock (TemplatingExecutor .class );
47
+ Mockito .when (executorMock .getFullTemplateContents ("outerTemplate.hbs" )).thenReturn ("Contents: {{>innerTemplate}}" );
48
+ Mockito .when (executorMock .getFullTemplateContents ("innerTemplate.hbs" )).thenReturn ("'Specific contents'" );
49
+
50
+ // When
51
+ String generatedFile = adapter .compileTemplate (executorMock , Map .of (), "outerTemplate.hbs" );
52
+
53
+ // Then
54
+ assertEquals (generatedFile , "Contents: 'Specific contents'" );
55
+ }
56
+
57
+ @ Test (description = "should prioritize public getters over breaking encapsulation" )
58
+ public void testResolverPriority () throws IOException {
59
+ // Given
60
+ HandlebarsEngineAdapter adapter = new HandlebarsEngineAdapter ();
61
+ TemplatingExecutor executorMock = Mockito .mock (TemplatingExecutor .class );
62
+ Mockito .when (executorMock .getFullTemplateContents ("outerTemplate.hbs" )).thenReturn (
63
+ "Contents: {{#propertyObj}}" + "\n " +
64
+ " public getter: {{valueMethodAndBean}}" + "\n " +
65
+ " public method: {{valueAndMethod}}" + "\n " +
66
+ " private property: {{valueOnly}}" + "{{/propertyObj}}" );
67
+
68
+ Map <String , Object > bundle = Map .of ("propertyObj" , new PropertyObject ());
69
+
70
+ // When
71
+ String generatedFile = adapter .compileTemplate (executorMock , bundle , "outerTemplate.hbs" );
72
+
73
+ // Then
74
+ assertEquals (generatedFile , "Contents: \n " +
75
+ " public getter: get_raw_data1_formatted\n " +
76
+ " public method: raw_data2_formatted\n " +
77
+ " private property: raw_data3" );
78
+ }
79
+
80
+ static class PropertyObject {
81
+ /**
82
+ * getter-exposed
83
+ */
84
+ private final String valueMethodAndBean = "raw_data1" ;
85
+
86
+ public String valueMethodAndBean () {
87
+ return valueMethodAndBean + "_formatted" ;
88
+ }
89
+
90
+ public String getValueMethodAndBean () {
91
+ return "get_" + valueMethodAndBean ();
92
+ }
93
+
94
+ /**
95
+ * method-exposed
96
+ */
97
+ private final String valueAndMethod = "raw_data2" ;
98
+
99
+ public String valueAndMethod () {
100
+ return valueAndMethod + "_formatted" ;
101
+ }
102
+
103
+ /**
104
+ * private
105
+ * note: ideally we long-term move towards respecting encapsulation where possible
106
+ */
107
+ @ SuppressWarnings ({"unused" , "java:S1068" }) // this private value is still read by our HandleBars engine
108
+ private final String valueOnly = "raw_data3" ;
109
+ }
36
110
}
0 commit comments