Skip to content

Commit

Permalink
refactor(test): common helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Mar 8, 2022
1 parent b5f4831 commit 8d7d490
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
41 changes: 41 additions & 0 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testutils

import (
"io"
"os"

"github.com/google/go-cmp/cmp"
)

type Helper interface {
Helper()
Fatalf(string, ...interface{})
}

func GoldenData(t Helper, identifier string) []byte {
t.Helper()

goldenPath := "testdata/" + identifier + ".golden"

f, err := os.Open(goldenPath)
if err != nil {
t.Fatalf("Error opening file %s: %s", goldenPath, err)
}
defer f.Close()

data, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Error reading file %s: %s", goldenPath, err)
}

return data
}

func Diff(t Helper, x interface{}, y interface{}) {
t.Helper()

diff := cmp.Diff(x, y)
if diff != "" {
t.Fatalf(diff)
}
}
58 changes: 14 additions & 44 deletions pkg/asciicast/asciicast_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package asciicast_test

import (
"io"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/mrmarble/termsvg/internal/testutils"
"github.com/mrmarble/termsvg/pkg/asciicast"
"github.com/sebdah/goldie/v2"
)

func TestReadRecords(t *testing.T) {
golden := goldenData(t, "TestUnmarshal")
golden := testutils.GoldenData(t, "TestUnmarshal")

record, err := asciicast.Unmarshal(golden)
if err != nil {
Expand All @@ -34,7 +32,7 @@ func TestReadRecords(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
diff(t, tc.output, tc.input)
testutils.Diff(t, tc.output, tc.input)
})
}
}
Expand All @@ -58,7 +56,7 @@ func TestToRelativeTime(t *testing.T) {

for _, event := range cast.Events {
t.Run(event.EventData, func(t *testing.T) {
diff(t, event.Time, float64(1))
testutils.Diff(t, event.Time, float64(1))
})
}
}
Expand All @@ -69,19 +67,19 @@ func TestCompress(t *testing.T) {

cast.Compress()

diff(t, len(cast.Events), 2)
diff(t, cast.Events[0].EventData, "FirstSecond")
diff(t, cast.Events[1].EventData, "Third")
testutils.Diff(t, len(cast.Events), 2)
testutils.Diff(t, cast.Events[0].EventData, "FirstSecond")
testutils.Diff(t, cast.Events[1].EventData, "Third")
}

func TestToAbsoluteTime(t *testing.T) {
cast := setup(t)

cast.ToAbsoluteTime()

diff(t, cast.Events[0].Time, float64(1))
diff(t, cast.Events[1].Time, float64(3))
diff(t, cast.Events[2].Time, float64(6))
testutils.Diff(t, cast.Events[0].Time, float64(1))
testutils.Diff(t, cast.Events[1].Time, float64(3))
testutils.Diff(t, cast.Events[2].Time, float64(6))
}

func TestCapRelativeTime(t *testing.T) {
Expand All @@ -91,7 +89,7 @@ func TestCapRelativeTime(t *testing.T) {

for _, event := range cast.Events {
t.Run(event.EventData, func(t *testing.T) {
diff(t, event.Time, 0.5)
testutils.Diff(t, event.Time, 0.5)
})
}
}
Expand All @@ -101,9 +99,9 @@ func TestAdjustSpeed(t *testing.T) {

cast.AdjustSpeed(2.0)

diff(t, cast.Events[0].Time, float64(0.5))
diff(t, cast.Events[1].Time, float64(1))
diff(t, cast.Events[2].Time, float64(1.5))
testutils.Diff(t, cast.Events[0].Time, float64(0.5))
testutils.Diff(t, cast.Events[1].Time, float64(1))
testutils.Diff(t, cast.Events[2].Time, float64(1.5))
}

func setup(t *testing.T) *asciicast.Cast {
Expand All @@ -122,31 +120,3 @@ func setup(t *testing.T) *asciicast.Cast {

return cast
}

func diff(t *testing.T, x interface{}, y interface{}) {
t.Helper()

diff := cmp.Diff(x, y)
if diff != "" {
t.Fatalf(diff)
}
}

func goldenData(t *testing.T, identifier string) []byte {
t.Helper()

goldenPath := "testdata/" + identifier + ".golden"

f, err := os.Open(goldenPath)
if err != nil {
t.Fatalf("Error opening file %s: %s", goldenPath, err)
}
defer f.Close()

data, err := io.ReadAll(f)
if err != nil {
t.Fatalf("Error reading file %s: %s", goldenPath, err)
}

return data
}
13 changes: 2 additions & 11 deletions pkg/css/css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package css_test
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/mrmarble/termsvg/internal/testutils"
"github.com/mrmarble/termsvg/pkg/css"
)

Expand All @@ -21,16 +21,7 @@ func TestParse(t *testing.T) {

for name, test := range tests {
t.Run(name, func(t *testing.T) {
diff(t, test.input.Compile(), test.output)
testutils.Diff(t, test.input.Compile(), test.output)
})
}
}

func diff(t *testing.T, x interface{}, y interface{}) {
t.Helper()

diff := cmp.Diff(x, y)
if diff != "" {
t.Fatalf(diff)
}
}

0 comments on commit 8d7d490

Please sign in to comment.