| 1 | package yaml_testscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.21.0 io/" |
| 6 | "reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.21.0 testing/" |
| 9 | |
| 10 | "github.com/goccy/go-yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/" |
| 11 | "github.com/goccy/go-yaml/parserscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml/parser`/" |
| 12 | ) |
| 13 | |
| 14 | func TestRoundTripWithCommentscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/TestRoundTripWithComment().(t *testingscip-go gomod github.com/golang/go/src go1.21.0 testing/.Tscip-go gomod github.com/golang/go/src go1.21.0 testing/T#) { |
| 15 | yml := ` |
| 16 | # head comment |
| 17 | key: value # line comment |
| 18 | ` |
| 19 | var v struct { |
| 20 | Key string |
| 21 | } |
| 22 | comments := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.CommentMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/CommentMap#{} |
| 23 | |
| 24 | if err := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.UnmarshalWithOptionsscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/UnmarshalWithOptions().([]byte(yml), &v, yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.Strictscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/Strict().(), yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.CommentToMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/CommentToMap().(comments)); err != nil { |
| 25 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 26 | } |
| 27 | out, err := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.MarshalWithOptionsscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/MarshalWithOptions().(v, yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.WithCommentscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/WithComment().(comments)) |
| 28 | if err != nil { |
| 29 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 30 | } |
| 31 | got := "\n" + string(out) |
| 32 | if yml != got { |
| 33 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to get round tripped yaml: %s", got) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestStreamDecodingWithCommentscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/TestStreamDecodingWithComment().(t *testingscip-go gomod github.com/golang/go/src go1.21.0 testing/.Tscip-go gomod github.com/golang/go/src go1.21.0 testing/T#) { |
| 38 | yml := ` |
| 39 | # comment |
| 40 | --- |
| 41 | a: |
| 42 | b: |
| 43 | c: # comment |
| 44 | --- |
| 45 | foo: bar # comment |
| 46 | --- |
| 47 | - a |
| 48 | - b |
| 49 | - c # comment |
| 50 | ` |
| 51 | cm := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.CommentMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/CommentMap#{} |
| 52 | dec := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.NewDecoderscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/NewDecoder().(stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.21.0 strings/NewReader().(yml), yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.CommentToMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/CommentToMap().(cm)) |
| 53 | var commentPathsWithDocIndex [][]string |
| 54 | for { |
| 55 | var v any |
| 56 | if err := dec.Decodescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/Decoder#Decode().(&v); err != nil { |
| 57 | if err == ioscip-go gomod github.com/golang/go/src go1.21.0 io/.EOFscip-go gomod github.com/golang/go/src go1.21.0 io/EOF. { |
| 58 | break |
| 59 | } |
| 60 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 61 | } |
| 62 | paths := make([]string, 0, len(cm)) |
| 63 | for k := range cm { |
| 64 | paths = append(paths, k) |
| 65 | } |
| 66 | commentPathsWithDocIndex = append(commentPathsWithDocIndex, paths) |
| 67 | for k := range cm { |
| 68 | delete(cm, k) |
| 69 | } |
| 70 | } |
| 71 | if !reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.21.0 reflect/DeepEqual().(commentPathsWithDocIndex, [][]string{ |
| 72 | {"$.a.b.c"}, |
| 73 | {"$.foo"}, |
| 74 | {"$[2]"}, |
| 75 | }) { |
| 76 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to get comment: %v", commentPathsWithDocIndex) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestDecodeKeepAddressscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/TestDecodeKeepAddress().(t *testingscip-go gomod github.com/golang/go/src go1.21.0 testing/.Tscip-go gomod github.com/golang/go/src go1.21.0 testing/T#) { |
| 81 | var data = ` |
| 82 | a: &a [_] |
| 83 | b: &b [*a,*a] |
| 84 | c: &c [*b,*b] |
| 85 | d: &d [*c,*c] |
| 86 | ` |
| 87 | var v map[string]any |
| 88 | if err := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.Unmarshalscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/Unmarshal().([]byte(data), &v); err != nil { |
| 89 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 90 | } |
| 91 | a := v["a"] |
| 92 | b := v["b"] |
| 93 | for _, vv := range v["b"].([]any) { |
| 94 | if fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21.0 fmt/Sprintf().("%p", a) != fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21.0 fmt/Sprintf().("%p", vv) { |
| 95 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to decode b element as keep address") |
| 96 | } |
| 97 | } |
| 98 | for _, vv := range v["c"].([]any) { |
| 99 | if fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21.0 fmt/Sprintf().("%p", b) != fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.21.0 fmt/Sprintf().("%p", vv) { |
| 100 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to decode c element as keep address") |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestSmartAnchorscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/TestSmartAnchor().(t *testingscip-go gomod github.com/golang/go/src go1.21.0 testing/.Tscip-go gomod github.com/golang/go/src go1.21.0 testing/T#) { |
| 106 | var data = ` |
| 107 | a: &a [_,_,_,_,_,_,_,_,_,_,_,_,_,_,_] |
| 108 | b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a,*a] |
| 109 | c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b,*b] |
| 110 | d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c,*c] |
| 111 | e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d,*d] |
| 112 | f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e,*e] |
| 113 | g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f,*f] |
| 114 | h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g,*g] |
| 115 | i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h,*h] |
| 116 | ` |
| 117 | var v any |
| 118 | if err := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.Unmarshalscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/Unmarshal().([]byte(data), &v); err != nil { |
| 119 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 120 | } |
| 121 | got, err := yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.MarshalWithOptionsscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/MarshalWithOptions().(v, yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.WithSmartAnchorscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/WithSmartAnchor().()) |
| 122 | if err != nil { |
| 123 | t.Fatalscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatal().(err) |
| 124 | } |
| 125 | expected := ` |
| 126 | a: &a |
| 127 | - _ |
| 128 | - _ |
| 129 | - _ |
| 130 | - _ |
| 131 | - _ |
| 132 | - _ |
| 133 | - _ |
| 134 | - _ |
| 135 | - _ |
| 136 | - _ |
| 137 | - _ |
| 138 | - _ |
| 139 | - _ |
| 140 | - _ |
| 141 | - _ |
| 142 | b: &b |
| 143 | - *a |
| 144 | - *a |
| 145 | - *a |
| 146 | - *a |
| 147 | - *a |
| 148 | - *a |
| 149 | - *a |
| 150 | - *a |
| 151 | - *a |
| 152 | - *a |
| 153 | c: &c |
| 154 | - *b |
| 155 | - *b |
| 156 | - *b |
| 157 | - *b |
| 158 | - *b |
| 159 | - *b |
| 160 | - *b |
| 161 | - *b |
| 162 | - *b |
| 163 | - *b |
| 164 | d: &d |
| 165 | - *c |
| 166 | - *c |
| 167 | - *c |
| 168 | - *c |
| 169 | - *c |
| 170 | - *c |
| 171 | - *c |
| 172 | - *c |
| 173 | - *c |
| 174 | - *c |
| 175 | e: &e |
| 176 | - *d |
| 177 | - *d |
| 178 | - *d |
| 179 | - *d |
| 180 | - *d |
| 181 | - *d |
| 182 | - *d |
| 183 | - *d |
| 184 | - *d |
| 185 | - *d |
| 186 | f: &f |
| 187 | - *e |
| 188 | - *e |
| 189 | - *e |
| 190 | - *e |
| 191 | - *e |
| 192 | - *e |
| 193 | - *e |
| 194 | - *e |
| 195 | - *e |
| 196 | - *e |
| 197 | g: &g |
| 198 | - *f |
| 199 | - *f |
| 200 | - *f |
| 201 | - *f |
| 202 | - *f |
| 203 | - *f |
| 204 | - *f |
| 205 | - *f |
| 206 | - *f |
| 207 | - *f |
| 208 | h: &h |
| 209 | - *g |
| 210 | - *g |
| 211 | - *g |
| 212 | - *g |
| 213 | - *g |
| 214 | - *g |
| 215 | - *g |
| 216 | - *g |
| 217 | - *g |
| 218 | - *g |
| 219 | i: |
| 220 | - *h |
| 221 | - *h |
| 222 | - *h |
| 223 | - *h |
| 224 | - *h |
| 225 | - *h |
| 226 | - *h |
| 227 | - *h |
| 228 | - *h |
| 229 | - *h |
| 230 | ` |
| 231 | if stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.21.0 strings/TrimPrefix().(expected, "\n") != string(got) { |
| 232 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to encode: %s", string(got)) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestCustomErrorMessagescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml_test`/TestCustomErrorMessage().(t *testingscip-go gomod github.com/golang/go/src go1.21.0 testing/.Tscip-go gomod github.com/golang/go/src go1.21.0 testing/T#) { |
| 237 | data := ` |
| 238 | foo: |
| 239 | bar: |
| 240 | foo: 2 |
| 241 | baz: |
| 242 | foo: 3 |
| 243 | foo: 2 |
| 244 | ` |
| 245 | if _, err := parserscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml/parser`/.ParseBytesscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml/parser`/ParseBytes().([]byte(data), 0); err == nil { |
| 246 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("expected error") |
| 247 | } else { |
| 248 | yamlErr, ok := err.(yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.Errorscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/Error#) |
| 249 | if !ok { |
| 250 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("failed to get yaml.Error from error: %T", err) |
| 251 | } |
| 252 | expected := ` |
| 253 | [7:1] custom message |
| 254 | 4 | foo: 2 |
| 255 | 5 | baz: |
| 256 | 6 | foo: 3 |
| 257 | > 7 | foo: 2 |
| 258 | ^ |
| 259 | ` |
| 260 | got := "\n" + yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/.FormatErrorWithTokenscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/FormatErrorWithToken.("custom message", yamlErr.GetTokenscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml/internal/errors`/Error#GetToken.(), false, true) |
| 261 | if expected != got { |
| 262 | t.Fatalfscip-go gomod github.com/golang/go/src go1.21.0 testing/common#Fatalf().("unexpected error message:\nexpected:\n%s\nbut got:\n%s", expected, got) |
| 263 | } |
| 264 | } |
| 265 | } |