Repository: MarvinJWendt/testza
Pull request #89 (2022-03-20 01:19:12)

test: added output consistency tests


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 693fd12..6e5f689 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,12 @@
 ### Features
 - print custom messages
 
+### Bug Fixes
+- fixed nested snapshots
+
+### Test
+- added output consistency tests for failed assertions
+
 ### Code Refactoring
 - renamed `Mock` to `Fuzz`
 
diff --git a/README.md b/README.md
index ab323c1..0068e64 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@
 </a>
 
 <a href="https://codecov.io/gh/MarvinJWendt/testza">
-<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3142-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
+<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3160-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
 </a>
   
 <a href="https://pkg.go.dev/github.com/MarvinJWendt/testza" target="_blank">
diff --git a/snapshot.go b/snapshot.go
index 6019e83..a6c8b06 100644
--- a/snapshot.go
+++ b/snapshot.go
@@ -114,6 +114,12 @@ func snapshotValidateFromDir(dir string, t testRunner, name string, actual inter
 func SnapshotCreateOrValidate(t testRunner, name string, object interface{}, msg ...interface{}) error {
 	dir := getCurrentScriptDirectory() + "/testdata/snapshots/"
 	snapshotPath := path.Clean(dir + name + ".testza")
+	if strings.Contains(name, "/") {
+		err := os.MkdirAll(path.Dir(snapshotPath), 0600)
+		if err != nil {
+			return fmt.Errorf("creating snapshot directories failed: %w", err)
+		}
+	}
 
 	if _, err := os.Stat(snapshotPath); err == nil {
 		err = snapshotValidateFromDir(dir, t, name, object, msg...)


Test patch:
diff --git a/assert_test.go b/assert_test.go
index 65dc2c4..a7563a3 100644
--- a/assert_test.go
+++ b/assert_test.go
@@ -17,6 +17,45 @@ import (
 	. "github.com/MarvinJWendt/testza"
 )
 
+type testMock struct {
+	ErrorCalled  bool
+	ErrorMessage string
+}
+
+func (m *testMock) fail(msg ...interface{}) {
+	m.ErrorCalled = true
+	m.ErrorMessage = fmt.Sprint(msg...)
+}
+
+func (m *testMock) Error(args ...interface{}) {
+	m.fail(args...)
+}
+
+// Errorf is a mock of testing.T.
+func (m *testMock) Errorf(format string, args ...interface{}) {
+	m.fail(fmt.Sprintf(format, args...))
+}
+
+// Fail is a mock of testing.T.
+func (m *testMock) Fail() {
+	m.fail()
+}
+
+// FailNow is a mock of testing.T.
+func (m *testMock) FailNow() {
+	m.fail()
+}
+
+// Fatal is a mock of testing.T.
+func (m *testMock) Fatal(args ...interface{}) {
+	m.fail(args...)
+}
+
+// Fatalf is a mock of testing.T.
+func (m *testMock) Fatalf(format string, args ...interface{}) {
+	m.fail(fmt.Sprintf(format, args...))
+}
+
 type assertionTestStruct struct {
 	Name string
 	Age  int
@@ -1272,3 +1311,35 @@ func TestAssertSubset_fail(t *testing.T) {
 		AssertNoSubset(t, []assertionTestStruct{s1, s2, s3}, []assertionTestStruct{s3, s4})
 	})
 }
+
+// -- Assert output consistency --
+func TestOutputConsistency(t *testing.T) {
+	var tm testMock
+
+	t.Run("failed test", func(t *testing.T) {
+		pterm.DisableStyling()
+		AssertEqual(&tm, true, false)
+		pterm.EnableStyling()
+
+		err := SnapshotCreateOrValidate(t, t.Name(), tm.ErrorMessage)
+		AssertNoError(t, err)
+	})
+
+	t.Run("failed test with custom msg", func(t *testing.T) {
+		pterm.DisableStyling()
+		AssertEqual(&tm, true, false, "Custom message!")
+		pterm.EnableStyling()
+
+		err := SnapshotCreateOrValidate(t, t.Name(), tm.ErrorMessage)
+		AssertNoError(t, err)
+	})
+
+	t.Run("failed test with custom formatted msg", func(t *testing.T) {
+		pterm.DisableStyling()
+		AssertEqual(&tm, true, false, "Custom %s!", "message")
+		pterm.EnableStyling()
+
+		err := SnapshotCreateOrValidate(t, t.Name(), tm.ErrorMessage)
+		AssertNoError(t, err)
+	})
+}
diff --git a/testdata/snapshots/TestOutputConsistency/failed_test.testza b/testdata/snapshots/TestOutputConsistency/failed_test.testza
new file mode 100644
index 0000000..87140ee
--- /dev/null
+++ b/testdata/snapshots/TestOutputConsistency/failed_test.testza
@@ -0,0 +1,1 @@
+(string) (len=139) "\n\n   0| Two objects that should be equal, are not equal.\n   1| \n   2| Expected:\n   3| (bool) true\n   4| \n   5| Actual:\n   6| (bool) false\n\n"
diff --git a/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_formatted_msg.testza b/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_formatted_msg.testza
new file mode 100644
index 0000000..f96d68d
--- /dev/null
+++ b/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_formatted_msg.testza
@@ -0,0 +1,1 @@
+(string) (len=177) "\n\n   0| Two objects that should be equal, are not equal.\n   1| \n   2| Message: Custom message!\n   3| \n   4| Expected:\n   5| (bool) true\n   6| \n   7| Actual:\n   8| (bool) false\n\n"
diff --git a/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_msg.testza b/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_msg.testza
new file mode 100644
index 0000000..f96d68d
--- /dev/null
+++ b/testdata/snapshots/TestOutputConsistency/failed_test_with_custom_msg.testza
@@ -0,0 +1,1 @@
+(string) (len=177) "\n\n   0| Two objects that should be equal, are not equal.\n   1| \n   2| Message: Custom message!\n   3| \n   4| Expected:\n   5| (bool) true\n   6| \n   7| Actual:\n   8| (bool) false\n\n"