From f14fca241579e330d62f7098ea9699376e74f293 Mon Sep 17 00:00:00 2001 From: "Andrew Chen (Eng)" Date: Fri, 16 Nov 2018 17:19:17 -0800 Subject: [PATCH] Add test for WritableNativeMap Summary: A simple test for WritableNativeMap. Also includes a test to verify that the key name exists in the error message if a dynamic cast exception occurs (this will be implemented in the future) Reviewed By: mdvacca Differential Revision: D12914375 fbshipit-source-id: 654674b1b32e7b3f38cc1364a2302d1ce08ec33e --- .../tests/core/WritableNativeMapTest.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 ReactAndroid/src/androidTest/java/com/facebook/react/tests/core/WritableNativeMapTest.java diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/core/WritableNativeMapTest.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/core/WritableNativeMapTest.java new file mode 100644 index 00000000000000..bb2ad2c9babf52 --- /dev/null +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/core/WritableNativeMapTest.java @@ -0,0 +1,104 @@ +package com.facebook.react.tests.core; + +import static org.fest.assertions.api.Assertions.assertThat; + +import android.support.test.runner.AndroidJUnit4; +import com.facebook.react.bridge.UnexpectedNativeTypeException; +import com.facebook.react.bridge.WritableNativeArray; +import com.facebook.react.bridge.WritableNativeMap; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class WritableNativeMapTest { + + private WritableNativeMap mMap; + + @Before + public void setup() { + mMap = new WritableNativeMap(); + mMap.putBoolean("boolean", true); + mMap.putDouble("double", 1.2); + mMap.putInt("int", 1); + mMap.putString("string", "abc"); + mMap.putMap("map", new WritableNativeMap()); + mMap.putArray("array", new WritableNativeArray()); + mMap.putBoolean("dvacca", true); + mMap.setUseNativeAccessor(true); + } + + @Test + public void testBoolean() { + assertThat(mMap.getBoolean("boolean")).isEqualTo(true); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testBooleanInvalidType() { + mMap.getBoolean("string"); + } + + @Test + public void testDouble() { + assertThat(mMap.getDouble("double")).isEqualTo(1.2); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testDoubleInvalidType() { + mMap.getDouble("string"); + } + + @Test + public void testInt() { + assertThat(mMap.getInt("int")).isEqualTo(1); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testIntInvalidType() { + mMap.getInt("string"); + } + + @Test + public void testString() { + assertThat(mMap.getString("string")).isEqualTo("abc"); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testStringInvalidType() { + mMap.getString("int"); + } + + @Test + public void testMap() { + assertThat(mMap.getMap("map")).isNotNull(); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testMapInvalidType() { + mMap.getMap("string"); + } + + @Test + public void testArray() { + assertThat(mMap.getArray("array")).isNotNull(); + } + + @Test(expected = UnexpectedNativeTypeException.class) + public void testArrayInvalidType() { + mMap.getArray("string"); + } + + @Ignore("Needs to be implemented") + @Test + public void testErrorMessageContainsKey() { + String key = "fkg"; + try { + mMap.getString(key); + Assert.fail("Expected an UnexpectedNativeTypeException to be thrown"); + } catch (UnexpectedNativeTypeException e) { + assertThat(e.getMessage()).contains(key); + } + } +}