fix: check for panics on page registration. Fix header append bug: was appending multiple ctx_id to the header; feat: handle complex signal init values as json; add tests; other small improvemnts
This commit is contained in:
64
signal_test.go
Normal file
64
signal_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package via
|
||||
|
||||
import (
|
||||
// "net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-via/via/h"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSignalReturnAsString(t *testing.T) {
|
||||
testcases := []struct {
|
||||
given any
|
||||
expected string
|
||||
}{
|
||||
{"test", "test"},
|
||||
{"another", "another"},
|
||||
{1, "1"},
|
||||
{-99, "-99"},
|
||||
{1.1, "1.1"},
|
||||
{-34.345, "-34.345"},
|
||||
{true, "true"},
|
||||
{false, "false"},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
var sig *signal
|
||||
v := New()
|
||||
v.Page("/", func(c *Context) {
|
||||
c.View(func() h.H { return nil })
|
||||
sig = c.Signal(testcase.given)
|
||||
})
|
||||
|
||||
assert.Equal(t, testcase.expected, sig.String())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalReturnAsStringComplexTypes(t *testing.T) {
|
||||
testcases := []struct {
|
||||
given any
|
||||
expected string
|
||||
}{
|
||||
{[]string{"test"}, `["test"]`},
|
||||
{[]int{1, 2}, "[1, 2]"},
|
||||
{struct{ Val string }{"test"}, `{"Val": "test"}`},
|
||||
{struct {
|
||||
Num int
|
||||
IsPositive bool
|
||||
}{1, true}, `{"Num": 1, "IsPositive": true}`},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
var sig *signal
|
||||
v := New()
|
||||
v.Page("/", func(c *Context) {
|
||||
c.View(func() h.H { return nil })
|
||||
sig = c.Signal(testcase.given)
|
||||
})
|
||||
|
||||
assert.JSONEq(t, testcase.expected, sig.String())
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user