| 1 | import json |
| 2 | |
| 3 | @[json_as_number] |
| 4 | pub enum MessageType { |
| 5 | error = 1 |
| 6 | warning = 2 |
| 7 | info = 3 |
| 8 | log = 4 |
| 9 | } |
| 10 | |
| 11 | pub enum MessageType2 { |
| 12 | error = 1 |
| 13 | warning = 2 |
| 14 | info = 3 |
| 15 | log = 4 |
| 16 | } |
| 17 | |
| 18 | enum TestEnum { |
| 19 | one = 1 |
| 20 | two |
| 21 | } |
| 22 | |
| 23 | type TestAlias = TestEnum |
| 24 | type TestSum = TestEnum | string |
| 25 | type TestSum2 = MessageType | string |
| 26 | type TestAliasAttr = MessageType |
| 27 | |
| 28 | struct TestStruct { |
| 29 | test []TestEnum |
| 30 | test2 TestEnum |
| 31 | test3 TestAlias |
| 32 | test4 TestSum |
| 33 | test5 MessageType |
| 34 | } |
| 35 | |
| 36 | struct TestStruct2 { |
| 37 | a TestAliasAttr |
| 38 | b TestSum2 |
| 39 | c TestSum2 |
| 40 | } |
| 41 | |
| 42 | struct Test { |
| 43 | ab ?int |
| 44 | a ?MessageType |
| 45 | } |
| 46 | |
| 47 | struct Test2 { |
| 48 | a ?MessageType2 |
| 49 | } |
| 50 | |
| 51 | type TSum = MessageType | string |
| 52 | type TSum2 = MessageType2 | string |
| 53 | |
| 54 | struct Test3 { |
| 55 | a ?TSum |
| 56 | } |
| 57 | |
| 58 | struct Test4 { |
| 59 | a ?TSum2 |
| 60 | } |
| 61 | |
| 62 | fn test_encode_with_enum() { |
| 63 | out := json.encode(TestStruct{ |
| 64 | test: [TestEnum.one, TestEnum.one] |
| 65 | test2: TestEnum.two |
| 66 | test3: TestEnum.one |
| 67 | test4: TestEnum.two |
| 68 | test5: .log |
| 69 | }) |
| 70 | assert out == '{"test":["one","one"],"test2":"two","test3":"one","test4":"two","test5":4}' |
| 71 | } |
| 72 | |
| 73 | fn test_encode_direct_enum() { |
| 74 | assert json.encode(TestEnum.one) == '"one"' |
| 75 | } |
| 76 | |
| 77 | fn test_encode_alias_and_sumtype() { |
| 78 | assert json.decode(TestStruct, |
| 79 | '{"test":["one","one"],"test2":"two","test3": "one", "test4": "two", "test5":4}')! == TestStruct{ |
| 80 | test: [.one, .one] |
| 81 | test2: .two |
| 82 | test3: TestAlias(.one) |
| 83 | test4: TestSum('two') |
| 84 | test5: .log |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn test_enum_attr() { |
| 89 | assert dump(json.encode(MessageType.log)) == '4' |
| 90 | assert dump(json.encode(MessageType.error)) == '1' |
| 91 | } |
| 92 | |
| 93 | fn test_enum_attr_decode() { |
| 94 | assert json.decode(TestStruct2, '{"a": 1, "b":4, "c": "test"}')! == TestStruct2{ |
| 95 | a: .error |
| 96 | b: MessageType.log |
| 97 | c: 'test' |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn test_enum_attr_encode() { |
| 102 | assert json.encode(TestStruct2{ |
| 103 | a: .error |
| 104 | b: MessageType.log |
| 105 | c: 'test' |
| 106 | }) == '{"a":1,"b":4,"c":"test"}' |
| 107 | } |
| 108 | |
| 109 | fn test_option_enum() { |
| 110 | assert dump(json.encode(Test{none, none})) == '{}' |
| 111 | assert dump(json.encode(Test{none, MessageType.log})) == '{"a":4}' |
| 112 | t := dump(json.decode(Test, '{"a":4}')!) |
| 113 | assert t.ab == none |
| 114 | assert t.a? == .log |
| 115 | |
| 116 | t2 := dump(json.decode(Test, '{"a":null}')!) |
| 117 | assert t2.a == none |
| 118 | |
| 119 | assert json.encode(Test2{none}) == '{}' |
| 120 | assert dump(json.encode(Test2{MessageType2.log})) == '{"a":"log"}' |
| 121 | z := dump(json.decode(Test2, '{"a":"log"}')!) |
| 122 | assert z.a? == .log |
| 123 | a := dump(json.decode(Test2, '{"a": null}')!) |
| 124 | assert a.a == none |
| 125 | } |
| 126 | |
| 127 | fn test_option_sumtype_enum() { |
| 128 | assert dump(json.encode(Test3{none})) == '{}' |
| 129 | assert dump(json.encode(Test3{ a: 'foo' })) == '{"a":"foo"}' |
| 130 | assert dump(json.encode(Test3{ a: MessageType.warning })) == '{"a":2}' |
| 131 | |
| 132 | assert dump(json.encode(Test4{none})) == '{}' |
| 133 | assert dump(json.encode(Test4{ a: 'foo' })) == '{"a":"foo"}' |
| 134 | assert dump(json.encode(Test4{ a: MessageType2.warning })) == '{"a":"warning"}' |
| 135 | } |
| 136 | |