| 1 | //go:build !windows |
| 2 | |
| 3 | package yaml_test |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "encoding/json" |
| 8 | "io" |
| 9 | "runtime/debug" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/goccy/go-yaml" |
| 13 | "github.com/goccy/go-yaml/testdata/yaml-test-suite" |
| 14 | ) |
| 15 | |
| 16 | var failureTestNames = []string{ |
| 17 | "anchors-on-empty-scalars", // no json. |
| 18 | "aliases-in-flow-objects", // no json. |
| 19 | "aliases-in-explicit-block-mapping", // no json. |
| 20 | "block-mapping-with-missing-keys", // no json. |
| 21 | "empty-implicit-key-in-single-pair-flow-sequences", // no json. |
| 22 | "empty-keys-in-block-and-flow-mapping", // no json. |
| 23 | "empty-lines-at-end-of-document", // no json. |
| 24 | "flow-mapping-separate-values", // no json. |
| 25 | "flow-sequence-in-flow-mapping", // no json. |
| 26 | "implicit-flow-mapping-key-on-one-line", // no json. |
| 27 | "mapping-key-and-flow-sequence-item-anchors", // no json. |
| 28 | "nested-implicit-complex-keys", // no json. |
| 29 | "question-mark-edge-cases/00", // no json. |
| 30 | "question-mark-edge-cases/01", // no json. |
| 31 | "single-character-streams/01", // no json. |
| 32 | "single-pair-implicit-entries", // no json. |
| 33 | "spec-example-2-11-mapping-between-sequences", // no json. |
| 34 | "spec-example-6-12-separation-spaces", // no json. |
| 35 | "spec-example-7-16-flow-mapping-entries", // no json. |
| 36 | "spec-example-7-3-completely-empty-flow-nodes", // no json. |
| 37 | "spec-example-8-18-implicit-block-mapping-entries", // no json. |
| 38 | "spec-example-8-19-compact-block-mappings", // no json. |
| 39 | "syntax-character-edge-cases/00", // no json. |
| 40 | "tags-on-empty-scalars", // no json. |
| 41 | "various-combinations-of-explicit-block-mappings", // no json. |
| 42 | "various-trailing-comments", // no json. |
| 43 | "various-trailing-comments-1-3", // no json. |
| 44 | "zero-indented-sequences-in-explicit-mapping-keys", // no json. |
| 45 | |
| 46 | "colon-at-the-beginning-of-adjacent-flow-scalar", |
| 47 | "comment-without-whitespace-after-doublequoted-scalar", |
| 48 | "construct-binary", |
| 49 | "dash-in-flow-sequence", |
| 50 | "flow-collections-over-many-lines/01", |
| 51 | "flow-mapping-colon-on-line-after-key/02", |
| 52 | "invalid-comment-after-comma", |
| 53 | "invalid-comment-after-end-of-flow-sequence", |
| 54 | "invalid-comma-in-tag", |
| 55 | "plain-dashes-in-flow-sequence", |
| 56 | "spec-example-9-3-bare-documents", |
| 57 | "spec-example-9-6-stream", |
| 58 | "spec-example-9-6-stream-1-3", |
| 59 | "tabs-in-various-contexts/003", |
| 60 | "tabs-that-look-like-indentation/04", |
| 61 | "tag-shorthand-used-in-documents-but-only-defined-in-the-first", |
| 62 | "trailing-line-of-spaces/01", // last '\n' character is needed ? |
| 63 | "wrong-indented-flow-sequence", // error ? |
| 64 | "wrong-indented-multiline-quoted-scalar", // error ? |
| 65 | } |
| 66 | |
| 67 | var failureTestNameMap map[string]struct{} |
| 68 | |
| 69 | func init() { |
| 70 | failureTestNameMap = make(map[string]struct{}) |
| 71 | for _, name := range failureTestNames { |
| 72 | failureTestNameMap[name] = struct{}{} |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestYAMLTestSuite(t *testing.T) { |
| 77 | tests, err := yamltestsuite.TestSuites() |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | |
| 82 | for _, test := range tests { |
| 83 | if _, exists := failureTestNameMap[test.Name]; exists { |
| 84 | continue |
| 85 | } |
| 86 | t.Run(test.Name, func(t *testing.T) { |
| 87 | defer func() { |
| 88 | if e := recover(); e != nil { |
| 89 | t.Fatalf("panic occurred.\n[input]\n%s\nstack[%s]", string(test.InYAML), debug.Stack()) |
| 90 | } |
| 91 | }() |
| 92 | |
| 93 | if test.Error { |
| 94 | var v any |
| 95 | if err := yaml.Unmarshal(test.InYAML, &v); err == nil { |
| 96 | t.Fatalf("expected error.\n[input]\n%s\n", string(test.InYAML)) |
| 97 | } |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | dec := yaml.NewDecoder(bytes.NewReader(test.InYAML)) |
| 102 | var idx int |
| 103 | for { |
| 104 | var v any |
| 105 | if err := dec.Decode(&v); err != nil { |
| 106 | if err == io.EOF { |
| 107 | break |
| 108 | } |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if len(test.InJSON) <= idx { |
| 112 | t.Fatalf("expected json value is nothing but got %v", v) |
| 113 | } |
| 114 | expected, err := json.Marshal(test.InJSON[idx]) |
| 115 | if err != nil { |
| 116 | t.Fatalf("failed to encode json value: %v", err) |
| 117 | } |
| 118 | got, err := json.Marshal(v) |
| 119 | if err != nil { |
| 120 | t.Fatalf("failed to encode json value: %v", err) |
| 121 | } |
| 122 | if !bytes.Equal(expected, got) { |
| 123 | t.Fatalf("json mismatch [%s]:\n[expected]\n%s\n[got]\n%s\n", test.Name, string(expected), string(got)) |
| 124 | } |
| 125 | idx++ |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | total := len(tests) |
| 130 | failed := len(failureTestNames) |
| 131 | passed := total - failed |
| 132 | passedRate := float32(passed) / float32(total) * 100 |
| 133 | t.Logf("total:[%d] passed:[%d] failure:[%d] passedRate:[%f%%]", total, passed, failed, passedRate) |
| 134 | } |