| 1 | package tomltestscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 5 | "mathscip-go gomod github.com/golang/go/src go1.18 math/" |
| 6 | "reflectscip-go gomod github.com/golang/go/src go1.18 reflect/" |
| 7 | "sortscip-go gomod github.com/golang/go/src go1.18 sort/" |
| 8 | "strconvscip-go gomod github.com/golang/go/src go1.18 strconv/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.18 time/" |
| 11 | ) |
| 12 | |
| 13 | // CompareTOML compares the given arguments. |
| 14 | // |
| 15 | // The returned value is a copy of Test with Failure set to a (human-readable) |
| 16 | // description of the first element that is unequal. If both arguments are equal |
| 17 | // Test is returned unchanged. |
| 18 | // |
| 19 | // Reflect.DeepEqual could work here, but it won't tell us how the two |
| 20 | // structures are different. |
| 21 | func (r Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#) CompareTOMLscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#CompareTOML().(want, have any) Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test# { |
| 22 | if isTomlValuescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/isTomlValue().(want) { |
| 23 | if !isTomlValuescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/isTomlValue().(have) { |
| 24 | return r.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Type for key %q differs:\n"+ |
| 25 | " Expected: %s (%s)\n"+ |
| 26 | " Your encoder: %s (%s)", |
| 27 | r.Keyscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Key., fmtValscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtVal().(want), fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(want), fmtValscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtVal().(have), fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(have)) |
| 28 | } |
| 29 | |
| 30 | if !deepEqualscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/deepEqual().(want, have) { |
| 31 | return r.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Values for key %q differ:\n"+ |
| 32 | " Expected: %s (%s)\n"+ |
| 33 | " Your encoder: %s (%s)", |
| 34 | r.Keyscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Key., fmtValscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtVal().(want), fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(want), fmtValscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtVal().(have), fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(have)) |
| 35 | } |
| 36 | return r |
| 37 | } |
| 38 | |
| 39 | switch w := want.(type) { |
| 40 | case map[string]any: |
| 41 | return r.cmpTOMLMapscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#cmpTOMLMap().(w, have) |
| 42 | case []map[string]any: |
| 43 | ww := make([]any, 0, len(w)) |
| 44 | for _, v := range w { |
| 45 | ww = append(ww, v) |
| 46 | } |
| 47 | return r.cmpTOMLArraysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#cmpTOMLArrays().(ww, have) |
| 48 | case []any: |
| 49 | return r.cmpTOMLArraysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#cmpTOMLArrays().(w, have) |
| 50 | default: |
| 51 | return r.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Unrecognized TOML structure: %s", fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(want)) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (r Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#) cmpTOMLMapscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#cmpTOMLMap().(want map[string]any, have any) Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test# { |
| 56 | haveMap, ok := have.(map[string]any) |
| 57 | if !ok { |
| 58 | return r.mismatchscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#mismatch().("table", want, haveMap) |
| 59 | } |
| 60 | |
| 61 | wantKeys, haveKeys := mapKeysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/mapKeys().(want), mapKeysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/mapKeys().(haveMap) |
| 62 | |
| 63 | // Check that the keys of each map are equivalent. |
| 64 | for _, k := range wantKeys { |
| 65 | if _, ok := haveMap[k]; !ok { |
| 66 | bunk := r.kjoinscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#kjoin().(k) |
| 67 | return bunk.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Could not find key %q in encoder output", bunk.Keyscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Key.) |
| 68 | } |
| 69 | } |
| 70 | for _, k := range haveKeys { |
| 71 | if _, ok := want[k]; !ok { |
| 72 | bunk := r.kjoinscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#kjoin().(k) |
| 73 | return bunk.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Could not find key %q in expected output", bunk.Keyscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Key.) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Okay, now make sure that each value is equivalent. |
| 78 | for _, k := range wantKeys { |
| 79 | if sub := r.kjoinscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#kjoin().(k).CompareTOMLscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#CompareTOML().(want[k], haveMap[k]); sub.Failedscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Failed().() { |
| 80 | return sub |
| 81 | } |
| 82 | } |
| 83 | return r |
| 84 | } |
| 85 | |
| 86 | func (r Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#) cmpTOMLArraysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#cmpTOMLArrays().(want []any, have any) Testscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test# { |
| 87 | // Slice can be decoded to []any for an array of primitives, or |
| 88 | // []map[string]any for an array of tables. |
| 89 | // |
| 90 | // TODO: it would be nicer if it could always decode to []any? |
| 91 | haveSlice, ok := have.([]any) |
| 92 | if !ok { |
| 93 | tblArray, ok := have.([]map[string]any) |
| 94 | if !ok { |
| 95 | return r.mismatchscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#mismatch().("array", want, have) |
| 96 | } |
| 97 | |
| 98 | haveSlice = make([]any, len(tblArray)) |
| 99 | for i := range tblArray { |
| 100 | haveSlice[i] = tblArray[i] |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if len(want) != len(haveSlice) { |
| 105 | return r.failscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#fail().("Array lengths differ for key %q"+ |
| 106 | " Expected: %[2]v (len=%[4]d)\n"+ |
| 107 | " Your encoder: %[3]v (len=%[5]d)", |
| 108 | r.Keyscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Key., want, haveSlice, len(want), len(haveSlice)) |
| 109 | } |
| 110 | for i := 0; i < len(want); i++ { |
| 111 | if sub := r.CompareTOMLscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#CompareTOML().(want[i], haveSlice[i]); sub.Failedscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/Test#Failed().() { |
| 112 | return sub |
| 113 | } |
| 114 | } |
| 115 | return r |
| 116 | } |
| 117 | |
| 118 | // reflect.DeepEqual() that deals with NaN != NaN |
| 119 | func deepEqualscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/deepEqual().(want, have any) bool { |
| 120 | var wantF, haveF float64 |
| 121 | switch f := want.(type) { |
| 122 | case float32: |
| 123 | wantF = float64(f) |
| 124 | case float64: |
| 125 | wantF = f |
| 126 | } |
| 127 | switch f := have.(type) { |
| 128 | case float32: |
| 129 | haveF = float64(f) |
| 130 | case float64: |
| 131 | haveF = f |
| 132 | } |
| 133 | if mathscip-go gomod github.com/golang/go/src go1.18 math/.IsNaNscip-go gomod github.com/golang/go/src go1.18 math/IsNaN().(wantF) && mathscip-go gomod github.com/golang/go/src go1.18 math/.IsNaNscip-go gomod github.com/golang/go/src go1.18 math/IsNaN().(haveF) { |
| 134 | return true |
| 135 | } |
| 136 | |
| 137 | // Time.Equal deals with some edge-cases such as offset +0000 and Z being |
| 138 | // identical. |
| 139 | if haveT, ok := have.(timescip-go gomod github.com/golang/go/src go1.18 time/.Timescip-go gomod github.com/golang/go/src go1.18 time/Time#); ok { |
| 140 | if wantT, ok := want.(timescip-go gomod github.com/golang/go/src go1.18 time/.Timescip-go gomod github.com/golang/go/src go1.18 time/Time#); ok { |
| 141 | return wantT.Equalscip-go gomod github.com/golang/go/src go1.18 time/Time#Equal().(haveT) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.18 reflect/DeepEqual().(want, have) |
| 146 | } |
| 147 | |
| 148 | func isTomlValuescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/isTomlValue().(v any) bool { |
| 149 | switch v.(type) { |
| 150 | case map[string]any, []map[string]any, []any: |
| 151 | return false |
| 152 | } |
| 153 | return true |
| 154 | } |
| 155 | |
| 156 | // fmt %T with "interface {}" replaced with "any", which is far more readable. |
| 157 | func fmtTypescip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtType().(t any) string { return stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.18 strings/ReplaceAll().(fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%T", t), "interface {}", "any") } |
| 158 | func fmtHashVscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtHashV().(t any) string { return stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.18 strings/ReplaceAll().(fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%#v", t), "interface {}", "any") } |
| 159 | |
| 160 | func fmtValscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/fmtVal().(v any) string { |
| 161 | switch vv := v.(type) { |
| 162 | case float64: |
| 163 | return strconvscip-go gomod github.com/golang/go/src go1.18 strconv/.FormatFloatscip-go gomod github.com/golang/go/src go1.18 strconv/FormatFloat().(vv, 'f', -1, 64) |
| 164 | default: |
| 165 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("%v", vv) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func mapKeysscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/mapKeys().[M ~map[string]Vscip-go gomod github.com/BurntSushi/toml . `github.com/BurntSushi/toml/internal/toml-test`/V#, V any](m M) []string { |
| 170 | keys := make([]string, 0, len(m)) |
| 171 | for k := range m { |
| 172 | keys = append(keys, k) |
| 173 | } |
| 174 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(keys) |
| 175 | return keys |
| 176 | } |