From d112f3fb84dbb7c6053afd919c6af10765c8065f Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 16 Jun 2016 17:55:50 -0400 Subject: [PATCH] Add some tests for bundlefile and improve the error messages for LoadFile Signed-off-by: Daniel Nephin --- testutil/assert/assert.go | 47 +++++++++++++++++++++++++++++++++++++++ testutil/pkg.go | 1 + 2 files changed, 48 insertions(+) create mode 100644 testutil/assert/assert.go create mode 100644 testutil/pkg.go diff --git a/testutil/assert/assert.go b/testutil/assert/assert.go new file mode 100644 index 0000000..fffdfd0 --- /dev/null +++ b/testutil/assert/assert.go @@ -0,0 +1,47 @@ +// Package assert contains functions for making assertions in unit tests +package assert + +import ( + "strings" +) + +// TestingT is an interface which defines the methods of testing.T that are +// required by this package +type TestingT interface { + Fatalf(string, ...interface{}) +} + +// Equal compare the actual value to the expected value and fails the test if +// they are not equal. +func Equal(t TestingT, actual, expected interface{}) { + if expected != actual { + t.Fatalf("Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual) + } +} + +// NilError asserts that the error is nil, otherwise it fails the test. +func NilError(t TestingT, err error) { + if err != nil { + t.Fatalf("Expected no error, got: %s", err.Error()) + } +} + +// Error asserts that error is not nil, and contains the expected text, +// otherwise it fails the test. +func Error(t TestingT, err error, contains string) { + if err == nil { + t.Fatalf("Expected an error, but error was nil") + } + + if !strings.Contains(err.Error(), contains) { + t.Fatalf("Expected error to contain '%s', got '%s'", contains, err.Error()) + } +} + +// Contains asserts that the string contains a substring, otherwise it fails the +// test. +func Contains(t TestingT, actual, contains string) { + if !strings.Contains(actual, contains) { + t.Fatalf("Expected '%s' to contain '%s'", actual, contains) + } +} diff --git a/testutil/pkg.go b/testutil/pkg.go new file mode 100644 index 0000000..110b2e6 --- /dev/null +++ b/testutil/pkg.go @@ -0,0 +1 @@ +package testutil