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(""+"buffer:\n"+"line:\n");
45  }
46 
50  @Test
51  public void testAttributesIgnore() {
52  final Rec rec = new Rec();
53  rec.parser.parse("a[a]b[]c[[]d[]]e", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
54  rec.checkResult(""+"buffer:\n"+"line:\n"+"segment:(black/black)abc[]d]e\n");
55  }
56 
60  @Test
61  public void testAttributes1() {
62  final Rec rec = new Rec();
67  rec.parser.parse("[fixed]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
68  rec.parser.parse("[arcane]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
69  rec.parser.parse("[hand]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
70  rec.parser.parse("[strange]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
71  rec.parser.parse("[print]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
72  rec.parser.parse("[color=red]a b c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
73  rec.checkResult(""+//
74  "buffer:\n"+//
75  "line:\n"+//
76  "segment:(black/black)a \n"+//
77  "segment:(black/black)b \n"+//
78  "segment:(black/black)c\n"+//
79  "line:\n"+//
80  "segment:(bold)(black/black)a \n"+//
81  "segment:(bold)(black/black)b \n"+//
82  "segment:(bold)(black/black)c\n"+//
83  "line:\n"+//
84  "segment:(italic)(black/black)a \n"+//
85  "segment:(italic)(black/black)b \n"+//
86  "segment:(italic)(black/black)c\n"+//
87  "line:\n"+//
88  "segment:(underline)(black/black)a \n"+//
89  "segment:(underline)(black/black)b \n"+//
90  "segment:(underline)(black/black)c\n"+//
91  "line:\n"+//
92  "segment:(fixed)(black/black)a \n"+//
93  "segment:(fixed)(black/black)b \n"+//
94  "segment:(fixed)(black/black)c\n"+//
95  "line:\n"+//
96  "segment:(arcane)(black/black)a \n"+//
97  "segment:(arcane)(black/black)b \n"+//
98  "segment:(arcane)(black/black)c\n"+//
99  "line:\n"+//
100  "segment:(hand)(black/black)a \n"+//
101  "segment:(hand)(black/black)b \n"+//
102  "segment:(hand)(black/black)c\n"+//
103  "line:\n"+//
104  "segment:(strange)(black/black)a \n"+//
105  "segment:(strange)(black/black)b \n"+//
106  "segment:(strange)(black/black)c\n"+//
107  "line:\n"+//
108  "segment:(black/black)a \n"+//
109  "segment:(black/black)b \n"+//
110  "segment:(black/black)c\n"+//
111  "line:\n"+//
112  "segment:(red/black)a \n"+//
113  "segment:(red/black)b \n"+//
114  "segment:(red/black)c\n");
115  }
116 
120  @Test
121  public void testAttributes2() {
122  final Rec rec = new Rec();
123  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);
124  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);
125  rec.checkResult(""+//
126  "buffer:\n"+//
127  "line:\n"+//
128  "segment:(black/black)a\n"+//
129  "segment:(bold)(black/black)b\n"+//
130  "segment:(bold)(italic)(black/black)c\n"+//
131  "segment:(bold)(italic)(underline)(black/black)d\n"+//
132  "segment:(italic)(underline)(black/black)e\n"+//
133  "segment:(underline)(black/black)f\n"+//
134  "segment:(black/black)g\n"+//
135  "line:\n"+//
136  "segment:(black/black)Hello \n"+//
137  "segment:(bold)(black/black) \n"+//
138  "segment:(bold)(black/black)all \n"+//
139  "segment:(bold)(black/black)crossfire\n"+//
140  "segment:(black/black) \n"+//
141  "segment:(black/black)members \n");
142  }
143 
147  @Test
148  public void testAttributesFont1() {
149  final Rec rec = new Rec();
150  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);
151  rec.checkResult(""+//
152  "buffer:\n"+//
153  "line:\n"+//
154  "segment:(bold)(black/black)a\n"+//
155  "segment:(bold)(fixed)(black/black)b\n"+//
156  "segment:(bold)(arcane)(black/black)c\n"+//
157  "segment:(bold)(hand)(black/black)d\n"+//
158  "segment:(bold)(strange)(black/black)e\n"+//
159  "segment:(bold)(black/black)f\n");
160  }
161 
166  @Test
167  public void testAttributesFont2() {
168  final Rec rec = new Rec();
169  rec.parser.parse("a[fixed]b[/fixed]c", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
170  rec.checkResult(""+//
171  "buffer:\n"+//
172  "line:\n"+//
173  "segment:(black/black)a\n"+//
174  "segment:(fixed)(black/black)bc\n");
175  }
176 
180  @Test
181  public void testAttributesColor() {
182  final Rec rec = new Rec();
183  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);
184  rec.checkResult(""+//
185  "buffer:\n"+//
186  "line:\n"+//
187  "segment:(bold)(black/black)a\n"+//
188  "segment:(bold)(red/black)b\n"+//
189  "segment:(bold)(blue/black)c\n"+//
190  "segment:(bold)(green/black)d\n"+//
191  "segment:(bold)(black/black)e\n"+//
192  "segment:(bold)(java.awt.Color[r=1,g=171,b=79]/black)f\n");
193  }
194 
198  @Test
199  public void testAttributesReset() {
200  final Rec rec = new Rec();
201  rec.parser.parse("[b][i][ul][hand][color=red]first", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
203  rec.checkResult(""+//
204  "buffer:\n"+//
205  "line:\n"+//
206  "segment:(bold)(italic)(underline)(hand)(red/black)first\n"+//
207  "line:\n"+//
208  "segment:(black/black)second\n");
209  }
210 
214  @Test
215  public void testMultiLine() {
216  final Rec rec = new Rec();
217  rec.parser.parse("first\n[b]second\nth[/b]ird[i]\n"+"fourth", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
218  rec.checkResult(""+//
219  "buffer:\n"+//
220  "line:\n"+//
221  "segment:(black/black)first\n"+//
222  "line:\n"+//
223  "segment:(bold)(black/black)second\n"+//
224  "line:\n"+//
225  "segment:(bold)(black/black)th\n"+//
226  "segment:(black/black)ird\n"+//
227  "line:\n"+//
228  "segment:(italic)(black/black)fourth\n");
229  }
230 
234  @Test
235  public void testDropUnClosedTag() {
236  final Rec rec = new Rec();
237  rec.parser.parse("abc[fixed", MessageType.MSG_TYPE_BOOK, MessageType.MSG_SUBTYPE_BOOK_CLASP_1, Color.BLACK, rec.buffer);
238  rec.checkResult(""+//
239  "buffer:\n"+//
240  "line:\n"+//
241  "segment:(black/black)abc\n");
242  }
243 
247  private static class Rec {
248 
252  @NotNull
253  private final Parser parser;
254 
258  @NotNull
259  private final Buffer buffer;
260 
264  private Rec() {
265  parser = new Parser(Color.BLACK);
266  buffer = new TestBuffer(1000, 200, 10);
267  }
268 
273  private void checkResult(@NotNull final String expected) {
274  Assert.assertEquals(expected, dumpBuffer());
275  }
276 
281  @NotNull
282  private String dumpBuffer() {
283  final StringBuilder sb = new StringBuilder();
284  dumpBuffer(sb, buffer);
285  return sb.toString();
286  }
287 
293  private static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer) {
294  sb.append("buffer:\n");
295  synchronized (buffer.getSyncObject()) {
296  for (final Line line : buffer.lines(0)) {
297  dumpLine(sb, line);
298  }
299  }
300  }
301 
307  private static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Line line) {
308  sb.append("line:\n");
309  for (final TextSegment segment : line.segments()) {
310  sb.append(segment);
311  }
312  }
313 
314  }
315 
316 }
com.realtime.crossfire.jxclient
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)
Definition: Parser.java:138
com.realtime.crossfire.jxclient.gui.log.ParserTest.testEmpty
void testEmpty()
Definition: ParserTest.java:41
com.realtime.crossfire.jxclient.gui.log.ParserTest.testMultiLine
void testMultiLine()
Definition: ParserTest.java:215
com.realtime.crossfire.jxclient.gui.log.Line
Definition: Line.java:39
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributes1
void testAttributes1()
Definition: ParserTest.java:61
com.realtime.crossfire.jxclient.gui.log.Buffer
Definition: Buffer.java:41
com.realtime.crossfire.jxclient.gui.log.TextSegment
Definition: TextSegment.java:40
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.checkResult
void checkResult(@NotNull final String expected)
Definition: ParserTest.java:273
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesFont1
void testAttributesFont1()
Definition: ParserTest.java:148
com.realtime.crossfire.jxclient.gui.log.Buffer.lines
final List< Line > lines
Definition: Buffer.java:63
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesFont2
void testAttributesFont2()
Definition: ParserTest.java:167
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec
Definition: ParserTest.java:247
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.parser
final Parser parser
Definition: ParserTest.java:253
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.buffer
final Buffer buffer
Definition: ParserTest.java:259
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpBuffer
String dumpBuffer()
Definition: ParserTest.java:282
com.realtime.crossfire.jxclient.protocol.MessageType
Definition: MessageType.java:33
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesReset
void testAttributesReset()
Definition: ParserTest.java:199
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpBuffer
static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer)
Definition: ParserTest.java:293
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesColor
void testAttributesColor()
Definition: ParserTest.java:181
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.Rec
Rec()
Definition: ParserTest.java:264
com.realtime.crossfire.jxclient.gui.log.ParserTest.Rec.dumpLine
static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Line line)
Definition: ParserTest.java:307
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.gui.log.ParserTest
Definition: ParserTest.java:35
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributes2
void testAttributes2()
Definition: ParserTest.java:121
com.realtime.crossfire.jxclient.gui.log.ParserTest.testAttributesIgnore
void testAttributesIgnore()
Definition: ParserTest.java:51
com.realtime.crossfire.jxclient.gui.log.ParserTest.testDropUnClosedTag
void testDropUnClosedTag()
Definition: ParserTest.java:235
com.realtime.crossfire.jxclient.gui.log.TestBuffer
Definition: TestBuffer.java:11
com.realtime.crossfire.jxclient.gui.log.Buffer.getSyncObject
Object getSyncObject()
Definition: Buffer.java:327
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.Parser
Definition: Parser.java:40
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_BOOK
static final int MSG_TYPE_BOOK
Definition: MessageType.java:38