aboutsummaryrefslogtreecommitdiff
path: root/internal/behavior/desc_test.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-19 03:15:59 -0400
committerhistoria <[not public]>2026-09-19 03:15:59 -0400
commit0e60567a7a0addb9421769ee7be7853bfa8788cb (patch)
tree7645112db0e4eae95b8a83c1e4f9aab38bede655 /internal/behavior/desc_test.go
parentcc33072eced3d5460512c09c42ec4531c807d7af (diff)
downloadthehouseoficarus-0e60567a7a0addb9421769ee7be7853bfa8788cb.tar.gz
refactor: simplifcation, change how rooms are stored in player yaml, remove legacy code
Diffstat (limited to 'internal/behavior/desc_test.go')
-rw-r--r--internal/behavior/desc_test.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/internal/behavior/desc_test.go b/internal/behavior/desc_test.go
new file mode 100644
index 0000000..d506530
--- /dev/null
+++ b/internal/behavior/desc_test.go
@@ -0,0 +1,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)
+ }
+}