Skip to content

Commit

Permalink
replace is pkg with small helper
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 20, 2023
1 parent 5da0815 commit b5d537b
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 124 deletions.
7 changes: 3 additions & 4 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/linkdata/jaws/jid"
"github.com/linkdata/jaws/what"
"github.com/matryer/is"
)

type testUi struct {
Expand Down Expand Up @@ -53,7 +52,7 @@ func (tss *testUi) JawsUpdate(e *Element) {
}

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

Expand All @@ -66,7 +65,7 @@ func TestElement_Tag(t *testing.T) {
}

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

Expand Down Expand Up @@ -163,7 +162,7 @@ func TestElement_Queued(t *testing.T) {
}

func TestElement_ReplacePanicsOnMissingId(t *testing.T) {
is := is.New(t)
is := testHelper{t}
rq := newTestRequest()
defer rq.Close()
defer func() {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.21

require (
github.com/linkdata/deadlock v0.4.0
github.com/matryer/is v1.4.1
nhooyr.io/websocket v1.8.7
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/linkdata/deadlock v0.4.0 h1:OM4Vqn5LinkHiCy9IqcA5aJ7zKU1kv5XhC7kiW4Lckc=
github.com/linkdata/deadlock v0.4.0/go.mod h1:MvI1hZGGcTghOKTcL6lATkgXkVVIOwHZ62l1Y0xprUI=
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down
42 changes: 42 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jaws

import (
"reflect"
"testing"
)

func isNil(object interface{}) bool {
if object != nil {
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
}
return false
}

type testHelper struct{ *testing.T }

func (th testHelper) Equal(a, b any) {
th.Helper()
if !(isNil(a) || isNil(b)) {
if !reflect.DeepEqual(a, b) {
th.Errorf("%#v != %#v", a, b)
}
}
}

func (th testHelper) True(a bool) {
th.Helper()
if !a {
th.Error("not true")
}
}

func (th testHelper) NoErr(err error) {
th.Helper()
if err != nil {
th.Error(err)
}
}
29 changes: 14 additions & 15 deletions jaws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (
"time"

"github.com/linkdata/jaws/what"
"github.com/matryer/is"
)

func TestJaws_parseIP(t *testing.T) {
is := is.New(t)
is := testHelper{t}
is.Equal(parseIP(""), nil)
is.True(parseIP("192.168.0.1").Equal(net.IPv4(192, 168, 0, 1)))
is.True(parseIP("192.168.0.2:1234").Equal(net.IPv4(192, 168, 0, 2)))
Expand All @@ -31,7 +30,7 @@ func TestJaws_parseIP(t *testing.T) {
func TestJaws_getCookieSessionsIds(t *testing.T) {
const sessId = 1234
sessCookie := JawsKeyString(sessId)
is := is.New(t)
is := testHelper{t}
is.Equal(getCookieSessionsIds(nil, "meh"), nil)
is.Equal(getCookieSessionsIds(http.Header{}, "meh"), nil)
is.Equal(getCookieSessionsIds(http.Header{"Cookie": []string{}}, "meh"), nil)
Expand All @@ -48,7 +47,7 @@ func TestJaws_MultipleCloseCalls(t *testing.T) {
}

func TestJaws_MakeID(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()
go jw.Serve()
Expand All @@ -60,7 +59,7 @@ func TestJaws_MakeID(t *testing.T) {
}

func TestJaws_maybePanic(t *testing.T) {
is := is.New(t)
is := testHelper{t}
defer func() {
if recover() == nil {
is.Fail()
Expand All @@ -70,7 +69,7 @@ func TestJaws_maybePanic(t *testing.T) {
}

func TestJaws_Logger(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()
var b bytes.Buffer
Expand All @@ -83,7 +82,7 @@ func TestJaws_Logger(t *testing.T) {
}

func TestJaws_MustLog(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()

Expand Down Expand Up @@ -114,7 +113,7 @@ func TestJaws_BroadcastDoesntBlockWhenClosed(t *testing.T) {
}

func TestJaws_BroadcastWaitsWhenFull(t *testing.T) {
is := is.New(t)
is := testHelper{t}

jw := New()
go jw.ServeWithTimeout(testTimeout)
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestJaws_BroadcastWaitsWhenFull(t *testing.T) {
}

func TestJaws_BroadcastFullClosesChannel(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
go jw.ServeWithTimeout(time.Millisecond)

Expand Down Expand Up @@ -208,7 +207,7 @@ func TestJaws_BroadcastFullClosesChannel(t *testing.T) {
}

func TestJaws_UseRequest(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()

Expand Down Expand Up @@ -244,7 +243,7 @@ func TestJaws_UseRequest(t *testing.T) {
}

func TestJaws_BlockingRandomPanics(t *testing.T) {
is := is.New(t)
is := testHelper{t}
defer func() {
if recover() == nil {
is.Fail()
Expand All @@ -259,7 +258,7 @@ func TestJaws_BlockingRandomPanics(t *testing.T) {

func TestJaws_CleansUpUnconnected(t *testing.T) {
const numReqs = 1000
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()
var b bytes.Buffer
Expand Down Expand Up @@ -308,7 +307,7 @@ func TestJaws_CleansUpUnconnected(t *testing.T) {
}

func TestJaws_UnconnectedLivesUntilDeadline(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
defer jw.Close()
hr := httptest.NewRequest(http.MethodGet, "/", nil)
Expand Down Expand Up @@ -369,7 +368,7 @@ func TestJaws_BroadcastsCallable(t *testing.T) {
}

func TestJaws_subscribeOnClosedReturnsNil(t *testing.T) {
is := is.New(t)
is := testHelper{t}
jw := New()
jw.Close()
<-jw.doneCh
Expand All @@ -386,7 +385,7 @@ func TestJaws_subscribeOnClosedReturnsNil(t *testing.T) {
func TestJaws_GenerateHeadHTML(t *testing.T) {
const extraScript = "someExtraScript.js?disregard"
const extraStyle = "http://other.server/someExtraStyle.css"
is := is.New(t)
is := testHelper{t}
jw := New()
jw.Close()

Expand Down
24 changes: 16 additions & 8 deletions jawsboot/jawsboot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import (

"github.com/linkdata/jaws"
"github.com/linkdata/jaws/jawsboot"
"github.com/matryer/is"
)

func TestJawsBoot_Setup(t *testing.T) {
is := is.New(t)
jw := jaws.New()
defer jw.Close()
is.NoErr(jawsboot.Setup(jw))
if err := jawsboot.Setup(jw); err != nil {
t.Fatal(err)
}

rq := jw.NewRequest(nil)
txt := rq.HeadHTML()
is.Equal(strings.Contains(string(txt), rq.JawsKeyString()), true)
is.Equal(strings.Contains(string(txt), jaws.JavascriptPath), true)
is.Equal(strings.Contains(string(txt), jawsboot.DefaultBootstrapVersion), true)
is.Equal(strings.Contains(string(txt), jawsboot.DefaultBootstrapCDN), true)
txt := string(rq.HeadHTML())
if !strings.Contains(txt, rq.JawsKeyString()) {
t.Error(txt)
}
if !strings.Contains(txt, jaws.JavascriptPath) {
t.Error(txt)
}
if !strings.Contains(txt, jawsboot.DefaultBootstrapVersion) {
t.Error(txt)
}
if !strings.Contains(txt, jawsboot.DefaultBootstrapCDN) {
t.Error(txt)
}
}
6 changes: 2 additions & 4 deletions js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"strconv"
"strings"
"testing"

"github.com/matryer/is"
)

func Test_Javascript(t *testing.T) {
const prefix = "/jaws/jaws."
const suffix = ".js"
is := is.New(t)
is := testHelper{t}

h := fnv.New64a()
_, err := h.Write(JavascriptText)
Expand All @@ -36,7 +34,7 @@ func Test_Javascript(t *testing.T) {
func Test_HeadHTML(t *testing.T) {
const extraScript = "someExtraScript.js"
const extraStyle = "someExtraStyle.css"
is := is.New(t)
is := testHelper{t}
txt := HeadHTML(nil, nil)
is.Equal(strings.Contains(string(txt), JavascriptPath), false)
txt = HeadHTML([]string{JavascriptPath, extraScript}, []string{extraStyle})
Expand Down
4 changes: 1 addition & 3 deletions namedbool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package jaws
import (
"html/template"
"testing"

"github.com/matryer/is"
)

func TestNamedBool(t *testing.T) {
is := is.New(t)
is := testHelper{t}

nba := NewNamedBoolArray()
nba.Add("1", "one")
Expand Down
4 changes: 1 addition & 3 deletions namedboolarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"html/template"
"sort"
"testing"

"github.com/matryer/is"
)

func Test_NamedBoolArray(t *testing.T) {
is := is.New(t)
is := testHelper{t}
nba := NewNamedBoolArray()
is.Equal(len(nba.data), 0)

Expand Down
Loading

0 comments on commit b5d537b

Please sign in to comment.