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
|
package bjc.utils.parserutils;
import static bjc.utils.parserutils.TokenUtils.descapeString;
import static bjc.utils.parserutils.TokenUtils.removeDQuotedStrings;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/*
* Tests for TokenUtils
*/
@SuppressWarnings("javadoc")
public class TokenUtilsTest {
@Rule
public ExpectedException exp = ExpectedException.none();
/*
* Test removeDQuoted
*/
/*
* Check handling of mismatched strings with no matching strings.
*/
@Test
public void testRemoveDQuoted_MismatchedStringNoMatch() throws IllegalArgumentException {
exp.expect(IllegalArgumentException.class);
exp.expectMessage(containsString("Opening quote was at position 0"));
removeDQuotedStrings("\"hello");
}
/*
* Check handling of mismatched strings with a matching string.
*/
@Test
public void testRemoveDQuoted_MismatchedStringMatch() throws IllegalArgumentException {
exp.expect(IllegalArgumentException.class);
exp.expectMessage(containsString("Opening quote was at position 7"));
removeDQuotedStrings("\"hello\"\"");
}
/*
* Check handling of strings with a single embedded string.
*/
@Test
public void testRemoveDQuoted_SingleString() {
final List<String> onSingleMatchString = removeDQuotedStrings("hello\"there\"");
assertThat(onSingleMatchString, hasItems("hello", "\"there\""));
}
/*
* Check handling of strings with multiple quoted strings in a row.
*/
@Test
public void testRemoveDQuoted_MultipleSerialString() {
final List<String> onMultipleSerialMatchString = removeDQuotedStrings("\"hello\"\"there\"");
assertThat(onMultipleSerialMatchString, hasItems("\"hello\"", "\"there\""));
}
/*
* Check handling of strings with multiple interleaved strings.
*/
@Test
public void testRemoveDQuoted_MultipleInterleavedString() {
final List<String> onMultipleInterleaveMatchString = removeDQuotedStrings("one\"two\"three\"four\"");
assertThat(onMultipleInterleaveMatchString, hasItems("one", "\"two\"", "three", "\"four\""));
}
/*
* Check handling of strings without embedded strings.
*/
@Test
public void testRemoveDQuote_NoString() {
final List<String> onNonmatchingString = removeDQuotedStrings("hello");
assertThat(onNonmatchingString, hasItems("hello"));
}
/*
* Check handling of empty strings.
*/
@Test
public void testRemoveDQuote_EmptyString() {
final List<String> onEmptyString = removeDQuotedStrings("");
assertThat(onEmptyString, hasItems(""));
}
/*
* Test descapeString
*/
/*
* Check handling of empty strings.
*/
@Test
public void testDescapeString_EmptyString() {
final String onEmptyString = descapeString("");
assertThat(onEmptyString, is(""));
}
/*
* Check handling of strings without escapes
*/
@Test
public void testDescapeString_NonescapeString() {
final String onNonescapeString = descapeString("hello there");
assertThat(onNonescapeString, is("hello there"));
}
/*
* Check handling of strings with single escapes.
*/
@Test
public void testDescapeString_SingleEscapeString() {
final String onSingleEscapeString = descapeString("hello\\tthere");
assertThat(onSingleEscapeString, is("hello\tthere"));
}
/*
* Check handling of strings with multiple escapes.
*/
@Test
public void testDescapeString_MultipleEscapeString() {
final String onMultipleEscapeString = descapeString("hello\\tthere\\tworld");
assertThat(onMultipleEscapeString, is("hello\tthere\tworld"));
}
/*
* Check handling of strings with invalid single escapes.
*/
@Test
public void testDescapeString_InvalidSingleEscapeString() throws IllegalArgumentException {
exp.expect(IllegalArgumentException.class);
exp.expectMessage(containsString("at position 0"));
descapeString("\\x");
}
}
|