aboutsummaryrefslogtreecommitdiffstats
path: root/javaparser-testing/src/test/java/com/github/javaparser/ast/expr/LiteralStringValueExprTest.java
blob: 1b9dfef1a820d40edc8d85586b76e6ea17c7524d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
 * Copyright (C) 2011, 2013-2017 The JavaParser Team.
 *
 * This file is part of JavaParser.
 *
 * JavaParser can be used either under the terms of
 * a) the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * b) the terms of the Apache License
 *
 * You should have received a copy of both licenses in LICENCE.LGPL and
 * LICENCE.APACHE. Please refer to those files for details.
 *
 * JavaParser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */
package com.github.javaparser.ast.expr;

import com.github.javaparser.JavaParser;
import org.assertj.core.data.Percentage;
import org.junit.Test;

import static com.github.javaparser.JavaParser.*;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("OctalInteger")
public class LiteralStringValueExprTest {

    @Test
    public void trivialLiteralsAreConverted() {
        assertThat(new CharLiteralExpr('\t').getValue()).isEqualTo("\\t");
        assertThat(new CharLiteralExpr('\b').getValue()).isEqualTo("\\b");
        assertThat(new CharLiteralExpr('\f').getValue()).isEqualTo("\\f");
        assertThat(new CharLiteralExpr('\r').getValue()).isEqualTo("\\r");
        assertThat(new CharLiteralExpr('\n').getValue()).isEqualTo("\\n");
        assertThat(new CharLiteralExpr('\\').getValue()).isEqualTo("\\\\");
        assertThat(new CharLiteralExpr('\"').getValue()).isEqualTo("\\\"");

        assertThat(new IntegerLiteralExpr("0B0").asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr("0b0").asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr("0X0").asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr("0x0").asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(0).asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(00).asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(0B0).asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(0b0).asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(0X0).asInt()).isEqualTo(0);
        assertThat(new IntegerLiteralExpr(0x0).asInt()).isEqualTo(0);

        assertThat(new LongLiteralExpr("0B0L").asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr("0b0L").asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr("0X0L").asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr("0x0L").asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(0L).asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(00L).asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(0B0L).asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(0b0L).asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(0X0L).asLong()).isEqualTo(0);
        assertThat(new LongLiteralExpr(0x0L).asLong()).isEqualTo(0);

        assertThat(new DoubleLiteralExpr("0.0f").asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr("0.0F").asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr("0.0d").asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr("0.0D").asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr(0.0F).asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr(0.0f).asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr(0.0D).asDouble()).isEqualTo(0.0);
        assertThat(new DoubleLiteralExpr(0.0d).asDouble()).isEqualTo(0.0);
    }

    @Test
    public void lowerAndUpperBoundIntegersAreConverted() {
        IntegerLiteralExpr dec = parseExpression("2147483647");
        IntegerLiteralExpr posOct = parseExpression("0177_7777_7777");
        IntegerLiteralExpr negOct = parseExpression("0377_7777_7777");
        IntegerLiteralExpr posHex = parseExpression("0x7fff_ffff");
        IntegerLiteralExpr negHex = parseExpression("0xffff_ffff");
        IntegerLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111");
        IntegerLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000");

        assertThat(dec.asInt()).isEqualTo(2147483647);
        assertThat(posOct.asInt()).isEqualTo(2147483647); // 0177_7777_7777
        assertThat(negOct.asInt()).isEqualTo(-1); // 0377_7777_7777
        assertThat(posHex.asInt()).isEqualTo(0x7fff_ffff);
        assertThat(negHex.asInt()).isEqualTo(0xffff_ffff);
        assertThat(posBin.asInt()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111);
        assertThat(negBin.asInt()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000);
    }

    @Test
    public void lowerAndUpperBoundLongsAreConverted() {
        LongLiteralExpr dec = parseExpression("9223372036854775807L");
        LongLiteralExpr posOct = parseExpression("07_7777_7777_7777_7777_7777L");
        LongLiteralExpr negOct = parseExpression("010_0000_0000_0000_0000_0000L");
        LongLiteralExpr posHex = parseExpression("0x7fff_ffff_ffff_ffffL");
        LongLiteralExpr negHex = parseExpression("0xffff_ffff_ffff_ffffL");
        LongLiteralExpr posBin = parseExpression("0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L");
        LongLiteralExpr negBin = parseExpression("0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L");

        assertThat(dec.asLong()).isEqualTo(9223372036854775807L);
        assertThat(posOct.asLong()).isEqualTo(9223372036854775807L); // 07_7777_7777_7777_7777_7777L
        assertThat(negOct.asLong()).isEqualTo(-9223372036854775808L); // 010_0000_0000_0000_0000_0000L
        assertThat(posHex.asLong()).isEqualTo(0x7fff_ffff_ffff_ffffL);
        assertThat(negHex.asLong()).isEqualTo(0xffff_ffff_ffff_ffffL);
        assertThat(posBin.asLong()).isEqualTo(0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L);
        assertThat(negBin.asLong()).isEqualTo(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000L);
    }

    @Test
    public void charLiteralsAreConverted() {
        CharLiteralExpr a = parseExpression("'a'");
        CharLiteralExpr percent = parseExpression("'%'");
        CharLiteralExpr tab = parseExpression("'\\t'");
        CharLiteralExpr newLine = parseExpression("'\\n'");
        CharLiteralExpr slash = parseExpression("'\\\\'");
        CharLiteralExpr quote = parseExpression("'\\''");
        CharLiteralExpr omega = parseExpression("'\\u03a9'");
        CharLiteralExpr unicode = parseExpression("'\\uFFFF'");
        CharLiteralExpr ascii = parseExpression("'\\177'");
        CharLiteralExpr trademark = parseExpression("'™'");

        assertThat(a.asChar()).isEqualTo('a');
        assertThat(percent.asChar()).isEqualTo('%');
        assertThat(tab.asChar()).isEqualTo('\t');
        assertThat(newLine.asChar()).isEqualTo('\n');
        assertThat(slash.asChar()).isEqualTo('\\');
        assertThat(quote.asChar()).isEqualTo('\'');
        assertThat(omega.asChar()).isEqualTo('\u03a9');
        assertThat(unicode.asChar()).isEqualTo('\uFFFF');
        assertThat(ascii.asChar()).isEqualTo('\177');
        assertThat(trademark.asChar()).isEqualTo('™');
    }

    @Test
    public void lowerAndUpperBoundDoublesAreConverted() {
        DoubleLiteralExpr posFloat = parseExpression("3.4028235e38f");
        DoubleLiteralExpr negFloat = parseExpression("1.40e-45f");
        DoubleLiteralExpr posDouble = parseExpression("1.7976931348623157e308");
        DoubleLiteralExpr negDouble = parseExpression("4.9e-324");
        DoubleLiteralExpr posHexFloat = parseExpression("0x1.fffffffffffffp1023");
        DoubleLiteralExpr negHexFloat = parseExpression("0x0.0000000000001P-1022");

        assertThat(posFloat.asDouble()).isCloseTo(3.4028235e38f, Percentage.withPercentage(1));
        assertThat(negFloat.asDouble()).isCloseTo(1.40e-45f, Percentage.withPercentage(1));
        assertThat(posDouble.asDouble()).isEqualTo(1.7976931348623157e308);
        assertThat(negDouble.asDouble()).isEqualTo(4.9e-324);
        assertThat(posHexFloat.asDouble()).isEqualTo(0x1.fffffffffffffp1023);
        assertThat(negHexFloat.asDouble()).isEqualTo(0x0.0000000000001P-1022);
    }

    @Test
    public void specialCharactersInStringsAreEscaped() {
        assertThat(new StringLiteralExpr("\n").getValue()).isEqualTo("\\n");
        assertThat(new StringLiteralExpr("\r").getValue()).isEqualTo("\\r");
        assertThat(new StringLiteralExpr("").setEscapedValue("\n").getValue()).isEqualTo("\\n");
        assertThat(new StringLiteralExpr("").setEscapedValue("\r").getValue()).isEqualTo("\\r");
        assertThat(new StringLiteralExpr("").setEscapedValue("\n").asString()).isEqualTo("\n");
        assertThat(new StringLiteralExpr("").setEscapedValue("\r").asString()).isEqualTo("\r");
        assertThat(new StringLiteralExpr("Hello\nWorld\rHello\"World\'").asString()).isEqualTo("Hello\nWorld\rHello\"World\'");
    }

}