Skip to content

Commit

Permalink
Merge pull request #12 from muonsoft/new-assertions
Browse files Browse the repository at this point in the history
New assertions
  • Loading branch information
strider2038 authored Jan 25, 2024
2 parents 0acc792 + a2f2e2c commit 1c6c9af
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.51
version: v1.54

- name: Run tests
run: go test -v $(go list ./... | grep -v vendor)
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ issues:
- scopelint
- gochecknoglobals
- goerr113

linters-settings:
depguard:
rules:
main:
files:
- $all
- "!$test"
- "!**/test/**/*"
allow:
- $gostd
- github.com
test:
files:
- "$test"
allow:
- $gostd
- github.com
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func TestYourAPI(t *testing.T) {

// fluent string assertions
json.Node("stringNode").IsString()
json.Node("emptyString").IsString().IsEmpty()
json.Node("stringNode").IsString().IsNotEmpty()
json.Node("stringNode").IsString().EqualTo("stringValue")
json.Node("stringNode").IsString().EqualToOneOf("stringValue", "nextValue")
json.Node("stringNode").IsString().NotEqualTo("invalid")
Expand All @@ -115,6 +117,8 @@ func TestYourAPI(t *testing.T) {

// numeric assertions
json.Node("integerNode").IsInteger()
json.Node("zeroInteger").IsInteger().IsZero()
json.Node("integerNode").IsInteger().IsNotZero()
json.Node("integerNode").IsInteger().EqualTo(123)
json.Node("integerNode").IsInteger().NotEqualTo(321)
json.Node("integerNode").IsInteger().GreaterThan(122)
Expand All @@ -123,6 +127,8 @@ func TestYourAPI(t *testing.T) {
json.Node("integerNode").IsInteger().LessThanOrEqual(123)
json.Node("floatNode").IsFloat()
json.Node("floatNode").IsNumber()
json.Node("zeroFloat").IsNumber().IsZero()
json.Node("floatNode").IsNumber().IsNotZero()
json.Node("floatNode").IsNumber().EqualTo(123.123)
json.Node("floatNode").IsNumber().NotEqualTo(321.123)
json.Node("floatNode").IsNumber().EqualToWithDelta(123.123, 0.1)
Expand Down Expand Up @@ -244,6 +250,14 @@ func TestYourAPI(t *testing.T) {
assert.Equal(t, "23e98a0c-26c8-410f-978f-d1d67228af87", json.Node("uuid").IsUUID().Value().String())
assert.Equal(t, "23e98a0c-26c8-410f-978f-d1d67228af87", json.Node("uuid").UUID().String())

// standalone JWT assertion
assertjson.IsJWT(t,
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiaHR0cHM6Ly9hdWRpZW5jZTEuZXhhbXBsZS5jb20iLCJodHRwczovL2F1ZGllbmNlMi5leGFtcGxlLmNvbSJdLCJleHAiOjQ4MjAzNjAxMzEsImlhdCI6MTY2Njc1NjUzMSwiaXNzIjoiaHR0cHM6Ly9pc3N1ZXIuZXhhbXBsZS5jb20iLCJqdGkiOiJhYmMxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsIm5iZiI6MTY2Njc1NjUzMSwic3ViIjoiaHR0cHM6Ly9zdWJqZWN0LmV4YW1wbGUuY29tIn0.fGUvIn-BV8bPKkZdrxUneew3_qBe-knptL9a_TkNA4M",
func(token *jwt.Token) (interface{}, error) { return []byte("your-256-bit-secret"), nil },
).WithPayload(func(json *assertjson.AssertJSON) {
json.Node("name").IsString().EqualTo("John Doe")
})

// debug helpers
json.Node("bookstore", "books", 1).Print()
})
Expand Down
120 changes: 119 additions & 1 deletion assertjson/assertjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func TestFileHas(t *testing.T) {

// fluent string assertions
json.Node("stringNode").IsString()
json.Node("emptyString").IsString().IsEmpty()
json.Node("stringNode").IsString().IsNotEmpty()
json.Node("stringNode").IsString().EqualTo("stringValue")
json.Node("stringNode").IsString().EqualToOneOf("stringValue", "nextValue")
json.Node("stringNode").IsString().NotEqualTo("invalid")
Expand All @@ -67,6 +69,8 @@ func TestFileHas(t *testing.T) {

// numeric assertions
json.Node("integerNode").IsInteger()
json.Node("zeroInteger").IsInteger().IsZero()
json.Node("integerNode").IsInteger().IsNotZero()
json.Node("integerNode").IsInteger().EqualTo(123)
json.Node("integerNode").IsInteger().NotEqualTo(321)
json.Node("integerNode").IsInteger().GreaterThan(122)
Expand All @@ -81,6 +85,8 @@ func TestFileHas(t *testing.T) {
json.Node("integerNode").IsNumberLessThanOrEqual(123)
json.Node("floatNode").IsFloat()
json.Node("floatNode").IsNumber()
json.Node("zeroFloat").IsNumber().IsZero()
json.Node("floatNode").IsNumber().IsNotZero()
json.Node("floatNode").IsNumber().EqualTo(123.123)
json.Node("floatNode").IsNumber().NotEqualTo(321.123)
json.Node("floatNode").IsNumber().EqualToWithDelta(123.123, 0.1)
Expand Down Expand Up @@ -227,6 +233,14 @@ func TestFileHas(t *testing.T) {
Raw,
)

// standalone JWT assertion
assertjson.IsJWT(t,
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiaHR0cHM6Ly9hdWRpZW5jZTEuZXhhbXBsZS5jb20iLCJodHRwczovL2F1ZGllbmNlMi5leGFtcGxlLmNvbSJdLCJleHAiOjQ4MjAzNjAxMzEsImlhdCI6MTY2Njc1NjUzMSwiaXNzIjoiaHR0cHM6Ly9pc3N1ZXIuZXhhbXBsZS5jb20iLCJqdGkiOiJhYmMxMjM0NSIsIm5hbWUiOiJKb2huIERvZSIsIm5iZiI6MTY2Njc1NjUzMSwic3ViIjoiaHR0cHM6Ly9zdWJqZWN0LmV4YW1wbGUuY29tIn0.fGUvIn-BV8bPKkZdrxUneew3_qBe-knptL9a_TkNA4M",
func(token *jwt.Token) (interface{}, error) { return []byte("your-256-bit-secret"), nil },
).WithPayload(func(json *assertjson.AssertJSON) {
json.Node("name").IsString().EqualTo("John Doe")
})

// debug helpers
json.Node("bookstore", "books", 1).Print()
})
Expand Down Expand Up @@ -488,6 +502,40 @@ func TestHas(t *testing.T) {
`value at path "key" is not a number`,
},
},
{
name: "JSON node is number is zero",
json: `{"key": 0}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsNumber().IsZero()
},
},
{
name: "JSON node is number is zero fails",
json: `{"key": 1.2}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsNumber().IsZero()
},
wantMessages: []string{
`failed asserting that JSON node "key": is zero, actual is 1.200000`,
},
},
{
name: "JSON node is number is not zero",
json: `{"key": 1.2}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsNumber().IsNotZero()
},
},
{
name: "JSON node is number is not zero fails",
json: `{"key": 0}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsNumber().IsNotZero()
},
wantMessages: []string{
`failed asserting that JSON node "key": is not zero`,
},
},
{
name: "JSON node is number equal to",
json: `{"key": 123.123}`,
Expand Down Expand Up @@ -652,6 +700,40 @@ func TestHas(t *testing.T) {
`value at path "key" is float, not integer`,
},
},
{
name: "JSON node is integer is zero",
json: `{"key": 0}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsInteger().IsZero()
},
},
{
name: "JSON node is integer is zero fails",
json: `{"key": 1}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsInteger().IsZero()
},
wantMessages: []string{
`failed asserting that JSON node "key": is zero, actual is 1`,
},
},
{
name: "JSON node is integer is not zero",
json: `{"key": 1}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsInteger().IsNotZero()
},
},
{
name: "JSON node is integer is not zero fails",
json: `{"key": 0}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsInteger().IsNotZero()
},
wantMessages: []string{
`failed asserting that JSON node "key": is not zero`,
},
},
{
name: "JSON node is integer equal to",
json: `{"key": 123}`,
Expand Down Expand Up @@ -788,6 +870,40 @@ func TestHas(t *testing.T) {
`failed asserting that JSON node "key" is string`,
},
},
{
name: "JSON node is string is empty",
json: `{"key": ""}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsString().IsEmpty()
},
},
{
name: "JSON node is string is empty fails",
json: `{"key": "value"}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsString().IsEmpty()
},
wantMessages: []string{
`failed asserting that JSON node "key": is empty string, actual is "value"`,
},
},
{
name: "JSON node is string is not empty",
json: `{"key": "value"}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsString().IsNotEmpty()
},
},
{
name: "JSON node is string is not empty fails",
json: `{"key": ""}`,
assert: func(json *assertjson.AssertJSON) {
json.Node("key").IsString().IsNotEmpty()
},
wantMessages: []string{
`failed asserting that JSON node "key": is not empty string`,
},
},
{
name: "JSON node is string equal to",
json: `{"key": "value"}`,
Expand Down Expand Up @@ -1053,6 +1169,8 @@ func TestHas(t *testing.T) {
assert: func(json *assertjson.AssertJSON) {
json.Node("key").
IsString().
IsEmpty().
IsNotEmpty().
EqualTo("").
NotEqualTo("").
EqualToOneOf("").
Expand Down Expand Up @@ -2700,7 +2818,7 @@ func TestAssertNode_Exists(t *testing.T) {

const tokenSecret = "your-256-bit-secret"

func getJWTSecret(token *jwt.Token) (interface{}, error) {
func getJWTSecret(_ *jwt.Token) (interface{}, error) {
return []byte(tokenSecret), nil
}

Expand Down
13 changes: 13 additions & 0 deletions assertjson/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import (
"github.com/stretchr/testify/assert"
)

// IsJWT asserts that the string contains valid JWT.
func IsJWT(t TestingT, value string, keyFunc jwt.Keyfunc) *JWTAssertion {
t.Helper()
token, err := jwt.Parse(value, keyFunc)
if err == nil {
return &JWTAssertion{t: t, token: token}
}

assert.Fail(t, fmt.Sprintf(`failed asserting that string contains JWT: %s`, err.Error()))

return nil
}

// JWTAssertion is used to build a chain of assertions for the JWT node.
type JWTAssertion struct {
t TestingT
Expand Down
62 changes: 62 additions & 0 deletions assertjson/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ type NumberAssertion struct {
value float64
}

// IsZero asserts that the JSON node has a numeric value equals to zero.
func (a *NumberAssertion) IsZero(msgAndArgs ...interface{}) *NumberAssertion {
if a == nil {
return nil
}
a.t.Helper()

if a.value != 0 {
a.fail(
fmt.Sprintf(`is zero, actual is %f`, a.value),
msgAndArgs...,
)
}

return nil
}

// IsNotZero asserts that the JSON node has a numeric value not equals to zero.
func (a *NumberAssertion) IsNotZero(msgAndArgs ...interface{}) *NumberAssertion {
if a == nil {
return nil
}
a.t.Helper()

if a.value == 0 {
a.fail(`is not zero`, msgAndArgs...)
}

return nil
}

// EqualTo asserts that the JSON node has a numeric value equals to the given value.
func (a *NumberAssertion) EqualTo(expected float64, msgAndArgs ...interface{}) *NumberAssertion {
if a == nil {
Expand Down Expand Up @@ -259,6 +290,37 @@ type IntegerAssertion struct {
value int
}

// IsZero asserts that the JSON node has an integer value equals to 0.
func (a *IntegerAssertion) IsZero(msgAndArgs ...interface{}) *IntegerAssertion {
if a == nil {
return nil
}
a.t.Helper()

if a.value != 0 {
a.fail(
fmt.Sprintf(`is zero, actual is %d`, a.value),
msgAndArgs...,
)
}

return nil
}

// IsNotZero asserts that the JSON node has an integer value not equals to 0.
func (a *IntegerAssertion) IsNotZero(msgAndArgs ...interface{}) *IntegerAssertion {
if a == nil {
return nil
}
a.t.Helper()

if a.value == 0 {
a.fail(`is not zero`, msgAndArgs...)
}

return nil
}

// EqualTo asserts that the JSON node has an integer value equals to the given value.
func (a *IntegerAssertion) EqualTo(expected int, msgAndArgs ...interface{}) *IntegerAssertion {
if a == nil {
Expand Down
29 changes: 29 additions & 0 deletions assertjson/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ type StringAssertion struct {
value string
}

// IsEmpty asserts that the JSON node has a string value equals to empty string.
func (a *StringAssertion) IsEmpty(msgAndArgs ...interface{}) *StringAssertion {
if a == nil {
return nil
}
a.t.Helper()
if a.value != "" {
a.fail(
fmt.Sprintf(`is empty string, actual is "%s"`, a.value),
msgAndArgs...,
)
}

return a
}

// IsNotEmpty asserts that the JSON node has a string value not equals to empty string.
func (a *StringAssertion) IsNotEmpty(msgAndArgs ...interface{}) *StringAssertion {
if a == nil {
return nil
}
a.t.Helper()
if a.value == "" {
a.fail(`is not empty string`, msgAndArgs...)
}

return a
}

// EqualTo asserts that the JSON node has a string value equals to the given value.
func (a *StringAssertion) EqualTo(expectedValue string, msgAndArgs ...interface{}) *StringAssertion {
if a == nil {
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/object.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"nullNode": null,
"falseBooleanNode": false,
"trueBooleanNode": true,
"emptyString": "",
"stringNode": "stringValue",
"zeroInteger": 0,
"integerNode": 123,
"zeroFloat": 0.0,
"floatNode": 123.123,
"uuid": "23e98a0c-26c8-410f-978f-d1d67228af87",
"nilUUID": "00000000-0000-0000-0000-000000000000",
Expand Down

0 comments on commit 1c6c9af

Please sign in to comment.