@@ -14,6 +14,9 @@ pub struct Span<'a> {
14
14
15
15
impl < ' a > Display for Span < ' a > {
16
16
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
17
+ if self . content . is_empty ( ) {
18
+ return Ok ( ( ) ) ;
19
+ }
17
20
f. write_str ( "<span class=\" " ) ?;
18
21
if let Some ( color) = & self . color {
19
22
f. write_str ( color) ?;
@@ -110,3 +113,104 @@ pub fn api_words<T: Display>(s: T) -> askama::Result<&'static str> {
110
113
_ => "Unknown" ,
111
114
} )
112
115
}
116
+
117
+ #[ cfg( test) ]
118
+ mod tests {
119
+ use super :: * ;
120
+ #[ test]
121
+ fn test_api_words ( ) {
122
+ assert_eq ! ( api_words( "Operational" ) . unwrap( ) , "OK" ) ;
123
+ assert_eq ! ( api_words( "PossibleProblems" ) . unwrap( ) , "Flaky" ) ;
124
+ assert_eq ! ( api_words( "DefiniteProblems" ) . unwrap( ) , "Down" ) ;
125
+ assert_eq ! ( api_words( "operational" ) . unwrap( ) , "Unknown" ) ;
126
+ }
127
+ #[ test]
128
+ fn test_api_colors ( ) {
129
+ assert_eq ! ( api_color( "Operational" ) . unwrap( ) , "green" ) ;
130
+ assert_eq ! ( api_color( "PossibleProblems" ) . unwrap( ) , "yellow" ) ;
131
+ assert_eq ! ( api_color( "DefiniteProblems" ) . unwrap( ) , "red" ) ;
132
+ assert_eq ! ( api_color( "operational" ) . unwrap( ) , "blue" ) ;
133
+ }
134
+ #[ test]
135
+ fn test_span_no_color ( ) {
136
+ let class = [ "test" . to_string ( ) ] ;
137
+ let span = Span :: new ( & class, None , "test content" ) ;
138
+ assert_eq ! (
139
+ span. to_string( ) ,
140
+ "<span class=\" test \" >test content</span>"
141
+ ) ;
142
+ }
143
+ #[ test]
144
+ fn test_span_color_5 ( ) {
145
+ let class = [ "test" . to_string ( ) , "test2" . to_string ( ) ] ;
146
+ let span = Span :: new ( & class, Some ( "motd-style-5" ) , "test content" ) ;
147
+ assert_eq ! (
148
+ span. to_string( ) ,
149
+ "<span class=\" motd-style-5 test test2 \" >test content</span>"
150
+ ) ;
151
+ }
152
+ #[ test]
153
+ fn test_span_empty_class ( ) {
154
+ let class = [ ] ;
155
+ let span = Span :: new ( & class, Some ( "motd-style-5" ) , "test content" ) ;
156
+ assert_eq ! (
157
+ span. to_string( ) ,
158
+ "<span class=\" motd-style-5 \" >test content</span>"
159
+ ) ;
160
+ }
161
+ #[ test]
162
+ fn test_span_empty_both ( ) {
163
+ let class = [ ] ;
164
+ let span = Span :: new ( & class, None , "test content" ) ;
165
+ assert_eq ! ( span. to_string( ) , "<span class=\" \" >test content</span>" ) ;
166
+ }
167
+ #[ test]
168
+ fn test_span_escaping ( ) {
169
+ let class = [ ] ;
170
+ let span = Span :: new ( & class, None , "<script>alert(\" bad\" );</script>" ) ;
171
+ assert_eq ! (
172
+ span. to_string( ) ,
173
+ "<span class=\" \" ><script>alert("bad");</script></span>"
174
+ ) ;
175
+ }
176
+ #[ test]
177
+ fn test_colorize_none ( ) {
178
+ let input = "No color codes" ;
179
+ assert_eq ! (
180
+ mojang_colorize( input) . unwrap( ) ,
181
+ "<span class=\" \" >No color codes</span>"
182
+ ) ;
183
+ }
184
+ #[ test]
185
+ fn test_colorize_one_color ( ) {
186
+ let input = "§acolor a" ;
187
+ assert_eq ! (
188
+ mojang_colorize( input) . unwrap( ) ,
189
+ "<span class=\" motd-style-a \" >color a</span>"
190
+ ) ;
191
+ }
192
+ #[ test]
193
+ fn test_colorize_color_immediate_change ( ) {
194
+ let input = "§a§bcolor b" ;
195
+ assert_eq ! (
196
+ mojang_colorize( input) . unwrap( ) ,
197
+ "<span class=\" motd-style-b \" >color b</span>"
198
+ ) ;
199
+ }
200
+ #[ test]
201
+ fn test_colorize_color_reset ( ) {
202
+ let input = "§acolor a§rblank§bcolor b" ;
203
+ assert_eq ! (
204
+ mojang_colorize( input) . unwrap( ) ,
205
+ r#"<span class="motd-style-a ">color a</span><span class="">blank</span><span class="motd-style-b ">color b</span>"#
206
+ ) ;
207
+ }
208
+ #[ test]
209
+ fn test_colorize_additive ( ) {
210
+ let input = "§a§nunderlined" ;
211
+ assert_eq ! (
212
+ mojang_colorize( input) . unwrap( ) ,
213
+ r#"<span class="motd-style-a motd-style-n ">underlined</span>"#
214
+ ) ;
215
+ }
216
+ }
0 commit comments