001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.escape.testing; 018 019import static com.google.common.escape.Escapers.computeReplacement; 020import static org.junit.Assert.assertThrows; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.escape.CharEscaper; 024import com.google.common.escape.Escaper; 025import com.google.common.escape.UnicodeEscaper; 026import junit.framework.Assert; 027 028/** 029 * Extra assert methods for testing Escaper implementations. 030 * 031 * @author David Beaumont 032 * @since 15.0 033 */ 034@GwtCompatible 035public final class EscaperAsserts { 036 private EscaperAsserts() {} 037 038 /** 039 * Asserts that an escaper behaves correctly with respect to null inputs. 040 * 041 * @param escaper the non-null escaper to test 042 */ 043 @SuppressWarnings("NullArgumentForNonNullParameter") // test of a bogus call 044 public static void assertBasic(Escaper escaper) { 045 // Escapers operate on characters: no characters, no escaping. 046 Assert.assertEquals("", escaper.escape("")); 047 // Assert that escapers throw null pointer exceptions. 048 assertThrows(NullPointerException.class, () -> escaper.escape(null)); 049 } 050 051 /** 052 * Asserts that an escaper escapes the given character into the expected string. 053 * 054 * @param escaper the non-null escaper to test 055 * @param expected the expected output string 056 * @param c the character to escape 057 */ 058 public static void assertEscaping(CharEscaper escaper, String expected, char c) { 059 String escaped = computeReplacement(escaper, c); 060 Assert.assertNotNull(escaped); 061 Assert.assertEquals(expected, escaped); 062 } 063 064 /** 065 * Asserts that a Unicode escaper escapes the given code point into the expected string. 066 * 067 * @param escaper the non-null escaper to test 068 * @param expected the expected output string 069 * @param cp the Unicode code point to escape 070 */ 071 public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) { 072 String escaped = computeReplacement(escaper, cp); 073 Assert.assertNotNull(escaped); 074 Assert.assertEquals(expected, escaped); 075 } 076 077 /** 078 * Asserts that an escaper does not escape the given character. 079 * 080 * @param escaper the non-null escaper to test 081 * @param c the character to test 082 */ 083 public static void assertUnescaped(CharEscaper escaper, char c) { 084 Assert.assertNull(computeReplacement(escaper, c)); 085 } 086 087 /** 088 * Asserts that a Unicode escaper does not escape the given character. 089 * 090 * @param escaper the non-null escaper to test 091 * @param cp the Unicode code point to test 092 */ 093 public static void assertUnescaped(UnicodeEscaper escaper, int cp) { 094 Assert.assertNull(computeReplacement(escaper, cp)); 095 } 096 097 /** 098 * Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into the expected string. 099 * 100 * @param escaper the non-null escaper to test 101 * @param expected the expected output string 102 * @param hi the high surrogate pair character 103 * @param lo the low surrogate pair character 104 */ 105 public static void assertUnicodeEscaping( 106 UnicodeEscaper escaper, String expected, char hi, char lo) { 107 int cp = Character.toCodePoint(hi, lo); 108 String escaped = computeReplacement(escaper, cp); 109 Assert.assertNotNull(escaped); 110 Assert.assertEquals(expected, escaped); 111 } 112}