File: common_test.go

package info (click to toggle)
golang-github-facebookgo-ensure 0.0~git20160127.0.b4ab57d-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 120 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 914 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
package ensure_test

import (
	"bytes"
	"fmt"
	"os"
	"regexp"
	"strings"
	"testing"

	"github.com/facebookgo/ensure"
)

var log = os.Getenv("ENSURE_LOG") == "1"

type capture struct {
	bytes.Buffer
}

func (c *capture) Fatal(a ...interface{}) {
	fmt.Fprint(&c.Buffer, a...)
}

var equalPrefix = strings.Repeat("\b", 20)

func (c *capture) Equal(t testing.TB, expected string) {
	// trim the deleteSelf '\b' prefix
	actual := strings.TrimLeft(c.String(), "\b")
	ensure.DeepEqual(t, actual, expected)
	if log && expected != "" {
		t.Log(equalPrefix, expected)
	}
}

func (c *capture) Contains(t testing.TB, suffix string) {
	ensure.StringContains(t, c.String(), suffix)
	if log && suffix != "" {
		t.Log(equalPrefix, suffix)
	}
}

func (c *capture) Matches(t testing.TB, pattern string) {
	re := regexp.MustCompile(pattern)
	s := c.String()
	ensure.True(t, re.MatchString(s), s, "does not match pattern", pattern)
}