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:
Joao Goncalves
2025-11-17 16:46:33 -01:00
parent 472351d9a5
commit f5a786730a
6 changed files with 167 additions and 33 deletions

64
signal_test.go Normal file
View 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())
}
}