Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 1 | package yaml_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 5 | "flag" |
| 6 | "fmt" |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 7 | "io" |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 8 | "io/ioutil" |
| 9 | "strconv" |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 10 | "strings" |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 11 | "testing" |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 12 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 13 | "cuelang.org/go/cue/ast" |
| 14 | "cuelang.org/go/cue/format" |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 15 | "cuelang.org/go/internal/third_party/yaml" |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 18 | var update = flag.Bool("update", false, "update test data") |
| 19 | |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 20 | var unmarshalIntTest = 123 |
| 21 | |
| 22 | var unmarshalTests = []struct { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 23 | data string |
| 24 | want string |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 25 | }{ |
| 26 | { |
| 27 | "", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 28 | "", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 29 | }, |
| 30 | { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 31 | "{}", |
| 32 | "", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 33 | }, { |
| 34 | "v: hi", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 35 | `v: "hi"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 36 | }, { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 37 | "v: hi", |
| 38 | `v: "hi"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 39 | }, { |
| 40 | "v: true", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 41 | "v: true", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 42 | }, { |
| 43 | "v: 10", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 44 | "v: 10", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 45 | }, { |
| 46 | "v: 0b10", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 47 | "v: 0b10", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 48 | }, { |
| 49 | "v: 0xA", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 50 | "v: 0xA", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 51 | }, { |
| 52 | "v: 4294967296", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 53 | "v: 4294967296", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 54 | }, { |
| 55 | "v: 0.1", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 56 | "v: 0.1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 57 | }, { |
| 58 | "v: .1", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 59 | "v: .1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 60 | }, { |
| 61 | "v: .Inf", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 62 | "v: +Inf", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 63 | }, { |
| 64 | "v: -.Inf", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 65 | "v: -Inf", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 66 | }, { |
| 67 | "v: -10", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 68 | "v: -10", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 69 | }, { |
| 70 | "v: -.1", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 71 | "v: -.1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 72 | }, |
| 73 | |
| 74 | // Simple values. |
| 75 | { |
| 76 | "123", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 77 | "123", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 78 | }, |
| 79 | |
| 80 | // Floats from spec |
| 81 | { |
| 82 | "canonical: 6.8523e+5", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 83 | "canonical: 6.8523e+5", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 84 | }, { |
| 85 | "expo: 685.230_15e+03", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 86 | "expo: 685.230_15e+03", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 87 | }, { |
| 88 | "fixed: 685_230.15", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 89 | "fixed: 685_230.15", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 90 | }, { |
| 91 | "neginf: -.inf", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 92 | "neginf: -Inf", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 93 | }, { |
| 94 | "fixed: 685_230.15", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 95 | "fixed: 685_230.15", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 96 | }, |
| 97 | //{"sexa: 190:20:30.15", map[string]interface{}{"sexa": 0}}, // Unsupported |
| 98 | //{"notanum: .NaN", map[string]interface{}{"notanum": math.NaN()}}, // Equality of NaN fails. |
| 99 | |
| 100 | // Bools from spec |
| 101 | { |
| 102 | "canonical: y", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 103 | "canonical: true", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 104 | }, { |
| 105 | "answer: NO", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 106 | "answer: false", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 107 | }, { |
| 108 | "logical: True", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 109 | "logical: true", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 110 | }, { |
| 111 | "option: on", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 112 | "option: true", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 113 | }, |
| 114 | // Ints from spec |
| 115 | { |
| 116 | "canonical: 685230", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 117 | "canonical: 685230", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 118 | }, { |
| 119 | "decimal: +685_230", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 120 | "decimal: +685_230", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 121 | }, { |
| 122 | "octal: 02472256", |
Marcel van Lohuizen | fc6303c | 2019-02-07 17:49:04 +0100 | [diff] [blame] | 123 | "octal: 0o2472256", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 124 | }, { |
| 125 | "hexa: 0x_0A_74_AE", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 126 | "hexa: 0x_0A_74_AE", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 127 | }, { |
| 128 | "bin: 0b1010_0111_0100_1010_1110", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 129 | "bin: 0b1010_0111_0100_1010_1110", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 130 | }, { |
| 131 | "bin: -0b101010", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 132 | "bin: -0b101010", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 133 | }, { |
| 134 | "bin: -0b1000000000000000000000000000000000000000000000000000000000000000", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 135 | "bin: -0b1000000000000000000000000000000000000000000000000000000000000000", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 136 | }, { |
| 137 | "decimal: +685_230", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 138 | "decimal: +685_230", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 139 | }, |
| 140 | |
| 141 | //{"sexa: 190:20:30", map[string]interface{}{"sexa": 0}}, // Unsupported |
| 142 | |
| 143 | // Nulls from spec |
| 144 | { |
| 145 | "empty:", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 146 | "empty: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 147 | }, { |
| 148 | "canonical: ~", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 149 | "canonical: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 150 | }, { |
| 151 | "english: null", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 152 | "english: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 153 | }, { |
| 154 | "~: null key", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 155 | `"~": "null key"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 156 | }, |
| 157 | |
| 158 | // Flow sequence |
| 159 | { |
| 160 | "seq: [A,B]", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 161 | `seq: ["A", "B"]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 162 | }, { |
| 163 | "seq: [A,B,C,]", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 164 | `seq: ["A", "B", "C"]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 165 | }, { |
| 166 | "seq: [A,1,C]", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 167 | `seq: ["A", 1, "C"]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 168 | }, |
| 169 | // Block sequence |
| 170 | { |
| 171 | "seq:\n - A\n - B", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 172 | `seq: [ |
| 173 | "A", |
| 174 | "B", |
| 175 | ]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 176 | }, { |
| 177 | "seq:\n - A\n - B\n - C", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 178 | `seq: [ |
| 179 | "A", |
| 180 | "B", |
| 181 | "C", |
| 182 | ]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 183 | }, { |
| 184 | "seq:\n - A\n - 1\n - C", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 185 | `seq: [ |
| 186 | "A", |
| 187 | 1, |
| 188 | "C", |
| 189 | ]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 190 | }, |
| 191 | |
| 192 | // Literal block scalar |
| 193 | { |
| 194 | "scalar: | # Comment\n\n literal\n\n \ttext\n\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 195 | `scalar: """ |
| 196 | |
| 197 | literal |
| 198 | |
| 199 | \ttext |
| 200 | |
| 201 | """`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 202 | }, |
| 203 | |
| 204 | // Folded block scalar |
| 205 | { |
| 206 | "scalar: > # Comment\n\n folded\n line\n \n next\n line\n * one\n * two\n\n last\n line\n\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 207 | `scalar: """ |
| 208 | |
| 209 | folded line |
| 210 | next line |
| 211 | * one |
| 212 | * two |
| 213 | |
| 214 | last line |
| 215 | |
| 216 | """`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 217 | }, |
| 218 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 219 | // Structs |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 220 | { |
| 221 | "a: {b: c}", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 222 | `a: {b: "c"}`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 223 | }, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 224 | { |
| 225 | "hello: world", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 226 | `hello: "world"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 227 | }, { |
| 228 | "a:", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 229 | "a: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 230 | }, { |
| 231 | "a: 1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 232 | "a: 1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 233 | }, { |
| 234 | "a: 1.0", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 235 | "a: 1.0", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 236 | }, { |
| 237 | "a: [1, 2]", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 238 | "a: [1, 2]", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 239 | }, { |
| 240 | "a: y", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 241 | "a: true", |
| 242 | }, { |
| 243 | "{ a: 1, b: {c: 1} }", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 244 | `a: 1, b: {c: 1}`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 245 | }, |
| 246 | |
| 247 | // Some cross type conversions |
| 248 | { |
| 249 | "v: 42", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 250 | "v: 42", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 251 | }, { |
| 252 | "v: -42", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 253 | "v: -42", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 254 | }, { |
| 255 | "v: 4294967296", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 256 | "v: 4294967296", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 257 | }, { |
| 258 | "v: -4294967296", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 259 | "v: -4294967296", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 260 | }, |
| 261 | |
| 262 | // int |
| 263 | { |
| 264 | "int_max: 2147483647", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 265 | "int_max: 2147483647", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 266 | }, |
| 267 | { |
| 268 | "int_min: -2147483648", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 269 | "int_min: -2147483648", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 270 | }, |
| 271 | { |
| 272 | "int_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 273 | "int_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 274 | }, |
| 275 | |
| 276 | // int64 |
| 277 | { |
| 278 | "int64_max: 9223372036854775807", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 279 | "int64_max: 9223372036854775807", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 280 | }, |
| 281 | { |
| 282 | "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 283 | "int64_max_base2: 0b111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 284 | }, |
| 285 | { |
| 286 | "int64_min: -9223372036854775808", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 287 | "int64_min: -9223372036854775808", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 288 | }, |
| 289 | { |
| 290 | "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 291 | "int64_neg_base2: -0b111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 292 | }, |
| 293 | { |
| 294 | "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 295 | "int64_overflow: 9223372036854775808", // math.MaxInt64 + 1 |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 296 | }, |
| 297 | |
| 298 | // uint |
| 299 | { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 300 | "uint_max: 4294967295", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 301 | "uint_max: 4294967295", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 302 | }, |
| 303 | |
| 304 | // uint64 |
| 305 | { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 306 | "uint64_max: 18446744073709551615", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 307 | "uint64_max: 18446744073709551615", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 308 | }, |
| 309 | { |
| 310 | "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 311 | "uint64_max_base2: 0b1111111111111111111111111111111111111111111111111111111111111111", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 312 | }, |
| 313 | { |
| 314 | "uint64_maxint64: 9223372036854775807", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 315 | "uint64_maxint64: 9223372036854775807", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 316 | }, |
| 317 | |
| 318 | // float32 |
| 319 | { |
| 320 | "float32_max: 3.40282346638528859811704183484516925440e+38", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 321 | "float32_max: 3.40282346638528859811704183484516925440e+38", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 322 | }, |
| 323 | { |
| 324 | "float32_nonzero: 1.401298464324817070923729583289916131280e-45", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 325 | "float32_nonzero: 1.401298464324817070923729583289916131280e-45", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 326 | }, |
| 327 | { |
| 328 | "float32_maxuint64: 18446744073709551615", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 329 | "float32_maxuint64: 18446744073709551615", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 330 | }, |
| 331 | { |
| 332 | "float32_maxuint64+1: 18446744073709551616", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 333 | `"float32_maxuint64+1": 18446744073709551616`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 334 | }, |
| 335 | |
| 336 | // float64 |
| 337 | { |
| 338 | "float64_max: 1.797693134862315708145274237317043567981e+308", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 339 | "float64_max: 1.797693134862315708145274237317043567981e+308", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 340 | }, |
| 341 | { |
| 342 | "float64_nonzero: 4.940656458412465441765687928682213723651e-324", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 343 | "float64_nonzero: 4.940656458412465441765687928682213723651e-324", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 344 | }, |
| 345 | { |
| 346 | "float64_maxuint64: 18446744073709551615", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 347 | "float64_maxuint64: 18446744073709551615", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 348 | }, |
| 349 | { |
| 350 | "float64_maxuint64+1: 18446744073709551616", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 351 | `"float64_maxuint64+1": 18446744073709551616`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 352 | }, |
| 353 | |
| 354 | // Overflow cases. |
| 355 | { |
| 356 | "v: 4294967297", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 357 | "v: 4294967297", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 358 | }, { |
| 359 | "v: 128", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 360 | "v: 128", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 361 | }, |
| 362 | |
| 363 | // Quoted values. |
| 364 | { |
| 365 | "'1': '\"2\"'", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 366 | `"1": "\"2\""`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 367 | }, { |
| 368 | "v:\n- A\n- 'B\n\n C'\n", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 369 | `v: [ |
| 370 | "A", |
| 371 | """ |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 372 | B |
| 373 | C |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 374 | """, |
| 375 | ]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 376 | }, |
| 377 | |
| 378 | // Explicit tags. |
| 379 | { |
| 380 | "v: !!float '1.1'", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 381 | "v: 1.1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 382 | }, { |
| 383 | "v: !!float 0", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 384 | "v: float & 0", // Should this be 0.0? |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 385 | }, { |
| 386 | "v: !!float -1", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 387 | "v: float & -1", // Should this be -1.0? |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 388 | }, { |
| 389 | "v: !!null ''", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 390 | "v: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 391 | }, { |
| 392 | "%TAG !y! tag:yaml.org,2002:\n---\nv: !y!int '1'", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 393 | "v: 1", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 394 | }, |
| 395 | |
| 396 | // Non-specific tag (Issue #75) |
| 397 | { |
| 398 | "v: ! test", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 399 | // TODO: map[string]interface{}{"v": "test"}, |
| 400 | "", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 401 | }, |
| 402 | |
| 403 | // Anchors and aliases. |
| 404 | { |
| 405 | "a: &x 1\nb: &y 2\nc: *x\nd: *y\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 406 | `a: 1 |
| 407 | b: 2 |
| 408 | c: 1 |
| 409 | d: 2`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 410 | }, { |
| 411 | "a: &a {c: 1}\nb: *a", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 412 | `a: {c: 1} |
Marcel van Lohuizen | 4a8c4ca | 2019-05-31 17:32:35 +0200 | [diff] [blame] | 413 | b: { |
Marcel van Lohuizen | 9af9a90 | 2019-09-07 20:30:10 +0200 | [diff] [blame] | 414 | c: 1 |
| 415 | }`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 416 | }, { |
| 417 | "a: &a [1, 2]\nb: *a", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 418 | "a: [1, 2]\nb: [1, 2]", // TODO: a: [1, 2], b: a |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 419 | }, |
| 420 | |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 421 | { |
| 422 | "foo: ''", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 423 | `foo: ""`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 424 | }, { |
| 425 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 426 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 427 | }, |
| 428 | |
| 429 | // Support for ~ |
| 430 | { |
| 431 | "foo: ~", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 432 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 433 | }, |
| 434 | |
| 435 | // Bug #1191981 |
| 436 | { |
| 437 | "" + |
| 438 | "%YAML 1.1\n" + |
| 439 | "--- !!str\n" + |
| 440 | `"Generic line break (no glyph)\n\` + "\n" + |
| 441 | ` Generic line break (glyphed)\n\` + "\n" + |
| 442 | ` Line separator\u2028\` + "\n" + |
| 443 | ` Paragraph separator\u2029"` + "\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 444 | `""" |
| 445 | Generic line break (no glyph) |
| 446 | Generic line break (glyphed) |
| 447 | Line separator\u2028Paragraph separator\u2029 |
| 448 | """`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 449 | }, |
| 450 | |
| 451 | // bug 1243827 |
| 452 | { |
| 453 | "a: -b_c", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 454 | `a: "-b_c"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 455 | }, |
| 456 | { |
| 457 | "a: +b_c", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 458 | `a: "+b_c"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 459 | }, |
| 460 | { |
| 461 | "a: 50cent_of_dollar", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 462 | `a: "50cent_of_dollar"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 463 | }, |
| 464 | |
| 465 | // issue #295 (allow scalars with colons in flow mappings and sequences) |
| 466 | { |
| 467 | "a: {b: https://github.com/go-yaml/yaml}", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 468 | `a: {b: "https://github.com/go-yaml/yaml"}`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 469 | }, |
| 470 | { |
| 471 | "a: [https://github.com/go-yaml/yaml]", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 472 | `a: ["https://github.com/go-yaml/yaml"]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 473 | }, |
| 474 | |
| 475 | // Duration |
| 476 | { |
| 477 | "a: 3s", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 478 | `a: "3s"`, // for now |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 479 | }, |
| 480 | |
| 481 | // Issue #24. |
| 482 | { |
| 483 | "a: <foo>", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 484 | `a: "<foo>"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 485 | }, |
| 486 | |
| 487 | // Base 60 floats are obsolete and unsupported. |
| 488 | { |
| 489 | "a: 1:1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 490 | `a: "1:1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 491 | }, |
| 492 | |
| 493 | // Binary data. |
| 494 | { |
| 495 | "a: !!binary gIGC\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 496 | `a: '\x80\x81\x82'`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 497 | }, { |
| 498 | "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 499 | "a: '" + strings.Repeat(`\x90`, 54) + "'", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 500 | }, { |
| 501 | "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 502 | "a: '" + strings.Repeat(`\x00`, 52) + "'", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 503 | }, |
| 504 | |
| 505 | // Ordered maps. |
| 506 | { |
| 507 | "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 508 | `b: 2, a: 1, d: 4, c: 3, sub: {e: 5}`, |
| 509 | }, |
| 510 | |
| 511 | // Spacing |
| 512 | { |
| 513 | ` |
| 514 | a: {} |
| 515 | c: 1 |
| 516 | d: [ |
| 517 | ] |
| 518 | e: [] |
| 519 | `, |
| 520 | `a: {} |
| 521 | c: 1 |
| 522 | d: [ |
| 523 | ] |
| 524 | e: []`, |
| 525 | }, |
| 526 | |
| 527 | { |
| 528 | ` |
| 529 | a: |
| 530 | - { "a": 1, "b": 2 } |
| 531 | - { "c": 1, "d": 2 } |
| 532 | `, |
| 533 | `a: [{ |
| 534 | a: 1, b: 2 |
| 535 | }, { |
| 536 | c: 1, d: 2 |
| 537 | }]`, |
| 538 | }, |
| 539 | |
| 540 | { |
| 541 | "a:\n b:\n c: d\n e: f\n", |
| 542 | `a: { |
| 543 | b: { |
| 544 | c: "d" |
| 545 | e: "f" |
| 546 | } |
Marcel van Lohuizen | 4a8c4ca | 2019-05-31 17:32:35 +0200 | [diff] [blame] | 547 | }`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 548 | }, |
| 549 | |
| 550 | // Issue #39. |
| 551 | { |
| 552 | "a:\n b:\n c: d\n", |
Marcel van Lohuizen | 4a8c4ca | 2019-05-31 17:32:35 +0200 | [diff] [blame] | 553 | `a: { |
| 554 | b: { |
| 555 | c: "d" |
| 556 | } |
| 557 | }`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 558 | }, |
| 559 | |
| 560 | // Timestamps |
| 561 | { |
| 562 | // Date only. |
| 563 | "a: 2015-01-01\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 564 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 565 | }, |
| 566 | { |
| 567 | // RFC3339 |
| 568 | "a: 2015-02-24T18:19:39.12Z\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 569 | `a: "2015-02-24T18:19:39.12Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 570 | }, |
| 571 | { |
| 572 | // RFC3339 with short dates. |
| 573 | "a: 2015-2-3T3:4:5Z", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 574 | `a: "2015-2-3T3:4:5Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 575 | }, |
| 576 | { |
| 577 | // ISO8601 lower case t |
| 578 | "a: 2015-02-24t18:19:39Z\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 579 | `a: "2015-02-24t18:19:39Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 580 | }, |
| 581 | { |
| 582 | // space separate, no time zone |
| 583 | "a: 2015-02-24 18:19:39\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 584 | `a: "2015-02-24 18:19:39"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 585 | }, |
| 586 | // Some cases not currently handled. Uncomment these when |
| 587 | // the code is fixed. |
| 588 | // { |
| 589 | // // space separated with time zone |
| 590 | // "a: 2001-12-14 21:59:43.10 -5", |
| 591 | // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 592 | // }, |
| 593 | // { |
| 594 | // // arbitrary whitespace between fields |
| 595 | // "a: 2001-12-14 \t\t \t21:59:43.10 \t Z", |
| 596 | // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 597 | // }, |
| 598 | { |
| 599 | // explicit string tag |
| 600 | "a: !!str 2015-01-01", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 601 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 602 | }, |
| 603 | { |
| 604 | // explicit timestamp tag on quoted string |
| 605 | "a: !!timestamp \"2015-01-01\"", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 606 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 607 | }, |
| 608 | { |
| 609 | // explicit timestamp tag on unquoted string |
| 610 | "a: !!timestamp 2015-01-01", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 611 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 612 | }, |
| 613 | { |
| 614 | // quoted string that's a valid timestamp |
| 615 | "a: \"2015-01-01\"", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 616 | "a: \"2015-01-01\"", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 617 | }, |
| 618 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 619 | // Empty list |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 620 | { |
| 621 | "a: []", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 622 | "a: []", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 623 | }, |
| 624 | |
| 625 | // UTF-16-LE |
| 626 | { |
| 627 | "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n\x00", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 628 | `ñoño: "very yes"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 629 | }, |
| 630 | // UTF-16-LE with surrogate. |
| 631 | { |
| 632 | "\xff\xfe\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \x00=\xd8\xd4\xdf\n\x00", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 633 | `ñoño: "very yes 🟔"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 634 | }, |
| 635 | |
| 636 | // UTF-16-BE |
| 637 | { |
| 638 | "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 639 | `ñoño: "very yes"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 640 | }, |
| 641 | // UTF-16-BE with surrogate. |
| 642 | { |
| 643 | "\xfe\xff\x00\xf1\x00o\x00\xf1\x00o\x00:\x00 \x00v\x00e\x00r\x00y\x00 \x00y\x00e\x00s\x00 \xd8=\xdf\xd4\x00\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 644 | `ñoño: "very yes 🟔"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 645 | }, |
| 646 | |
| 647 | // YAML Float regex shouldn't match this |
| 648 | { |
| 649 | "a: 123456e1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 650 | `a: "123456e1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 651 | }, { |
| 652 | "a: 123456E1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 653 | `a: "123456E1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 654 | }, |
| 655 | // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes |
| 656 | { |
| 657 | "First occurrence: &anchor Foo\nSecond occurrence: *anchor\nOverride anchor: &anchor Bar\nReuse anchor: *anchor\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 658 | `"First occurrence": "Foo" |
| 659 | "Second occurrence": "Foo" |
| 660 | "Override anchor": "Bar" |
| 661 | "Reuse anchor": "Bar"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 662 | }, |
| 663 | // Single document with garbage following it. |
| 664 | { |
| 665 | "---\nhello\n...\n}not yaml", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 666 | `"hello"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 667 | }, |
| 668 | } |
| 669 | |
| 670 | type M map[interface{}]interface{} |
| 671 | |
| 672 | type inlineB struct { |
| 673 | B int |
| 674 | inlineC `yaml:",inline"` |
| 675 | } |
| 676 | |
| 677 | type inlineC struct { |
| 678 | C int |
| 679 | } |
| 680 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 681 | func cueStr(node ast.Node) string { |
| 682 | if s, ok := node.(*ast.StructLit); ok { |
| 683 | node = &ast.File{ |
| 684 | Decls: s.Elts, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 685 | } |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 686 | } |
Marcel van Lohuizen | 4954491 | 2019-06-26 13:16:48 +0200 | [diff] [blame] | 687 | b, _ := format.Node(node) |
| 688 | return strings.TrimSpace(string(b)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | func newDecoder(t *testing.T, data string) *yaml.Decoder { |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 692 | dec, err := yaml.NewDecoder("test.yaml", strings.NewReader(data)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 693 | if err != nil { |
| 694 | t.Fatal(err) |
| 695 | } |
| 696 | return dec |
| 697 | } |
| 698 | |
| 699 | func callUnmarshal(t *testing.T, data string) (ast.Expr, error) { |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 700 | return yaml.Unmarshal("test.yaml", []byte(data)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | func TestUnmarshal(t *testing.T) { |
| 704 | for i, item := range unmarshalTests { |
| 705 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 706 | t.Logf("test %d: %q", i, item.data) |
| 707 | expr, err := callUnmarshal(t, item.data) |
| 708 | if _, ok := err.(*yaml.TypeError); !ok && err != nil { |
| 709 | t.Fatal("expected error to be nil") |
| 710 | } |
| 711 | if got := cueStr(expr); got != item.want { |
| 712 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 713 | } |
| 714 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 718 | // // TODO(v3): This test should also work when unmarshaling onto an interface{}. |
| 719 | // func (s *S) TestUnmarshalFullTimestamp(c *C) { |
| 720 | // // Full timestamp in same format as encoded. This is confirmed to be |
| 721 | // // properly decoded by Python as a timestamp as well. |
| 722 | // var str = "2015-02-24T18:19:39.123456789-03:00" |
| 723 | // expr, err := yaml.Unmarshal([]byte(str)) |
| 724 | // c.Assert(err, IsNil) |
| 725 | // c.Assert(t, Equals, time.Date(2015, 2, 24, 18, 19, 39, 123456789, t.Location())) |
| 726 | // c.Assert(t.In(time.UTC), Equals, time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC)) |
| 727 | // } |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 728 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 729 | func TestDecoderSingleDocument(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 730 | // Test that Decoder.Decode works as expected on |
| 731 | // all the unmarshal tests. |
| 732 | for i, item := range unmarshalTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 733 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 734 | if item.data == "" { |
| 735 | // Behaviour differs when there's no YAML. |
| 736 | return |
| 737 | } |
| 738 | expr, err := newDecoder(t, item.data).Decode() |
| 739 | if _, ok := err.(*yaml.TypeError); !ok && err != nil { |
| 740 | t.Errorf("err should be nil, was %v", err) |
| 741 | } |
| 742 | if got := cueStr(expr); got != item.want { |
| 743 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 744 | } |
| 745 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | var decoderTests = []struct { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 750 | data string |
| 751 | want string |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 752 | }{{ |
| 753 | "", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 754 | "", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 755 | }, { |
| 756 | "a: b", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 757 | `a: "b"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 758 | }, { |
| 759 | "---\na: b\n...\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 760 | `a: "b"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 761 | }, { |
| 762 | "---\n'hello'\n...\n---\ngoodbye\n...\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 763 | `"hello"` + "\n" + `"goodbye"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 764 | }} |
| 765 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 766 | func TestDecoder(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 767 | for i, item := range decoderTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 768 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 769 | var values []string |
| 770 | dec := newDecoder(t, item.data) |
| 771 | for { |
| 772 | expr, err := dec.Decode() |
| 773 | if err == io.EOF { |
| 774 | break |
| 775 | } |
| 776 | if err != nil { |
| 777 | t.Errorf("err should be nil, was %v", err) |
| 778 | } |
| 779 | values = append(values, cueStr(expr)) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 780 | } |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 781 | got := strings.Join(values, "\n") |
| 782 | if got != item.want { |
| 783 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 784 | } |
| 785 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
| 789 | type errReader struct{} |
| 790 | |
| 791 | func (errReader) Read([]byte) (int, error) { |
| 792 | return 0, errors.New("some read error") |
| 793 | } |
| 794 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 795 | func TestUnmarshalNaN(t *testing.T) { |
| 796 | expr, err := callUnmarshal(t, "notanum: .NaN") |
| 797 | if err != nil { |
| 798 | t.Fatal("unexpected error", err) |
| 799 | } |
| 800 | got := cueStr(expr) |
| 801 | want := "notanum: NaN" |
| 802 | if got != want { |
| 803 | t.Errorf("got %v; want %v", got, want) |
| 804 | } |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | var unmarshalErrorTests = []struct { |
| 808 | data, error string |
| 809 | }{ |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 810 | {"\nv: !!float 'error'", "test.yaml:2: cannot decode !!str `error` as a !!float"}, |
| 811 | {"v: [A,", "test.yaml:1: did not find expected node content"}, |
| 812 | {"v:\n- [A,", "test.yaml:2: did not find expected node content"}, |
| 813 | {"a:\n- b: *,", "test.yaml:2: did not find expected alphabetic or numeric character"}, |
| 814 | {"a: *b\n", "test.yaml:1: unknown anchor 'b' referenced"}, |
| 815 | {"a: &a\n b: *a\n", "test.yaml:2: anchor 'a' value contains itself"}, |
| 816 | {"value: -", "test.yaml:1: block sequence entries are not allowed in this context"}, |
| 817 | {"a: !!binary ==", "test.yaml:1: !!binary value contains invalid base64 data"}, |
| 818 | {"{[.]}", `test.yaml:1: invalid map key: sequence`}, |
| 819 | {"{{.}}", `test.yaml:1: invalid map key: map`}, |
| 820 | {"b: *a\na: &a {c: 1}", `test.yaml:1: unknown anchor 'a' referenced`}, |
| 821 | {"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "test.yaml:1: did not find expected whitespace"}, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 824 | func TestUnmarshalErrors(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 825 | for i, item := range unmarshalErrorTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 826 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 827 | expr, err := callUnmarshal(t, item.data) |
| 828 | val := "" |
| 829 | if expr != nil { |
| 830 | val = cueStr(expr) |
| 831 | } |
| 832 | if err == nil || err.Error() != item.error { |
| 833 | t.Errorf("got %v; want %v; (value %v)", err, item.error, val) |
| 834 | } |
| 835 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 839 | func TestDecoderErrors(t *testing.T) { |
| 840 | for i, item := range unmarshalErrorTests { |
| 841 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 842 | _, err := newDecoder(t, item.data).Decode() |
| 843 | if err == nil || err.Error() != item.error { |
| 844 | t.Errorf("got %v; want %v", err, item.error) |
| 845 | } |
| 846 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 850 | func TestFiles(t *testing.T) { |
| 851 | files := []string{"merge"} |
| 852 | for _, test := range files { |
| 853 | t.Run(test, func(t *testing.T) { |
| 854 | testname := fmt.Sprintf("testdata/%s.test", test) |
| 855 | filename := fmt.Sprintf("testdata/%s.out", test) |
| 856 | mergeTests, err := ioutil.ReadFile(testname) |
| 857 | if err != nil { |
| 858 | t.Fatal(err) |
| 859 | } |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 860 | expr, err := yaml.Unmarshal("test.yaml", mergeTests) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 861 | if err != nil { |
| 862 | t.Fatal(err) |
| 863 | } |
| 864 | got := cueStr(expr) |
| 865 | if *update { |
| 866 | ioutil.WriteFile(filename, []byte(got), 0644) |
| 867 | return |
| 868 | } |
| 869 | b, err := ioutil.ReadFile(filename) |
| 870 | if err != nil { |
| 871 | t.Fatal(err) |
| 872 | } |
| 873 | if want := string(b); got != want { |
| 874 | t.Errorf("\n got: %v;\nwant: %v", got, want) |
| 875 | } |
| 876 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 880 | func TestFuzzCrashers(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 881 | cases := []string{ |
| 882 | // runtime error: index out of range |
| 883 | "\"\\0\\\r\n", |
| 884 | |
| 885 | // should not happen |
| 886 | " 0: [\n] 0", |
| 887 | "? ? \"\n\" 0", |
| 888 | " - {\n000}0", |
| 889 | "0:\n 0: [0\n] 0", |
| 890 | " - \"\n000\"0", |
| 891 | " - \"\n000\"\"", |
| 892 | "0:\n - {\n000}0", |
| 893 | "0:\n - \"\n000\"0", |
| 894 | "0:\n - \"\n000\"\"", |
| 895 | |
| 896 | // runtime error: index out of range |
| 897 | " \ufeff\n", |
| 898 | "? \ufeff\n", |
| 899 | "? \ufeff:\n", |
| 900 | "0: \ufeff\n", |
| 901 | "? \ufeff: \ufeff\n", |
| 902 | } |
| 903 | for _, data := range cases { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 904 | _, _ = callUnmarshal(t, data) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
| 908 | //var data []byte |
| 909 | //func init() { |
| 910 | // var err error |
| 911 | // data, err = ioutil.ReadFile("/tmp/file.yaml") |
| 912 | // if err != nil { |
| 913 | // panic(err) |
| 914 | // } |
| 915 | //} |
| 916 | // |
| 917 | //func (s *S) BenchmarkUnmarshal(c *C) { |
| 918 | // var err error |
| 919 | // for i := 0; i < c.N; i++ { |
| 920 | // var v map[string]interface{} |
| 921 | // err = yaml.Unmarshal(data, &v) |
| 922 | // } |
| 923 | // if err != nil { |
| 924 | // panic(err) |
| 925 | // } |
| 926 | //} |
| 927 | // |
| 928 | //func (s *S) BenchmarkMarshal(c *C) { |
| 929 | // var v map[string]interface{} |
| 930 | // yaml.Unmarshal(data, &v) |
| 931 | // c.ResetTimer() |
| 932 | // for i := 0; i < c.N; i++ { |
| 933 | // yaml.Marshal(&v) |
| 934 | // } |
| 935 | //} |