package assert import ( "reflect" "strings" "testing" "git.runcible.io/learning/pulley/internal/data" ) // Equal a generic function to test equivalence between two values // of the same type func Equal[T comparable](t *testing.T, actual, expected T) { //The t.Helper() function that we’re using in the code above indicates // to the Go test runner that our Equal() function is a test helper. // This means that when t.Errorf() is called from our Equal() function, // the Go test runner will report the filename and line number of the // code which called our Equal() function in the output. t.Helper() if actual != expected { t.Errorf("got: %v; want %v", actual, expected) } } func StringContains(t *testing.T, actual, expectedSubstring string) { t.Helper() if !strings.Contains(actual, expectedSubstring) { t.Errorf("got: %q; expected to contain %q", actual, expectedSubstring) } } func NilError(t *testing.T, actual error) { t.Helper() if actual != nil { t.Errorf("got: %v; expected: nil", actual) } } // MovieEqual is a helper function that compares movies attributes with // out checking timestamps func MovieEqual(t *testing.T, actual, expected data.Movie) { t.Helper() // This ensures the error points to the test, not this helper line if actual.ID != expected.ID { t.Errorf("ID: got %d; want %d", actual.ID, expected.ID) } if actual.Title != expected.Title { t.Errorf("Title: got %q; want %q", actual.Title, expected.Title) } if actual.Year != expected.Year { t.Errorf("Year: got %d; want %d", actual.Year, expected.Year) } if actual.Runtime != expected.Runtime { t.Errorf("Runtime: got %v; want %v", actual.Runtime, expected.Runtime) } if actual.Version != expected.Version { t.Errorf("Version: got %d; want %d", actual.Version, expected.Version) } // For slices, use reflect.DeepEqual or a simple loop if !reflect.DeepEqual(actual.Genres, expected.Genres) { t.Errorf("Genres: got %v; want %v", actual.Genres, expected.Genres) } }