Compare commits

...

5 Commits

Author SHA1 Message Date
Sean Leary
d51250f6b0 Merge pull request #692 from InACommandBlock/patch-1
Example.md syntax highlight and indentation
2022-10-09 22:28:36 -05:00
Sean Leary
444335d12a Merge pull request #691 from hendrixjoseph/issue-657-unit-tests-should-check-various-number-formats
create unit tests for various number formats
2022-10-07 11:18:41 -05:00
TheCommandBlock
12411b7981 Update Examples.md
Co-authored-by: JAYSE <104235500+JayseMayne@users.noreply.github.com>
2022-10-06 03:18:03 +02:00
TheCommandBlock
61801c623e Minor Adjustments Example.md
Added syntax highlighting, standardised indentation
2022-10-06 00:48:34 +02:00
hendrixjoseph
1915aab7c4 create unit tests for various number formats 2022-10-04 14:32:41 -04:00
2 changed files with 412 additions and 286 deletions

View File

@@ -1,7 +1,7 @@
<h1>Examples</h1> <h1>Examples</h1>
<p>Imports used in the examples: </p> <p>Imports used in the examples: </p>
``` ```java
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@@ -14,7 +14,7 @@ import java.util.Properties;
<h3>Using JSONArray</h3> <h3>Using JSONArray</h3>
``` ```java
private static void JSONExampleArray1() { private static void JSONExampleArray1() {
//We create a JSONObject from a String containing an array using JSONArray //We create a JSONObject from a String containing an array using JSONArray
//Firstly, we declare an Array in a String //Firstly, we declare an Array in a String
@@ -57,7 +57,7 @@ import java.util.Properties;
return res; return res;
} }
``` ```
``` ```java
private static void JSONExampleArray2() { private static void JSONExampleArray2() {
//We can also create an Array without a String by creating an empty array and adding elements to it //We can also create an Array without a String by creating an empty array and adding elements to it
@@ -81,7 +81,7 @@ import java.util.Properties;
<h3>Using JSONStringer</h3> <h3>Using JSONStringer</h3>
``` ```java
private static void JSONExampleStringer() { private static void JSONExampleStringer() {
//We initializate the JSONStringer //We initializate the JSONStringer
@@ -116,7 +116,7 @@ import java.util.Properties;
``` ```
<h3>Using JSONObject</h3> <h3>Using JSONObject</h3>
``` ```java
private static void JSONExampleObject1() { private static void JSONExampleObject1() {
//We can create a JSONObject from a String with the class builder //We can create a JSONObject from a String with the class builder
@@ -127,7 +127,7 @@ import java.util.Properties;
} }
``` ```
``` ```java
private static void JSONExampleObject2() { private static void JSONExampleObject2() {
//We can also create a JSONObject directly without messing around with any of the other functions. //We can also create a JSONObject directly without messing around with any of the other functions.
@@ -151,7 +151,7 @@ import java.util.Properties;
System.out.println("Final JSONOBject: " + example); System.out.println("Final JSONOBject: " + example);
} }
``` ```
``` ```java
private static void JSONExampleObject3() { private static void JSONExampleObject3() {
//We can also create a JSONObject with a Java Map //We can also create a JSONObject with a Java Map
@@ -170,7 +170,7 @@ import java.util.Properties;
``` ```
<h3>Using JSONWriter</h3> <h3>Using JSONWriter</h3>
``` ```java
private static void JSONExamplWriter() { private static void JSONExamplWriter() {
//This method works in a very similar way to Object and Stringer in the construction of the JSON. //This method works in a very similar way to Object and Stringer in the construction of the JSON.
@@ -202,7 +202,7 @@ import java.util.Properties;
} }
``` ```
``` ```java
private static void JSONExampleTokener() { private static void JSONExampleTokener() {
//A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject //A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject
@@ -223,7 +223,7 @@ import java.util.Properties;
<h3>Extra: Conversion to JSONArray</h3> <h3>Extra: Conversion to JSONArray</h3>
``` ```java
private static void JSONObjectToArray() { private static void JSONObjectToArray() {
//We start with a JSONObject //We start with a JSONObject
@@ -244,7 +244,7 @@ import java.util.Properties;
``` ```
<h3>XML Conversions</h3> <h3>XML Conversions</h3>
``` ```java
private static void XMLToExampleConversion() { private static void XMLToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@@ -258,7 +258,7 @@ import java.util.Properties;
System.out.println("Final XML: " + output); System.out.println("Final XML: " + output);
} }
``` ```
``` ```java
private static void XMLFromExampleConversion() { private static void XMLFromExampleConversion() {
//We start with a string with the XML format //We start with a string with the XML format
@@ -274,7 +274,7 @@ import java.util.Properties;
``` ```
<h3>Cookie Conversions</h3> <h3>Cookie Conversions</h3>
``` ```java
private static void CookieToExampleConversion() { private static void CookieToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@@ -290,7 +290,7 @@ import java.util.Properties;
System.out.println("Final Cookie: " + output); System.out.println("Final Cookie: " + output);
} }
``` ```
``` ```java
private static void CookieFromExampleConversion() { private static void CookieFromExampleConversion() {
//We start with a string with the Cookie format //We start with a string with the Cookie format
@@ -306,7 +306,7 @@ import java.util.Properties;
<h3>HTTP Conversions</h3> <h3>HTTP Conversions</h3>
``` ```java
private static void HTTPToExampleConversion() { private static void HTTPToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@@ -322,7 +322,7 @@ import java.util.Properties;
System.out.println("Final HTTP: " + output); System.out.println("Final HTTP: " + output);
} }
``` ```
``` ```java
private static void HTTPFromExampleConversion() { private static void HTTPFromExampleConversion() {
//We start with a string with the HTTP format //We start with a string with the HTTP format
@@ -337,7 +337,7 @@ import java.util.Properties;
``` ```
<h3>CDL Conversions</h3> <h3>CDL Conversions</h3>
``` ```java
private static void CDLToExampleConversion() { private static void CDLToExampleConversion() {
//We start with some JSONObjects with the same values in the keys but different values in the "values" //We start with some JSONObjects with the same values in the keys but different values in the "values"
@@ -360,7 +360,7 @@ private static void CDLToExampleConversion() {
System.out.println("Final CDL: \r\n" + output); System.out.println("Final CDL: \r\n" + output);
} }
``` ```
``` ```java
private static void CDLFromExampleConversion() { private static void CDLFromExampleConversion() {
//We start wtih a String with the CDL format //We start wtih a String with the CDL format
@@ -377,7 +377,7 @@ private static void CDLFromExampleConversion() {
``` ```
<h3>Properties Conversions</h3> <h3>Properties Conversions</h3>
``` ```java
private static Properties PropertyToExampleConversion() { private static Properties PropertyToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@@ -393,7 +393,7 @@ private static void CDLFromExampleConversion() {
return output; return output;
} }
``` ```
``` ```java
private static void PropertyFromExampleConversion() { private static void PropertyFromExampleConversion() {
//We start with a Properties object //We start with a Properties object
@@ -408,7 +408,7 @@ private static void CDLFromExampleConversion() {
``` ```
<h2>List of all examples methods</h2> <h2>List of all examples methods</h2>
``` ```java
public static void main(String[] args) { public static void main(String[] args) {
//JSONObjectToArray(); //JSONObjectToArray();
//JSONExampleArray1(); //JSONExampleArray1();

View File

@@ -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"));
}
}