Crossfire JXClient, Trunk
ParserTest.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.log;
24 
26 import java.awt.Color;
27 import org.jetbrains.annotations.NotNull;
28 import org.junit.Assert;
29 import org.junit.Test;
30 
35 public class ParserTest {
36 
40  @Test
41  public void testEmpty() {
42  final Rec rec = new Rec();
44  rec.checkResult("""
45  buffer:
46  line:
47  """);
48  }
49 
53  @Test
54  public void testAttributesIgnore() {
55  final Rec rec = new Rec();
56  rec.parser.parse("a[a]b[]c[[]d[]]e", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
57  rec.checkResult("""
58  buffer:
59  line:
60  segment:(black/black)abc[]d]e
61  """);
62  }
63 
67  @Test
68  public void testAttributes1() {
69  final Rec rec = new Rec();
74  rec.parser.parse("[fixed]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
75  rec.parser.parse("[arcane]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
76  rec.parser.parse("[hand]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
77  rec.parser.parse("[strange]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
78  rec.parser.parse("[print]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
79  rec.parser.parse("[color=red]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
80  rec.checkResult("""
81  buffer:
82  line:
83  segment:(black/black)a\s
84  segment:(black/black)b\s
85  segment:(black/black)c
86  line:
87  segment:(bold)(black/black)a\s
88  segment:(bold)(black/black)b\s
89  segment:(bold)(black/black)c
90  line:
91  segment:(italic)(black/black)a\s
92  segment:(italic)(black/black)b\s
93  segment:(italic)(black/black)c
94  line:
95  segment:(underline)(black/black)a\s
96  segment:(underline)(black/black)b\s
97  segment:(underline)(black/black)c
98  line:
99  segment:(fixed)(black/black)a\s
100  segment:(fixed)(black/black)b\s
101  segment:(fixed)(black/black)c
102  line:
103  segment:(arcane)(black/black)a\s
104  segment:(arcane)(black/black)b\s
105  segment:(arcane)(black/black)c
106  line:
107  segment:(hand)(black/black)a\s
108  segment:(hand)(black/black)b\s
109  segment:(hand)(black/black)c
110  line:
111  segment:(strange)(black/black)a\s
112  segment:(strange)(black/black)b\s
113  segment:(strange)(black/black)c
114  line:
115  segment:(black/black)a\s
116  segment:(black/black)b\s
117  segment:(black/black)c
118  line:
119  segment:(red/black)a\s
120  segment:(red/black)b\s
121  segment:(red/black)c
122  """);
123  }
124 
128  @Test
129  public void testAttributes2() {
130  final Rec rec = new Rec();
131  rec.parser.parse("a[b]b[i]c[ul]d[/b]e[/i]f[/ul]g", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
132  rec.parser.parse("Hello [b] all [b]crossfire[/b] members [/b]", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
133  rec.checkResult("""
134  buffer:
135  line:
136  segment:(black/black)a
137  segment:(bold)(black/black)b
138  segment:(bold)(italic)(black/black)c
139  segment:(bold)(italic)(underline)(black/black)d
140  segment:(italic)(underline)(black/black)e
141  segment:(underline)(black/black)f
142  segment:(black/black)g
143  line:
144  segment:(black/black)Hello\s
145  segment:(bold)(black/black)\s
146  segment:(bold)(black/black)all\s
147  segment:(bold)(black/black)crossfire
148  segment:(black/black)\s
149  segment:(black/black)members\s
150  """);
151  }
152 
156  @Test
157  public void testAttributesFont1() {
158  final Rec rec = new Rec();
159  rec.parser.parse("[b]a[fixed]b[arcane]c[hand]d[strange]e[print]f", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
160  rec.checkResult("""
161  buffer:
162  line:
163  segment:(bold)(black/black)a
164  segment:(bold)(fixed)(black/black)b
165  segment:(bold)(arcane)(black/black)c
166  segment:(bold)(hand)(black/black)d
167  segment:(bold)(strange)(black/black)e
168  segment:(bold)(black/black)f
169  """);
170  }
171 
176  @Test
177  public void testAttributesFont2() {
178  final Rec rec = new Rec();
179  rec.parser.parse("a[fixed]b[/fixed]c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
180  rec.checkResult("""
181  buffer:
182  line:
183  segment:(black/black)a
184  segment:(fixed)(black/black)bc
185  """);
186  }
187 
191  @Test
192  public void testAttributesColor() {
193  final Rec rec = new Rec();
194  rec.parser.parse("[b]a[color=red]b[color=blue]c[color=green]d[/color]e[color=#01AB4F]f[/color]", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
195  rec.checkResult("""
196  buffer:
197  line:
198  segment:(bold)(black/black)a
199  segment:(bold)(red/black)b
200  segment:(bold)(blue/black)c
201  segment:(bold)(green/black)d
202  segment:(bold)(black/black)e
203  segment:(bold)(java.awt.Color[r=1,g=171,b=79]/black)f
204  """);
205  }
206 
210  @Test
211  public void testAttributesReset() {
212  final Rec rec = new Rec();
213  rec.parser.parse("[b][i][ul][hand][color=red]first", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
215  rec.checkResult("""
216  buffer:
217  line:
218  segment:(bold)(italic)(underline)(hand)(red/black)first
219  line:
220  segment:(black/black)second
221  """);
222  }
223 
227  @Test
228  public void testMultiLine() {
229  final Rec rec = new Rec();
230  rec.parser.parse("""
231  first
232  [b]second
233  th[/b]ird[i]
235  rec.checkResult("""
236  buffer:
237  line:
238  segment:(black/black)first
239  line:
240  segment:(bold)(black/black)second
241  line:
242  segment:(bold)(black/black)th
243  segment:(black/black)ird
244  line:
245  segment:(italic)(black/black)fourth
246  """);
247  }
248 
252  @Test
253  public void testDropUnClosedTag() {
254  final Rec rec = new Rec();
255  rec.parser.parse("abc[fixed", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
256  rec.checkResult("""
257  buffer:
258  line:
259  segment:(black/black)abc
260  """);
261  }
262 
266  private static class Rec {
267 
271  @NotNull
272  private final Parser parser;
273 
277  @NotNull
278  private final Buffer buffer;
279 
283  private Rec() {
284  parser = new Parser(Color.BLACK);
285  buffer = new TestBuffer(1000, 200, 10);
286  }
287 
292  private void checkResult(@NotNull final String expected) {
293  Assert.assertEquals(expected, dumpBuffer());
294  }
295 
300  @NotNull
301  private String dumpBuffer() {
302  final StringBuilder sb = new StringBuilder();
303  dumpBuffer(sb, buffer);
304  return sb.toString();
305  }
306 
312  private static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer) {
313  sb.append("buffer:\n");
314  synchronized (buffer.getSyncObject()) {
315  for (final Line line : buffer.lines(0)) {
316  dumpLine(sb, line);
317  }
318  }
319  }
320 
326  private static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Line line) {
327  sb.append("line:\n");
328  for (final TextSegment segment : line.segments()) {
329  sb.append(segment);
330  }
331  }
332 
333  }
334 
335 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec
Encapsulates the state.
Definition: ParserTest.java:266
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesColor
void testAttributesColor()
Checks that color attributes are correctly parsed.
Definition: ParserTest.java:192
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributes1
void testAttributes1()
Checks that attributes are correctly parsed.
Definition: ParserTest.java:68
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesReset
void testAttributesReset()
Checks that attributes are reset for each message.
Definition: ParserTest.java:211
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.Rec
Rec()
Creates a new instance.
Definition: ParserTest.java:283
com.realtime.crossfire.jxclient.gui.log.ParserTest.testDropUnClosedTag
void testDropUnClosedTag()
Checks that an un-closed tag is dropped.
Definition: ParserTest.java:253
com.realtime.crossfire.jxclient.gui.log.Parser
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance.
Definition: Parser.java:40
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_BOOK
static final int MSG_TYPE_BOOK
drawextinfo message type: character did read a book.
Definition: MessageType.java:38
com.realtime.crossfire.jxclient.protocol.MessageType
Encapsulates the message type numbers for drawextinfo messages.
Definition: MessageType.java:33
com.realtime.crossfire.jxclient.gui.log.Buffer.lines
final List< Line > lines
The lines in display order.
Definition: Buffer.java:63
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpBuffer
static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer)
Appends the contents of a Buffer to a StringBuilder.
Definition: ParserTest.java:312
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.gui.log.Buffer.getSyncObject
Object getSyncObject()
Returns the object to synchronize on when calling lines(int).
Definition: Buffer.java:327
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpBuffer
String dumpBuffer()
Returns a string representation of buffer.
Definition: ParserTest.java:301
com.realtime.crossfire.jxclient.gui.log.TextSegment
One segment of a Line which should be displayed without changing attributes.
Definition: TextSegment.java:40
com.realtime.crossfire.jxclient.gui.log.Line
Manages the contents of one text line.
Definition: Line.java:39
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpLine
static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Line line)
Appends the contents of a Line to a StringBuilder.
Definition: ParserTest.java:326
com.realtime.crossfire.jxclient.gui.log.Parser.parse
void parse(@NotNull final CharSequence text, final int type, final int subtype, @NotNull final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
Definition: Parser.java:138
com.realtime.crossfire.jxclient.gui.log.Buffer
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesFont1
void testAttributesFont1()
Checks that font attributes are correctly parsed.
Definition: ParserTest.java:157
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_SUBTYPE_BOOK_CLASP_1
static final int MSG_SUBTYPE_BOOK_CLASP_1
Definition: MessageType.java:145
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.checkResult
void checkResult(@NotNull final String expected)
Checks for expected contents of buffer.
Definition: ParserTest.java:292
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.parser
final Parser parser
The default parser.
Definition: ParserTest.java:272
com.realtime
com
com.realtime.crossfire.jxclient.gui.log.ParserTest.testMultiLine
void testMultiLine()
Checks that multi-line messages are correctly parsed.
Definition: ParserTest.java:228
com.realtime.crossfire.jxclient.gui.log.TestBuffer
A Buffer implementation for tests.
Definition: TestBuffer.java:11
com.realtime.crossfire.jxclient.gui.log.ParserTest.testEmpty
void testEmpty()
Checks that an empty string does not add anything.
Definition: ParserTest.java:41
com.realtime.crossfire.jxclient.gui.log.ParserTest
Regression tests for Parser.
Definition: ParserTest.java:35
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributes2
void testAttributes2()
Checks that attributes are correctly parsed.
Definition: ParserTest.java:129
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.buffer
final Buffer buffer
The default buffer.
Definition: ParserTest.java:278
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesFont2
void testAttributesFont2()
Checks that font attributes are correctly parsed: [/fixed] is undefined/does not end [fixed] block.
Definition: ParserTest.java:177
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesIgnore
void testAttributesIgnore()
Checks that unknown attributes are ignored.
Definition: ParserTest.java:54