1
1
// Glaze Library
2
2
// For the license information refer to glaze.hpp
3
3
4
- #include " glaze/stencil/stencil.hpp"
5
-
6
4
#include " glaze/glaze.hpp"
7
- #include " glaze/stencil/stencilcount.hpp"
8
5
#include " ut/ut.hpp"
9
6
10
7
using namespace ut ;
@@ -15,6 +12,7 @@ struct person
15
12
std::string last_name{};
16
13
uint32_t age{};
17
14
bool hungry{};
15
+ bool employed{};
18
16
};
19
17
20
18
suite mustache_tests = [] {
@@ -50,17 +48,151 @@ suite mustache_tests = [] {
50
48
auto result = glz::stencil (layout, p).value_or (" error" );
51
49
expect (result == " Henry Foster" ) << result;
52
50
};
51
+
52
+ // **Regular Section Tests (#)**
53
+
54
+ " section_true" _test = [] {
55
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Employed{{/employed}})" ;
56
+
57
+ person p{" Alice" , " Johnson" , 28 , true , true }; // employed is true
58
+ auto result = glz::stencil (layout, p).value_or (" error" );
59
+ expect (result == " Alice Johnson Employed" ) << result;
60
+ };
61
+
62
+ " section_false" _test = [] {
63
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Employed{{/employed}})" ;
64
+
65
+ person p{" Bob" , " Smith" , 45 , false , false }; // employed is false
66
+ auto result = glz::stencil (layout, p).value_or (" error" );
67
+ expect (result == " Bob Smith " ) << result; // The section should be skipped
68
+ };
69
+
70
+ " section_with_inner_placeholders" _test = [] {
71
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Status: Employed, Age: {{age}}{{/employed}})" ;
72
+
73
+ person p{" Carol" , " Davis" , 30 , true , true };
74
+ auto result = glz::stencil (layout, p).value_or (" error" );
75
+ expect (result == " Carol Davis Status: Employed, Age: 30" ) << result;
76
+ };
77
+
78
+ " section_with_extra_text" _test = [] {
79
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Employed{{/employed}}. Welcome!)" ;
80
+
81
+ person p{" Dave" , " Miller" , 40 , true , true };
82
+ auto result = glz::stencil (layout, p).value_or (" error" );
83
+ expect (result == " Dave Miller Employed. Welcome!" ) << result;
84
+ };
85
+
86
+ " section_with_extra_text_skipped" _test = [] {
87
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Employed{{/employed}}. Welcome!)" ;
88
+
89
+ person p{" Eve" , " Wilson" , 22 , true , false }; // employed is false
90
+ auto result = glz::stencil (layout, p).value_or (" error" );
91
+ expect (result == " Eve Wilson . Welcome!" ) << result;
92
+ };
93
+
94
+ " nested_sections" _test = [] {
95
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Status: Employed {{#hungry}}and Hungry{{/hungry}}{{/employed}})" ;
96
+
97
+ person p1{" Frank" , " Taylor" , 50 , true , true }; // employed is true, hungry is true
98
+ auto result1 = glz::stencil (layout, p1);
99
+ expect (result1 == " Frank Taylor Status: Employed and Hungry" );
100
+
101
+ person p2{" Grace" , " Anderson" , 0 , false , true }; // employed is true, hungry is false
102
+ auto result2 = glz::stencil (layout, p2);
103
+ expect (result2 == " Grace Anderson Status: Employed " ) << result2.value ();
104
+ };
105
+
106
+ " section_unknown_key" _test = [] {
107
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#unknown}}Should not appear{{/unknown}})" ;
108
+
109
+ person p{" Henry" , " Foster" , 34 , false , true };
110
+ auto result = glz::stencil (layout, p);
111
+ expect (not result.has_value ());
112
+ expect (result.error () == glz::error_code::unknown_key);
113
+ };
114
+
115
+ " section_mismatched_closing_tag" _test = [] {
116
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{#employed}}Employed{{/employment}})" ; // Mismatched closing tag
117
+
118
+ person p{" Ivy" , " Thomas" , 29 , false , true };
119
+ auto result = glz::stencil (layout, p);
120
+ expect (not result.has_value ());
121
+ expect (result.error () == glz::error_code::unexpected_end);
122
+ };
123
+
124
+ // **Inverted Section Tests**
53
125
54
- " unsupported section " _test = [] {
55
- std::string_view layout = R"( {{# hungry}}I am hungry{{/hungry}})" ;
126
+ " inverted_section_true " _test = [] {
127
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^ hungry}}I'm not hungry{{/hungry}})" ;
56
128
57
- person p{" Henry" , " Foster" , 34 , true };
129
+ person p{" Henry" , " Foster" , 34 , false }; // hungry is false
130
+ auto result = glz::stencil (layout, p).value_or (" error" );
131
+ expect (result == " Henry Foster I'm not hungry" ) << result;
132
+ };
133
+
134
+ " inverted_section_false" _test = [] {
135
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry{{/hungry}})" ;
136
+
137
+ person p{" Henry" , " Foster" , 34 , true }; // hungry is true
138
+ auto result = glz::stencil (layout, p).value_or (" error" );
139
+ expect (result == " Henry Foster " ) << result; // The inverted section should be skipped
140
+ };
141
+
142
+ " inverted_section_with_extra_text_true" _test = [] {
143
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry{{/hungry}}. Have a nice day!)" ;
144
+
145
+ person p{" Henry" , " Foster" , 34 , false }; // hungry is false
146
+ auto result = glz::stencil (layout, p).value_or (" error" );
147
+ expect (result == " Henry Foster I'm not hungry. Have a nice day!" ) << result;
148
+ };
149
+
150
+ " inverted_section_with_extra_text_false" _test = [] {
151
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry{{/hungry}}. Have a nice day!)" ;
152
+
153
+ person p{" Henry" , " Foster" , 34 , true }; // hungry is true
154
+ auto result = glz::stencil (layout, p).value_or (" error" );
155
+ expect (result == " Henry Foster . Have a nice day!" ) << result;
156
+ };
157
+
158
+ " nested_inverted_section" _test = [] {
159
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry {{^employed}}and not employed{{/employed}}{{/hungry}})" ;
160
+
161
+ person p1{" Henry" , " Foster" , 34 , false , false };
162
+ auto result1 = glz::stencil (layout, p1).value_or (" error" );
163
+ expect (result1 == " Henry Foster I'm not hungry and not employed" ) << result1;
164
+
165
+ person p2{" Henry" , " Foster" , 34 , false , true };
166
+ auto result2 = glz::stencil (layout, p2).value_or (" error" );
167
+ expect (result2 == " Henry Foster I'm not hungry " ) << result2;
168
+
169
+ person p3{" Henry" , " Foster" , 34 , true , false };
170
+ std::string_view layout_skip = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry {{^employed}}and not employed{{/employed}}{{/hungry}})" ;
171
+ auto result3 = glz::stencil (layout_skip, p3).value_or (" error" );
172
+ expect (result3 == " Henry Foster " ) << result3;
173
+ };
174
+
175
+ " inverted_section_unknown_key" _test = [] {
176
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^unknown}}Should not appear{{/unknown}})" ;
177
+
178
+ person p{" Henry" , " Foster" , 34 , false };
58
179
auto result = glz::stencil (layout, p);
59
180
expect (not result.has_value ());
60
- expect (result.error () == glz::error_code::feature_not_supported);
181
+ expect (result.error () == glz::error_code::unknown_key);
182
+ };
183
+
184
+ " inverted_section_mismatched_closing_tag" _test = [] {
185
+ std::string_view layout = R"( {{first_name}} {{last_name}} {{^hungry}}I'm not hungry{{/hunger}})" ; // Mismatched closing tag
186
+
187
+ person p{" Henry" , " Foster" , 34 , false };
188
+ auto result = glz::stencil (layout, p);
189
+ expect (not result.has_value ());
190
+ expect (result.error () == glz::error_code::unexpected_end);
61
191
};
62
192
};
63
193
194
+ #include " glaze/stencil/stencilcount.hpp"
195
+
64
196
suite stencilcount_tests = [] {
65
197
" basic docstencil" _test = [] {
66
198
std::string_view layout = R"( # About
0 commit comments