pax_global_header 0000666 0000000 0000000 00000000064 14253467360 0014524 g ustar 00root root 0000000 0000000 52 comment=dcc6e35a95146d4568c4e426265968ffa04cf322
expectate-1.3.1/ 0000775 0000000 0000000 00000000000 14253467360 0013510 5 ustar 00root root 0000000 0000000 expectate-1.3.1/.github/ 0000775 0000000 0000000 00000000000 14253467360 0015050 5 ustar 00root root 0000000 0000000 expectate-1.3.1/.github/workflows/ 0000775 0000000 0000000 00000000000 14253467360 0017105 5 ustar 00root root 0000000 0000000 expectate-1.3.1/.github/workflows/go.yml 0000664 0000000 0000000 00000000657 14253467360 0020245 0 ustar 00root root 0000000 0000000 name: Test
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
- name: Check out code
uses: actions/checkout@v2
- name: Get dependencies
run: go get -v -t -d ./...
- name: Test
run: go test -v ./...
expectate-1.3.1/LICENSE 0000664 0000000 0000000 00000002061 14253467360 0014514 0 ustar 00root root 0000000 0000000 MIT License
Copyright (c) 2020 Steven C Kaufman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
expectate-1.3.1/README.md 0000664 0000000 0000000 00000014606 14253467360 0014776 0 ustar 00root root 0000000 0000000 ---
# Expectate: A lightweight testing utility for golang
---
# Quick Start
### Install:
```
go get github.com/gomagedon/expectate
```
```golang
package mylibrary_test
import (
"testing"
"github.com/gomagedon/expectate"
"github.com/foo/mylibrary"
)
func TestProject(t *testing.T) {
expect := expectate.Expect(t)
expect(mylibrary.HelloWorld()).ToBe("Hello world!\n")
}
```
## Test Equality of Two Structs
```golang
type Person struct {
Name string
Age int
Occupation string
}
func TestPeopleAreTheSame(t *testing.T) {
expect := expectate.Expect(t)
person1 := Person{
Name: "John Doe",
Age: 31,
Occupation: "Electrician",
}
person2 := Person{
Name: "John Smith",
Age: 31,
Occupation: "Electrician",
}
expect(person1).ToEqual(person2)
}
```
Output:
```
--- FAIL: TestPeopleAreTheSame
expect.go:46: expectate_test.Person{
- Name: "John Smith",
+ Name: "John Doe",
Age: 31,
Occupation: "Electrician",
}
```
Note that expectate uses `google/go-cmp` for testing strict equality. Custom cmp options are not supported for the `ToEqual()` method, but you can always call the cmp library directly.
# Simple API
Expectate uses only 4 methods!
- ### [`Expect()`](#expect)
- ### [`expect().ToBe()`](#tobe)
- ### [`expect().ToEqual()`](#toequal)
- ### [`expect().NotToBe()`](#nottobe)
- ### [`expect().NotToEqual()`](#nottoequal)
---
Expect()
The `Expect()` method is at the top level of the `expectate` package. It takes `*testing.T` as a parameter and returns an `ExpectorFunc` type.
Here's an example:
```golang
func TestSomething(t *testing.T) {
expect := expectate.Expect(t)
expect(something).ToBe(somethingElse)
// ...some other expectations
}
```
Alternatively:
```golang
func TestSomething(t *testing.T) {
expectate.Expect(t)(something).ToBe(somethingElse)
// ...some other expectations
}
```
---
expect().ToBe()
The `expect().ToBe()` method exists on the `Expector` type (which is what `expect()` returns). It takes any value and performs a simple equality check with that value and the initial value passed to `expect()`. If the two values are equal, the test passes. If not, it calls `t.Fatal()` with a generic error message.
#### Here's an example of a passing test:
```golang
func TestFooIsFoo(t *testing.T) {
expect := expectate.Expect(t)
expect("foo").ToBe("foo")
}
```
Result:
```
--- PASS (ok)
```
#### Here's an example of a failing test:
```golang
func TestFooIsBar(t *testing.T) {
expect := expectate.Expect(t)
expect("foo").ToBe("bar")
}
```
Result:
```
--- FAIL: TestFooIsBar
expect.go:34: foo is not bar
```
---
expect().ToEqual()
The `expect().ToEqual()` method exists on the `Expector` type (which is what `expect()` returns). It takes any value and performs a _deep_ equality check using [go-cmp](https://github.com/google/go-cmp) with that value and the initial value passed to `expect()`. If the two values are equal, the test passes. If not, it calls `t.Fatal()` with a go-cmp diff.
#### Here's an example of a passing test:
```golang
func TestPost1EqualsPost2(t *testing.T) {
expect := expectate.Expect(t)
post1 := Post{
Title: "Post",
Content: "Content of Post",
Likes: 2,
}
post2 := Post{
Title: "Post",
Content: "Content of Post",
Likes: 2,
}
expect(post1).ToEqual(post2)
}
```
Result:
```
--- PASS (ok)
```
#### Here's an example of a failing test:
```golang
func TestPost1EqualsPost2(t *testing.T) {
expect := expectate.Expect(t)
post1 := Post{
Title: "Post 1",
Content: "Content of Post 1",
Likes: 2,
}
post2 := Post{
Title: "Post 2",
Content: "Content of Post 2",
Likes: 1,
}
expect(post1).ToEqual(post2)
}
```
Output:
```
--- FAIL: TestPost1EqualsPost2
expect.go:43: main.Post{
- Title: "Post 2",
+ Title: "Post 1",
- Content: "Content of Post 2",
+ Content: "Content of Post 1",
- Likes: 1,
+ Likes: 2,
}
```
---
expect().NotToBe()
The `expect().NotToBe()` method exists on the `Expector` type (which is what `expect()` returns). It has the opposite behavior of `expect().ToBe()` It takes any value and performs a simple inequality check with that value and the initial value passed to `expect()`. If the two values are not equal, the test passes. If they are, it calls `t.Fatal()` with a generic error message.
#### Here's an example of a passing test:
```golang
func TestFooIsNotBar(t *testing.T) {
expect := expectate.Expect(t)
expect("foo").NotToBe("bar")
}
```
Result:
```
--- PASS (ok)
```
#### Here's an example of a failing test:
```golang
func TestFooIsNotFoo(t *testing.T) {
expect := expectate.Expect(t)
expect("foo").NotToBe("foo")
}
```
Result:
```
--- FAIL: TestFooIsBar
expect.go:34: foo is foo
```
---
expect().NotToEqual()
The `expect().NotToEqual()` method exists on the `Expector` type (which is what `expect()` returns). It has the opposite behavior as `expect().ToEqual()`. It takes any value and performs a _deep_ inequality check using [go-cmp](https://github.com/google/go-cmp) with that value and the initial value passed to `expect()`. If the two values are not equal, the test passes. If they are, it calls `t.Fatal()` with a generic error message.
#### Here's an example of a passing test:
```golang
func TestPost1_DoesNotEqual_Post2(t *testing.T) {
expect := expectate.Expect(t)
post1 := Post{
Title: "Post 1",
Content: "Content of Post 1",
Likes: 2,
}
post2 := Post{
Title: "Post 2",
Content: "Content of Post 2",
Likes: 1,
}
expect(post1).NotToEqual(post2)
}
```
Result:
```
--- PASS (ok)
```
#### Here's an example of a failing test:
```golang
func TestPost1_DoesNotEqual_Post2(t *testing.T) {
expect := expectate.Expect(t)
post1 := Post{
Title: "Post",
Content: "Content of Post",
Likes: 2,
}
post2 := Post{
Title: "Post",
Content: "Content of Post",
Likes: 2,
}
expect(post1).NotToEqual(post2)
}
```
Output:
```
--- FAIL: TestPost1_DoesNotEqual_Post2
expect.go:57: {Post 1 Content of Post 1 2} equals {Post 1 Content of Post 1 2}
```
expectate-1.3.1/expect.go 0000664 0000000 0000000 00000002736 14253467360 0015337 0 ustar 00root root 0000000 0000000 package expectate
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
// Fataler is satisfied by *testing.T but could be something else if needed
type Fataler interface {
Fatalf(format string, args ...interface{})
}
// Expect constructs an ExpectorFunc object
func Expect(t Fataler) ExpectorFunc {
expector := new(Expector)
expector.t = t
return func(subject interface{}) *Expector {
expector.subject = subject
return expector
}
}
type ExpectorFunc func(subject interface{}) *Expector
type Expector struct {
t Fataler
subject interface{}
}
// ToBe checks simple equality, i.e. (x == y)
func (e Expector) ToBe(expected interface{}) {
if e.subject != expected {
e.t.Fatalf("%s is not %s", format(e.subject), format(expected))
}
}
// ToEqual checks strict equality, i.e. (cmp.Diff(x, y) == "")
func (e Expector) ToEqual(expected interface{}) {
diff := cmp.Diff(expected, e.subject)
if diff != "" {
e.t.Fatalf(diff)
}
}
// NotToBe checks simple inequality, i.e. (x != y)
func (e Expector) NotToBe(expected interface{}) {
if e.subject == expected {
e.t.Fatalf("%s is %s", format(e.subject), format(expected))
}
}
// NotToEqual checks strict inequality, i.e. (!cmp.Equal(x, y))
func (e Expector) NotToEqual(expected interface{}) {
if cmp.Equal(e.subject, expected) {
e.t.Fatalf("%s equals %s", format(e.subject), format(expected))
}
}
func format(v interface{}) string {
str, ok := v.(string)
if ok {
return fmt.Sprintf("'%s'", str)
}
return fmt.Sprint(v)
}
expectate-1.3.1/expect_test.go 0000664 0000000 0000000 00000001146 14253467360 0016370 0 ustar 00root root 0000000 0000000 package expectate_test
import (
"fmt"
"time"
)
type MockTestingT struct {
FataledWith string
}
func (t *MockTestingT) Fatalf(format string, args ...interface{}) {
t.FataledWith = fmt.Sprintf(format, args...)
}
type Person struct {
Name string
Age int
Job string
Birthday time.Time
}
var samplePointerToPerson = &Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(1990, time.January, 1, 0, 0, 0, 0, time.UTC),
}
type ExpectTest struct {
name string
subject interface{}
object interface{}
expectedFailure string
}
expectate-1.3.1/go.mod 0000664 0000000 0000000 00000000130 14253467360 0014610 0 ustar 00root root 0000000 0000000 module github.com/gomagedon/expectate
go 1.15
require github.com/google/go-cmp v0.5.8
expectate-1.3.1/go.sum 0000664 0000000 0000000 00000000247 14253467360 0014646 0 ustar 00root root 0000000 0000000 github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
expectate-1.3.1/not_tobe_test.go 0000664 0000000 0000000 00000006073 14253467360 0016715 0 ustar 00root root 0000000 0000000 package expectate_test
import (
"testing"
"time"
"github.com/gomagedon/expectate"
)
var notToBeTests = []ExpectTest{
{
name: "2 is 2",
subject: 2,
object: 2,
expectedFailure: "2 is 2",
},
{
name: "2 is not 3",
subject: 2,
object: 3,
expectedFailure: "",
},
{
name: "'foo' is 'foo'",
subject: "foo",
object: "foo",
expectedFailure: "'foo' is 'foo'",
},
{
name: "'foo' is not 'bar'",
subject: "foo",
object: "bar",
expectedFailure: "",
},
{
name: "0 is 0",
subject: 0,
object: 0,
expectedFailure: "0 is 0",
},
{
name: "0 is not ''",
subject: 0,
object: "",
expectedFailure: "",
},
{
name: "0 is not nil",
subject: 0,
object: nil,
expectedFailure: "",
},
{
name: "pointer to struct is itself",
subject: samplePointerToPerson,
object: samplePointerToPerson,
expectedFailure: "&{John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC} is &{John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC}",
},
{
name: "pointer to struct is not copy of struct",
subject: samplePointerToPerson,
object: *samplePointerToPerson,
expectedFailure: "",
},
{
name: "pointer to struct is not pointer to copy of struct",
subject: &Person{
Name: "Philip Fry",
Age: 25,
Job: "Delivery Boy",
Birthday: time.Date(1980, time.July, 7, 0, 0, 0, 0, time.UTC),
},
object: &Person{
Name: "Philip Fry",
Age: 25,
Job: "Delivery Boy",
Birthday: time.Date(1980, time.July, 7, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "",
},
{
name: "struct is copy of struct",
subject: Person{
Name: "Hermes Conrad",
Age: 38,
Job: "Beaurocrat",
Birthday: time.Date(2967, time.August, 8, 0, 0, 0, 0, time.UTC),
},
object: Person{
Name: "Hermes Conrad",
Age: 38,
Job: "Beaurocrat",
Birthday: time.Date(2967, time.August, 8, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "{Hermes Conrad 38 Beaurocrat 2967-08-08 00:00:00 +0000 UTC} is {Hermes Conrad 38 Beaurocrat 2967-08-08 00:00:00 +0000 UTC}",
},
{
name: "struct is not struct with different values",
subject: Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
},
object: Person{
Name: "John Smith",
Age: 30,
Job: "Electrician",
Birthday: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "",
},
}
func TestNotToBe(t *testing.T) {
for _, test := range notToBeTests {
t.Run(test.name, func(t *testing.T) {
mockTestingT := new(MockTestingT)
expect := expectate.Expect(mockTestingT)
expect(test.subject).NotToBe(test.object)
if mockTestingT.FataledWith != test.expectedFailure {
t.Fatal("Expected:", test.expectedFailure,
"\nGot:", mockTestingT.FataledWith)
}
})
}
}
expectate-1.3.1/not_toequal_test.go 0000664 0000000 0000000 00000003053 14253467360 0017431 0 ustar 00root root 0000000 0000000 package expectate_test
import (
"testing"
"time"
"github.com/gomagedon/expectate"
)
var notToEqualTests = []ExpectTest{
{
name: "2 equals 2",
subject: 2,
object: 2,
expectedFailure: "2 equals 2",
},
{
name: "2 does not equal 3",
subject: 2,
object: 3,
expectedFailure: "",
},
{
name: "'foo' equals 'foo'",
subject: "foo",
object: "foo",
expectedFailure: "'foo' equals 'foo'",
},
{
name: "'foo' does not equal 'bar'",
subject: "foo",
object: "bar",
expectedFailure: "",
},
{
name: "pointer to struct equals pointer to copy of struct",
subject: &Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(1990, time.January, 1, 0, 0, 0, 0, time.UTC),
},
object: &Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(1990, time.January, 1, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "&{John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC} equals &{John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC}",
},
}
func TestNotToEqual(t *testing.T) {
for _, test := range notToEqualTests {
t.Run(test.name, func(t *testing.T) {
mockTestingT := new(MockTestingT)
expect := expectate.Expect(mockTestingT)
expect(test.subject).NotToEqual(test.object)
if mockTestingT.FataledWith != test.expectedFailure {
t.Fatal("Expected:", test.expectedFailure,
"\nGot:", mockTestingT.FataledWith)
}
})
}
}
expectate-1.3.1/tobe_test.go 0000664 0000000 0000000 00000006312 14253467360 0016031 0 ustar 00root root 0000000 0000000 package expectate_test
import (
"testing"
"time"
"github.com/gomagedon/expectate"
)
var toBeTests = []ExpectTest{
{
name: "2 is 2",
subject: 2,
object: 2,
expectedFailure: "",
},
{
name: "2 is not 3",
subject: 2,
object: 3,
expectedFailure: "2 is not 3",
},
{
name: "'foo' is 'foo'",
subject: "foo",
object: "foo",
expectedFailure: "",
},
{
name: "'foo' is not 'bar'",
subject: "foo",
object: "bar",
expectedFailure: "'foo' is not 'bar'",
},
{
name: "0 is 0",
subject: 0,
object: 0,
expectedFailure: "",
},
{
name: "0 is not ''",
subject: 0,
object: "",
expectedFailure: "0 is not ''",
},
{
name: "0 is not nil",
subject: 0,
object: nil,
expectedFailure: "0 is not ",
},
{
name: "pointer to struct is itself",
subject: samplePointerToPerson,
object: samplePointerToPerson,
expectedFailure: "",
},
{
name: "pointer to struct is not copy of struct",
subject: samplePointerToPerson,
object: *samplePointerToPerson,
expectedFailure: "&{John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC} is not {John Doe 30 Electrician 1990-01-01 00:00:00 +0000 UTC}",
},
{
name: "pointer to struct is not pointer to copy of struct",
subject: &Person{
Name: "Philip Fry",
Age: 25,
Job: "Delivery Boy",
Birthday: time.Date(1980, time.July, 7, 0, 0, 0, 0, time.UTC),
},
object: &Person{
Name: "Philip Fry",
Age: 25,
Job: "Delivery Boy",
Birthday: time.Date(1980, time.July, 7, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "&{Philip Fry 25 Delivery Boy 1980-07-07 00:00:00 +0000 UTC} is not &{Philip Fry 25 Delivery Boy 1980-07-07 00:00:00 +0000 UTC}",
},
{
name: "struct is copy of struct",
subject: Person{
Name: "Hermes Conrad",
Age: 38,
Job: "Beaurocrat",
Birthday: time.Date(2967, time.August, 8, 0, 0, 0, 0, time.UTC),
},
object: Person{
Name: "Hermes Conrad",
Age: 38,
Job: "Beaurocrat",
Birthday: time.Date(2967, time.August, 8, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "",
},
{
name: "struct is not struct with different values",
subject: Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
},
object: Person{
Name: "John Smith",
Age: 30,
Job: "Electrician",
Birthday: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "{John Doe 30 Electrician 2000-01-01 00:00:00 +0000 UTC} is not {John Smith 30 Electrician 2000-01-01 00:00:00 +0000 UTC}",
},
}
func TestToBe(t *testing.T) {
for _, test := range toBeTests {
t.Run(test.name, func(t *testing.T) {
mockTestingT := new(MockTestingT)
expect := expectate.Expect(mockTestingT)
expect(test.subject).ToBe(test.object)
if mockTestingT.FataledWith != test.expectedFailure {
t.Fatalf("Expected '%s'\nGot: '%s'", test.expectedFailure, mockTestingT.FataledWith)
}
})
}
}
expectate-1.3.1/toequal_test.go 0000664 0000000 0000000 00000002707 14253467360 0016556 0 ustar 00root root 0000000 0000000 package expectate_test
import (
"testing"
"time"
"github.com/gomagedon/expectate"
"github.com/google/go-cmp/cmp"
)
var toEqualTests = []ExpectTest{
{
name: "2 equals 2",
subject: 2,
object: 2,
expectedFailure: "",
},
{
name: "2 does not equal 3",
subject: 2,
object: 3,
expectedFailure: cmp.Diff(3, 2),
},
{
name: "'foo' equals 'foo'",
subject: "foo",
object: "foo",
expectedFailure: "",
},
{
name: "'foo' does not equal 'bar'",
subject: "foo",
object: "bar",
expectedFailure: cmp.Diff("bar", "foo"),
},
{
name: "pointer to struct is pointer to copy of struct",
subject: &Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(1990, time.January, 1, 0, 0, 0, 0, time.UTC),
},
object: &Person{
Name: "John Doe",
Age: 30,
Job: "Electrician",
Birthday: time.Date(1990, time.January, 1, 0, 0, 0, 0, time.UTC),
},
expectedFailure: "",
},
}
func TestToEqual(t *testing.T) {
for _, test := range toEqualTests {
t.Run(test.name, func(t *testing.T) {
mockTestingT := new(MockTestingT)
expect := expectate.Expect(mockTestingT)
expect(test.subject).ToEqual(test.object)
if mockTestingT.FataledWith != test.expectedFailure {
t.Fatal("Expected:", test.expectedFailure,
"\nGot:", mockTestingT.FataledWith)
}
})
}
}