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 com.github.javaparser.JavaParser;
24 import org.assertj.core.data.Percentage;
25 import org.junit.Test;
26 
27 import static com.github.javaparser.JavaParser.*;
28 import static org.assertj.core.api.Assertions.assertThat;
29 
30 @SuppressWarnings("OctalInteger")
31 public class LiteralStringValueExprTest {
32 
33     @Test
trivialLiteralsAreConverted()34     public void trivialLiteralsAreConverted() {
35         assertThat(new CharLiteralExpr('\t').getValue()).isEqualTo("\\t");
36         assertThat(new CharLiteralExpr('\b').getValue()).isEqualTo("\\b");
37         assertThat(new CharLiteralExpr('\f').getValue()).isEqualTo("\\f");
38         assertThat(new CharLiteralExpr('\r').getValue()).isEqualTo("\\r");
39         assertThat(new CharLiteralExpr('\n').getValue()).isEqualTo("\\n");
40         assertThat(new CharLiteralExpr('\\').getValue()).isEqualTo("\\\\");
41         assertThat(new CharLiteralExpr('\"').getValue()).isEqualTo("\\\"");
42 
43         assertThat(new IntegerLiteralExpr("0B0").asInt()).isEqualTo(0);
44         assertThat(new IntegerLiteralExpr("0b0").asInt()).isEqualTo(0);
45         assertThat(new IntegerLiteralExpr("0X0").asInt()).isEqualTo(0);
46         assertThat(new IntegerLiteralExpr("0x0").asInt()).isEqualTo(0);
47         assertThat(new IntegerLiteralExpr(0).asInt()).isEqualTo(0);
48         assertThat(new IntegerLiteralExpr(00).asInt()).isEqualTo(0);
49         assertThat(new IntegerLiteralExpr(0B0).asInt()).isEqualTo(0);
50         assertThat(new IntegerLiteralExpr(0b0).asInt()).isEqualTo(0);
51         assertThat(new IntegerLiteralExpr(0X0).asInt()).isEqualTo(0);
52         assertThat(new IntegerLiteralExpr(0x0).asInt()).isEqualTo(0);
53 
54         assertThat(new LongLiteralExpr("0B0L").asLong()).isEqualTo(0);
55         assertThat(new LongLiteralExpr("0b0L").asLong()).isEqualTo(0);
56         assertThat(new LongLiteralExpr("0X0L").asLong()).isEqualTo(0);
57         assertThat(new LongLiteralExpr("0x0L").asLong()).isEqualTo(0);
58         assertThat(new LongLiteralExpr(0L).asLong()).isEqualTo(0);
59         assertThat(new LongLiteralExpr(00L).asLong()).isEqualTo(0);
60         assertThat(new LongLiteralExpr(0B0L).asLong()).isEqualTo(0);
61         assertThat(new LongLiteralExpr(0b0L).asLong()).isEqualTo(0);
62         assertThat(new LongLiteralExpr(0X0L).asLong()).isEqualTo(0);
63         assertThat(new LongLiteralExpr(0x0L).asLong()).isEqualTo(0);
64 
65         assertThat(new DoubleLiteralExpr("0.0f").asDouble()).isEqualTo(0.0);
66         assertThat(new DoubleLiteralExpr("0.0F").asDouble()).isEqualTo(0.0);
67         assertThat(new DoubleLiteralExpr("0.0d").asDouble()).isEqualTo(0.0);
68         assertThat(new DoubleLiteralExpr("0.0D").asDouble()).isEqualTo(0.0);
69         assertThat(new DoubleLiteralExpr(0.0F).asDouble()).isEqualTo(0.0);
70         assertThat(new DoubleLiteralExpr(0.0f).asDouble()).isEqualTo(0.0);
71         assertThat(new DoubleLiteralExpr(0.0D).asDouble()).isEqualTo(0.0);
72         assertThat(new DoubleLiteralExpr(0.0d).asDouble()).isEqualTo(0.0);
73     }
74 
75     @Test
lowerAndUpperBoundIntegersAreConverted()76     public void lowerAndUpperBoundIntegersAreConverted() {
77         IntegerLiteralExpr dec = parseExpression("2147483647");
78         IntegerLiteralExpr posOct = parseExpression("0177_7777_7777");
79         IntegerLiteralExpr negOct = parseExpression("0377_7777_7777");
80         IntegerLiteralExpr posHex = parseExpression("0x7fff_ffff");
81         IntegerLiteralExpr negHex = parseExpression("0xffff_ffff");
82         IntegerLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111");
83         IntegerLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000");
84 
85         assertThat(dec.asInt()).isEqualTo(2147483647);
86         assertThat(posOct.asInt()).isEqualTo(2147483647); // 0177_7777_7777
87         assertThat(negOct.asInt()).isEqualTo(-1); // 0377_7777_7777
88         assertThat(posHex.asInt()).isEqualTo(0x7fff_ffff);
89         assertThat(negHex.asInt()).isEqualTo(0xffff_ffff);
90         assertThat(posBin.asInt()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111);
91         assertThat(negBin.asInt()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000);
92     }
93 
94     @Test
lowerAndUpperBoundLongsAreConverted()95     public void lowerAndUpperBoundLongsAreConverted() {
96         LongLiteralExpr dec = parseExpression("9223372036854775807L");
97         LongLiteralExpr posOct = parseExpression("07_7777_7777_7777_7777_7777L");
98         LongLiteralExpr negOct = parseExpression("010_0000_0000_0000_0000_0000L");
99         LongLiteralExpr posHex = parseExpression("0x7fff_ffff_ffff_ffffL");
100         LongLiteralExpr negHex = parseExpression("0xffff_ffff_ffff_ffffL");
101         LongLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L");
102         LongLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L");
103 
104         assertThat(dec.asLong()).isEqualTo(9223372036854775807L);
105         assertThat(posOct.asLong()).isEqualTo(9223372036854775807L); // 07_7777_7777_7777_7777_7777L
106         assertThat(negOct.asLong()).isEqualTo(-9223372036854775808L); // 010_0000_0000_0000_0000_0000L
107         assertThat(posHex.asLong()).isEqualTo(0x7fff_ffff_ffff_ffffL);
108         assertThat(negHex.asLong()).isEqualTo(0xffff_ffff_ffff_ffffL);
109         assertThat(posBin.asLong()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L);
110         assertThat(negBin.asLong()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L);
111     }
112 
113     @Test
charLiteralsAreConverted()114     public void charLiteralsAreConverted() {
115         CharLiteralExpr a = parseExpression("'a'");
116         CharLiteralExpr percent = parseExpression("'%'");
117         CharLiteralExpr tab = parseExpression("'\\t'");
118         CharLiteralExpr newLine = parseExpression("'\\n'");
119         CharLiteralExpr slash = parseExpression("'\\\\'");
120         CharLiteralExpr quote = parseExpression("'\\''");
121         CharLiteralExpr omega = parseExpression("'\\u03a9'");
122         CharLiteralExpr unicode = parseExpression("'\\uFFFF'");
123         CharLiteralExpr ascii = parseExpression("'\\177'");
124         CharLiteralExpr trademark = parseExpression("'™'");
125 
126         assertThat(a.asChar()).isEqualTo('a');
127         assertThat(percent.asChar()).isEqualTo('%');
128         assertThat(tab.asChar()).isEqualTo('\t');
129         assertThat(newLine.asChar()).isEqualTo('\n');
130         assertThat(slash.asChar()).isEqualTo('\\');
131         assertThat(quote.asChar()).isEqualTo('\'');
132         assertThat(omega.asChar()).isEqualTo('\u03a9');
133         assertThat(unicode.asChar()).isEqualTo('\uFFFF');
134         assertThat(ascii.asChar()).isEqualTo('\177');
135         assertThat(trademark.asChar()).isEqualTo('™');
136     }
137 
138     @Test
lowerAndUpperBoundDoublesAreConverted()139     public void lowerAndUpperBoundDoublesAreConverted() {
140         DoubleLiteralExpr posFloat = parseExpression("3.4028235e38f");
141         DoubleLiteralExpr negFloat = parseExpression("1.40e-45f");
142         DoubleLiteralExpr posDouble = parseExpression("1.7976931348623157e308");
143         DoubleLiteralExpr negDouble = parseExpression("4.9e-324");
144         DoubleLiteralExpr posHexFloat = parseExpression("0x1.fffffffffffffp1023");
145         DoubleLiteralExpr negHexFloat = parseExpression("0x0.0000000000001P-1022");
146 
147         assertThat(posFloat.asDouble()).isCloseTo(3.4028235e38f, Percentage.withPercentage(1));
148         assertThat(negFloat.asDouble()).isCloseTo(1.40e-45f, Percentage.withPercentage(1));
149         assertThat(posDouble.asDouble()).isEqualTo(1.7976931348623157e308);
150         assertThat(negDouble.asDouble()).isEqualTo(4.9e-324);
151         assertThat(posHexFloat.asDouble()).isEqualTo(0x1.fffffffffffffp1023);
152         assertThat(negHexFloat.asDouble()).isEqualTo(0x0.0000000000001P-1022);
153     }
154 
155     @Test
specialCharactersInStringsAreEscaped()156     public void specialCharactersInStringsAreEscaped() {
157         assertThat(new StringLiteralExpr("\n").getValue()).isEqualTo("\\n");
158         assertThat(new StringLiteralExpr("\r").getValue()).isEqualTo("\\r");
159         assertThat(new StringLiteralExpr("").setEscapedValue("\n").getValue()).isEqualTo("\\n");
160         assertThat(new StringLiteralExpr("").setEscapedValue("\r").getValue()).isEqualTo("\\r");
161         assertThat(new StringLiteralExpr("").setEscapedValue("\n").asString()).isEqualTo("\n");
162         assertThat(new StringLiteralExpr("").setEscapedValue("\r").asString()).isEqualTo("\r");
163         assertThat(new StringLiteralExpr("Hello\nWorld\rHello\"World\'").asString()).isEqualTo("Hello\nWorld\rHello\"World\'");
164     }
165 
166 }
167