Skip to content

Commit 80c1479

Browse files
committed
Merge branch 'master' of https://github.com/stleary/JSON-java into Pretty-Print-XML-Functionality
2 parents a2c0562 + 444335d commit 80c1479

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
11+
import org.json.JSONObject;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.junit.runners.Parameterized;
16+
import org.junit.runners.Parameterized.Parameters;
17+
18+
@RunWith(value = Parameterized.class)
19+
public class JSONObjectNumberTest {
20+
private final String objectString;
21+
private Integer value = 50;
22+
23+
@Parameters(name = "{index}: {0}")
24+
public static Collection<Object[]> data() {
25+
return Arrays.asList(new Object[][]{
26+
{"{value:50}", 1},
27+
{"{value:50.0}", 1},
28+
{"{value:5e1}", 1},
29+
{"{value:5E1}", 1},
30+
{"{value:5e1}", 1},
31+
{"{value:'50'}", 1},
32+
{"{value:-50}", -1},
33+
{"{value:-50.0}", -1},
34+
{"{value:-5e1}", -1},
35+
{"{value:-5E1}", -1},
36+
{"{value:-5e1}", -1},
37+
{"{value:'-50'}", -1}
38+
// JSON does not support octal or hex numbers;
39+
// see https://stackoverflow.com/a/52671839/6323312
40+
// "{value:062}", // octal 50
41+
// "{value:0x32}" // hex 50
42+
});
43+
}
44+
45+
public JSONObjectNumberTest(String objectString, int resultIsNegative) {
46+
this.objectString = objectString;
47+
this.value *= resultIsNegative;
48+
}
49+
50+
private JSONObject object;
51+
52+
@Before
53+
public void setJsonObject() {
54+
object = new JSONObject(objectString);
55+
}
56+
57+
@Test
58+
public void testGetNumber() {
59+
assertEquals(value.intValue(), object.getNumber("value").intValue());
60+
}
61+
62+
@Test
63+
public void testGetBigDecimal() {
64+
assertTrue(BigDecimal.valueOf(value).compareTo(object.getBigDecimal("value")) == 0);
65+
}
66+
67+
@Test
68+
public void testGetBigInteger() {
69+
assertEquals(BigInteger.valueOf(value), object.getBigInteger("value"));
70+
}
71+
72+
@Test
73+
public void testGetFloat() {
74+
assertEquals(value.floatValue(), object.getFloat("value"), 0.0f);
75+
}
76+
77+
@Test
78+
public void testGetDouble() {
79+
assertEquals(value.doubleValue(), object.getDouble("value"), 0.0d);
80+
}
81+
82+
@Test
83+
public void testGetInt() {
84+
assertEquals(value.intValue(), object.getInt("value"));
85+
}
86+
87+
@Test
88+
public void testGetLong() {
89+
assertEquals(value.longValue(), object.getLong("value"));
90+
}
91+
92+
@Test
93+
public void testOptNumber() {
94+
assertEquals(value.intValue(), object.optNumber("value").intValue());
95+
}
96+
97+
@Test
98+
public void testOptBigDecimal() {
99+
assertTrue(BigDecimal.valueOf(value).compareTo(object.optBigDecimal("value", null)) == 0);
100+
}
101+
102+
@Test
103+
public void testOptBigInteger() {
104+
assertEquals(BigInteger.valueOf(value), object.optBigInteger("value", null));
105+
}
106+
107+
@Test
108+
public void testOptFloat() {
109+
assertEquals(value.floatValue(), object.optFloat("value"), 0.0f);
110+
}
111+
112+
@Test
113+
public void testOptDouble() {
114+
assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d);
115+
}
116+
117+
@Test
118+
public void testOptInt() {
119+
assertEquals(value.intValue(), object.optInt("value"));
120+
}
121+
122+
@Test
123+
public void testOptLong() {
124+
assertEquals(value.longValue(), object.optLong("value"));
125+
}
126+
}

0 commit comments

Comments
 (0)