1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package behavior
import (
"strings"
"testing"
"gopkg.in/yaml.v3"
)
func TestDescListScalarForm(t *testing.T) {
var wrap struct {
Description DescList `yaml:"description"`
}
if err := yaml.Unmarshal([]byte("description: \"A dark room.\"\n"), &wrap); err != nil {
t.Fatalf("scalar description failed to decode: %v", err)
}
if len(wrap.Description) != 1 || wrap.Description[0].Text != "A dark room." {
t.Fatalf("scalar decode = %#v", wrap.Description)
}
}
func TestDescListSequenceForm(t *testing.T) {
src := "description:\n - text: \"Default\"\n - text: \"Lit\"\n condition:\n player_flag: lamp_lit\n"
var wrap struct {
Description DescList `yaml:"description"`
}
if err := yaml.Unmarshal([]byte(src), &wrap); err != nil {
t.Fatalf("sequence description failed to decode: %v", err)
}
if len(wrap.Description) != 2 {
t.Fatalf("got %d variants, want 2", len(wrap.Description))
}
if wrap.Description[1].Condition == nil {
t.Fatal("conditional variant lost its condition")
}
}
func TestDescListRoundTripPreservesCondition(t *testing.T) {
original := DescList{
{Text: "Default"},
{Text: "Lit", Condition: &Condition{PlayerFlag: "lamp_lit"}},
}
data, err := yaml.Marshal(struct {
Description DescList `yaml:"description"`
}{Description: original})
if err != nil {
t.Fatalf("marshal: %v", err)
}
// A conditional list must not collapse to the scalar shorthand.
if strings.Contains(string(data), "description: Default") {
t.Fatalf("conditional list collapsed to scalar:\n%s", data)
}
var loaded struct {
Description DescList `yaml:"description"`
}
if err := yaml.Unmarshal(data, &loaded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(loaded.Description) != 2 || loaded.Description[1].Condition == nil {
t.Fatalf("round trip lost structure: %#v", loaded.Description)
}
}
func TestDescListSingleVariantMarshalsAsScalar(t *testing.T) {
data, err := yaml.Marshal(struct {
Description DescList `yaml:"description"`
}{Description: DescList{{Text: "A dark room."}}})
if err != nil {
t.Fatalf("marshal: %v", err)
}
if !strings.Contains(string(data), "description: A dark room.") {
t.Fatalf("single unconditional variant should use the scalar form:\n%s", data)
}
}
func TestDescListEmptyStillMarshals(t *testing.T) {
data, err := yaml.Marshal(struct {
Description DescList `yaml:"description"`
}{})
if err != nil {
t.Fatalf("marshal empty: %v", err)
}
if !strings.Contains(string(data), "description:") {
t.Fatalf("empty description should still emit a key:\n%s", data)
}
}
|