|
1 | 1 | package org.javawebstack.abstractdata; |
2 | 2 |
|
| 3 | +import org.javawebstack.abstractdata.exception.AbstractCoercingException; |
3 | 4 | import org.junit.jupiter.api.Test; |
4 | 5 |
|
5 | 6 | import static org.junit.jupiter.api.Assertions.*; |
6 | 7 |
|
7 | 8 | class AbstractArrayTest { |
8 | 9 |
|
| 10 | + @Test |
| 11 | + void testIsArrayReturnsTrue() { |
| 12 | + assertTrue(new AbstractArray().isArray()); |
| 13 | + } |
| 14 | + |
| 15 | + @Test |
| 16 | + void testObjectThrowsInStrictMode() { |
| 17 | + assertThrows(AbstractCoercingException.class, () -> new AbstractArray().object(true)); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void testObjectCoercingWhenNotInStrictMode() { |
| 22 | + AbstractArray a = new AbstractArray(); |
| 23 | + a.add(0); |
| 24 | + a.add("1"); |
| 25 | + AbstractObject o = assertDoesNotThrow(() -> a.object(false)); |
| 26 | + assertTrue(o.hasNumber("0")); |
| 27 | + assertTrue(o.hasString("1")); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testObjectQueryReturnsNullWhenKeyIsNotFound() { |
| 32 | + assertNull(new AbstractArray().add(new AbstractObject()).object("1")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testObjectQueryReturnsValueWhenKeyIsFound() { |
| 37 | + assertNotNull(new AbstractArray().add(new AbstractObject()).object("0")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testArrayQueryReturnsNullWhenKeyIsNotFound() { |
| 42 | + assertNull(new AbstractArray().add(new AbstractArray()).array("1")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testArrayQueryReturnsValueWhenKeyIsFound() { |
| 47 | + assertNotNull(new AbstractArray().add(new AbstractArray()).array("0")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testPrimitiveQueryReturnsNullWhenKeyIsNotFound() { |
| 52 | + assertNull(new AbstractArray().add(0).primitive("1")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testPrimitiveQueryReturnsValueWhenKeyIsFound() { |
| 57 | + assertNotNull(new AbstractArray().add(0).primitive("0")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testStringQueryReturnsNullWhenKeyIsNotFound() { |
| 62 | + assertNull(new AbstractArray().add("abc").string("1")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testStringQueryReturnsValueWhenKeyIsFound() { |
| 67 | + assertEquals("abc", new AbstractArray().add("abc").string("0")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testNumberQueryReturnsNullWhenKeyIsNotFound() { |
| 72 | + assertNull(new AbstractArray().add(123).number("1")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testNumberQueryReturnsValueWhenKeyIsFound() { |
| 77 | + assertEquals(123, new AbstractArray().add(123).number("0")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testBoolQueryReturnsNullWhenKeyIsNotFound() { |
| 82 | + assertNull(new AbstractArray().add(true).bool("1")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testBoolQueryReturnsValueWhenKeyIsFound() { |
| 87 | + assertEquals(true, new AbstractArray().add(true).bool("0")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testQueryOrElseReturnsValueIfFoundAndNonNull() { |
| 92 | + AbstractElement expected = new AbstractPrimitive(123); |
| 93 | + AbstractElement orElse = new AbstractPrimitive("456"); |
| 94 | + AbstractArray array = new AbstractArray().add(expected); |
| 95 | + assertEquals(expected, array.query("0", orElse)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testQueryOrElseReturnsOrElseIfFoundAndNull() { |
| 100 | + AbstractElement orElse = new AbstractPrimitive("456"); |
| 101 | + AbstractArray array = new AbstractArray().addNull(); |
| 102 | + assertEquals(orElse, array.query("0", orElse)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testQueryOrElseReturnsOrElseIfNotFound() { |
| 107 | + AbstractElement unexpected = new AbstractPrimitive(123); |
| 108 | + AbstractElement orElse = new AbstractPrimitive("456"); |
| 109 | + AbstractArray array = new AbstractArray().add(unexpected); |
| 110 | + assertEquals(orElse, array.query("1", orElse)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testObjectQueryOrElseReturnsOrElseIfNotFound() { |
| 115 | + AbstractObject orElse = new AbstractObject().set("a", 1); |
| 116 | + assertSame(orElse, new AbstractArray().object("0", orElse)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testArrayQueryOrElseReturnsOrElseIfNotFound() { |
| 121 | + AbstractArray orElse = new AbstractArray().add(123); |
| 122 | + assertSame(orElse, new AbstractArray().array("0", orElse)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testPrimitiveQueryOrElseReturnsOrElseIfNotFound() { |
| 127 | + AbstractPrimitive orElse = new AbstractPrimitive(123); |
| 128 | + assertSame(orElse, new AbstractArray().primitive("0", orElse)); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void testStringQueryOrElseReturnsOrElseIfNotFound() { |
| 133 | + String orElse = "abc"; |
| 134 | + assertEquals(orElse, new AbstractArray().string("0", orElse)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testNumberQueryOrElseReturnsOrElseIfNotFound() { |
| 139 | + Number orElse = 123; |
| 140 | + assertEquals(orElse, new AbstractArray().number("0", orElse)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testBoolQueryOrElseReturnsOrElseIfNotFound() { |
| 145 | + Boolean orElse = true; |
| 146 | + assertEquals(orElse, new AbstractArray().bool("0", orElse)); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testQueryThrowsOnNull() { |
| 151 | + assertThrows(IllegalArgumentException.class, () -> new AbstractArray().query(null)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testQueryWithNonIntPathReturnsNull() { |
| 156 | + assertNull(new AbstractArray().add(1).query("abc")); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testQueryWithNonExistingIndexReturnsNull() { |
| 161 | + assertNull(new AbstractArray().add(0).query("1")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testQueryWithOnlyOneSegmentReturnsValue() { |
| 166 | + assertNotNull(new AbstractArray().add(1).query("0")); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void testClearClearsTheArrayAndReturnsThis() { |
| 171 | + AbstractArray array = new AbstractArray().add(1).add(2).add(3); |
| 172 | + assertEquals(3, array.size()); |
| 173 | + assertSame(array, array.clear()); |
| 174 | + assertEquals(0, array.size()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void testQueryWithObjectValueQueriesObject() { |
| 179 | + AbstractArray array = new AbstractArray(); |
| 180 | + array.add(new AbstractObject().set("a", 1)); |
| 181 | + assertEquals(1, array.query("0.a").number().intValue()); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void testQueryWithArrayValueQueriesArray() { |
| 186 | + AbstractArray array = new AbstractArray(); |
| 187 | + array.add(new AbstractArray().add(1)); |
| 188 | + assertEquals(1, array.query("0.0").number().intValue()); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void testQueryWithPrimitiveValueReturnsNull() { |
| 193 | + AbstractArray array = new AbstractArray(); |
| 194 | + array.add("abc"); |
| 195 | + assertNull(array.query("0.abc")); |
| 196 | + } |
| 197 | + |
9 | 198 | @Test |
10 | 199 | void testEqualsNormal() { |
11 | 200 | AbstractArray first = new AbstractArray(); |
|
0 commit comments