2
* Copyright (C) 2009 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.escape.testing;
19
import static com.google.common.escape.Escapers.computeReplacement;
21
import com.google.common.annotations.GwtCompatible;
22
import com.google.common.escape.CharEscaper;
23
import com.google.common.escape.Escaper;
24
import com.google.common.escape.UnicodeEscaper;
25
import java.io.IOException;
26
import junit.framework.Assert;
29
* Extra assert methods for testing Escaper implementations.
31
* @author David Beaumont
35
public final class EscaperAsserts {
36
private EscaperAsserts() {}
39
* Asserts that an escaper behaves correctly with respect to null inputs.
41
* @param escaper the non-null escaper to test
43
public static void assertBasic(Escaper escaper) throws IOException {
44
// Escapers operate on characters: no characters, no escaping.
45
Assert.assertEquals("", escaper.escape(""));
46
// Assert that escapers throw null pointer exceptions.
48
escaper.escape((String) null);
49
Assert.fail("exception not thrown when escaping a null string");
50
} catch (NullPointerException e) {
56
* Asserts that an escaper escapes the given character into the expected string.
58
* @param escaper the non-null escaper to test
59
* @param expected the expected output string
60
* @param c the character to escape
62
public static void assertEscaping(CharEscaper escaper, String expected, char c) {
64
String escaped = computeReplacement(escaper, c);
65
Assert.assertNotNull(escaped);
66
Assert.assertEquals(expected, escaped);
70
* Asserts that a Unicode escaper escapes the given code point into the expected string.
72
* @param escaper the non-null escaper to test
73
* @param expected the expected output string
74
* @param cp the Unicode code point to escape
76
public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) {
78
String escaped = computeReplacement(escaper, cp);
79
Assert.assertNotNull(escaped);
80
Assert.assertEquals(expected, escaped);
84
* Asserts that an escaper does not escape the given character.
86
* @param escaper the non-null escaper to test
87
* @param c the character to test
89
public static void assertUnescaped(CharEscaper escaper, char c) {
90
Assert.assertNull(computeReplacement(escaper, c));
94
* Asserts that a Unicode escaper does not escape the given character.
96
* @param escaper the non-null escaper to test
97
* @param cp the Unicode code point to test
99
public static void assertUnescaped(UnicodeEscaper escaper, int cp) {
100
Assert.assertNull(computeReplacement(escaper, cp));
104
* Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into the expected string.
106
* @param escaper the non-null escaper to test
107
* @param expected the expected output string
108
* @param hi the high surrogate pair character
109
* @param lo the low surrogate pair character
111
public static void assertUnicodeEscaping(
112
UnicodeEscaper escaper, String expected, char hi, char lo) {
114
int cp = Character.toCodePoint(hi, lo);
115
String escaped = computeReplacement(escaper, cp);
116
Assert.assertNotNull(escaped);
117
Assert.assertEquals(expected, escaped);