Snapshot htmlx stuff
This commit is contained in:
parent
44d920d336
commit
df2e58026a
@ -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)
|
||||
}
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
<Form Method="POST">
|
||||
<Input Label="Message" Name="message" Value=""></Input>
|
||||
</Form>
|
||||
25
http/props/htmlx/TestSnapshotForm.snap.json
Normal file
25
http/props/htmlx/TestSnapshotForm.snap.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
8
http/props/htmlx/TestSnapshotForm.snap.xml
Normal file
8
http/props/htmlx/TestSnapshotForm.snap.xml
Normal 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>
|
||||
4
http/props/htmlx/TestSnapshotLink.snap.json
Normal file
4
http/props/htmlx/TestSnapshotLink.snap.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"Href": "/register",
|
||||
"Name": "Register"
|
||||
}
|
||||
1
http/props/htmlx/TestSnapshotLink.snap.xml
Normal file
1
http/props/htmlx/TestSnapshotLink.snap.xml
Normal file
@ -0,0 +1 @@
|
||||
<A Href="/register">Register</A>
|
||||
@ -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)
|
||||
}
|
||||
@ -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})
|
||||
|
||||
54
http/props/htmlx/snapshot_test.go
Normal file
54
http/props/htmlx/snapshot_test.go
Normal 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)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user