Debian Codegraph @

internal/libyaml/dumper_test.go

raw symbols SCIP
1 // Tests for the Dump API, including WithAllDocuments functionality.
2
3 package libyaml
4
5 import (
6 "strings"
7 "testing"
8
9 "go.yaml.in/yaml/v4/internal/testutil/assert"
10 )
11
12 // TestDump_SingleValue tests dumping a single value
13 func TestDump_SingleValue(t *testing.T) {
14 type Config struct {
15 Name string `yaml:"name"`
16 }
17
18 config := Config{Name: "myconfig"}
19 data, err := Dump(config)
20 assert.NoError(t, err)
21
22 // Should not have document separator for single document
23 assert.True(t, strings.Contains(string(data), "name: myconfig"))
24 }
25
26 // TestDumpWithAllDocuments_TypedSlice tests dumping multiple values from typed slice
27 func TestDumpWithAllDocuments_TypedSlice(t *testing.T) {
28 type Config struct {
29 Name string `yaml:"name"`
30 }
31
32 configs := []Config{
33 {Name: "first"},
34 {Name: "second"},
35 {Name: "third"},
36 }
37
38 data, err := Dump(configs, WithAllDocuments())
39 assert.NoError(t, err)
40
41 // Should have document separators
42 assert.True(t, strings.Contains(string(data), "---"))
43 assert.True(t, strings.Contains(string(data), "name: first"))
44 assert.True(t, strings.Contains(string(data), "name: second"))
45 assert.True(t, strings.Contains(string(data), "name: third"))
46 }
47
48 // TestDumpWithAllDocuments_UntypedSlice tests dumping multiple values from []any
49 func TestDumpWithAllDocuments_UntypedSlice(t *testing.T) {
50 docs := []any{
51 map[string]string{"name": "first"},
52 map[string]string{"name": "second"},
53 }
54
55 data, err := Dump(docs, WithAllDocuments())
56 assert.NoError(t, err)
57
58 // Should have document separator
59 assert.True(t, strings.Contains(string(data), "---"))
60 assert.True(t, strings.Contains(string(data), "name: first"))
61 assert.True(t, strings.Contains(string(data), "name: second"))
62 }
63
64 // TestDumpWithAllDocuments_EmptySlice tests dumping an empty slice
65 func TestDumpWithAllDocuments_EmptySlice(t *testing.T) {
66 var docs []any
67
68 data, err := Dump(docs, WithAllDocuments())
69 // Empty slice produces an empty YAML stream
70 // This may produce an error or empty output depending on implementation
71 if err != nil {
72 // It's acceptable for empty slice to produce error
73 t.Logf("Empty slice produced error (acceptable): %v", err)
74 } else {
75 // Or it might produce empty/minimal output
76 assert.True(t, len(data) < 50)
77 }
78 }
79
80 // TestDumpWithAllDocuments_NonSlice tests that WithAllDocuments with non-slice returns error
81 func TestDumpWithAllDocuments_NonSlice(t *testing.T) {
82 single := map[string]string{"name": "single"}
83
84 _, err := Dump(single, WithAllDocuments())
85 assert.NotNil(t, err)
86 assert.ErrorMatches(t, ".*WithAllDocuments requires a slice input.*", err)
87 }