| 1 | package kdl |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | "github.com/tomwright/dasel/v3/parsing" |
| 9 | ) |
| 10 | |
| 11 | func newTestWriter(t *testing.T) parsing.Writer { |
| 12 | t.Helper() |
| 13 | w, err := newKDLWriter(parsing.DefaultWriterOptions()) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | return w |
| 18 | } |
| 19 | |
| 20 | func TestWriter_SimpleScalars(t *testing.T) { |
| 21 | val := model.NewMapValue() |
| 22 | _ = val.SetMapKey("name", model.NewStringValue("Bob")) |
| 23 | _ = val.SetMapKey("age", model.NewIntValue(76)) |
| 24 | |
| 25 | w := newTestWriter(t) |
| 26 | data, err := w.Write(val) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | result := string(data) |
| 32 | if !strings.Contains(result, `name "Bob"`) { |
| 33 | t.Errorf("expected name node, got:\n%s", result) |
| 34 | } |
| 35 | if !strings.Contains(result, "age 76") { |
| 36 | t.Errorf("expected age node, got:\n%s", result) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestWriter_BoolAndNull(t *testing.T) { |
| 41 | val := model.NewMapValue() |
| 42 | _ = val.SetMapKey("active", model.NewBoolValue(true)) |
| 43 | _ = val.SetMapKey("empty", model.NewNullValue()) |
| 44 | |
| 45 | w := newTestWriter(t) |
| 46 | data, err := w.Write(val) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | |
| 51 | result := string(data) |
| 52 | if !strings.Contains(result, "active #true") { |
| 53 | t.Errorf("expected bool node, got:\n%s", result) |
| 54 | } |
| 55 | if !strings.Contains(result, "empty") { |
| 56 | t.Errorf("expected empty node, got:\n%s", result) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestWriter_Slice(t *testing.T) { |
| 61 | val := model.NewMapValue() |
| 62 | plugins := model.NewSliceValue() |
| 63 | _ = plugins.Append(model.NewStringValue("git")) |
| 64 | _ = plugins.Append(model.NewStringValue("docker")) |
| 65 | _ = val.SetMapKey("plugin", plugins) |
| 66 | |
| 67 | w := newTestWriter(t) |
| 68 | data, err := w.Write(val) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | |
| 73 | result := string(data) |
| 74 | if strings.Count(result, "plugin") != 2 { |
| 75 | t.Errorf("expected 2 plugin nodes, got:\n%s", result) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestWriter_NestedMap(t *testing.T) { |
| 80 | val := model.NewMapValue() |
| 81 | server := model.NewMapValue() |
| 82 | _ = server.SetMapKey("host", model.NewStringValue("localhost")) |
| 83 | _ = server.SetMapKey("port", model.NewIntValue(80)) |
| 84 | _ = val.SetMapKey("server", server) |
| 85 | |
| 86 | w := newTestWriter(t) |
| 87 | data, err := w.Write(val) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | |
| 92 | result := string(data) |
| 93 | if !strings.Contains(result, "server") { |
| 94 | t.Errorf("expected server node, got:\n%s", result) |
| 95 | } |
| 96 | if !strings.Contains(result, `host="localhost"`) { |
| 97 | t.Errorf("expected host property, got:\n%s", result) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestWriter_CompactMode(t *testing.T) { |
| 102 | val := model.NewMapValue() |
| 103 | _ = val.SetMapKey("name", model.NewStringValue("Bob")) |
| 104 | |
| 105 | opts := parsing.WriterOptions{Compact: true} |
| 106 | w, err := newKDLWriter(opts) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | |
| 111 | data, err := w.Write(val) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | |
| 116 | result := string(data) |
| 117 | if strings.Contains(result, "\n") { |
| 118 | t.Errorf("compact mode should not contain newlines, got:\n%s", result) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestWriter_RoundTrip(t *testing.T) { |
| 123 | input := `name "Bob" |
| 124 | age 76 |
| 125 | active true` |
| 126 | |
| 127 | r := newTestReader(t) |
| 128 | val, err := r.Read([]byte(input)) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | |
| 133 | w := newTestWriter(t) |
| 134 | data, err := w.Write(val) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | |
| 139 | // Re-read and verify |
| 140 | val2, err := r.Read(data) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | |
| 145 | assertMapStringValue(t, val2, "name", "Bob") |
| 146 | assertMapIntValue(t, val2, "age", 76) |
| 147 | assertMapBoolValue(t, val2, "active", true) |
| 148 | } |
| 149 | |
| 150 | func TestWriter_MapWithArgsAndChildren(t *testing.T) { |
| 151 | // Build a map with $args, properties, and nested children |
| 152 | val := model.NewMapValue() |
| 153 | server := model.NewMapValue() |
| 154 | |
| 155 | args := model.NewSliceValue() |
| 156 | _ = args.Append(model.NewIntValue(80)) |
| 157 | _ = server.SetMapKey("$args", args) |
| 158 | |
| 159 | _ = server.SetMapKey("host", model.NewStringValue("localhost")) |
| 160 | |
| 161 | tls := model.NewMapValue() |
| 162 | _ = tls.SetMapKey("enabled", model.NewBoolValue(true)) |
| 163 | _ = server.SetMapKey("tls", tls) |
| 164 | |
| 165 | _ = val.SetMapKey("server", server) |
| 166 | |
| 167 | w := newTestWriter(t) |
| 168 | data, err := w.Write(val) |
| 169 | if err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | |
| 173 | result := string(data) |
| 174 | if !strings.Contains(result, "server 80") { |
| 175 | t.Errorf("expected server with arg 80, got:\n%s", result) |
| 176 | } |
| 177 | if !strings.Contains(result, `host="localhost"`) { |
| 178 | t.Errorf("expected host property, got:\n%s", result) |
| 179 | } |
| 180 | if !strings.Contains(result, "tls") { |
| 181 | t.Errorf("expected tls child, got:\n%s", result) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestWriter_V1Output(t *testing.T) { |
| 186 | val := model.NewMapValue() |
| 187 | _ = val.SetMapKey("active", model.NewBoolValue(true)) |
| 188 | _ = val.SetMapKey("disabled", model.NewBoolValue(false)) |
| 189 | _ = val.SetMapKey("empty", model.NewNullValue()) |
| 190 | |
| 191 | opts := parsing.DefaultWriterOptions() |
| 192 | opts.Ext["kdl-version"] = "1" |
| 193 | w, err := newKDLWriter(opts) |
| 194 | if err != nil { |
| 195 | t.Fatal(err) |
| 196 | } |
| 197 | |
| 198 | data, err := w.Write(val) |
| 199 | if err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | |
| 203 | result := string(data) |
| 204 | if !strings.Contains(result, "active true") { |
| 205 | t.Errorf("expected v1 'active true', got:\n%s", result) |
| 206 | } |
| 207 | if !strings.Contains(result, "disabled false") { |
| 208 | t.Errorf("expected v1 'disabled false', got:\n%s", result) |
| 209 | } |
| 210 | // null node has no args in v1 or v2, so just check it exists |
| 211 | if !strings.Contains(result, "empty") { |
| 212 | t.Errorf("expected 'empty' node, got:\n%s", result) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func TestWriter_V2Output(t *testing.T) { |
| 217 | val := model.NewMapValue() |
| 218 | _ = val.SetMapKey("active", model.NewBoolValue(true)) |
| 219 | _ = val.SetMapKey("disabled", model.NewBoolValue(false)) |
| 220 | |
| 221 | opts := parsing.DefaultWriterOptions() |
| 222 | opts.Ext["kdl-version"] = "2" |
| 223 | w, err := newKDLWriter(opts) |
| 224 | if err != nil { |
| 225 | t.Fatal(err) |
| 226 | } |
| 227 | |
| 228 | data, err := w.Write(val) |
| 229 | if err != nil { |
| 230 | t.Fatal(err) |
| 231 | } |
| 232 | |
| 233 | result := string(data) |
| 234 | if !strings.Contains(result, "active #true") { |
| 235 | t.Errorf("expected v2 'active #true', got:\n%s", result) |
| 236 | } |
| 237 | if !strings.Contains(result, "disabled #false") { |
| 238 | t.Errorf("expected v2 'disabled #false', got:\n%s", result) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestWriter_DefaultIsV2(t *testing.T) { |
| 243 | val := model.NewMapValue() |
| 244 | _ = val.SetMapKey("flag", model.NewBoolValue(true)) |
| 245 | |
| 246 | w := newTestWriter(t) |
| 247 | data, err := w.Write(val) |
| 248 | if err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | |
| 252 | result := string(data) |
| 253 | if !strings.Contains(result, "#true") { |
| 254 | t.Errorf("expected v2 output by default, got:\n%s", result) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | func TestWriter_InvalidVersion(t *testing.T) { |
| 259 | val := model.NewMapValue() |
| 260 | _ = val.SetMapKey("x", model.NewBoolValue(true)) |
| 261 | |
| 262 | opts := parsing.DefaultWriterOptions() |
| 263 | opts.Ext["kdl-version"] = "3" |
| 264 | w, err := newKDLWriter(opts) |
| 265 | if err != nil { |
| 266 | t.Fatal(err) |
| 267 | } |
| 268 | |
| 269 | _, err = w.Write(val) |
| 270 | if err == nil { |
| 271 | t.Fatal("expected error for invalid version") |
| 272 | } |
| 273 | if !strings.Contains(err.Error(), "unsupported output version") { |
| 274 | t.Errorf("expected unsupported version error, got: %v", err) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func TestWriter_V1RoundTrip(t *testing.T) { |
| 279 | val := model.NewMapValue() |
| 280 | _ = val.SetMapKey("name", model.NewStringValue("Bob")) |
| 281 | _ = val.SetMapKey("active", model.NewBoolValue(true)) |
| 282 | _ = val.SetMapKey("count", model.NewIntValue(42)) |
| 283 | |
| 284 | opts := parsing.DefaultWriterOptions() |
| 285 | opts.Ext["kdl-version"] = "1" |
| 286 | w, err := newKDLWriter(opts) |
| 287 | if err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | |
| 291 | data, err := w.Write(val) |
| 292 | if err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | |
| 296 | // Re-read and verify |
| 297 | r := newTestReader(t) |
| 298 | val2, err := r.Read(data) |
| 299 | if err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | |
| 303 | assertMapStringValue(t, val2, "name", "Bob") |
| 304 | assertMapBoolValue(t, val2, "active", true) |
| 305 | assertMapIntValue(t, val2, "count", 42) |
| 306 | } |