From df2e58026a74be1e57b2c48fdefeb45c117d30d0 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:08:27 +0900 Subject: [PATCH] Snapshot htmlx stuff --- assert/snapshot.go | 40 ++++++++------- http/props/htmlx/TestFormXmlRender.snap.xml | 3 -- http/props/htmlx/TestSnapshotForm.snap.json | 25 ++++++++++ http/props/htmlx/TestSnapshotForm.snap.xml | 8 +++ http/props/htmlx/TestSnapshotLink.snap.json | 4 ++ http/props/htmlx/TestSnapshotLink.snap.xml | 1 + http/props/htmlx/form_test.go | 17 ------- http/props/htmlx/link.go | 2 +- http/props/htmlx/snapshot_test.go | 54 +++++++++++++++++++++ 9 files changed, 116 insertions(+), 38 deletions(-) delete mode 100644 http/props/htmlx/TestFormXmlRender.snap.xml create mode 100644 http/props/htmlx/TestSnapshotForm.snap.json create mode 100644 http/props/htmlx/TestSnapshotForm.snap.xml create mode 100644 http/props/htmlx/TestSnapshotLink.snap.json create mode 100644 http/props/htmlx/TestSnapshotLink.snap.xml delete mode 100644 http/props/htmlx/form_test.go create mode 100644 http/props/htmlx/snapshot_test.go diff --git a/assert/snapshot.go b/assert/snapshot.go index 2f7442b..db67802 100644 --- a/assert/snapshot.go +++ b/assert/snapshot.go @@ -1,11 +1,12 @@ package assert import ( + "bytes" "encoding/json" "encoding/xml" "errors" + "fmt" "os" - "strings" "testing" ) @@ -14,19 +15,21 @@ func pathExists(path string) bool { return !errors.Is(err, os.ErrNotExist) } -func ensureSuffix(s, suffix string) string { - if !strings.HasSuffix(s, suffix) { - s = s + suffix +var newline = []byte("\n") + +func ensureSuffix(s, suffix []byte) []byte { + if !bytes.HasSuffix(s, suffix) { + s = append(s, suffix...) } return s } -func writeSnapshot(t *testing.T, path, actual string) { +func writeSnapshot(t *testing.T, path string, actual []byte) { f, err := os.Create(path) if err != nil { t.Fatalf("Failed to create snapshot file: %s", err) } - _, err = f.Write([]byte(actual)) + _, err = f.Write(actual) if err != nil { t.Fatalf("Failed to write to snapshot file: %s", err) } @@ -36,25 +39,24 @@ func writeSnapshot(t *testing.T, path, actual string) { } } -func TextSnapshot(t *testing.T, path, actual string) { +func snapshot(t *testing.T, path string, actual []byte) { if !pathExists(path) { writeSnapshot(t, path, actual) t.Errorf("Snapshot file created: %s", path) return } - actual = ensureSuffix(actual, "\n") + actual = ensureSuffix(actual, newline) - content, err := os.ReadFile(path) + expected, err := os.ReadFile(path) if err != nil { t.Fatalf("Failed to read snapshot file: %s", err) } - expected := string(content) - expected = ensureSuffix(expected, "\n") + expected = ensureSuffix(expected, newline) - if expected != actual { - t.Errorf("Value doesn't match snapshot %s:\n\nExpected:\n%s\n\nActual:\n%s\n", path, expected, actual) + if !bytes.Equal(expected, actual) { + t.Errorf("Value doesn't match snapshot %s:\n\nExpected:\n%s\n\nActual:\n%s\n", path, string(expected), string(actual)) } if os.Getenv("UPDATE_SNAPSHOTS") == "" { @@ -65,18 +67,22 @@ func TextSnapshot(t *testing.T, path, actual string) { t.Logf("Snapshot file %s updated\n", path) } -func JsonSnapshot(t *testing.T, path string, actual any) { +func SnapshotText(t *testing.T, actual string) { + snapshot(t, fmt.Sprintf("%s.snap.txt", t.Name()), []byte(actual)) +} + +func SnapshotJson(t *testing.T, actual any) { data, err := json.MarshalIndent(actual, "", " ") if err != nil { t.Fatalf("Snapshot failed to serialize actual to JSON: %s", err) } - TextSnapshot(t, path, string(data)) + snapshot(t, fmt.Sprintf("%s.snap.json", t.Name()), data) } -func XmlSnapshot(t *testing.T, path string, actual any) { +func SnapshotXml(t *testing.T, actual any) { data, err := xml.MarshalIndent(actual, "", " ") if err != nil { t.Fatalf("Snapshot failed to serialize actual to XML: %s", err) } - TextSnapshot(t, path, string(data)) + snapshot(t, fmt.Sprintf("%s.snap.xml", t.Name()), data) } diff --git a/http/props/htmlx/TestFormXmlRender.snap.xml b/http/props/htmlx/TestFormXmlRender.snap.xml deleted file mode 100644 index 62d57d9..0000000 --- a/http/props/htmlx/TestFormXmlRender.snap.xml +++ /dev/null @@ -1,3 +0,0 @@ -
diff --git a/http/props/htmlx/TestSnapshotForm.snap.json b/http/props/htmlx/TestSnapshotForm.snap.json new file mode 100644 index 0000000..f0d0e69 --- /dev/null +++ b/http/props/htmlx/TestSnapshotForm.snap.json @@ -0,0 +1,25 @@ +{ + "Method": "POST", + "Form": { + "Username": { + "Label": "Username", + "Name": "username", + "Required": true, + "Value": "" + }, + "Password": { + "Label": "Password", + "Type": "password", + "Name": "password", + "Required": true, + "Value": "" + }, + "RegisterLink": { + "Href": "/register", + "Name": "Register" + }, + "Submit": { + "Label": "Login" + } + } +} \ No newline at end of file diff --git a/http/props/htmlx/TestSnapshotForm.snap.xml b/http/props/htmlx/TestSnapshotForm.snap.xml new file mode 100644 index 0000000..dce450e --- /dev/null +++ b/http/props/htmlx/TestSnapshotForm.snap.xml @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/http/props/htmlx/TestSnapshotLink.snap.json b/http/props/htmlx/TestSnapshotLink.snap.json new file mode 100644 index 0000000..5a2940f --- /dev/null +++ b/http/props/htmlx/TestSnapshotLink.snap.json @@ -0,0 +1,4 @@ +{ + "Href": "/register", + "Name": "Register" +} \ No newline at end of file diff --git a/http/props/htmlx/TestSnapshotLink.snap.xml b/http/props/htmlx/TestSnapshotLink.snap.xml new file mode 100644 index 0000000..7f984c6 --- /dev/null +++ b/http/props/htmlx/TestSnapshotLink.snap.xml @@ -0,0 +1 @@ +Register \ No newline at end of file diff --git a/http/props/htmlx/form_test.go b/http/props/htmlx/form_test.go deleted file mode 100644 index 0ad91cb..0000000 --- a/http/props/htmlx/form_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package htmlx_test - -import ( - "testing" - - "lishwist/assert" - "lishwist/http/props/htmlx" -) - -func TestFormXmlRender(t *testing.T) { - form := htmlx.Form{ - Method: "POST", - Content: htmlx.Input{Label: "Message", Name: "message"}, - } - - assert.XmlSnapshot(t, "TestFormXmlRender.snap.xml", form) -} diff --git a/http/props/htmlx/link.go b/http/props/htmlx/link.go index eaf7970..a2f3cf7 100644 --- a/http/props/htmlx/link.go +++ b/http/props/htmlx/link.go @@ -7,7 +7,7 @@ type Link struct { Name string } -func (i *Link) MarshalXML(e *xml.Encoder, start xml.StartElement) error { +func (i Link) MarshalXML(e *xml.Encoder, start xml.StartElement) error { start.Name = xml.Name{Local: "A"} start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Href"}, Value: i.Href}) diff --git a/http/props/htmlx/snapshot_test.go b/http/props/htmlx/snapshot_test.go new file mode 100644 index 0000000..2cd6cb5 --- /dev/null +++ b/http/props/htmlx/snapshot_test.go @@ -0,0 +1,54 @@ +package htmlx_test + +import ( + "testing" + + "lishwist/assert" + "lishwist/http/props/htmlx" +) + +type content struct { + Username htmlx.Input + Password htmlx.Input + RegisterLink htmlx.Link + Submit htmlx.Submit +} + +func TestSnapshotForm(t *testing.T) { + form := htmlx.Form{ + Method: "POST", + Content: content{ + Username: htmlx.Input{ + Label: "Username", + Name: "username", + Required: true, + }, + Password: htmlx.Input{ + Label: "Password", + Name: "password", + Type: "password", + Required: true, + }, + RegisterLink: htmlx.Link{ + Name: "Register", + Href: "/register", + }, + Submit: htmlx.Submit{ + Label: "Login", + }, + }, + } + + assert.SnapshotXml(t, form) + assert.SnapshotJson(t, form) +} + +func TestSnapshotLink(t *testing.T) { + link := htmlx.Link{ + Name: "Register", + Href: "/register", + } + + assert.SnapshotXml(t, link) + assert.SnapshotJson(t, link) +}