-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
75 lines (70 loc) · 1.75 KB
/
common_test.go
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
75
package main
// © 2021 John Lenton
// MIT licensed.
// from https://github.com/chipaca/goctest
import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
"testing"
)
// test for the ‘common()’ func
func TestCommon(t *testing.T) {
tests := []struct {
a string
b string
c string
}{
{"foo", "", ""},
{"foo", "bar", ""},
{"fo", "foo", ""},
{"foo/ba", "foo/bar", "foo"},
{"foo/bar", "foo/baz", "foo"},
{"foo/bar/baz/quux/moo/blah", "foo/zip", "foo"},
}
for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
if aa := common(tt.a, tt.a); aa != tt.a {
t.Errorf("common(%q, %q) == %q, expected %q", tt.a, tt.a, aa, tt.a)
}
if bb := common(tt.b, tt.b); bb != tt.b {
t.Errorf("common(%q, %q) == %q, expected %q", tt.b, tt.b, bb, tt.b)
}
if ab := common(tt.a, tt.b); ab != tt.c {
t.Errorf("common(%q, %q) == %q, expected %q", tt.a, tt.b, ab, tt.c)
}
if ba := common(tt.b, tt.a); ba != tt.c {
t.Errorf("common(%q, %q) == %q, expected %q", tt.b, tt.a, ba, tt.c)
}
})
}
}
// check tht the output of ‘goctest -h’ mentioned in the README
// matches the usage string
func TestREADME(t *testing.T) {
rx := regexp.MustCompile("(?m)^(.)")
indented := rx.ReplaceAllString(usage, " $1")
readme, err := ioutil.ReadFile("README.md")
if err != nil {
t.Fatalf("can't open README: %v", err)
}
if !strings.Contains(string(readme), indented) {
t.Error("README.md does not contain usage")
// XXX: eww
f, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("i just wanted a tempfile: %v", err)
}
defer func() {
f.Close()
os.Remove(f.Name())
}()
f.WriteString(indented)
cmd := exec.Command("diff", "-u", "--color=always", f.Name(), "README.md")
cmd.Stdout = os.Stdout
cmd.Run()
}
}