-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from hendrixjoseph/issue-657-unit-tests-shoul…
…d-check-various-number-formats create unit tests for various number formats
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.json.junit; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.json.JSONObject; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
@RunWith(value = Parameterized.class) | ||
public class JSONObjectNumberTest { | ||
private final String objectString; | ||
private Integer value = 50; | ||
|
||
@Parameters(name = "{index}: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{"{value:50}", 1}, | ||
{"{value:50.0}", 1}, | ||
{"{value:5e1}", 1}, | ||
{"{value:5E1}", 1}, | ||
{"{value:5e1}", 1}, | ||
{"{value:'50'}", 1}, | ||
{"{value:-50}", -1}, | ||
{"{value:-50.0}", -1}, | ||
{"{value:-5e1}", -1}, | ||
{"{value:-5E1}", -1}, | ||
{"{value:-5e1}", -1}, | ||
{"{value:'-50'}", -1} | ||
// JSON does not support octal or hex numbers; | ||
// see https://stackoverflow.com/a/52671839/6323312 | ||
// "{value:062}", // octal 50 | ||
// "{value:0x32}" // hex 50 | ||
}); | ||
} | ||
|
||
public JSONObjectNumberTest(String objectString, int resultIsNegative) { | ||
this.objectString = objectString; | ||
this.value *= resultIsNegative; | ||
} | ||
|
||
private JSONObject object; | ||
|
||
@Before | ||
public void setJsonObject() { | ||
object = new JSONObject(objectString); | ||
} | ||
|
||
@Test | ||
public void testGetNumber() { | ||
assertEquals(value.intValue(), object.getNumber("value").intValue()); | ||
} | ||
|
||
@Test | ||
public void testGetBigDecimal() { | ||
assertTrue(BigDecimal.valueOf(value).compareTo(object.getBigDecimal("value")) == 0); | ||
} | ||
|
||
@Test | ||
public void testGetBigInteger() { | ||
assertEquals(BigInteger.valueOf(value), object.getBigInteger("value")); | ||
} | ||
|
||
@Test | ||
public void testGetFloat() { | ||
assertEquals(value.floatValue(), object.getFloat("value"), 0.0f); | ||
} | ||
|
||
@Test | ||
public void testGetDouble() { | ||
assertEquals(value.doubleValue(), object.getDouble("value"), 0.0d); | ||
} | ||
|
||
@Test | ||
public void testGetInt() { | ||
assertEquals(value.intValue(), object.getInt("value")); | ||
} | ||
|
||
@Test | ||
public void testGetLong() { | ||
assertEquals(value.longValue(), object.getLong("value")); | ||
} | ||
|
||
@Test | ||
public void testOptNumber() { | ||
assertEquals(value.intValue(), object.optNumber("value").intValue()); | ||
} | ||
|
||
@Test | ||
public void testOptBigDecimal() { | ||
assertTrue(BigDecimal.valueOf(value).compareTo(object.optBigDecimal("value", null)) == 0); | ||
} | ||
|
||
@Test | ||
public void testOptBigInteger() { | ||
assertEquals(BigInteger.valueOf(value), object.optBigInteger("value", null)); | ||
} | ||
|
||
@Test | ||
public void testOptFloat() { | ||
assertEquals(value.floatValue(), object.optFloat("value"), 0.0f); | ||
} | ||
|
||
@Test | ||
public void testOptDouble() { | ||
assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d); | ||
} | ||
|
||
@Test | ||
public void testOptInt() { | ||
assertEquals(value.intValue(), object.optInt("value")); | ||
} | ||
|
||
@Test | ||
public void testOptLong() { | ||
assertEquals(value.longValue(), object.optLong("value")); | ||
} | ||
} |