File: doc_test.go

package info (click to toggle)
golang-github-smartystreets-assertions 1.10.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 580 kB
  • sloc: python: 80; makefile: 41; sh: 15
file content (74 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package assertions

import (
	"bytes"
	"fmt"
	"reflect"
	"testing"
)

func TestGoConveyModeAffectsSerializer(t *testing.T) {
	if reflect.TypeOf(serializer) != reflect.TypeOf(new(noopSerializer)) {
		t.Error("Expected noop serializer as default")
	}

	GoConveyMode(true)
	if reflect.TypeOf(serializer) != reflect.TypeOf(new(failureSerializer)) {
		t.Error("Expected failure serializer")
	}

	GoConveyMode(false)
	if reflect.TypeOf(serializer) != reflect.TypeOf(new(noopSerializer)) {
		t.Error("Expected noop serializer")
	}
}

func TestPassingAssertion(t *testing.T) {
	fake := &FakeT{buffer: new(bytes.Buffer)}
	assertion := New(fake)
	passed := assertion.So(1, ShouldEqual, 1)

	if !passed {
		t.Error("Assertion failed when it should have passed.")
	}
	if fake.buffer.Len() > 0 {
		t.Error("Unexpected error message was printed.")
	}
}

func TestFailingAssertion(t *testing.T) {
	fake := &FakeT{buffer: new(bytes.Buffer)}
	assertion := New(fake)
	passed := assertion.So(1, ShouldEqual, 2)

	if passed {
		t.Error("Assertion passed when it should have failed.")
	}
	if fake.buffer.Len() == 0 {
		t.Error("Expected error message not printed.")
	}
}

func TestFailingGroupsOfAssertions(t *testing.T) {
	fake := &FakeT{buffer: new(bytes.Buffer)}
	assertion1 := New(fake)
	assertion2 := New(fake)

	assertion1.So(1, ShouldEqual, 2) // fail
	assertion2.So(1, ShouldEqual, 1) // pass

	if !assertion1.Failed() {
		t.Error("Expected the first assertion to have been marked as failed.")
	}
	if assertion2.Failed() {
		t.Error("Expected the second assertion to NOT have been marked as failed.")
	}
}

type FakeT struct {
	buffer *bytes.Buffer
}

func (this *FakeT) Error(args ...interface{}) {
	fmt.Fprint(this.buffer, args...)
}