Skip to content

Commit

Permalink
tests: Refactor if assert.* into `require.*
Browse files Browse the repository at this point in the history
This is a non functional change for tests
mstoykov committed Mar 1, 2023
1 parent adddc70 commit e565316
Showing 7 changed files with 48 additions and 57 deletions.
11 changes: 5 additions & 6 deletions js/common/bridge_test.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type bridgeTestFieldsType struct {
@@ -73,9 +74,8 @@ func TestFieldNameMapper(t *testing.T) {
t.Run(field, func(t *testing.T) {
t.Parallel()
f, ok := data.Typ.FieldByName(field)
if assert.True(t, ok, "no such field") {
assert.Equal(t, name, (FieldNameMapper{}).FieldName(data.Typ, f))
}
require.True(t, ok, "no such field")
assert.Equal(t, name, (FieldNameMapper{}).FieldName(data.Typ, f))
})
}
for meth, name := range data.Methods {
@@ -84,9 +84,8 @@ func TestFieldNameMapper(t *testing.T) {
t.Parallel()
m, ok := data.Typ.MethodByName(meth)
if name != "" {
if assert.True(t, ok, "no such method") {
assert.Equal(t, name, (FieldNameMapper{}).MethodName(data.Typ, m))
}
require.True(t, ok, "no such method")
assert.Equal(t, name, (FieldNameMapper{}).MethodName(data.Typ, m))
} else {
assert.False(t, ok, "exported by accident")
}
22 changes: 9 additions & 13 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
@@ -74,9 +74,8 @@ func TestCompile(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, src, code)
v, err := goja.New().RunProgram(pgm)
if assert.NoError(t, err) {
assert.Equal(t, int64(3), v.Export())
}
require.NoError(t, err)
assert.Equal(t, int64(3), v.Export())
})

t.Run("ES5 Wrap", func(t *testing.T) {
@@ -88,16 +87,13 @@ func TestCompile(t *testing.T) {
assert.Equal(t, "(function(module, exports){\nexports.d=1+(function() { return 2; })()\n})\n", code)
rt := goja.New()
v, err := rt.RunProgram(pgm)
if assert.NoError(t, err) {
fn, ok := goja.AssertFunction(v)
if assert.True(t, ok, "not a function") {
exp := make(map[string]goja.Value)
_, err := fn(goja.Undefined(), goja.Undefined(), rt.ToValue(exp))
if assert.NoError(t, err) {
assert.Equal(t, int64(3), exp["d"].Export())
}
}
}
require.NoError(t, err)
fn, ok := goja.AssertFunction(v)
require.True(t, ok, "not a function")
exp := make(map[string]goja.Value)
_, err = fn(goja.Undefined(), goja.Undefined(), rt.ToValue(exp))
require.NoError(t, err)
assert.Equal(t, int64(3), exp["d"].Export())
})

t.Run("ES5 Invalid", func(t *testing.T) {
13 changes: 6 additions & 7 deletions js/modules/k6/http/async_request_test.go
Original file line number Diff line number Diff line change
@@ -167,13 +167,12 @@ func TestAsyncRequestErrors(t *testing.T) {
require.ErrorContains(t, err, "another error")

logEntry := hook.LastEntry()
if assert.NotNil(t, logEntry) {
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
err, ok := logEntry.Data["error"].(error)
require.True(t, ok)
assert.ErrorContains(t, err, "unsupported protocol scheme")
assert.Equal(t, "Request Failed", logEntry.Message)
}
require.NotNil(t, logEntry)
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
err, ok := logEntry.Data["error"].(error)
require.True(t, ok)
assert.ErrorContains(t, err, "unsupported protocol scheme")
assert.Equal(t, "Request Failed", logEntry.Message)
})
})
t.Run("InvalidURL", func(t *testing.T) {
5 changes: 2 additions & 3 deletions js/modules/k6/http/http_test.go
Original file line number Diff line number Diff line change
@@ -39,9 +39,8 @@ func TestTagURL(t *testing.T) {
tag, err := httpext.NewURL(data.u, data.n)
require.NoError(t, err)
v, err := rt.RunString("http.url`" + expr + "`")
if assert.NoError(t, err) {
assert.Equal(t, tag, v.Export())
}
require.NoError(t, err)
assert.Equal(t, tag, v.Export())
})
}
}
20 changes: 10 additions & 10 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
@@ -242,11 +242,10 @@ func TestRequest(t *testing.T) {
assert.NoError(t, err)

logEntry := hook.LastEntry()
if assert.NotNil(t, logEntry) {
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
assert.Equal(t, sr("HTTPBIN_URL/redirect/11"), logEntry.Data["url"])
assert.Equal(t, "Stopped after 11 redirects and returned the redirection; pass { redirects: n } in request params or set global maxRedirects to silence this", logEntry.Message)
}
require.NotNil(t, logEntry)
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
assert.Equal(t, sr("HTTPBIN_URL/redirect/11"), logEntry.Data["url"])
assert.Equal(t, "Stopped after 11 redirects and returned the redirection; pass { redirects: n } in request params or set global maxRedirects to silence this", logEntry.Message)
})
})
t.Run("requestScopeRedirects", func(t *testing.T) {
@@ -529,11 +528,12 @@ func TestRequest(t *testing.T) {
assert.Contains(t, err.Error(), "another error")

logEntry := hook.LastEntry()
if assert.NotNil(t, logEntry) {
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
assert.Contains(t, logEntry.Data["error"].(error).Error(), "unsupported protocol scheme")
assert.Equal(t, "Request Failed", logEntry.Message)
}
require.NotNil(t, logEntry)
assert.Equal(t, logrus.WarnLevel, logEntry.Level)
err, ok := logEntry.Data["error"].(error)
require.True(t, ok)
assert.ErrorContains(t, err, "unsupported protocol scheme")
assert.Equal(t, "Request Failed", logEntry.Message)
})
})
t.Run("InvalidURL", func(t *testing.T) {
21 changes: 10 additions & 11 deletions js/modules/k6/http/response_test.go
Original file line number Diff line number Diff line change
@@ -149,19 +149,18 @@ func TestResponse(t *testing.T) {

t.Run("group", func(t *testing.T) {
g, err := root.Group("my group")
if assert.NoError(t, err) {
old := state.Group
state.Group = g
require.NoError(t, err)
old := state.Group
state.Group = g
state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) {
tagsAndMeta.SetTag("group", g.Path)
})
defer func() {
state.Group = old
state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) {
tagsAndMeta.SetTag("group", g.Path)
tagsAndMeta.SetTag("group", old.Path)
})
defer func() {
state.Group = old
state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) {
tagsAndMeta.SetTag("group", old.Path)
})
}()
}
}()

_, err = rt.RunString(sr(`
var res = http.request("GET", "HTTPBIN_URL/html");
13 changes: 6 additions & 7 deletions lib/options_test.go
Original file line number Diff line number Diff line change
@@ -275,13 +275,12 @@ func TestOptions(t *testing.T) {

var opts2 Options
assert.NoError(t, json.Unmarshal(optsData, &opts2))
if assert.Len(t, opts2.TLSAuth, len(opts.TLSAuth)) {
for i := 0; i < len(opts2.TLSAuth); i++ {
assert.Equal(t, opts.TLSAuth[i].TLSAuthFields, opts2.TLSAuth[i].TLSAuthFields)
cert, err := opts2.TLSAuth[i].Certificate()
assert.NoError(t, err)
assert.NotNil(t, cert)
}
require.Len(t, opts2.TLSAuth, len(opts.TLSAuth))
for i := 0; i < len(opts2.TLSAuth); i++ {
assert.Equal(t, opts.TLSAuth[i].TLSAuthFields, opts2.TLSAuth[i].TLSAuthFields)
cert, err := opts2.TLSAuth[i].Certificate()
assert.NoError(t, err)
assert.NotNil(t, cert)
}
})

0 comments on commit e565316

Please sign in to comment.