pax_global_header00006660000000000000000000000064142534673600014524gustar00rootroot0000000000000052 comment=dcc6e35a95146d4568c4e426265968ffa04cf322 expectate-1.3.1/000077500000000000000000000000001425346736000135105ustar00rootroot00000000000000expectate-1.3.1/.github/000077500000000000000000000000001425346736000150505ustar00rootroot00000000000000expectate-1.3.1/.github/workflows/000077500000000000000000000000001425346736000171055ustar00rootroot00000000000000expectate-1.3.1/.github/workflows/go.yml000066400000000000000000000006571425346736000202450ustar00rootroot00000000000000name: 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/LICENSE000066400000000000000000000020611425346736000145140ustar00rootroot00000000000000MIT 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.md000066400000000000000000000146061425346736000147760ustar00rootroot00000000000000--- # 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.go000066400000000000000000000027361425346736000153370ustar00rootroot00000000000000package 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.go000066400000000000000000000011461425346736000163700ustar00rootroot00000000000000package 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.mod000066400000000000000000000001301425346736000146100ustar00rootroot00000000000000module github.com/gomagedon/expectate go 1.15 require github.com/google/go-cmp v0.5.8 expectate-1.3.1/go.sum000066400000000000000000000002471425346736000146460ustar00rootroot00000000000000github.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.go000066400000000000000000000060731425346736000167150ustar00rootroot00000000000000package 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.go000066400000000000000000000030531425346736000174310ustar00rootroot00000000000000package 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.go000066400000000000000000000063121425346736000160310ustar00rootroot00000000000000package 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.go000066400000000000000000000027071425346736000165560ustar00rootroot00000000000000package 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) } }) } }