Skip to content

Commit

Permalink
The fixture provides a few additional equality assertion functions.
Browse files Browse the repository at this point in the history
Importing a dependency (github.com/smartystreets/assertions/should)
is sometimes overkill when all that is needed is an equality check.
  • Loading branch information
mdwhatcott committed Jun 7, 2017
1 parent d6e1e83 commit a7e028d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 21 deletions.
3 changes: 1 addition & 2 deletions advanced_examples/bowling_game_test.go
Expand Up @@ -3,7 +3,6 @@ package examples
import (
"testing"

"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func (this *BowlingGameScoringFixture) TestPerfectGame() {
}

func (this *BowlingGameScoringFixture) assertScore(expected int) {
this.So(this.game.Score(), should.Equal, expected)
this.AssertEqual(expected, this.game.Score())
}
func (this *BowlingGameScoringFixture) rollMany(times, pins int) {
for x := 0; x < times; x++ {
Expand Down
11 changes: 5 additions & 6 deletions advanced_examples/environment_controller_test.go
Expand Up @@ -3,7 +3,6 @@ package examples
import (
"testing"

"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)

Expand Down Expand Up @@ -35,22 +34,22 @@ func (this *EnvironmentControllerFixture) TestEverythingOffWhenComfortable() {

func (this *EnvironmentControllerFixture) TestCoolerAndBlowerWhenHot() {
this.setupHotEnvironment()
this.So(this.hardware.String(), should.Equal, "heater BLOWER COOLER low high")
this.AssertEqual("heater BLOWER COOLER low high", this.hardware.String())
}

func (this *EnvironmentControllerFixture) TestHeaterAndBlowerWhenCold() {
this.setupColdEnvironment()
this.So(this.hardware.String(), should.Equal, "HEATER BLOWER cooler low high")
this.AssertEqual("HEATER BLOWER cooler low high", this.hardware.String())
}

func (this *EnvironmentControllerFixture) TestHighAlarmOnIfAtThreshold() {
this.setupBlazingEnvironment()
this.So(this.hardware.String(), should.Equal, "heater BLOWER COOLER low HIGH")
this.AssertEqual("heater BLOWER COOLER low HIGH", this.hardware.String())
}

func (this *EnvironmentControllerFixture) TestLowAlarmOnIfAtThreshold() {
this.setupFreezingEnvironment()
this.So(this.hardware.String(), should.Equal, "HEATER BLOWER cooler LOW high")
this.AssertEqual("HEATER BLOWER cooler LOW high", this.hardware.String())
}

func (this *EnvironmentControllerFixture) setupComfortableEnvironment() {
Expand Down Expand Up @@ -83,5 +82,5 @@ func (this *EnvironmentControllerFixture) activateAllHardwareComponents() {
}

func (this *EnvironmentControllerFixture) assertAllHardwareComponentsAreDeactivated() {
this.So(this.hardware.String(), should.Equal, "heater blower cooler low high")
this.AssertEqual("heater blower cooler low high", this.hardware.String())
}
13 changes: 9 additions & 4 deletions basic_examples/examples_test.go
Expand Up @@ -28,12 +28,17 @@ func (this *ExampleFixture) TeardownStuff() {

// This is an actual test case:
func (this *ExampleFixture) TestWithAssertions() {
// Here's how to use the functions from the `should`
// package at github.com/smartystreets/assertions/should
// to perform assertions:
// Built-in assertion functions:
this.Assert(1 == 1, "One should equal one")
this.AssertEqual(1, 1)
this.AssertDeepEqual(1, 1)
this.AssertSprintEqual(1, 1.0)
this.AssertSprintfEqual(uint(1), int64(1), "%d")

// External assertion functions from the `should` package:
this.So(42, should.Equal, 42)
this.So("Hello, World!", should.ContainSubstring, "World")
this.Ok(1 == 1, "One should equal one")

}

func (this *ExampleFixture) SkipTestWithNothing() {
Expand Down
22 changes: 19 additions & 3 deletions fixture.go
Expand Up @@ -3,6 +3,7 @@ package gunit
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strings"
)
Expand Down Expand Up @@ -34,7 +35,7 @@ func newFixture(t testingT, verbose bool) *Fixture {
}

// So is a convenience method for reporting assertion failure messages,
// say from the assertion functions found in github.com/smartystreets/assertions/should.
// from the many assertion functions found in github.com/smartystreets/assertions/should.
// Example: this.So(actual, should.Equal, expected)
func (this *Fixture) So(
actual interface{},
Expand All @@ -50,16 +51,29 @@ func (this *Fixture) So(
return !failed
}

// Ok tests a boolean which, if not true, marks the current test case as failed and
// Assert tests a boolean which, if not true, marks the current test case as failed and
// prints the provided message.
func (this *Fixture) Ok(condition bool, messages ...string) {
func (this *Fixture) Assert(condition bool, messages ...string) {
if !condition {
if len(messages) == 0 {
messages = append(messages, "Expected condition to be true, was false instead.")
}
this.fail(strings.Join(messages, ", "))
}
}
func (this *Fixture) AssertEqual(expected, actual interface{}) {
this.Assert(expected == actual, fmt.Sprintf(comparisonFormat, fmt.Sprint(expected), fmt.Sprint(actual)))
}
func (this *Fixture) AssertSprintEqual(expected, actual interface{}) {
this.AssertEqual(fmt.Sprint(expected), fmt.Sprint(actual))
}
func (this *Fixture) AssertSprintfEqual(expected, actual interface{}, format string) {
this.AssertEqual(fmt.Sprintf(format, expected), fmt.Sprintf(format, actual))
}
func (this *Fixture) AssertDeepEqual(expected, actual interface{}) {
this.Assert(reflect.DeepEqual(expected, actual),
fmt.Sprintf(comparisonFormat, fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", actual)))
}

func (this *Fixture) Error(args ...interface{}) { this.fail(fmt.Sprint(args...)) }
func (this *Fixture) Errorf(f string, args ...interface{}) { this.fail(fmt.Sprintf(f, args...)) }
Expand Down Expand Up @@ -92,3 +106,5 @@ func (this *Fixture) recoverPanic(r interface{}) {
this.Println("* (Additional tests may have been skipped as a result of the panic shown above.)")
this.t.Fail()
}

const comparisonFormat = "Expected: [%s]\nActual: [%s]"
124 changes: 118 additions & 6 deletions fixture_test.go
Expand Up @@ -68,10 +68,10 @@ func TestSoFailsAndLogs(t *testing.T) {
}
}

func TestOkPasses(t *testing.T) {
func TestAssertPasses(t *testing.T) {
test := Setup(false)

test.fixture.Ok(true)
test.fixture.Assert(true)
test.fixture.finalize()

if test.out.Len() > 0 {
Expand All @@ -82,10 +82,10 @@ func TestOkPasses(t *testing.T) {
}
}

func TestOkFailsAndLogs(t *testing.T) {
func TestAssertFailsAndLogs(t *testing.T) {
test := Setup(false)

test.fixture.Ok(false)
test.fixture.Assert(false)
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "Expected condition to be true, was false instead.") {
Expand All @@ -96,10 +96,10 @@ func TestOkFailsAndLogs(t *testing.T) {
}
}

func TestOkWithCustomMessageFailsAndLogs(t *testing.T) {
func TestAssertWithCustomMessageFailsAndLogs(t *testing.T) {
test := Setup(false)

test.fixture.Ok(false, "gophers!")
test.fixture.Assert(false, "gophers!")
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "gophers!") {
Expand All @@ -110,6 +110,118 @@ func TestOkWithCustomMessageFailsAndLogs(t *testing.T) {
}
}

func TestAssertEqualPasses(t *testing.T) {
test := Setup(false)

test.fixture.AssertEqual(1, 1)
test.fixture.finalize()

if test.out.Len() > 0 {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if test.fakeT.failed {
t.Error("Test was erroneously marked as failed.")
}
}

func TestAssertEqualFails(t *testing.T) {
test := Setup(false)

test.fixture.AssertEqual(1, 2)
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "Expected: [1]\nActual: [2]") {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if !test.fakeT.failed {
t.Error("Test should have been marked as failed.")
}
}

func TestAssertSprintEqualPasses(t *testing.T) {
test := Setup(false)

test.fixture.AssertSprintEqual(1, 1.0)
test.fixture.finalize()

if test.out.Len() > 0 {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if test.fakeT.failed {
t.Error("Test was erroneously marked as failed.")
}
}

func TestAssertSprintEqualFails(t *testing.T) {
test := Setup(false)

test.fixture.AssertSprintEqual(1, 2)
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "Expected: [1]\nActual: [2]") {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if !test.fakeT.failed {
t.Error("Test should have been marked as failed.")
}
}

func TestAssertSprintfEqualPasses(t *testing.T) {
test := Setup(false)

test.fixture.AssertSprintfEqual(1, uint(1), "%d")
test.fixture.finalize()

if test.out.Len() > 0 {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if test.fakeT.failed {
t.Error("Test was erroneously marked as failed.")
}
}

func TestAssertSprintfEqualFails(t *testing.T) {
test := Setup(false)

test.fixture.AssertSprintfEqual(1, 2, "%d")
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "Expected: [1]\nActual: [2]") {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if !test.fakeT.failed {
t.Error("Test should have been marked as failed.")
}
}

func TestAssertDeepEqualPasses(t *testing.T) {
test := Setup(false)

test.fixture.AssertDeepEqual(1, 1)
test.fixture.finalize()

if test.out.Len() > 0 {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if test.fakeT.failed {
t.Error("Test was erroneously marked as failed.")
}
}

func TestAssertDeepEqualFails(t *testing.T) {
test := Setup(false)

test.fixture.AssertDeepEqual(1, 2)
test.fixture.finalize()

if output := test.out.String(); !strings.Contains(output, "Expected: [1]\nActual: [2]") {
t.Errorf("Unexpected ouput: '%s'", test.out.String())
}
if !test.fakeT.failed {
t.Error("Test should have been marked as failed.")
}
}

func TestErrorFailsAndLogs(t *testing.T) {
test := Setup(false)

Expand Down

0 comments on commit a7e028d

Please sign in to comment.