|
| 1 | +//@ wasmtime-flags = '-Wcomponent-model-map' |
| 2 | + |
| 3 | +package export_wit_world |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + test "wit_component/test_maps_to_test" |
| 8 | + |
| 9 | + . "go.bytecodealliance.org/pkg/wit/types" |
| 10 | +) |
| 11 | + |
| 12 | +func Run() { |
| 13 | + testNamedRoundtrip() |
| 14 | + testBytesRoundtrip() |
| 15 | + testEmptyRoundtrip() |
| 16 | + testOptionRoundtrip() |
| 17 | + testRecordRoundtrip() |
| 18 | + testInlineRoundtrip() |
| 19 | + testLargeRoundtrip() |
| 20 | + testMultiParamRoundtrip() |
| 21 | + testNestedRoundtrip() |
| 22 | + testVariantRoundtrip() |
| 23 | + testResultRoundtrip() |
| 24 | + testTupleRoundtrip() |
| 25 | + testSingleEntryRoundtrip() |
| 26 | +} |
| 27 | + |
| 28 | +func testNamedRoundtrip() { |
| 29 | + input := test.NamesById{ |
| 30 | + 1: "uno", |
| 31 | + 2: "two", |
| 32 | + } |
| 33 | + result := test.NamedRoundtrip(input) |
| 34 | + assertEqual(result["uno"], uint32(1)) |
| 35 | + assertEqual(result["two"], uint32(2)) |
| 36 | +} |
| 37 | + |
| 38 | +func testBytesRoundtrip() { |
| 39 | + input := test.BytesByName{ |
| 40 | + "hello": []uint8("world"), |
| 41 | + "bin": {0, 1, 2}, |
| 42 | + } |
| 43 | + result := test.BytesRoundtrip(input) |
| 44 | + assertSliceEqual(result["hello"], []uint8("world")) |
| 45 | + assertSliceEqual(result["bin"], []uint8{0, 1, 2}) |
| 46 | +} |
| 47 | + |
| 48 | +func testEmptyRoundtrip() { |
| 49 | + input := test.NamesById{} |
| 50 | + result := test.EmptyRoundtrip(input) |
| 51 | + assertEqual(len(result), 0) |
| 52 | +} |
| 53 | + |
| 54 | +func testOptionRoundtrip() { |
| 55 | + input := map[string]Option[uint32]{ |
| 56 | + "some": Some[uint32](42), |
| 57 | + "none": None[uint32](), |
| 58 | + } |
| 59 | + result := test.OptionRoundtrip(input) |
| 60 | + assertEqual(len(result), 2) |
| 61 | + assertEqual(result["some"].Some(), uint32(42)) |
| 62 | + assertEqual(result["none"].Tag(), OptionNone) |
| 63 | +} |
| 64 | + |
| 65 | +func testRecordRoundtrip() { |
| 66 | + entry := test.LabeledEntry{ |
| 67 | + Label: "test-label", |
| 68 | + Values: test.NamesById{ |
| 69 | + 10: "ten", |
| 70 | + 20: "twenty", |
| 71 | + }, |
| 72 | + } |
| 73 | + result := test.RecordRoundtrip(entry) |
| 74 | + assertEqual(result.Label, "test-label") |
| 75 | + assertEqual(len(result.Values), 2) |
| 76 | + assertEqual(result.Values[10], "ten") |
| 77 | + assertEqual(result.Values[20], "twenty") |
| 78 | +} |
| 79 | + |
| 80 | +func testInlineRoundtrip() { |
| 81 | + input := map[uint32]string{ |
| 82 | + 1: "one", |
| 83 | + 2: "two", |
| 84 | + } |
| 85 | + result := test.InlineRoundtrip(input) |
| 86 | + assertEqual(len(result), 2) |
| 87 | + assertEqual(result["one"], uint32(1)) |
| 88 | + assertEqual(result["two"], uint32(2)) |
| 89 | +} |
| 90 | + |
| 91 | +func testLargeRoundtrip() { |
| 92 | + input := make(test.NamesById) |
| 93 | + for i := uint32(0); i < 100; i++ { |
| 94 | + input[i] = fmt.Sprintf("value-%d", i) |
| 95 | + } |
| 96 | + result := test.LargeRoundtrip(input) |
| 97 | + assertEqual(len(result), 100) |
| 98 | + for i := uint32(0); i < 100; i++ { |
| 99 | + assertEqual(result[i], fmt.Sprintf("value-%d", i)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func testMultiParamRoundtrip() { |
| 104 | + names := test.NamesById{ |
| 105 | + 1: "one", |
| 106 | + 2: "two", |
| 107 | + } |
| 108 | + bytes := test.BytesByName{ |
| 109 | + "key": {42}, |
| 110 | + } |
| 111 | + ids, bytesOut := test.MultiParamRoundtrip(names, bytes) |
| 112 | + assertEqual(len(ids), 2) |
| 113 | + assertEqual(ids["one"], uint32(1)) |
| 114 | + assertEqual(ids["two"], uint32(2)) |
| 115 | + assertEqual(len(bytesOut), 1) |
| 116 | + assertSliceEqual(bytesOut["key"], []uint8{42}) |
| 117 | +} |
| 118 | + |
| 119 | +func testNestedRoundtrip() { |
| 120 | + input := map[string]map[uint32]string{ |
| 121 | + "group-a": { |
| 122 | + 1: "one", |
| 123 | + 2: "two", |
| 124 | + }, |
| 125 | + "group-b": { |
| 126 | + 10: "ten", |
| 127 | + }, |
| 128 | + } |
| 129 | + result := test.NestedRoundtrip(input) |
| 130 | + assertEqual(len(result), 2) |
| 131 | + assertEqual(result["group-a"][1], "one") |
| 132 | + assertEqual(result["group-a"][2], "two") |
| 133 | + assertEqual(result["group-b"][10], "ten") |
| 134 | +} |
| 135 | + |
| 136 | +func testVariantRoundtrip() { |
| 137 | + m := test.NamesById{1: "one"} |
| 138 | + asMap := test.VariantRoundtrip(test.MakeMapOrStringAsMap(m)) |
| 139 | + assertEqual(asMap.Tag(), test.MapOrStringAsMap) |
| 140 | + assertEqual(asMap.AsMap()[1], "one") |
| 141 | + |
| 142 | + asStr := test.VariantRoundtrip(test.MakeMapOrStringAsString("hello")) |
| 143 | + assertEqual(asStr.Tag(), test.MapOrStringAsString) |
| 144 | + assertEqual(asStr.AsString(), "hello") |
| 145 | +} |
| 146 | + |
| 147 | +func testResultRoundtrip() { |
| 148 | + m := test.NamesById{5: "five"} |
| 149 | + okResult := test.ResultRoundtrip(Ok[test.NamesById, string](m)) |
| 150 | + assertEqual(okResult.Tag(), ResultOk) |
| 151 | + assertEqual(okResult.Ok()[5], "five") |
| 152 | + |
| 153 | + errResult := test.ResultRoundtrip(Err[test.NamesById, string]("bad input")) |
| 154 | + assertEqual(errResult.Tag(), ResultErr) |
| 155 | + assertEqual(errResult.Err(), "bad input") |
| 156 | +} |
| 157 | + |
| 158 | +func testTupleRoundtrip() { |
| 159 | + m := test.NamesById{7: "seven"} |
| 160 | + resultMap, resultNum := test.TupleRoundtrip(Tuple2[test.NamesById, uint64]{m, 42}) |
| 161 | + assertEqual(len(resultMap), 1) |
| 162 | + assertEqual(resultMap[7], "seven") |
| 163 | + assertEqual(resultNum, uint64(42)) |
| 164 | +} |
| 165 | + |
| 166 | +func testSingleEntryRoundtrip() { |
| 167 | + input := test.NamesById{99: "ninety-nine"} |
| 168 | + result := test.SingleEntryRoundtrip(input) |
| 169 | + assertEqual(len(result), 1) |
| 170 | + assertEqual(result[99], "ninety-nine") |
| 171 | +} |
| 172 | + |
| 173 | +func assertEqual[T comparable](a T, b T) { |
| 174 | + if a != b { |
| 175 | + panic(fmt.Sprintf("%v not equal to %v", a, b)) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func assertSliceEqual[T comparable](a []T, b []T) { |
| 180 | + if len(a) != len(b) { |
| 181 | + panic(fmt.Sprintf("slices have different lengths: %d vs %d", len(a), len(b))) |
| 182 | + } |
| 183 | + for i := range a { |
| 184 | + if a[i] != b[i] { |
| 185 | + panic(fmt.Sprintf("slices differ at index %d: %v vs %v", i, a[i], b[i])) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments