-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
MiniMessageParserTest.java
517 lines (440 loc) · 20.2 KB
/
MiniMessageParserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.minimessage;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tree.Node;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static net.kyori.adventure.text.event.ClickEvent.openUrl;
import static net.kyori.adventure.text.event.ClickEvent.runCommand;
import static net.kyori.adventure.text.event.HoverEvent.showText;
import static net.kyori.adventure.text.format.NamedTextColor.BLUE;
import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.GREEN;
import static net.kyori.adventure.text.format.NamedTextColor.RED;
import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
import static net.kyori.adventure.text.format.Style.style;
import static net.kyori.adventure.text.format.TextDecoration.BOLD;
import static net.kyori.adventure.text.format.TextDecoration.UNDERLINED;
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.component;
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.parsed;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class MiniMessageParserTest extends AbstractTest {
@Test
void testStripSimple() {
final String input = "<yellow>TEST<green> nested</green>Test";
final String expected = "TEST nestedTest";
assertEquals(expected, PARSER.stripTags(input));
}
@Test
void testStripComplex() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here</click><blue> to <bold>FEEL</underlined> it";
final String expected = "<test> random strangerclick here to FEEL it";
assertEquals(expected, PARSER.stripTags(input));
}
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/169
@Test
void testStripComplexInner() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here <please></click><blue> to <bold>FEEL</underlined> it";
final String expected = "<test> random strangerclick here <please> to FEEL it";
assertEquals(expected, PARSER.stripTags(input));
}
@Test
void testStripInner() {
final String input = "<hover:show_text:\"<red>test:TEST\">TEST";
final String expected = "TEST";
assertEquals(expected, PARSER.stripTags(input));
}
@Test
void testStripTags() {
final String input = "Hello, <red><name>!";
final String expected = "Hello, !";
assertEquals(expected, PARSER.stripTags(input, parsed("name", "you")));
}
@Test
void testEscapeSimple() {
final String input = "<yellow>TEST<green> nested</green>Test";
final String expected = "\\<yellow>TEST\\<green> nested\\</green>Test";
assertEquals(expected, PARSER.escapeTags(input));
}
@Test
void testEscapeComplex() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here</click><blue> to <bold>FEEL</underlined> it";
final String expected = "\\<yellow><test> random \\<bold>stranger\\</bold>\\<click:run_command:test command>\\<underlined>\\<red>click here\\</click>\\<blue> to \\<bold>FEEL\\</underlined> it";
assertEquals(expected, PARSER.escapeTags(input));
}
@Test
void testEscapeInner() {
final String input = "<hover:show_text:\"<red>test:TEST\">TEST";
final String expected = "\\<hover:show_text:\"\\<red>test:TEST\">TEST";
assertEquals(expected, PARSER.escapeTags(input));
}
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/169
@Test
void testEscapeComplexInner() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here <notToken></click><blue> to <bold>FEEL</underlined> it";
final String expected = "\\<yellow><test> random \\<bold>stranger\\</bold>\\<click:run_command:test command>\\<underlined>\\<red>click here <notToken>\\</click>\\<blue> to \\<bold>FEEL\\</underlined> it";
assertEquals(expected, PARSER.escapeTags(input));
}
@Test
void testEscapePlaceholders() {
final String input = "Hello, <red><name>!";
final String expected = "Hello, \\<red>\\<name>!";
assertEquals(expected, PARSER.escapeTags(input, parsed("name", "you")));
}
@Test
void testUnescape() {
final String input = "<yellow>TEST\\<green> nested\\</green>Test";
final String expected = "TEST<green> nested</green>Test";
final Component comp = PARSER.deserialize(input);
assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp));
}
@Test
void testNoUnescape() {
final String input = "<yellow>TEST\\<green> \\\\< nested\\</green>Test";
final String expected = "TEST<green> \\< nested</green>Test";
final TextComponent comp = (TextComponent) PARSER.deserialize(input);
assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp));
}
@Test
void testEscapeParse() {
final String expected = "<red>test</red>";
final String escaped = MiniMessage.miniMessage().escapeTags(expected);
final Component comp = MiniMessage.miniMessage().deserialize(escaped);
assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp));
}
@Test
void testNiceMix() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here</click><blue> to <b>FEEL</underlined> it";
final Component expected = empty().color(YELLOW)
.append(text("Hello!"))
.append(text(" random "))
.append(text("stranger").decorate(BOLD))
.append(text("click here").color(RED).decorate(UNDERLINED).clickEvent(runCommand("test command")))
.append(empty().color(BLUE)
.append(text(" to "))
.append(text("FEEL</underlined> it").decorate(BOLD))
);
this.assertParsedEquals(expected, input, component("test", text("Hello!")));
}
@Test
void testInvalidTag() {
final String input = "<red><test>";
final Component expected = text("<test>").color(RED);
this.assertParsedEquals(expected, input);
}
@Test
void testInvalidTagComplex() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><oof></oof><underlined><red>click here</click><blue> to <bold>FEEL</underlined> it";
final Component expected = empty().color(YELLOW)
.append(text("<test> random "))
.append(text("stranger").decorate(BOLD))
.append(empty().clickEvent(runCommand("test command"))
.append(text("<oof></oof>"))
.append(text("click here").color(RED).decorate(UNDERLINED))
).append(empty().color(BLUE)
.append(text(" to "))
.append(text("FEEL</underlined> it").decorate(BOLD))
);
this.assertParsedEquals(expected, input);
}
@Test
void testBackSpace() {
final String input = "\\!/ IMPORTANT \\!/";
final Component expected = text("\\!/ IMPORTANT \\!/");
this.assertParsedEquals(expected, input);
}
@Test
void testGH5() {
final String input = "<dark_gray>»<gray> To download it from the internet, <click:open_url:'<pack_url>'><hover:show_text:\"<green>/!\\ install it from Options/ResourcePacks in your game\"><green><bold>CLICK HERE</bold></hover></click>";
final Component expected = empty().color(DARK_GRAY)
.append(text("»"))
.append(empty().color(GRAY)
.append(text(" To download it from the internet, "))
.append(text("CLICK HERE").decorate(BOLD).color(GREEN).clickEvent(openUrl("https://www.google.com")).hoverEvent(showText(text("/!\\ install it from Options/ResourcePacks in your game").color(GREEN))))
);
this.assertParsedEquals(expected, input, parsed("pack_url", "https://www.google.com"));
}
@Test
void testGH5Modified() {
final String input = "<dark_gray>»<gray> To download it from the internet, <click:open_url:'<pack_url>'><hover:show_text:'<green>/!\\ install it from \\'Options/ResourcePacks\\' in your game'><green><bold>CLICK HERE</bold></hover></click>";
final Component expected = empty().color(DARK_GRAY)
.append(text("»"))
.append(empty().color(GRAY)
.append(text(" To download it from the internet, "))
.append(text("CLICK HERE").decorate(BOLD).color(GREEN).hoverEvent(showText(text("/!\\ install it from 'Options/ResourcePacks' in your game").color(GREEN))).clickEvent(openUrl("https://www.google.com")))
);
// should work
this.assertParsedEquals(expected, input, parsed("pack_url", "https://www.google.com"));
}
@Test
void testGH5Quoted() {
final String input = "<dark_gray>»<gray> To download it from the internet, <click:open_url:\"https://www.google.com\"><hover:show_text:\"<green>/!\\ install it from Options/ResourcePacks in your game\"><green><bold>CLICK HERE</bold></hover></click>";
final Component expected = empty().color(DARK_GRAY)
.append(text("»"))
.append(empty().color(GRAY)
.append(text(" To download it from the internet, "))
.append(text("CLICK HERE").decorate(BOLD).color(GREEN).clickEvent(openUrl("https://www.google.com")).hoverEvent(showText(text("/!\\ install it from Options/ResourcePacks in your game").color(GREEN))))
);
// should work
this.assertParsedEquals(expected, input);
// shouldnt throw an error
PARSER.deserialize(input, parsed("url", "https://www.google.com"));
}
@Test
void testDoubleNewLine() {
final Component expected = text("Hello\n\nWorld").color(RED);
final String input = "<red>Hello\n\nWorld";
this.assertParsedEquals(expected, input);
}
@Test
void testMismatchedTags() {
final Component expected = text("hello</red>").color(GREEN);
final String input = "<green>hello</red>";
this.assertParsedEquals(expected, input);
}
@Test
void testQuoteEscapingInArguments() {
final Component expected = translatable("test", text("\"\""));
final Component expected1 = translatable("test", text("''"));
final Component expected2 = translatable("test", text("''"));
final Component expected3 = translatable("test", text("\"\""));
final String input = "<lang:test:'\"\"'>";
final String input1 = "<lang:test:'\\'\\''>";
final String input2 = "<lang:test:\"''\">";
final String input3 = "<lang:test:\"\\\"\\\"\">";
this.assertParsedEquals(expected, input);
this.assertParsedEquals(expected1, input1);
this.assertParsedEquals(expected2, input2);
this.assertParsedEquals(expected3, input3);
}
@Test
void testNonTerminatingQuote() {
final Component expected = empty().append(text("Remember the<3\"").color(RED)).append(text(" bug"));
final Component expected1 = empty().append(text("Remember the<3'").color(RED)).append(text(" bug"));
final Component expected2 = empty().append(text("Remember the<h:\"").color(RED)).append(text(" bug"));
final Component expected3 = empty().append(text("Remember the<h:\"").color(RED)).append(text(" \"bug"));
// This one is an actually valid use of quotes
final Component expected4 = empty().append(text("Remember the<h:\"</red> \">bug").color(RED));
final String input = "<red>Remember the<3\"</red> bug";
final String input1 = "<red>Remember the<3'</red> bug";
final String input2 = "<red>Remember the<h:\"</red> bug";
final String input3 = "<red>Remember the<h:\"</red> \"bug";
final String input4 = "<red>Remember the<h:\"</red> \">bug";
this.assertParsedEquals(expected, input);
this.assertParsedEquals(expected1, input1);
this.assertParsedEquals(expected2, input2);
this.assertParsedEquals(expected3, input3);
this.assertParsedEquals(expected4, input4);
}
// GH-68, GH-93
@Test
void testAngleBracketsShit() {
final Component expected = empty().color(GRAY)
.append(text("<"))
.append(empty().color(YELLOW)
.append(text("TEST"))
.append(text("> Woo << double <3").color(GRAY))
);
final String input = "<gray><<yellow>TEST<gray>> Woo << double <3";
this.assertParsedEquals(expected, input);
}
// GH-111
@Test
void testNoSwallowSpace() {
final Component expected = empty().color(RED).hoverEvent(showText(text("Test")))
.append(text(" "))
.append(translatable("item.minecraft.stick"));
final String input = "<red><hover:show_text:\"Test\"> <lang:item.minecraft.stick>";
this.assertParsedEquals(expected, input);
}
// GH-134
@Test
void testEscapeOutsideOfContext() {
final String input = "\\\"";
final Component expected = text("\\\"");
this.assertParsedEquals(expected, input);
}
@Test
void testEscapesEscapablePlainText() {
final String input = "\\\\<red>hi";
final Component expected = text()
.content("\\")
.append(Component.text("hi", NamedTextColor.RED))
.build();
this.assertParsedEquals(expected, input);
}
@Test
void testEscapeInsideOfContext() {
final String input = "<hover:show_text:'Look at\\\\ this \\''>Test";
final Component expected = text()
.content("Test")
.hoverEvent(text("Look at\\ this '"))
.build();
this.assertParsedEquals(expected, input);
}
@Test
void testEscapeAtEnd() {
final String input = "Please don't crash \\";
final Component expected = text("Please don't crash \\");
this.assertParsedEquals(expected, input);
}
@Test
void testCaseInsensitive() {
final String input1 = "<red>this is <BOLD>an error</bold> message";
final String input2 = "<C:reD>also red";
final Component expected1 = text()
.color(RED)
.append(text("this is "))
.append(text("an error", style(BOLD)))
.append(text(" message"))
.build();
final Component expected2 = text("also red", RED);
this.assertParsedEquals(expected1, input1);
this.assertParsedEquals(expected2, input2);
}
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/165
@Test
void testClosingTagAtRootLevel() {
final String input = "one</blue>two";
final Component expected = text("one</blue>two");
this.assertParsedEquals(expected, input);
}
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/166
@Test
void testEmptyTagPart() {
final String input = "<hover:show_text:\"\">text</hover>";
final String input2 = "<hover:show_text:>text</hover>";
final Component expected = text("text").hoverEvent(showText(empty()));
this.assertParsedEquals(expected, input);
this.assertParsedEquals(expected, input2);
}
@Test
void testTreeOutput() {
final String input = "<red> RED <blue> <name> <click:open_url:https://github.com> good <action> </click>";
final TagResolver resolver = TagResolver.resolver(Placeholder.parsed("name", "you"), Placeholder.component("action", Component.text("click")));
final Node tree = MiniMessage.miniMessage().deserializeToTree(input, resolver);
final String expected = "Node {\n" +
" TagNode('red') {\n" +
" TextNode(' RED ')\n" +
" TagNode('blue') {\n" +
" TextNode(' you ')\n" +
" TagNode('click', 'open_url', 'https://github.com') {\n" +
" TextNode(' good ')\n" +
" TagNode('action') {\n" +
" }\n" +
" TextNode(' ')\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n";
assertEquals(expected, tree.toString());
}
@Test
void testTagsSelfClosable() {
final String input = "<red>hello <lang:gameMode.creative/> there";
final Component parsed = Component.text()
.content("hello ")
.color(NamedTextColor.RED)
.append(
Component.translatable("gameMode.creative"),
Component.text(" there")
)
.build();
this.assertParsedEquals(parsed, input);
}
@Test
void testIgnorableSelfClosable() {
final String input = "<red/>things";
final Component parsed = Component.text().append(
Component.text("", NamedTextColor.RED),
Component.text("things")
)
.build();
this.assertParsedEquals(parsed, input);
}
@Test
void testLegacySymbolForbidden() {
final String failingTest = "Hello §Cfriends";
// Non-strict
System.out.println(assertThrows(ParsingException.class, () -> PARSER.deserialize(failingTest)).getMessage());
}
@Test
void testInvalidTagNames() {
final String input1 = "Hello <this_is_%not_allowed> but ignored?";
final String input2 = "Hello <this_is_not_allowed!> but ignored?";
final String input3 = "Hello <!?this_is_not_allowed> but ignored?";
final String input4 = "Hello <##this_is_%not_allowed> but ignored?";
final String input5 = "<3 >Mini<3 />Message</3 >";
final String input6 = "this message <\"red\">isn't red";
this.assertParsedEquals(Component.text(input1), input1);
this.assertParsedEquals(Component.text(input2), input2);
this.assertParsedEquals(Component.text(input3), input3);
this.assertParsedEquals(Component.text(input4), input4);
this.assertParsedEquals(Component.text(input5), input5);
this.assertParsedEquals(Component.text(input6), input6);
}
@Test
void testValidTagNames() {
final String passingTest = "Hello <this_is_allowed> but cool?";
final String passingTest1 = "Hello <this-is-allowed> but cool?";
final String passingTest2 = "Hello <!allowed> but cool?";
final String passingTest3 = "Hello <?allowed> but cool?";
final String passingTest4 = "Hello <#allowed> but cool?";
assertDoesNotThrow(() -> PARSER.deserialize(passingTest));
assertDoesNotThrow(() -> PARSER.deserialize(passingTest1));
assertDoesNotThrow(() -> PARSER.deserialize(passingTest2));
assertDoesNotThrow(() -> PARSER.deserialize(passingTest3));
assertDoesNotThrow(() -> PARSER.deserialize(passingTest4));
}
@Test
void invalidPreprocessTagNames() {
final String input = "Some<##>of<>these<tag>are<3 >tags";
final Component expected = Component.text("Some<##>of<>these(meow)are<3 >tags");
final TagResolver alwaysMatchingResolver = new TagResolver() {
@Override
public Tag resolve(final @NotNull String name, final @NotNull ArgumentQueue arguments, final @NotNull Context ctx) throws ParsingException {
return Tag.preProcessParsed("(meow)");
}
@Override
public boolean has(final @NotNull String name) {
return true;
}
};
assertParsedEquals(expected, input, alwaysMatchingResolver);
}
}