-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
'Tester' package is moved from a separate repository into this one.
- Loading branch information
1 parent
99a48d2
commit 0fa84fa
Showing
5 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Tester | ||
|
||
This package provides various useful functions to perform tests. | ||
|
||
This package has methods for: | ||
* Assertions: | ||
* Equality – `MustBeEqual` | ||
* Difference – `MustBeDifferent` | ||
|
||
|
||
* Checking errors: | ||
* Error is set – `MustBeAnError` | ||
* No error is set – `MustBeNoError` | ||
|
||
|
||
These methods can be used to compare values, to check errors and even to make | ||
more complex comparisons from simple ones. | ||
|
||
When a comparison fails, it not only shows the values which failed, but also | ||
prints a 'Diff', a difference between these values. As opposed to many existing | ||
_Golang_ libraries in the Internet, this package uses the object-oriented | ||
programming style (O.O.P.) as in such mature programming languages as _C#_ | ||
and _Java_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package tester | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Test object helps to make tests. | ||
type Test struct { | ||
t *testing.T | ||
} | ||
|
||
// New creates a test. | ||
func New(t *testing.T) *Test { | ||
return &Test{t: t} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tester | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_New(t *testing.T) { | ||
var aTestingT *testing.T | ||
var result *Test | ||
|
||
aTestingT = new(testing.T) | ||
result = New(aTestingT) | ||
if result.t != aTestingT { | ||
t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package tester | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/kr/pretty" | ||
) | ||
|
||
// Assertion methods. | ||
// | ||
// Assertion methods help in comparing values and checking errors. This package | ||
// provides a convenient way to control mismatches. When the comparison fails, | ||
// it shows not only the two values which failed, but also a 'Diff' | ||
// (difference) between them. | ||
|
||
// Error messages. | ||
const ( | ||
ErrErrorWasExpected = "An Error was expected, but None was received" | ||
ErrfNoErrorWasExpected = "No Error was expected, but One was received: %v" | ||
ErrfNotEqual = `Values should be equal, but they are not. | ||
A=%v | ||
B=%v | ||
Diff=%v` | ||
ErrfNotDifferent = `Values should be different, but they are not. | ||
A=%v | ||
B=%v | ||
Diff=%v` | ||
) | ||
|
||
func errorIsSet(err error) bool { | ||
return err != nil | ||
} | ||
|
||
func errorIsEmpty(err error) bool { | ||
return err == nil | ||
} | ||
|
||
func interfacesAreEqual(a any, b any) bool { | ||
return reflect.DeepEqual(a, b) | ||
} | ||
|
||
func interfacesAreDifferent(a any, b any) bool { | ||
return !reflect.DeepEqual(a, b) | ||
} | ||
|
||
// MustBeAnError ensures that the error is not nil. | ||
// If the error is nil, it stops the test. | ||
func (test *Test) MustBeAnError(err error) { | ||
if errorIsEmpty(err) { | ||
test.t.Error(ErrErrorWasExpected) | ||
test.t.FailNow() | ||
} | ||
} | ||
|
||
// MustBeNoError ensures that the error is nil. | ||
// If the error is not nil, it stops the test. | ||
func (test *Test) MustBeNoError(err error) { | ||
if errorIsSet(err) { | ||
test.t.Errorf(ErrfNoErrorWasExpected, err) | ||
test.t.FailNow() | ||
} | ||
} | ||
|
||
// MustBeEqual ensures that two variables have equal values. | ||
// If not, it stops the test. | ||
func (test *Test) MustBeEqual(a any, b any) { | ||
if interfacesAreDifferent(a, b) { | ||
msg := fmt.Sprintf( | ||
ErrfNotEqual, | ||
pretty.Sprint(a), | ||
pretty.Sprint(b), | ||
pretty.Diff(a, b), | ||
) | ||
test.t.Errorf(msg) | ||
test.t.FailNow() | ||
} | ||
} | ||
|
||
// MustBeDifferent ensures that two variables have different values. | ||
// If not, it stops the test. | ||
func (test *Test) MustBeDifferent(a any, b any) { | ||
if interfacesAreEqual(a, b) { | ||
msg := fmt.Sprintf( | ||
ErrfNotDifferent, | ||
pretty.Sprint(a), | ||
pretty.Sprint(b), | ||
pretty.Diff(a, b), | ||
) | ||
test.t.Errorf(msg) | ||
test.t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package tester | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func Test_errorIsSet(t *testing.T) { | ||
var result bool | ||
|
||
// Test #1. Negative. | ||
result = errorIsSet(nil) | ||
if result != false { | ||
t.FailNow() | ||
} | ||
|
||
// Test #2. Positive. | ||
result = errorIsSet(errors.New("some error")) | ||
if result != true { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func Test_errorIsEmpty(t *testing.T) { | ||
var result bool | ||
|
||
// Test #1. Negative. | ||
result = errorIsEmpty(errors.New("some error")) | ||
if result != false { | ||
t.FailNow() | ||
} | ||
|
||
// Test #2. Positive. | ||
result = errorIsEmpty(nil) | ||
if result != true { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func Test_interfacesAreEqual(t *testing.T) { | ||
var result bool | ||
|
||
type TestTypeX struct { | ||
Age int | ||
Name string | ||
} | ||
|
||
// Test #1. Negative. | ||
result = interfacesAreEqual( | ||
TestTypeX{Age: 10, Name: "John"}, | ||
TestTypeX{Age: 11, Name: "Jack"}, | ||
) | ||
if result != false { | ||
t.FailNow() | ||
} | ||
|
||
// Test #2. Positive. | ||
result = interfacesAreEqual( | ||
TestTypeX{Age: 12, Name: "Meredith"}, | ||
TestTypeX{Age: 12, Name: "Meredith"}, | ||
) | ||
if result != true { | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func Test_interfacesAreDifferent(t *testing.T) { | ||
var result bool | ||
|
||
type TestTypeX struct { | ||
Age int | ||
Name string | ||
} | ||
|
||
// Test #1. Negative. | ||
result = interfacesAreDifferent( | ||
TestTypeX{Age: 12, Name: "Meredith"}, | ||
TestTypeX{Age: 12, Name: "Meredith"}, | ||
) | ||
if result != false { | ||
t.FailNow() | ||
} | ||
|
||
// Test #2. Positive. | ||
result = interfacesAreDifferent( | ||
TestTypeX{Age: 10, Name: "John"}, | ||
TestTypeX{Age: 11, Name: "Jack"}, | ||
) | ||
if result != true { | ||
t.FailNow() | ||
} | ||
} |