Snapshot htmlx stuff

This commit is contained in:
Teajey 2025-12-10 16:08:27 +09:00
parent 44d920d336
commit df2e58026a
Signed by: Teajey
GPG Key ID: 970E790FE834A713
9 changed files with 116 additions and 38 deletions

View File

@ -1,11 +1,12 @@
package assert package assert
import ( import (
"bytes"
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt"
"os" "os"
"strings"
"testing" "testing"
) )
@ -14,19 +15,21 @@ func pathExists(path string) bool {
return !errors.Is(err, os.ErrNotExist) return !errors.Is(err, os.ErrNotExist)
} }
func ensureSuffix(s, suffix string) string { var newline = []byte("\n")
if !strings.HasSuffix(s, suffix) {
s = s + suffix func ensureSuffix(s, suffix []byte) []byte {
if !bytes.HasSuffix(s, suffix) {
s = append(s, suffix...)
} }
return s return s
} }
func writeSnapshot(t *testing.T, path, actual string) { func writeSnapshot(t *testing.T, path string, actual []byte) {
f, err := os.Create(path) f, err := os.Create(path)
if err != nil { if err != nil {
t.Fatalf("Failed to create snapshot file: %s", err) t.Fatalf("Failed to create snapshot file: %s", err)
} }
_, err = f.Write([]byte(actual)) _, err = f.Write(actual)
if err != nil { if err != nil {
t.Fatalf("Failed to write to snapshot file: %s", err) 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) { if !pathExists(path) {
writeSnapshot(t, path, actual) writeSnapshot(t, path, actual)
t.Errorf("Snapshot file created: %s", path) t.Errorf("Snapshot file created: %s", path)
return return
} }
actual = ensureSuffix(actual, "\n") actual = ensureSuffix(actual, newline)
content, err := os.ReadFile(path) expected, err := os.ReadFile(path)
if err != nil { if err != nil {
t.Fatalf("Failed to read snapshot file: %s", err) t.Fatalf("Failed to read snapshot file: %s", err)
} }
expected := string(content)
expected = ensureSuffix(expected, "\n") expected = ensureSuffix(expected, newline)
if 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, 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") == "" { 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) 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, "", " ") data, err := json.MarshalIndent(actual, "", " ")
if err != nil { if err != nil {
t.Fatalf("Snapshot failed to serialize actual to JSON: %s", err) 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, "", " ") data, err := xml.MarshalIndent(actual, "", " ")
if err != nil { if err != nil {
t.Fatalf("Snapshot failed to serialize actual to XML: %s", err) 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)
} }

View File

@ -1,3 +0,0 @@
<Form Method="POST">
<Input Label="Message" Name="message" Value=""></Input>
</Form>

View File

@ -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"
}
}
}

View File

@ -0,0 +1,8 @@
<Form Method="POST">
<content>
<Input Label="Username" Name="username" Required="true" Value=""></Input>
<Input Label="Password" Type="password" Name="password" Required="true" Value=""></Input>
<A Href="/register">Register</A>
<Submit>Login</Submit>
</content>
</Form>

View File

@ -0,0 +1,4 @@
{
"Href": "/register",
"Name": "Register"
}

View File

@ -0,0 +1 @@
<A Href="/register">Register</A>

View File

@ -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)
}

View File

@ -7,7 +7,7 @@ type Link struct {
Name string 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.Name = xml.Name{Local: "A"}
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Href"}, Value: i.Href}) start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Href"}, Value: i.Href})

View File

@ -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)
}