|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package okio |
| 17 | + |
| 18 | +import kotlin.test.Test |
| 19 | +import kotlin.test.assertEquals |
| 20 | +import kotlin.test.assertFailsWith |
| 21 | +import okio.ByteString.Companion.encodeUtf8 |
| 22 | + |
| 23 | +class TypedOptionsTest { |
| 24 | + @Test |
| 25 | + fun happyPath() { |
| 26 | + val colors = listOf("Red", "Green", "Blue") |
| 27 | + val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } |
| 28 | + val buffer = Buffer().writeUtf8("bluegreenyellow") |
| 29 | + assertEquals("Blue", buffer.select(colorOptions)) |
| 30 | + assertEquals("greenyellow", buffer.snapshot().utf8()) |
| 31 | + assertEquals("Green", buffer.select(colorOptions)) |
| 32 | + assertEquals("yellow", buffer.snapshot().utf8()) |
| 33 | + assertEquals(null, buffer.select(colorOptions)) |
| 34 | + assertEquals("yellow", buffer.snapshot().utf8()) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun typedOptionsConstructor() { |
| 39 | + val colors = listOf("Red", "Green", "Blue") |
| 40 | + val colorOptions = TypedOptions( |
| 41 | + colors, |
| 42 | + Options.of("red".encodeUtf8(), "green".encodeUtf8(), "blue".encodeUtf8()), |
| 43 | + ) |
| 44 | + val buffer = Buffer().writeUtf8("bluegreenyellow") |
| 45 | + assertEquals("Blue", buffer.select(colorOptions)) |
| 46 | + assertEquals("greenyellow", buffer.snapshot().utf8()) |
| 47 | + assertEquals("Green", buffer.select(colorOptions)) |
| 48 | + assertEquals("yellow", buffer.snapshot().utf8()) |
| 49 | + assertEquals(null, buffer.select(colorOptions)) |
| 50 | + assertEquals("yellow", buffer.snapshot().utf8()) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun typedOptionsConstructorEnforcesSizeMatch() { |
| 55 | + val colors = listOf("Red", "Green", "Blue") |
| 56 | + assertFailsWith<IllegalArgumentException> { |
| 57 | + TypedOptions( |
| 58 | + colors, |
| 59 | + Options.of("red".encodeUtf8(), "green".encodeUtf8()), |
| 60 | + ) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun listFunctionsWork() { |
| 66 | + val colors = listOf("Red", "Green", "Blue") |
| 67 | + val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } |
| 68 | + assertEquals(3, colorOptions.size) |
| 69 | + assertEquals("Red", colorOptions[0]) |
| 70 | + assertEquals("Green", colorOptions[1]) |
| 71 | + assertEquals("Blue", colorOptions[2]) |
| 72 | + assertFailsWith<IndexOutOfBoundsException> { |
| 73 | + colorOptions[3] |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Confirm we can mutate the collection used to create our [TypedOptions] without corrupting its |
| 79 | + * behavior. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + fun safeToMutateSourceCollectionAfterConstruction() { |
| 83 | + val colors = mutableListOf("Red", "Green") |
| 84 | + val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } |
| 85 | + colors[0] = "Black" |
| 86 | + |
| 87 | + val buffer = Buffer().writeUtf8("red") |
| 88 | + assertEquals("Red", buffer.select(colorOptions)) |
| 89 | + } |
| 90 | +} |
0 commit comments