1 /* 2 * Copyright (C) 2016 Square, Inc. 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 package com.squareup.javapoet; 17 18 import static org.junit.Assert.assertEquals; 19 20 import org.junit.Test; 21 22 public class UtilTest { characterLiteral()23 @Test public void characterLiteral() { 24 assertEquals("a", Util.characterLiteralWithoutSingleQuotes('a')); 25 assertEquals("b", Util.characterLiteralWithoutSingleQuotes('b')); 26 assertEquals("c", Util.characterLiteralWithoutSingleQuotes('c')); 27 assertEquals("%", Util.characterLiteralWithoutSingleQuotes('%')); 28 // common escapes 29 assertEquals("\\b", Util.characterLiteralWithoutSingleQuotes('\b')); 30 assertEquals("\\t", Util.characterLiteralWithoutSingleQuotes('\t')); 31 assertEquals("\\n", Util.characterLiteralWithoutSingleQuotes('\n')); 32 assertEquals("\\f", Util.characterLiteralWithoutSingleQuotes('\f')); 33 assertEquals("\\r", Util.characterLiteralWithoutSingleQuotes('\r')); 34 assertEquals("\"", Util.characterLiteralWithoutSingleQuotes('"')); 35 assertEquals("\\'", Util.characterLiteralWithoutSingleQuotes('\'')); 36 assertEquals("\\\\", Util.characterLiteralWithoutSingleQuotes('\\')); 37 // octal escapes 38 assertEquals("\\u0000", Util.characterLiteralWithoutSingleQuotes('\0')); 39 assertEquals("\\u0007", Util.characterLiteralWithoutSingleQuotes('\7')); 40 assertEquals("?", Util.characterLiteralWithoutSingleQuotes('\77')); 41 assertEquals("\\u007f", Util.characterLiteralWithoutSingleQuotes('\177')); 42 assertEquals("¿", Util.characterLiteralWithoutSingleQuotes('\277')); 43 assertEquals("ÿ", Util.characterLiteralWithoutSingleQuotes('\377')); 44 // unicode escapes 45 assertEquals("\\u0000", Util.characterLiteralWithoutSingleQuotes('\u0000')); 46 assertEquals("\\u0001", Util.characterLiteralWithoutSingleQuotes('\u0001')); 47 assertEquals("\\u0002", Util.characterLiteralWithoutSingleQuotes('\u0002')); 48 assertEquals("€", Util.characterLiteralWithoutSingleQuotes('\u20AC')); 49 assertEquals("☃", Util.characterLiteralWithoutSingleQuotes('\u2603')); 50 assertEquals("♠", Util.characterLiteralWithoutSingleQuotes('\u2660')); 51 assertEquals("♣", Util.characterLiteralWithoutSingleQuotes('\u2663')); 52 assertEquals("♥", Util.characterLiteralWithoutSingleQuotes('\u2665')); 53 assertEquals("♦", Util.characterLiteralWithoutSingleQuotes('\u2666')); 54 assertEquals("✵", Util.characterLiteralWithoutSingleQuotes('\u2735')); 55 assertEquals("✺", Util.characterLiteralWithoutSingleQuotes('\u273A')); 56 assertEquals("/", Util.characterLiteralWithoutSingleQuotes('\uFF0F')); 57 } 58 stringLiteral()59 @Test public void stringLiteral() { 60 stringLiteral("abc"); 61 stringLiteral("♦♥♠♣"); 62 stringLiteral("€\\t@\\t$", "€\t@\t$", " "); 63 stringLiteral("abc();\\n\"\n + \"def();", "abc();\ndef();", " "); 64 stringLiteral("This is \\\"quoted\\\"!", "This is \"quoted\"!", " "); 65 stringLiteral("e^{i\\\\pi}+1=0", "e^{i\\pi}+1=0", " "); 66 } 67 stringLiteral(String string)68 void stringLiteral(String string) { 69 stringLiteral(string, string, " "); 70 } 71 stringLiteral(String expected, String value, String indent)72 void stringLiteral(String expected, String value, String indent) { 73 assertEquals("\"" + expected + "\"", Util.stringLiteralWithDoubleQuotes(value, indent)); 74 } 75 } 76