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 | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 414 | c: 1}`, // TODO fix this spacing. Expansions low priority though. |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 415 | }, { |
| 416 | "a: &a [1, 2]\nb: *a", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 417 | "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] | 418 | }, |
| 419 | |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 420 | { |
| 421 | "foo: ''", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 422 | `foo: ""`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 423 | }, { |
| 424 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 425 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 426 | }, |
| 427 | |
| 428 | // Support for ~ |
| 429 | { |
| 430 | "foo: ~", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 431 | "foo: null", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 432 | }, |
| 433 | |
| 434 | // Bug #1191981 |
| 435 | { |
| 436 | "" + |
| 437 | "%YAML 1.1\n" + |
| 438 | "--- !!str\n" + |
| 439 | `"Generic line break (no glyph)\n\` + "\n" + |
| 440 | ` Generic line break (glyphed)\n\` + "\n" + |
| 441 | ` Line separator\u2028\` + "\n" + |
| 442 | ` Paragraph separator\u2029"` + "\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 443 | `""" |
| 444 | Generic line break (no glyph) |
| 445 | Generic line break (glyphed) |
| 446 | Line separator\u2028Paragraph separator\u2029 |
| 447 | """`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 448 | }, |
| 449 | |
| 450 | // bug 1243827 |
| 451 | { |
| 452 | "a: -b_c", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 453 | `a: "-b_c"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 454 | }, |
| 455 | { |
| 456 | "a: +b_c", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 457 | `a: "+b_c"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 458 | }, |
| 459 | { |
| 460 | "a: 50cent_of_dollar", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 461 | `a: "50cent_of_dollar"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 462 | }, |
| 463 | |
| 464 | // issue #295 (allow scalars with colons in flow mappings and sequences) |
| 465 | { |
| 466 | "a: {b: https://github.com/go-yaml/yaml}", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 467 | `a: {b: "https://github.com/go-yaml/yaml"}`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 468 | }, |
| 469 | { |
| 470 | "a: [https://github.com/go-yaml/yaml]", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 471 | `a: ["https://github.com/go-yaml/yaml"]`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 472 | }, |
| 473 | |
| 474 | // Duration |
| 475 | { |
| 476 | "a: 3s", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 477 | `a: "3s"`, // for now |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 478 | }, |
| 479 | |
| 480 | // Issue #24. |
| 481 | { |
| 482 | "a: <foo>", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 483 | `a: "<foo>"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 484 | }, |
| 485 | |
| 486 | // Base 60 floats are obsolete and unsupported. |
| 487 | { |
| 488 | "a: 1:1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 489 | `a: "1:1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 490 | }, |
| 491 | |
| 492 | // Binary data. |
| 493 | { |
| 494 | "a: !!binary gIGC\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 495 | `a: '\x80\x81\x82'`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 496 | }, { |
| 497 | "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 498 | "a: '" + strings.Repeat(`\x90`, 54) + "'", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 499 | }, { |
| 500 | "a: !!binary |\n " + strings.Repeat("A", 70) + "\n ==\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 501 | "a: '" + strings.Repeat(`\x00`, 52) + "'", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 502 | }, |
| 503 | |
| 504 | // Ordered maps. |
| 505 | { |
| 506 | "{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}", |
Marcel van Lohuizen | 4697b78 | 2019-07-12 22:19:43 +0200 | [diff] [blame] | 507 | `b: 2, a: 1, d: 4, c: 3, sub: {e: 5}`, |
| 508 | }, |
| 509 | |
| 510 | // Spacing |
| 511 | { |
| 512 | ` |
| 513 | a: {} |
| 514 | c: 1 |
| 515 | d: [ |
| 516 | ] |
| 517 | e: [] |
| 518 | `, |
| 519 | `a: {} |
| 520 | c: 1 |
| 521 | d: [ |
| 522 | ] |
| 523 | e: []`, |
| 524 | }, |
| 525 | |
| 526 | { |
| 527 | ` |
| 528 | a: |
| 529 | - { "a": 1, "b": 2 } |
| 530 | - { "c": 1, "d": 2 } |
| 531 | `, |
| 532 | `a: [{ |
| 533 | a: 1, b: 2 |
| 534 | }, { |
| 535 | c: 1, d: 2 |
| 536 | }]`, |
| 537 | }, |
| 538 | |
| 539 | { |
| 540 | "a:\n b:\n c: d\n e: f\n", |
| 541 | `a: { |
| 542 | b: { |
| 543 | c: "d" |
| 544 | e: "f" |
| 545 | } |
Marcel van Lohuizen | 4a8c4ca | 2019-05-31 17:32:35 +0200 | [diff] [blame] | 546 | }`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 547 | }, |
| 548 | |
| 549 | // Issue #39. |
| 550 | { |
| 551 | "a:\n b:\n c: d\n", |
Marcel van Lohuizen | 4a8c4ca | 2019-05-31 17:32:35 +0200 | [diff] [blame] | 552 | `a: { |
| 553 | b: { |
| 554 | c: "d" |
| 555 | } |
| 556 | }`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 557 | }, |
| 558 | |
| 559 | // Timestamps |
| 560 | { |
| 561 | // Date only. |
| 562 | "a: 2015-01-01\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 563 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 564 | }, |
| 565 | { |
| 566 | // RFC3339 |
| 567 | "a: 2015-02-24T18:19:39.12Z\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 568 | `a: "2015-02-24T18:19:39.12Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 569 | }, |
| 570 | { |
| 571 | // RFC3339 with short dates. |
| 572 | "a: 2015-2-3T3:4:5Z", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 573 | `a: "2015-2-3T3:4:5Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 574 | }, |
| 575 | { |
| 576 | // ISO8601 lower case t |
| 577 | "a: 2015-02-24t18:19:39Z\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 578 | `a: "2015-02-24t18:19:39Z"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 579 | }, |
| 580 | { |
| 581 | // space separate, no time zone |
| 582 | "a: 2015-02-24 18:19:39\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 583 | `a: "2015-02-24 18:19:39"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 584 | }, |
| 585 | // Some cases not currently handled. Uncomment these when |
| 586 | // the code is fixed. |
| 587 | // { |
| 588 | // // space separated with time zone |
| 589 | // "a: 2001-12-14 21:59:43.10 -5", |
| 590 | // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 591 | // }, |
| 592 | // { |
| 593 | // // arbitrary whitespace between fields |
| 594 | // "a: 2001-12-14 \t\t \t21:59:43.10 \t Z", |
| 595 | // map[string]interface{}{"a": time.Date(2001, 12, 14, 21, 59, 43, .1e9, time.UTC)}, |
| 596 | // }, |
| 597 | { |
| 598 | // explicit string tag |
| 599 | "a: !!str 2015-01-01", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 600 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 601 | }, |
| 602 | { |
| 603 | // explicit timestamp tag on quoted string |
| 604 | "a: !!timestamp \"2015-01-01\"", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 605 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 606 | }, |
| 607 | { |
| 608 | // explicit timestamp tag on unquoted string |
| 609 | "a: !!timestamp 2015-01-01", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 610 | `a: "2015-01-01"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 611 | }, |
| 612 | { |
| 613 | // quoted string that's a valid timestamp |
| 614 | "a: \"2015-01-01\"", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 615 | "a: \"2015-01-01\"", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 616 | }, |
| 617 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 618 | // Empty list |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 619 | { |
| 620 | "a: []", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 621 | "a: []", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 622 | }, |
| 623 | |
| 624 | // UTF-16-LE |
| 625 | { |
| 626 | "\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] | 627 | `ñoño: "very yes"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 628 | }, |
| 629 | // UTF-16-LE with surrogate. |
| 630 | { |
| 631 | "\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] | 632 | `ñoño: "very yes 🟔"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 633 | }, |
| 634 | |
| 635 | // UTF-16-BE |
| 636 | { |
| 637 | "\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] | 638 | `ñoño: "very yes"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 639 | }, |
| 640 | // UTF-16-BE with surrogate. |
| 641 | { |
| 642 | "\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] | 643 | `ñoño: "very yes 🟔"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 644 | }, |
| 645 | |
| 646 | // YAML Float regex shouldn't match this |
| 647 | { |
| 648 | "a: 123456e1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 649 | `a: "123456e1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 650 | }, { |
| 651 | "a: 123456E1\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 652 | `a: "123456E1"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 653 | }, |
| 654 | // yaml-test-suite 3GZX: Spec Example 7.1. Alias Nodes |
| 655 | { |
| 656 | "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] | 657 | `"First occurrence": "Foo" |
| 658 | "Second occurrence": "Foo" |
| 659 | "Override anchor": "Bar" |
| 660 | "Reuse anchor": "Bar"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 661 | }, |
| 662 | // Single document with garbage following it. |
| 663 | { |
| 664 | "---\nhello\n...\n}not yaml", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 665 | `"hello"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 666 | }, |
| 667 | } |
| 668 | |
| 669 | type M map[interface{}]interface{} |
| 670 | |
| 671 | type inlineB struct { |
| 672 | B int |
| 673 | inlineC `yaml:",inline"` |
| 674 | } |
| 675 | |
| 676 | type inlineC struct { |
| 677 | C int |
| 678 | } |
| 679 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 680 | func cueStr(node ast.Node) string { |
| 681 | if s, ok := node.(*ast.StructLit); ok { |
| 682 | node = &ast.File{ |
| 683 | Decls: s.Elts, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 684 | } |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 685 | } |
Marcel van Lohuizen | 4954491 | 2019-06-26 13:16:48 +0200 | [diff] [blame] | 686 | b, _ := format.Node(node) |
| 687 | return strings.TrimSpace(string(b)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | func newDecoder(t *testing.T, data string) *yaml.Decoder { |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 691 | dec, err := yaml.NewDecoder("test.yaml", strings.NewReader(data)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 692 | if err != nil { |
| 693 | t.Fatal(err) |
| 694 | } |
| 695 | return dec |
| 696 | } |
| 697 | |
| 698 | func callUnmarshal(t *testing.T, data string) (ast.Expr, error) { |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 699 | return yaml.Unmarshal("test.yaml", []byte(data)) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | func TestUnmarshal(t *testing.T) { |
| 703 | for i, item := range unmarshalTests { |
| 704 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 705 | t.Logf("test %d: %q", i, item.data) |
| 706 | expr, err := callUnmarshal(t, item.data) |
| 707 | if _, ok := err.(*yaml.TypeError); !ok && err != nil { |
| 708 | t.Fatal("expected error to be nil") |
| 709 | } |
| 710 | if got := cueStr(expr); got != item.want { |
| 711 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 712 | } |
| 713 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 717 | // // TODO(v3): This test should also work when unmarshaling onto an interface{}. |
| 718 | // func (s *S) TestUnmarshalFullTimestamp(c *C) { |
| 719 | // // Full timestamp in same format as encoded. This is confirmed to be |
| 720 | // // properly decoded by Python as a timestamp as well. |
| 721 | // var str = "2015-02-24T18:19:39.123456789-03:00" |
| 722 | // expr, err := yaml.Unmarshal([]byte(str)) |
| 723 | // c.Assert(err, IsNil) |
| 724 | // c.Assert(t, Equals, time.Date(2015, 2, 24, 18, 19, 39, 123456789, t.Location())) |
| 725 | // c.Assert(t.In(time.UTC), Equals, time.Date(2015, 2, 24, 21, 19, 39, 123456789, time.UTC)) |
| 726 | // } |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 727 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 728 | func TestDecoderSingleDocument(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 729 | // Test that Decoder.Decode works as expected on |
| 730 | // all the unmarshal tests. |
| 731 | for i, item := range unmarshalTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 732 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 733 | if item.data == "" { |
| 734 | // Behaviour differs when there's no YAML. |
| 735 | return |
| 736 | } |
| 737 | expr, err := newDecoder(t, item.data).Decode() |
| 738 | if _, ok := err.(*yaml.TypeError); !ok && err != nil { |
| 739 | t.Errorf("err should be nil, was %v", err) |
| 740 | } |
| 741 | if got := cueStr(expr); got != item.want { |
| 742 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 743 | } |
| 744 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
| 748 | var decoderTests = []struct { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 749 | data string |
| 750 | want string |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 751 | }{{ |
| 752 | "", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 753 | "", |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 754 | }, { |
| 755 | "a: b", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 756 | `a: "b"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 757 | }, { |
| 758 | "---\na: b\n...\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 759 | `a: "b"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 760 | }, { |
| 761 | "---\n'hello'\n...\n---\ngoodbye\n...\n", |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 762 | `"hello"` + "\n" + `"goodbye"`, |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 763 | }} |
| 764 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 765 | func TestDecoder(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 766 | for i, item := range decoderTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 767 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 768 | var values []string |
| 769 | dec := newDecoder(t, item.data) |
| 770 | for { |
| 771 | expr, err := dec.Decode() |
| 772 | if err == io.EOF { |
| 773 | break |
| 774 | } |
| 775 | if err != nil { |
| 776 | t.Errorf("err should be nil, was %v", err) |
| 777 | } |
| 778 | values = append(values, cueStr(expr)) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 779 | } |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 780 | got := strings.Join(values, "\n") |
| 781 | if got != item.want { |
| 782 | t.Errorf("\n got: %v;\nwant: %v", got, item.want) |
| 783 | } |
| 784 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | type errReader struct{} |
| 789 | |
| 790 | func (errReader) Read([]byte) (int, error) { |
| 791 | return 0, errors.New("some read error") |
| 792 | } |
| 793 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 794 | func TestUnmarshalNaN(t *testing.T) { |
| 795 | expr, err := callUnmarshal(t, "notanum: .NaN") |
| 796 | if err != nil { |
| 797 | t.Fatal("unexpected error", err) |
| 798 | } |
| 799 | got := cueStr(expr) |
| 800 | want := "notanum: NaN" |
| 801 | if got != want { |
| 802 | t.Errorf("got %v; want %v", got, want) |
| 803 | } |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | var unmarshalErrorTests = []struct { |
| 807 | data, error string |
| 808 | }{ |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 809 | {"\nv: !!float 'error'", "test.yaml:2: cannot decode !!str `error` as a !!float"}, |
| 810 | {"v: [A,", "test.yaml:1: did not find expected node content"}, |
| 811 | {"v:\n- [A,", "test.yaml:2: did not find expected node content"}, |
| 812 | {"a:\n- b: *,", "test.yaml:2: did not find expected alphabetic or numeric character"}, |
| 813 | {"a: *b\n", "test.yaml:1: unknown anchor 'b' referenced"}, |
| 814 | {"a: &a\n b: *a\n", "test.yaml:2: anchor 'a' value contains itself"}, |
| 815 | {"value: -", "test.yaml:1: block sequence entries are not allowed in this context"}, |
| 816 | {"a: !!binary ==", "test.yaml:1: !!binary value contains invalid base64 data"}, |
| 817 | {"{[.]}", `test.yaml:1: invalid map key: sequence`}, |
| 818 | {"{{.}}", `test.yaml:1: invalid map key: map`}, |
| 819 | {"b: *a\na: &a {c: 1}", `test.yaml:1: unknown anchor 'a' referenced`}, |
| 820 | {"%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] | 821 | } |
| 822 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 823 | func TestUnmarshalErrors(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 824 | for i, item := range unmarshalErrorTests { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 825 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 826 | expr, err := callUnmarshal(t, item.data) |
| 827 | val := "" |
| 828 | if expr != nil { |
| 829 | val = cueStr(expr) |
| 830 | } |
| 831 | if err == nil || err.Error() != item.error { |
| 832 | t.Errorf("got %v; want %v; (value %v)", err, item.error, val) |
| 833 | } |
| 834 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 838 | func TestDecoderErrors(t *testing.T) { |
| 839 | for i, item := range unmarshalErrorTests { |
| 840 | t.Run(fmt.Sprintf("test %d: %q", i, item.data), func(t *testing.T) { |
| 841 | _, err := newDecoder(t, item.data).Decode() |
| 842 | if err == nil || err.Error() != item.error { |
| 843 | t.Errorf("got %v; want %v", err, item.error) |
| 844 | } |
| 845 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 849 | func TestFiles(t *testing.T) { |
| 850 | files := []string{"merge"} |
| 851 | for _, test := range files { |
| 852 | t.Run(test, func(t *testing.T) { |
| 853 | testname := fmt.Sprintf("testdata/%s.test", test) |
| 854 | filename := fmt.Sprintf("testdata/%s.out", test) |
| 855 | mergeTests, err := ioutil.ReadFile(testname) |
| 856 | if err != nil { |
| 857 | t.Fatal(err) |
| 858 | } |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 859 | expr, err := yaml.Unmarshal("test.yaml", mergeTests) |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 860 | if err != nil { |
| 861 | t.Fatal(err) |
| 862 | } |
| 863 | got := cueStr(expr) |
| 864 | if *update { |
| 865 | ioutil.WriteFile(filename, []byte(got), 0644) |
| 866 | return |
| 867 | } |
| 868 | b, err := ioutil.ReadFile(filename) |
| 869 | if err != nil { |
| 870 | t.Fatal(err) |
| 871 | } |
| 872 | if want := string(b); got != want { |
| 873 | t.Errorf("\n got: %v;\nwant: %v", got, want) |
| 874 | } |
| 875 | }) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 879 | func TestFuzzCrashers(t *testing.T) { |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 880 | cases := []string{ |
| 881 | // runtime error: index out of range |
| 882 | "\"\\0\\\r\n", |
| 883 | |
| 884 | // should not happen |
| 885 | " 0: [\n] 0", |
| 886 | "? ? \"\n\" 0", |
| 887 | " - {\n000}0", |
| 888 | "0:\n 0: [0\n] 0", |
| 889 | " - \"\n000\"0", |
| 890 | " - \"\n000\"\"", |
| 891 | "0:\n - {\n000}0", |
| 892 | "0:\n - \"\n000\"0", |
| 893 | "0:\n - \"\n000\"\"", |
| 894 | |
| 895 | // runtime error: index out of range |
| 896 | " \ufeff\n", |
| 897 | "? \ufeff\n", |
| 898 | "? \ufeff:\n", |
| 899 | "0: \ufeff\n", |
| 900 | "? \ufeff: \ufeff\n", |
| 901 | } |
| 902 | for _, data := range cases { |
Marcel van Lohuizen | 2156c81 | 2018-12-10 16:05:07 +0100 | [diff] [blame] | 903 | _, _ = callUnmarshal(t, data) |
Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | //var data []byte |
| 908 | //func init() { |
| 909 | // var err error |
| 910 | // data, err = ioutil.ReadFile("/tmp/file.yaml") |
| 911 | // if err != nil { |
| 912 | // panic(err) |
| 913 | // } |
| 914 | //} |
| 915 | // |
| 916 | //func (s *S) BenchmarkUnmarshal(c *C) { |
| 917 | // var err error |
| 918 | // for i := 0; i < c.N; i++ { |
| 919 | // var v map[string]interface{} |
| 920 | // err = yaml.Unmarshal(data, &v) |
| 921 | // } |
| 922 | // if err != nil { |
| 923 | // panic(err) |
| 924 | // } |
| 925 | //} |
| 926 | // |
| 927 | //func (s *S) BenchmarkMarshal(c *C) { |
| 928 | // var v map[string]interface{} |
| 929 | // yaml.Unmarshal(data, &v) |
| 930 | // c.ResetTimer() |
| 931 | // for i := 0; i < c.N; i++ { |
| 932 | // yaml.Marshal(&v) |
| 933 | // } |
| 934 | //} |