Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 19, 2023
1 parent 3f0d7c3 commit 4b8df4f
Show file tree
Hide file tree
Showing 23 changed files with 665 additions and 298 deletions.
2 changes: 2 additions & 0 deletions boolgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
)

var _ BoolSetter = (*testSetter[bool])(nil)

func Test_makeBoolGetter_panic(t *testing.T) {
defer func() {
if x := recover(); x != nil {
Expand Down
2 changes: 2 additions & 0 deletions floatgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
)

var _ FloatSetter = (*testSetter[float64])(nil)

func Test_makeFloatGetter_panic(t *testing.T) {
defer func() {
if x := recover(); x != nil {
Expand Down
2 changes: 2 additions & 0 deletions htmlgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
)

var _ HtmlGetter = (*testSetter[template.HTML])(nil)

func Test_makeHtmlGetter_panic(t *testing.T) {
defer func() {
if x := recover(); x != nil {
Expand Down
205 changes: 0 additions & 205 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,30 +619,6 @@ func checkHtml(is *is.I, rq *testRequest, h template.HTML, tag interface{}, txt
}
}

func TestRequest_Elements(t *testing.T) {
is := is.New(t)
rq := newTestRequest()
defer rq.Close()

chk := func(h template.HTML, tag interface{}, txt string) { is.Helper(); checkHtml(is, rq, h, tag, txt) }

var avs []*atomic.Value
for i := 0; i < 16; i++ {
av := &atomic.Value{}
av.Store(fmt.Sprintf("t%d", i))
avs = append(avs, av)
}

chk(rq.Div(avs[1], "s1"), avs[1], "s1")
chk(rq.Li(avs[3], "s3"), avs[3], "s3")
chk(rq.A(avs[5], "s5"), avs[5], "s5")
chk(rq.Button(avs[6], "s6"), avs[6], "s6")
avs[7].Store("randomimg.png")
chk(rq.Img(avs[7]), avs[7], "src=\"randomimg.png\"")
avs[8].Store("\"randomimg.png\"")
chk(rq.Img(avs[8]), avs[8], "src=\"randomimg.png\"")
}

func jidForTag(rq *Request, tag interface{}) jid.Jid {
if elems := rq.GetElements(tag); len(elems) > 0 {
return elems[0].jid
Expand Down Expand Up @@ -677,62 +653,6 @@ func TestRequest_Password(t *testing.T) {
}
}

func TestRequest_Number(t *testing.T) {
const elemVal = 21.5
is := is.New(t)
rq := newTestRequest()
defer rq.Close()

var av atomic.Value
av.Store(elemVal)
elemId := &av

chk := func(h template.HTML, tag interface{}, txt string) { is.Helper(); checkHtml(is, rq, h, tag, txt) }

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Number(elemId, &av, func(rq *Request, jidstr string, val float64) error {
is.True(rq.GetElement(jid.ParseString(jidstr)) != nil)
switch val {
case 4.3:
// ok
case 0:
// ok
default:
is.Fail()
}
gotCall <- struct{}{}
return nil
}, "disabled")
chk(h, elemId, "21.5")
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "4.3"}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: ""} // should call with zero
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "meh"} // should fail with alert
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
is.Fail()
case msgstr := <-rq.outCh:
msg, ok := wsParse([]byte(msgstr))
if !ok {
t.Log(strconv.Quote(msgstr))
}
is.True(ok)
is.Equal(msg.What, what.Alert)
}
}

func TestRequest_Range(t *testing.T) {
const elemVal = float64(3.14)
is := is.New(t)
Expand Down Expand Up @@ -761,111 +681,6 @@ func TestRequest_Range(t *testing.T) {
}
}

func TestRequest_Checkbox(t *testing.T) {
const elemVal = true
is := is.New(t)
rq := newTestRequest()
defer rq.Close()

var av atomic.Value
av.Store(elemVal)
elemId := &av

chk := func(h template.HTML, tag interface{}, txt string) { is.Helper(); checkHtml(is, rq, h, tag, txt) }

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Checkbox(&av, func(rq *Request, id string, val bool) error {
is.True(rq.GetElement(jid.ParseString(id)) != nil)
is.Equal(val, false)
gotCall <- struct{}{}
return nil
})
chk(h, &av, "checked")
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "false"}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: ""}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}
rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "wut"}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
is.Fail()
case msgstr := <-rq.outCh:
msg, ok := wsParse([]byte(msgstr))
is.True(ok)
is.Equal(msg.What, what.Alert)
}
}

func TestRequest_Date(t *testing.T) {
var elemVal time.Time
is := is.New(t)
rq := newTestRequest()
defer rq.Close()

var av atomic.Value
av.Store(elemVal)
elemId := &av

chk := func(h template.HTML, tag interface{}, txt string) { is.Helper(); checkHtml(is, rq, h, tag, txt) }

gotCall := make(chan struct{})
defer close(gotCall)
h := rq.Date(elemId, &av, func(rq *Request, id string, val time.Time) error {
defer func() {
gotCall <- struct{}{}
}()
is.True(rq.GetElement(jid.ParseString(id)) != nil)
if !val.IsZero() {
is.Equal(val.Year(), 1970)
is.Equal(val.Month(), time.January)
is.Equal(val.Day(), 2)
}
return nil
}, "")

chk(h, elemId, elemVal.Format(ISO8601))

rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "1970-01-02"}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}

rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: ""}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
}

rq.inCh <- wsMsg{Jid: jidForTag(rq.Request, elemId), What: what.Input, Data: "foobar!"}
select {
case <-time.NewTimer(testTimeout).C:
is.Fail()
case <-gotCall:
is.Fail()
case msgstr := <-rq.outCh:
msg, ok := wsParse([]byte(msgstr))
is.True(ok)
if msg.What != what.Alert {
log.Println(len(rq.outCh), msg)
}
is.Equal(msg.What, what.Alert)
}
}

func TestRequest_Radio(t *testing.T) {
is := is.New(t)
rq := newTestRequest()
Expand Down Expand Up @@ -895,26 +710,6 @@ func TestRequest_Radio(t *testing.T) {
}
}

func TestRequest_Select(t *testing.T) {
is := is.New(t)
rq := newTestRequest()
defer rq.Close()

chk := func(h template.HTML, tag interface{}, txt string) { is.Helper(); checkHtml(is, rq, h, tag, txt) }

a := NewNamedBoolArray()
a.Add("1", "one")
a.Add("2", "two")

h := rq.Select(a, "disabled")
chk(h, a, "disabled")
is.Equal(strings.Contains(string(h), "selected"), false)

a.Set("1", true)
h = rq.Select(a)
chk(h, a, "selected")
}

func TestRequest_ConnectFn(t *testing.T) {
is := is.New(t)
rq := newTestRequest()
Expand Down
73 changes: 1 addition & 72 deletions stringgetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,9 @@ import (
"strings"
"sync/atomic"
"testing"

"github.com/linkdata/deadlock"
)

type testStringSetter struct {
mu deadlock.Mutex
s string
err error
setCount int
setCalled chan struct{}
getCount int
getCalled chan struct{}
}

func newTestStringSetter(s string) *testStringSetter {
return &testStringSetter{
s: s,
setCalled: make(chan struct{}),
getCalled: make(chan struct{}),
}
}

func (ss *testStringSetter) Get() (s string) {
ss.mu.Lock()
s = ss.s
ss.mu.Unlock()
return
}

func (ss *testStringSetter) Set(s string) {
ss.mu.Lock()
ss.s = s
ss.mu.Unlock()
}

func (ss *testStringSetter) SetCount() (n int) {
ss.mu.Lock()
n = ss.setCount
ss.mu.Unlock()
return
}

func (ss *testStringSetter) GetCount() (n int) {
ss.mu.Lock()
n = ss.getCount
ss.mu.Unlock()
return
}

func (ss *testStringSetter) JawsGetString(e *Element) (s string) {
ss.mu.Lock()
ss.getCount++
if ss.getCount == 1 {
close(ss.getCalled)
}
s = ss.s
ss.mu.Unlock()
return
}

func (ss *testStringSetter) JawsSetString(e *Element, s string) (err error) {
ss.mu.Lock()
ss.setCount++
if ss.setCount == 1 {
close(ss.setCalled)
}
if err = ss.err; err == nil {
ss.s = s
}
ss.mu.Unlock()
return
}

var _ StringSetter = (*testStringSetter)(nil)
var _ StringSetter = (*testSetter[string])(nil)

func Test_makeStringGetter_panic(t *testing.T) {
defer func() {
Expand Down
Loading

0 comments on commit 4b8df4f

Please sign in to comment.