|
| 1 | +package helium314.keyboard.latin.utils |
| 2 | + |
| 3 | +import org.junit.Assert.assertEquals |
| 4 | +import org.junit.Assert.assertTrue |
| 5 | +import org.junit.Test |
| 6 | +import org.junit.runner.RunWith |
| 7 | +import org.robolectric.RobolectricTestRunner |
| 8 | + |
| 9 | +@RunWith(RobolectricTestRunner::class) |
| 10 | +class JsonUtilsTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + fun testJsonStrToList_validList() { |
| 14 | + val jsonStr = "[{\"Integer\": 1}, {\"String\": \"hello\"}, {\"Integer\": 42}]" |
| 15 | + val list = JsonUtils.jsonStrToList(jsonStr) |
| 16 | + |
| 17 | + assertEquals(3, list.size) |
| 18 | + assertEquals(1, list[0]) |
| 19 | + assertEquals("hello", list[1]) |
| 20 | + assertEquals(42, list[2]) |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + fun testJsonStrToList_emptyString() { |
| 25 | + val list = JsonUtils.jsonStrToList("") |
| 26 | + assertTrue(list.isEmpty()) |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun testJsonStrToList_emptyArray() { |
| 31 | + val list = JsonUtils.jsonStrToList("[]") |
| 32 | + assertTrue(list.isEmpty()) |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + fun testJsonStrToList_invalidName() { |
| 37 | + // Objects with invalid property names should be skipped gracefully. |
| 38 | + val jsonStr = "[{\"Boolean\": true}, {\"Integer\": 123}]" |
| 39 | + val list = JsonUtils.jsonStrToList(jsonStr) |
| 40 | + |
| 41 | + assertEquals(1, list.size) |
| 42 | + assertEquals(123, list[0]) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testJsonStrToList_malformedJson() { |
| 47 | + // A malformed JSON should result in returning an empty list without throwing an unhandled exception. |
| 48 | + val jsonStr = "[malformed" |
| 49 | + val list = JsonUtils.jsonStrToList(jsonStr) |
| 50 | + assertTrue(list.isEmpty()) |
| 51 | + } |
| 52 | +} |
0 commit comments