Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the test coverage about DefaultVerifier #227

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package spectest

import (
"fmt"
"net/http"
"testing"
)

type mockTestingT struct{}

func (m *mockTestingT) Errorf(format string, args ...interface{}) {}
func (m *mockTestingT) Fatal(args ...interface{}) {}
func (m *mockTestingT) Fatalf(format string, args ...interface{}) {}
func (m *mockTestingT) Name() string { return "mock" }

func TestApiTestAssertStatusCodes(t *testing.T) {
tests := []struct {
responseStatus []int
Expand All @@ -30,3 +38,215 @@ func TestApiTestAssertStatusCodes(t *testing.T) {
}
}
}

func Test_DefaultVerifier_True(t *testing.T) {
t.Parallel()
verifier := &DefaultVerifier{}
mock := &mockTestingT{}
tests := []struct {
name string
args bool
want bool
}{
{
name: "should return true",
args: true,
want: true,
},
{
name: "should return false",
args: false,
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := verifier.True(mock, tt.args)
if actual != tt.want {
t.Fatalf("Expected %t but received %t", actual, tt.want)
}
})
}
}

func Test_DefaultVerifier_JSONEq(t *testing.T) {
t.Parallel()

verifier := &DefaultVerifier{}
mock := &mockTestingT{}

type args struct {
expected string
actual string
}

tests := []struct {
name string
args args
want bool
}{
{
name: "should return true",
args: args{
expected: `{"name":"John","age":30,"car":null}`,
actual: `{"name":"John","age":30,"car":null}`,
},
want: true,
},
{
name: "should failure with different values",
args: args{
expected: `{"name":"John","age":30,"car":null}`,
actual: `{"name":"John","age":31,"car":null}`,
},
want: false,
},
{
name: "should failure to parse expected",
args: args{
expected: `{"name":"John","age":30,"car":null`,
actual: `{"name":"John","age":30,"car":null}`,
},
want: false,
},
{
name: "should failure to parse actual",
args: args{
expected: `{"name":"John","age":30,"car":null}`,
actual: `{"name":"John","age":30,"car":null`,
},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := verifier.JSONEq(mock, tt.args.expected, tt.args.actual)
if actual != tt.want {
t.Fatalf("Expected %t but received %t", actual, tt.want)
}
})
}
}

func Test_DefaultVerifier_Equal(t *testing.T) {
t.Parallel()

verifier := &DefaultVerifier{}
mock := &mockTestingT{}

var notOperationFunc = func() {}

type args struct {
expected interface{}
actual interface{}
}

tests := []struct {
name string
args args
want bool
}{
{
name: "should return true",
args: args{
expected: 1,
actual: 1,
},
want: true,
},
{
name: "should return false because not operation function was given",
args: args{
expected: notOperationFunc,
actual: notOperationFunc,
},
want: false,
},
{
name: "should return false because different values",
args: args{
expected: 1,
actual: 2,
},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := verifier.Equal(mock, tt.args.expected, tt.args.actual)
if actual != tt.want {
t.Fatalf("Expected %t but received %t", actual, tt.want)
}
})
}
}

func Test_DefaultVerifier_Fail(t *testing.T) {
t.Parallel()

verifier := &DefaultVerifier{}
mock := &mockTestingT{}

tests := []struct {
name string
args []interface{}
t TestingT
}{
{
// FIXME: change the name of this test more better
name: "pat1",
args: []interface{}{},
t: mock,
},
{
name: "pat2",
args: []interface{}{"foo"},
t: mock,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := verifier.Fail(tt.t, tt.name, tt.args...)
if res {
t.Fatal("Expected false but received true")
}
})
}
}

func Test_DefaultVerifier_NoError(t *testing.T) {
t.Parallel()

verifier := &DefaultVerifier{}
mock := &mockTestingT{}

tests := []struct {
name string
args error
want bool
}{
{
name: "should return true",
args: nil,
want: true,
},
{
name: "should return false",
args: fmt.Errorf("error"),
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := verifier.NoError(mock, tt.args)
if actual != tt.want {
t.Fatalf("Expected %t but received %t", actual, tt.want)
}
})
}
}