Implement JavaOnlyArray#getType

Reviewed By: AaaChiuuu

Differential Revision: D3504601

fbshipit-source-id: 37dbbcdd8cc0523cb8f1673047995c4410e8f7be
This commit is contained in:
Andrew Y. Chen 2016-06-30 19:35:56 -07:00 committed by Facebook Github Bot
parent ff31128200
commit 2b88482701
2 changed files with 64 additions and 0 deletions

View File

@ -90,6 +90,23 @@ public class JavaOnlyArray implements ReadableArray, WritableArray {
@Override
public ReadableType getType(int index) {
Object object = mBackingList.get(index);
if (object == null) {
return ReadableType.Null;
} else if (object instanceof Boolean) {
return ReadableType.Boolean;
} else if (object instanceof Double ||
object instanceof Float ||
object instanceof Integer) {
return ReadableType.Number;
} else if (object instanceof String) {
return ReadableType.String;
} else if (object instanceof ReadableArray) {
return ReadableType.Array;
} else if (object instanceof ReadableMap) {
return ReadableType.Map;
}
return null;
}

View File

@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.bridge;
import org.junit.Test;
import static org.fest.assertions.api.Assertions.assertThat;
/**
* Tests for {@link JavaOnlyArray}
*/
public class JavaOnlyArrayTest {
@Test
public void testGetType() throws Exception {
JavaOnlyArray values = JavaOnlyArray.of(
1,
2f,
3.,
"4",
false,
JavaOnlyArray.of(),
JavaOnlyMap.of(),
null);
ReadableType[] expectedTypes = new ReadableType[] {
ReadableType.Number,
ReadableType.Number,
ReadableType.Number,
ReadableType.String,
ReadableType.Boolean,
ReadableType.Array,
ReadableType.Map,
ReadableType.Null
};
for (int i = 0; i < values.size(); i++) {
assertThat(values.getType(i)).isEqualTo(expectedTypes[i]);
}
}
}