Skip to content
Merged
Show file tree
Hide file tree
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
43 changes: 37 additions & 6 deletions enable/stubs/colors/enable_colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,56 @@
package colors

import (
"fmt"
"testing"

target "github.com/go-openapi/testify/v2/assert"
colorstub "github.com/go-openapi/testify/v2/internal/assertions/enable/colors"
)

func TestEnableColors(t *testing.T) {
t.Parallel()
// Test execution is serialized on the sensitive package initialization section
//
// Proper test coverage of Enable() is assured by the testintegration package.

for _, opt := range []Option{
WithSanitizedTheme("light"),
WithDark(),
WithLight(),
WithTheme(ThemeDark),
} {
mock := new(mockT)
target.NotPanics(mock, func() {
testEnableWithLock(opt) // executed serially

_ = target.JSONEq(mock, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`)
})
// we may call several times with different options, but only the first setup is going to be used:
// colorization is set lazily with a sync.Once global by the consuming package.
t.Log(mock.errorString())
}
}

func testEnableWithLock(opt Option) {
colorstub.Enable(
func() []colorstub.Option {
return []colorstub.Option{
colorstub.WithEnable(true),
colorstub.WithSanitizedTheme("light"),
opt,
}
})
}

type mockT struct {
errorFmt string
args []any
}

func (m *mockT) Errorf(format string, args ...any) {
m.errorFmt = format
m.args = args
}

mock := new(testing.T)
target.NotPanics(mock, func() {
_ = target.JSONEq(mock, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`)
})
func (m *mockT) errorString() string {
return fmt.Sprintf(m.errorFmt, m.args...)
}
66 changes: 66 additions & 0 deletions enable/yaml/forward_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,69 @@ func TestYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
t.Error("YAMLEq should return false")
}
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if !assert.YAMLEqBytes([]byte(expectedYAML), []byte(actualYAML)) {
t.Error("YAMLEqBytes should return true")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if assert.YAMLEqBytes([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)) {
t.Error("YAMLEqBytes should return false")
}
})
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if !assert.YAMLEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "equivalent") {
t.Error("YAMLEqf should return true")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if assert.YAMLEqf(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "not equivalent") {
t.Error("YAMLEqf should return false")
}
})
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if !assert.YAMLEqBytesf([]byte(expectedYAML), []byte(actualYAML), yamlCheckMsg, "equivalent bytes") {
t.Error("YAMLEqBytesf should return true")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

assert := target.New(new(testing.T))
if assert.YAMLEqBytesf([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`), yamlCheckMsg, "not equivalent bytes") {
t.Error("YAMLEqBytesf should return false")
}
})
}
104 changes: 94 additions & 10 deletions enable/yaml/forward_requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestRequireYAMLEqWrapper_EqualYAMLString(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
Expand All @@ -24,7 +24,7 @@ func TestRequireYAMLEqWrapper_EqualYAMLString(t *testing.T) {
func TestRequireYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
Expand All @@ -36,7 +36,7 @@ func TestRequireYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
func TestRequireYAMLEqWrapper_HashOfArraysAndHashes(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

expected := `
Expand Down Expand Up @@ -74,7 +74,7 @@ array:
func TestRequireYAMLEqWrapper_Array(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
Expand All @@ -86,7 +86,7 @@ func TestRequireYAMLEqWrapper_Array(t *testing.T) {
func TestRequireYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
Expand All @@ -98,7 +98,7 @@ func TestRequireYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
func TestRequireYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
Expand All @@ -110,7 +110,7 @@ func TestRequireYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
func TestRequireYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`{"foo": "bar"}`, "Simple String")
Expand All @@ -122,7 +122,7 @@ func TestRequireYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
func TestRequireYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq("Simple String", `{"foo": "bar", "hello": "world"}`)
Expand All @@ -134,7 +134,7 @@ func TestRequireYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
func TestRequireYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq("Simple String", "Simple String")
Expand All @@ -146,11 +146,95 @@ func TestRequireYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
func TestRequireYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
t.Parallel()

mock := new(MockT)
mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
if !mock.Failed {
t.Error("Check should fail")
}
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqBytes([]byte(expectedYAML), []byte(actualYAML))
if mock.Failed {
t.Error("Check should pass")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqBytes([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
if !mock.Failed {
t.Error("Check should fail")
}
})
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "equivalent")
if mock.Failed {
t.Error("Check should pass")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqf(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "not equivalent")
if !mock.Failed {
t.Error("Check should fail")
}
})
}

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

t.Run("should pass", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqBytesf([]byte(expectedYAML), []byte(actualYAML), yamlCheckMsg, "equivalent bytes")
if mock.Failed {
t.Error("Check should pass")
}
})

t.Run("should fail", func(t *testing.T) {
t.Parallel()

mock := new(mockT)
mockRequire := target.New(mock)

mockRequire.YAMLEqBytesf([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`), yamlCheckMsg, "not equivalent bytes")
if !mock.Failed {
t.Error("Check should fail")
}
})
}
Loading
Loading