|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package optional_test |
| 5 | + |
| 6 | +import ( |
| 7 | + std_json "encoding/json" //nolint:depguard |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/modules/json" |
| 11 | + "code.gitea.io/gitea/modules/optional" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +type testSerializationStruct struct { |
| 18 | + NormalString string `json:"normal_string" yaml:"normal_string"` |
| 19 | + NormalBool bool `json:"normal_bool" yaml:"normal_bool"` |
| 20 | + OptBool optional.Option[bool] `json:"optional_bool,omitempty" yaml:"optional_bool,omitempty"` |
| 21 | + OptString optional.Option[string] `json:"optional_string,omitempty" yaml:"optional_string,omitempty"` |
| 22 | + OptTwoBool optional.Option[bool] `json:"optional_two_bool" yaml:"optional_two_bool"` |
| 23 | + OptTwoString optional.Option[string] `json:"optional_twostring" yaml:"optional_two_string"` |
| 24 | +} |
| 25 | + |
| 26 | +func TestOptionalToJson(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + obj *testSerializationStruct |
| 30 | + want string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "empty", |
| 34 | + obj: new(testSerializationStruct), |
| 35 | + want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "some", |
| 39 | + obj: &testSerializationStruct{ |
| 40 | + NormalString: "a string", |
| 41 | + NormalBool: true, |
| 42 | + OptBool: optional.Some(false), |
| 43 | + OptString: optional.Some(""), |
| 44 | + OptTwoBool: optional.None[bool](), |
| 45 | + OptTwoString: optional.None[string](), |
| 46 | + }, |
| 47 | + want: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_twostring":null}`, |
| 48 | + }, |
| 49 | + } |
| 50 | + for _, tc := range tests { |
| 51 | + t.Run(tc.name, func(t *testing.T) { |
| 52 | + b, err := json.Marshal(tc.obj) |
| 53 | + assert.NoError(t, err) |
| 54 | + assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected") |
| 55 | + |
| 56 | + b, err = std_json.Marshal(tc.obj) |
| 57 | + assert.NoError(t, err) |
| 58 | + assert.EqualValues(t, tc.want, string(b), "std json module returned unexpected") |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestOptionalFromJson(t *testing.T) { |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + data string |
| 67 | + want testSerializationStruct |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "empty", |
| 71 | + data: `{}`, |
| 72 | + want: testSerializationStruct{ |
| 73 | + NormalString: "", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "some", |
| 78 | + data: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_twostring":null}`, |
| 79 | + want: testSerializationStruct{ |
| 80 | + NormalString: "a string", |
| 81 | + NormalBool: true, |
| 82 | + OptBool: optional.Some(false), |
| 83 | + OptString: optional.Some(""), |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + for _, tc := range tests { |
| 88 | + t.Run(tc.name, func(t *testing.T) { |
| 89 | + var obj1 testSerializationStruct |
| 90 | + err := json.Unmarshal([]byte(tc.data), &obj1) |
| 91 | + assert.NoError(t, err) |
| 92 | + assert.EqualValues(t, tc.want, obj1, "gitea json module returned unexpected") |
| 93 | + |
| 94 | + var obj2 testSerializationStruct |
| 95 | + err = std_json.Unmarshal([]byte(tc.data), &obj2) |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.EqualValues(t, tc.want, obj2, "std json module returned unexpected") |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestOptionalToYaml(t *testing.T) { |
| 103 | + tests := []struct { |
| 104 | + name string |
| 105 | + obj *testSerializationStruct |
| 106 | + want string |
| 107 | + }{ |
| 108 | + { |
| 109 | + name: "empty", |
| 110 | + obj: new(testSerializationStruct), |
| 111 | + want: `normal_string: "" |
| 112 | +normal_bool: false |
| 113 | +optional_two_bool: null |
| 114 | +optional_two_string: null |
| 115 | +`, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "some", |
| 119 | + obj: &testSerializationStruct{ |
| 120 | + NormalString: "a string", |
| 121 | + NormalBool: true, |
| 122 | + OptBool: optional.Some(false), |
| 123 | + OptString: optional.Some(""), |
| 124 | + }, |
| 125 | + want: `normal_string: a string |
| 126 | +normal_bool: true |
| 127 | +optional_bool: false |
| 128 | +optional_string: "" |
| 129 | +optional_two_bool: null |
| 130 | +optional_two_string: null |
| 131 | +`, |
| 132 | + }, |
| 133 | + } |
| 134 | + for _, tc := range tests { |
| 135 | + t.Run(tc.name, func(t *testing.T) { |
| 136 | + b, err := yaml.Marshal(tc.obj) |
| 137 | + assert.NoError(t, err) |
| 138 | + assert.EqualValues(t, tc.want, string(b), "yaml module returned unexpected") |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestOptionalFromYaml(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + name string |
| 146 | + data string |
| 147 | + want testSerializationStruct |
| 148 | + }{ |
| 149 | + { |
| 150 | + name: "empty", |
| 151 | + data: ``, |
| 152 | + want: testSerializationStruct{}, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "empty but init", |
| 156 | + data: `normal_string: "" |
| 157 | +normal_bool: false |
| 158 | +optional_bool: |
| 159 | +optional_two_bool: |
| 160 | +optional_two_string: |
| 161 | +`, |
| 162 | + want: testSerializationStruct{}, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "some", |
| 166 | + data: ` |
| 167 | +normal_string: a string |
| 168 | +normal_bool: true |
| 169 | +optional_bool: false |
| 170 | +optional_string: "" |
| 171 | +optional_two_bool: null |
| 172 | +optional_twostring: null |
| 173 | +`, |
| 174 | + want: testSerializationStruct{ |
| 175 | + NormalString: "a string", |
| 176 | + NormalBool: true, |
| 177 | + OptBool: optional.Some(false), |
| 178 | + OptString: optional.Some(""), |
| 179 | + }, |
| 180 | + }, |
| 181 | + } |
| 182 | + for _, tc := range tests { |
| 183 | + t.Run(tc.name, func(t *testing.T) { |
| 184 | + var obj testSerializationStruct |
| 185 | + err := yaml.Unmarshal([]byte(tc.data), &obj) |
| 186 | + assert.NoError(t, err) |
| 187 | + assert.EqualValues(t, tc.want, obj, "yaml module returned unexpected") |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments