|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + _, b, _, _ = runtime.Caller(0) |
| 14 | + basePath = filepath.Dir(b) |
| 15 | +) |
| 16 | + |
| 17 | +func doMainTest(t *testing.T, input string, expected string, mainArgs []string) { |
| 18 | + execArgs := append([]string{"run", basePath}, mainArgs...) |
| 19 | + cmd := exec.Command("go", execArgs...) |
| 20 | + |
| 21 | + cmd.Stdin = strings.NewReader(input) |
| 22 | + var out bytes.Buffer |
| 23 | + cmd.Stdout = &out |
| 24 | + |
| 25 | + if err := cmd.Run(); err != nil { |
| 26 | + t.Errorf("%v", err) |
| 27 | + } |
| 28 | + actual := out.String() |
| 29 | + |
| 30 | + if actual != expected { |
| 31 | + t.Errorf("%v does not match expected: %v", actual, expected) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestSimpleReplaceWithNewlineAtEOF(t *testing.T) { |
| 36 | + mainArgs := []string{ |
| 37 | + "http://uss-enterprise.com", |
| 38 | + "https://ncc-1701-d.space", |
| 39 | + } |
| 40 | + |
| 41 | + input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n" |
| 42 | + expected := "Space, the final frontier!\nCheck out: https://ncc-1701-d.space/decks/10/sections/forward\n" |
| 43 | + doMainTest(t, input, expected, mainArgs) |
| 44 | +} |
| 45 | + |
| 46 | +func TestSimpleReplaceWithoutNewlineAtEOF(t *testing.T) { |
| 47 | + mainArgs := []string{ |
| 48 | + "http://uss-enterprise.com", |
| 49 | + "https://ncc-1701-d.space", |
| 50 | + } |
| 51 | + input := "I tend bar, and I listen.\nhttp://uss-enterprise.com/personnel/guinan" |
| 52 | + expected := "I tend bar, and I listen.\nhttps://ncc-1701-d.space/personnel/guinan" |
| 53 | + doMainTest(t, input, expected, mainArgs) |
| 54 | +} |
| 55 | + |
| 56 | +func TestMultipleReplaceWithNewlineAtEOF(t *testing.T) { |
| 57 | + mainArgs := []string{ |
| 58 | + "http://uss-enterprise.com", |
| 59 | + "https://ncc-1701-d.space", |
| 60 | + |
| 61 | + "sections", |
| 62 | + "areas", |
| 63 | + |
| 64 | + "https", |
| 65 | + "warp", |
| 66 | + } |
| 67 | + input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n" |
| 68 | + expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward\n" |
| 69 | + doMainTest(t, input, expected, mainArgs) |
| 70 | +} |
| 71 | + |
| 72 | +func TestMultipleReplaceWithoutNewlineAtEOF(t *testing.T) { |
| 73 | + mainArgs := []string{ |
| 74 | + "http://uss-enterprise.com", |
| 75 | + "https://ncc-1701-d.space", |
| 76 | + |
| 77 | + "sections", |
| 78 | + "areas", |
| 79 | + |
| 80 | + "https", |
| 81 | + "warp", |
| 82 | + } |
| 83 | + input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward" |
| 84 | + expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward" |
| 85 | + doMainTest(t, input, expected, mainArgs) |
| 86 | +} |
0 commit comments