| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import org.junit.Test; |
| 8 |
import org.junit.Before; |
| 9 |
|
| 10 |
import static org.junit.Assert.assertThat; |
| 11 |
import static org.hamcrest.core.Is.is; |
| 12 |
|
| 13 |
import org.mozilla.javascript.NativeArray; |
| 14 |
|
| 15 |
public class NativeArrayTest { |
| 16 |
private NativeArray array; |
| 17 |
|
| 18 |
@Before |
| 19 |
public void init() { |
| 20 |
array = new NativeArray(1); |
| 21 |
} |
| 22 |
|
| 23 |
@Test |
| 24 |
public void getIdsShouldIncludeBothIndexAndNormalProperties() { |
| 25 |
array.put(0, array, "index"); |
| 26 |
array.put("a", array, "normal"); |
| 27 |
|
| 28 |
assertThat(array.getIds(), is(new Object[]{0, "a"})); |
| 29 |
} |
| 30 |
|
| 31 |
@Test |
| 32 |
public void deleteShouldRemoveIndexProperties() { |
| 33 |
array.put(0, array, "a"); |
| 34 |
array.delete(0); |
| 35 |
assertThat(array.has(0, array), is(false)); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void deleteShouldRemoveNormalProperties() { |
| 40 |
array.put("p", array, "a"); |
| 41 |
array.delete("p"); |
| 42 |
assertThat(array.has("p", array), is(false)); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void putShouldAddIndexProperties() { |
| 47 |
array.put(0, array, "a"); |
| 48 |
assertThat(array.has(0, array), is(true)); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void putShouldAddNormalProperties() { |
| 53 |
array.put("p", array, "a"); |
| 54 |
assertThat(array.has("p", array), is(true)); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void getShouldReturnIndexProperties() { |
| 59 |
array.put(0, array, "a"); |
| 60 |
array.put("p", array, "b"); |
| 61 |
assertThat((String) array.get(0, array), is("a")); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void getShouldReturnNormalProperties() { |
| 66 |
array.put("p", array, "a"); |
| 67 |
assertThat((String) array.get("p", array), is("a")); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void hasShouldBeFalseForANewArray() { |
| 72 |
assertThat(new NativeArray(0).has(0, array), is(false)); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void getIndexIdsShouldBeEmptyForEmptyArray() { |
| 77 |
assertThat(new NativeArray(0).getIndexIds(), is(new Integer[]{})); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void getIndexIdsShouldBeAZeroForSimpleSingletonArray() { |
| 82 |
array.put(0, array, "a"); |
| 83 |
assertThat(array.getIndexIds(), is(new Integer[]{ 0 })); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void getIndexIdsShouldWorkWhenIndicesSetAsString() { |
| 88 |
array.put("0", array, "a"); |
| 89 |
assertThat(array.getIndexIds(), is(new Integer[]{ 0 })); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void getIndexIdsShouldNotIncludeNegativeIds() { |
| 94 |
array.put(-1, array, "a"); |
| 95 |
assertThat(array.getIndexIds(), is(new Integer[]{})); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void getIndexIdsShouldIncludeIdsLessThan2ToThe32() { |
| 100 |
int maxIndex = (int) (1L << 31) - 1; |
| 101 |
array.put(maxIndex, array, "a"); |
| 102 |
assertThat(array.getIndexIds(), is(new Integer[]{ maxIndex })); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void getIndexIdsShouldNotIncludeIdsGreaterThanOrEqualTo2ToThe32() { |
| 107 |
array.put((1L<<31)+"", array, "a"); |
| 108 |
assertThat(array.getIndexIds(), is(new Integer[]{})); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void getIndexIdsShouldNotReturnNonNumericIds() { |
| 113 |
array.put("x", array, "a"); |
| 114 |
assertThat(array.getIndexIds(), is(new Integer[]{})); |
| 115 |
} |
| 116 |
|
| 117 |
} |