Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test JSObject #2508

Merged
merged 13 commits into from
Jun 24, 2020
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ jobs:
working-directory: ./android
steps:
- uses: actions/checkout@v2
- run: ./gradlew clean build -b capacitor/build.gradle -Pandroid.useAndroidX=true -Pandroid.enableJetifier=true
- run: ./gradlew clean test build -b capacitor/build.gradle -Pandroid.useAndroidX=true -Pandroid.enableJetifier=true
1 change: 1 addition & 0 deletions android/capacitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
testImplementation 'org.json:json:20140107'
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public String getString(String key, String defaultValue) {
return defaultValue;
}

public Integer getInteger(String key) {
return getInteger(key, null);
}

public Integer getInteger(String key, Integer defaultValue) {
try {
return super.getInt(key);
Expand All @@ -62,10 +66,6 @@ public Integer getInteger(String key, Integer defaultValue) {
return defaultValue;
}

public Integer getInteger(String key) {
return getInteger(key, null);
}

public Boolean getBoolean(String key, Boolean defaultValue) {
try {
return super.getBoolean(key);
Expand Down
209 changes: 209 additions & 0 deletions android/capacitor/src/test/java/com/getcapacitor/JSObjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.getcapacitor;

import org.json.JSONException;
import org.junit.Test;

import static org.junit.Assert.*;

public class JSObjectTest {

@Test
public void getStringReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() {
JSObject jsObject = new JSObject();

String actualValue = jsObject.getString("should be null");

assertNull(actualValue);
}

@Test
public void getStringReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": \"this is the key value\"}");

String expectedValue = jsObject.getString("thisKeyExists");
String actualValue = "this is the key value";

assertEquals(expectedValue, actualValue);
}

@Test
public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException {
JSObject jsObject = new JSObject();

String expectedValue = jsObject.getString("thisKeyDoesNotExist", "default value");
String actualValue = "default value";

assertEquals(expectedValue, actualValue);
}

@Test
public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsInteger() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": 1}");

String expectedValue = jsObject.getString("thisKeyExists", "default value");
String actualValue = "default value";

assertEquals(expectedValue, actualValue);
}

@Test
public void getIntegerReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() {
JSObject jsObject = new JSObject();

Integer actualValue = jsObject.getInteger("should be null");

assertNull(actualValue);
}

@Test
public void getIntegerReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": 1}");

Integer expectedValue = jsObject.getInteger("thisKeyExists");
Integer actualValue = 1;

assertEquals(expectedValue, actualValue);
}

@Test
public void getIntegerReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException {
JSObject jsObject = new JSObject();

Integer expectedValue = jsObject.getInteger("thisKeyDoesNotExist", 1);
Integer actualValue = 1;

assertEquals(expectedValue, actualValue);
}

@Test
public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsString() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": \"not an integer\"}");

Integer expectedValue = jsObject.getInteger("thisKeyExists", 1);
Integer actualValue = 1;

assertEquals(expectedValue, actualValue);
}

@Test
public void getBoolReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() {
JSObject jsObject = new JSObject();

Boolean actualValue = jsObject.getBool("should be null");

assertNull(actualValue);
}

@Test
public void getBoolReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": true}");

Boolean expectedValue = jsObject.getBool("thisKeyExists");
Boolean actualValue = true;

assertEquals(expectedValue, actualValue);
}

@Test
public void getBooleanReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException {
JSObject jsObject = new JSObject();

Boolean expectedValue = jsObject.getBoolean("thisKeyDoesNotExist", true);
Boolean actualValue = true;

assertEquals(expectedValue, actualValue);
}

@Test
public void getBooleanReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsString() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": \"not an integer\"}");

Boolean expectedValue = jsObject.getBoolean("thisKeyExists", true);
Boolean actualValue = true;

assertEquals(expectedValue, actualValue);
}

@Test
public void getJSObjectReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() {
JSObject jsObject = new JSObject();

JSObject actualValue = jsObject.getJSObject("should be null");

assertNull(actualValue);
}

@Test
public void getJsObjectReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException {
JSObject jsObject = new JSObject("{\"thisKeyExists\": { \"innerObjectKey\": \"innerObjectValue\" }}");

String actualValue = jsObject.getJSObject("thisKeyExists").getString("innerObjectKey");
String expectedValue = "innerObjectValue";

assertEquals(expectedValue, actualValue);
}

@Test
public void getJSObjectReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException {
JSObject jsObject = new JSObject();

String actualValue = jsObject.getJSObject("thisKeyExists", new JSObject("{\"thisKeyExists\": \"default string\"}")).getString("thisKeyExists");
String expectedValue = "default string";

assertEquals(expectedValue, actualValue);
}

@Test
public void putBoolean_AddsValueToJSObject_UnderCorrectKey() {
JSObject jsObject = new JSObject();
jsObject.put("bool", true);

Boolean actualValue = jsObject.getBool("bool");

assertTrue(actualValue);
}

@Test
public void putInteger_AddsValueToJSObject_UnderCorrectKey() {
JSObject jsObject = new JSObject();
jsObject.put("integer", 1);

Integer expectedValue = 1;
Integer actualValue = jsObject.getInteger("integer");

assertEquals(actualValue, expectedValue);
}

@Test
public void putLong_AddsValueToJSObject_UnderCorrectKey() throws JSONException {
JSObject jsObject = new JSObject();
jsObject.put("long", 1l);

Long expectedValue = 1l;
Long actualValue = jsObject.getLong("long");

assertEquals(actualValue, expectedValue);
}

@Test
public void putDouble_AddsValueToJSObject_UnderCorrectKey() throws JSONException {
JSObject jsObject = new JSObject();
jsObject.put("double", 1d);

Double expectedValue = 1d;
Double actualValue = jsObject.getDouble("double");

assertEquals(actualValue, expectedValue);
}

@Test
public void putString_AddsValueToJSObject_UnderCorrectKey() throws JSONException {
JSObject jsObject = new JSObject();
jsObject.put("string", "test");

String expectedValue = "test";
String actualValue = jsObject.getString("string");

assertEquals(actualValue, expectedValue);
}
}