-
Notifications
You must be signed in to change notification settings - Fork 18
/
main_test.go
86 lines (71 loc) 路 2.2 KB
/
main_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
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
var (
_, b, _, _ = runtime.Caller(0)
basePath = filepath.Dir(b)
)
func doMainTest(t *testing.T, input string, expected string, mainArgs []string) {
execArgs := append([]string{"run", basePath}, mainArgs...)
cmd := exec.Command("go", execArgs...)
cmd.Stdin = strings.NewReader(input)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
t.Errorf("%v", err)
}
actual := out.String()
if actual != expected {
t.Errorf("%v does not match expected: %v", actual, expected)
}
}
func TestSimpleReplaceWithNewlineAtEOF(t *testing.T) {
mainArgs := []string{
"http://uss-enterprise.com",
"https://ncc-1701-d.space",
}
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n"
expected := "Space, the final frontier!\nCheck out: https://ncc-1701-d.space/decks/10/sections/forward\n"
doMainTest(t, input, expected, mainArgs)
}
func TestSimpleReplaceWithoutNewlineAtEOF(t *testing.T) {
mainArgs := []string{
"http://uss-enterprise.com",
"https://ncc-1701-d.space",
}
input := "I tend bar, and I listen.\nhttp://uss-enterprise.com/personnel/guinan"
expected := "I tend bar, and I listen.\nhttps://ncc-1701-d.space/personnel/guinan"
doMainTest(t, input, expected, mainArgs)
}
func TestMultipleReplaceWithNewlineAtEOF(t *testing.T) {
mainArgs := []string{
"http://uss-enterprise.com",
"https://ncc-1701-d.space",
"sections",
"areas",
"https",
"warp",
}
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n"
expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward\n"
doMainTest(t, input, expected, mainArgs)
}
func TestMultipleReplaceWithoutNewlineAtEOF(t *testing.T) {
mainArgs := []string{
"http://uss-enterprise.com",
"https://ncc-1701-d.space",
"sections",
"areas",
"https",
"warp",
}
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward"
expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward"
doMainTest(t, input, expected, mainArgs)
}