1 /* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2017 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 package com.github.javaparser.ast.expr; 22 23 import org.assertj.core.data.Percentage; 24 import org.junit.jupiter.api.Test; 25 26 import static com.github.javaparser.StaticJavaParser.parseExpression; 27 import static org.assertj.core.api.Assertions.assertThat; 28 29 @SuppressWarnings("OctalInteger") 30 class LiteralStringValueExprTest { 31 32 @Test trivialLiteralsAreConverted()33 void trivialLiteralsAreConverted() { 34 assertThat(new CharLiteralExpr('\t').getValue()).isEqualTo("\\t"); 35 assertThat(new CharLiteralExpr('\b').getValue()).isEqualTo("\\b"); 36 assertThat(new CharLiteralExpr('\f').getValue()).isEqualTo("\\f"); 37 assertThat(new CharLiteralExpr('\r').getValue()).isEqualTo("\\r"); 38 assertThat(new CharLiteralExpr('\n').getValue()).isEqualTo("\\n"); 39 assertThat(new CharLiteralExpr('\\').getValue()).isEqualTo("\\\\"); 40 assertThat(new CharLiteralExpr('\"').getValue()).isEqualTo("\\\""); 41 42 assertThat(new IntegerLiteralExpr("0B0").asInt()).isEqualTo(0); 43 assertThat(new IntegerLiteralExpr("0b0").asInt()).isEqualTo(0); 44 assertThat(new IntegerLiteralExpr("0X0").asInt()).isEqualTo(0); 45 assertThat(new IntegerLiteralExpr("0x0").asInt()).isEqualTo(0); 46 assertThat(new IntegerLiteralExpr(0).asInt()).isEqualTo(0); 47 assertThat(new IntegerLiteralExpr(00).asInt()).isEqualTo(0); 48 assertThat(new IntegerLiteralExpr(0B0).asInt()).isEqualTo(0); 49 assertThat(new IntegerLiteralExpr(0b0).asInt()).isEqualTo(0); 50 assertThat(new IntegerLiteralExpr(0X0).asInt()).isEqualTo(0); 51 assertThat(new IntegerLiteralExpr(0x0).asInt()).isEqualTo(0); 52 53 assertThat(new LongLiteralExpr("0B0L").asLong()).isEqualTo(0); 54 assertThat(new LongLiteralExpr("0b0L").asLong()).isEqualTo(0); 55 assertThat(new LongLiteralExpr("0X0L").asLong()).isEqualTo(0); 56 assertThat(new LongLiteralExpr("0x0L").asLong()).isEqualTo(0); 57 assertThat(new LongLiteralExpr(0L).asLong()).isEqualTo(0); 58 assertThat(new LongLiteralExpr(00L).asLong()).isEqualTo(0); 59 assertThat(new LongLiteralExpr(0B0L).asLong()).isEqualTo(0); 60 assertThat(new LongLiteralExpr(0b0L).asLong()).isEqualTo(0); 61 assertThat(new LongLiteralExpr(0X0L).asLong()).isEqualTo(0); 62 assertThat(new LongLiteralExpr(0x0L).asLong()).isEqualTo(0); 63 64 assertThat(new DoubleLiteralExpr("0.0f").asDouble()).isEqualTo(0.0); 65 assertThat(new DoubleLiteralExpr("0.0F").asDouble()).isEqualTo(0.0); 66 assertThat(new DoubleLiteralExpr("0.0d").asDouble()).isEqualTo(0.0); 67 assertThat(new DoubleLiteralExpr("0.0D").asDouble()).isEqualTo(0.0); 68 assertThat(new DoubleLiteralExpr(0.0F).asDouble()).isEqualTo(0.0); 69 assertThat(new DoubleLiteralExpr(0.0f).asDouble()).isEqualTo(0.0); 70 assertThat(new DoubleLiteralExpr(0.0D).asDouble()).isEqualTo(0.0); 71 assertThat(new DoubleLiteralExpr(0.0d).asDouble()).isEqualTo(0.0); 72 } 73 74 @Test lowerAndUpperBoundIntegersAreConverted()75 void lowerAndUpperBoundIntegersAreConverted() { 76 IntegerLiteralExpr dec = parseExpression("2147483647"); 77 IntegerLiteralExpr posOct = parseExpression("0177_7777_7777"); 78 IntegerLiteralExpr negOct = parseExpression("0377_7777_7777"); 79 IntegerLiteralExpr posHex = parseExpression("0x7fff_ffff"); 80 IntegerLiteralExpr negHex = parseExpression("0xffff_ffff"); 81 IntegerLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111"); 82 IntegerLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000"); 83 84 assertThat(dec.asInt()).isEqualTo(2147483647); 85 assertThat(posOct.asInt()).isEqualTo(2147483647); // 0177_7777_7777 86 assertThat(negOct.asInt()).isEqualTo(-1); // 0377_7777_7777 87 assertThat(posHex.asInt()).isEqualTo(0x7fff_ffff); 88 assertThat(negHex.asInt()).isEqualTo(0xffff_ffff); 89 assertThat(posBin.asInt()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111); 90 assertThat(negBin.asInt()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000); 91 } 92 93 @Test lowerAndUpperBoundLongsAreConverted()94 void lowerAndUpperBoundLongsAreConverted() { 95 LongLiteralExpr dec = parseExpression("9223372036854775807L"); 96 LongLiteralExpr posOct = parseExpression("07_7777_7777_7777_7777_7777L"); 97 LongLiteralExpr negOct = parseExpression("010_0000_0000_0000_0000_0000L"); 98 LongLiteralExpr posHex = parseExpression("0x7fff_ffff_ffff_ffffL"); 99 LongLiteralExpr negHex = parseExpression("0xffff_ffff_ffff_ffffL"); 100 LongLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L"); 101 LongLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L"); 102 103 assertThat(dec.asLong()).isEqualTo(9223372036854775807L); 104 assertThat(posOct.asLong()).isEqualTo(9223372036854775807L); // 07_7777_7777_7777_7777_7777L 105 assertThat(negOct.asLong()).isEqualTo(-9223372036854775808L); // 010_0000_0000_0000_0000_0000L 106 assertThat(posHex.asLong()).isEqualTo(0x7fff_ffff_ffff_ffffL); 107 assertThat(negHex.asLong()).isEqualTo(0xffff_ffff_ffff_ffffL); 108 assertThat(posBin.asLong()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L); 109 assertThat(negBin.asLong()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L); 110 } 111 112 @Test charLiteralsAreConverted()113 void charLiteralsAreConverted() { 114 CharLiteralExpr a = parseExpression("'a'"); 115 CharLiteralExpr percent = parseExpression("'%'"); 116 CharLiteralExpr tab = parseExpression("'\\t'"); 117 CharLiteralExpr newLine = parseExpression("'\\n'"); 118 CharLiteralExpr slash = parseExpression("'\\\\'"); 119 CharLiteralExpr quote = parseExpression("'\\''"); 120 CharLiteralExpr omega = parseExpression("'\\u03a9'"); 121 CharLiteralExpr unicode = parseExpression("'\\uFFFF'"); 122 CharLiteralExpr ascii = parseExpression("'\\177'"); 123 CharLiteralExpr trademark = parseExpression("'™'"); 124 125 assertThat(a.asChar()).isEqualTo('a'); 126 assertThat(percent.asChar()).isEqualTo('%'); 127 assertThat(tab.asChar()).isEqualTo('\t'); 128 assertThat(newLine.asChar()).isEqualTo('\n'); 129 assertThat(slash.asChar()).isEqualTo('\\'); 130 assertThat(quote.asChar()).isEqualTo('\''); 131 assertThat(omega.asChar()).isEqualTo('\u03a9'); 132 assertThat(unicode.asChar()).isEqualTo('\uFFFF'); 133 assertThat(ascii.asChar()).isEqualTo('\177'); 134 assertThat(trademark.asChar()).isEqualTo('™'); 135 } 136 137 @Test lowerAndUpperBoundDoublesAreConverted()138 void lowerAndUpperBoundDoublesAreConverted() { 139 DoubleLiteralExpr posFloat = parseExpression("3.4028235e38f"); 140 DoubleLiteralExpr negFloat = parseExpression("1.40e-45f"); 141 DoubleLiteralExpr posDouble = parseExpression("1.7976931348623157e308"); 142 DoubleLiteralExpr negDouble = parseExpression("4.9e-324"); 143 DoubleLiteralExpr posHexFloat = parseExpression("0x1.fffffffffffffp1023"); 144 DoubleLiteralExpr negHexFloat = parseExpression("0x0.0000000000001P-1022"); 145 146 assertThat(posFloat.asDouble()).isCloseTo(3.4028235e38f, Percentage.withPercentage(1)); 147 assertThat(negFloat.asDouble()).isCloseTo(1.40e-45f, Percentage.withPercentage(1)); 148 assertThat(posDouble.asDouble()).isEqualTo(1.7976931348623157e308); 149 assertThat(negDouble.asDouble()).isEqualTo(4.9e-324); 150 assertThat(posHexFloat.asDouble()).isEqualTo(0x1.fffffffffffffp1023); 151 assertThat(negHexFloat.asDouble()).isEqualTo(0x0.0000000000001P-1022); 152 } 153 154 @Test specialCharactersInStringsAreEscaped()155 void specialCharactersInStringsAreEscaped() { 156 assertThat(new StringLiteralExpr("\n").getValue()).isEqualTo("\\n"); 157 assertThat(new StringLiteralExpr("\r").getValue()).isEqualTo("\\r"); 158 assertThat(new StringLiteralExpr("").setEscapedValue("\n").getValue()).isEqualTo("\\n"); 159 assertThat(new StringLiteralExpr("").setEscapedValue("\r").getValue()).isEqualTo("\\r"); 160 assertThat(new StringLiteralExpr("").setEscapedValue("\n").asString()).isEqualTo("\n"); 161 assertThat(new StringLiteralExpr("").setEscapedValue("\r").asString()).isEqualTo("\r"); 162 assertThat(new StringLiteralExpr("Hello\nWorld\rHello\"World\'").asString()).isEqualTo("Hello\nWorld\rHello\"World\'"); 163 } 164 165 } 166