Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 1 | package expect_test |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "fmt" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron/lib/expect" |
Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func TestSimple(t *testing.T) { |
| 16 | buf := []byte{} |
| 17 | buffer := bytes.NewBuffer(buf) |
| 18 | buffer.WriteString("bar\n") |
| 19 | buffer.WriteString("baz\n") |
| 20 | buffer.WriteString("oops\n") |
| 21 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 22 | s.Expect("bar") |
| 23 | s.Expect("baz") |
| 24 | if err := s.Error(); err != nil { |
| 25 | t.Error(err) |
| 26 | } |
| 27 | // This will fail the test. |
| 28 | s.Expect("not oops") |
| 29 | if err := s.Error(); err == nil { |
| 30 | t.Error("unexpected success") |
| 31 | } else { |
| 32 | t.Log(s.Error()) |
| 33 | } |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 34 | s.ExpectEOF() |
| 35 | } |
| 36 | |
| 37 | func TestExpectf(t *testing.T) { |
| 38 | buf := []byte{} |
| 39 | buffer := bytes.NewBuffer(buf) |
| 40 | buffer.WriteString("bar 22\n") |
| 41 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 42 | s.Expectf("bar %d", 22) |
| 43 | if err := s.Error(); err != nil { |
| 44 | t.Error(err) |
| 45 | } |
| 46 | s.ExpectEOF() |
| 47 | } |
| 48 | |
| 49 | func TestEOF(t *testing.T) { |
| 50 | buf := []byte{} |
| 51 | buffer := bytes.NewBuffer(buf) |
| 52 | buffer.WriteString("bar 22\n") |
| 53 | buffer.WriteString("baz 22\n") |
| 54 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 55 | s.Expectf("bar %d", 22) |
| 56 | s.ExpectEOF() |
| 57 | if err := s.Error(); err == nil { |
| 58 | t.Error("unexpected success") |
| 59 | } else { |
| 60 | t.Log(s.Error()) |
| 61 | } |
Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func TestExpectRE(t *testing.T) { |
| 65 | buf := []byte{} |
| 66 | buffer := bytes.NewBuffer(buf) |
| 67 | buffer.WriteString("bar=baz\n") |
| 68 | buffer.WriteString("aaa\n") |
| 69 | buffer.WriteString("bbb\n") |
| 70 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 71 | if got, want := s.ExpectVar("bar"), "baz"; got != want { |
| 72 | t.Errorf("got %v, want %v", got, want) |
| 73 | } |
| 74 | s.ExpectRE("zzz|aaa", -1) |
| 75 | if err := s.Error(); err != nil { |
| 76 | t.Error(err) |
| 77 | } |
| 78 | if got, want := s.ExpectRE("(.*)", -1), [][]string{{"bbb", "bbb"}}; !reflect.DeepEqual(got, want) { |
| 79 | t.Errorf("got %v, want %v", got, want) |
| 80 | } |
| 81 | if got, want := s.ExpectRE("(.*", -1), [][]string{{"bbb", "bbb"}}; !reflect.DeepEqual(got, want) { |
| 82 | // this will have failed the test also. |
| 83 | if err := s.Error(); err == nil || !strings.Contains(err.Error(), "error parsing regexp") { |
| 84 | t.Errorf("missing or wrong error: %v", s.Error()) |
| 85 | } |
| 86 | } |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 87 | s.ExpectEOF() |
Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Cosmos Nicolaou | 5cda14f | 2014-10-27 09:58:20 -0700 | [diff] [blame] | 90 | func TestExpectSetRE(t *testing.T) { |
| 91 | buf := []byte{} |
| 92 | buffer := bytes.NewBuffer(buf) |
| 93 | buffer.WriteString("bar=baz\n") |
| 94 | buffer.WriteString("abc\n") |
| 95 | buffer.WriteString("def\n") |
| 96 | buffer.WriteString("abc\n") |
| 97 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 98 | s.ExpectSetRE("^bar=.*$", "def$", "^abc$", "^a..$") |
| 99 | if s.Error() != nil { |
| 100 | t.Errorf("unexpected error: %s", s.Error()) |
| 101 | } |
| 102 | buffer.WriteString("ooh\n") |
| 103 | buffer.WriteString("aah\n") |
| 104 | s.ExpectSetRE("bar=.*", "def") |
Cosmos Nicolaou | a429eff | 2014-11-19 14:02:34 -0800 | [diff] [blame] | 105 | if got, want := s.Error(), "expect_test.go:104: found no match for \"bar=.*\""; got == nil || got.Error() != want { |
Cosmos Nicolaou | 5cda14f | 2014-10-27 09:58:20 -0700 | [diff] [blame] | 106 | t.Errorf("got %v, want %q", got, want) |
| 107 | } |
| 108 | s.ExpectEOF() |
| 109 | } |
| 110 | |
Cosmos Nicolaou | a429eff | 2014-11-19 14:02:34 -0800 | [diff] [blame] | 111 | func TestExpectSetEventuallyRE(t *testing.T) { |
| 112 | buf := []byte{} |
| 113 | buffer := bytes.NewBuffer(buf) |
| 114 | buffer.WriteString("bar=baz\n") |
| 115 | buffer.WriteString("abc\n") |
| 116 | buffer.WriteString("def\n") |
| 117 | buffer.WriteString("abc\n") |
| 118 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 119 | s.SetVerbosity(testing.Verbose()) |
| 120 | s.ExpectSetEventuallyRE("^bar=.*$", "def") |
| 121 | if s.Error() != nil { |
| 122 | t.Errorf("unexpected error: %s", s.Error()) |
| 123 | } |
| 124 | s.ExpectSetEventuallyRE("abc") |
| 125 | if got, want := s.Error(), "expect_test.go:124: found no match for \"abc\""; got == nil || got.Error() != want { |
| 126 | t.Errorf("got %q, want %q", got, want) |
| 127 | } |
| 128 | // Need to clear the EOF from the previous ExpectSetEventuallyRE call |
| 129 | buf = []byte{} |
| 130 | buffer = bytes.NewBuffer(buf) |
| 131 | s = expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 132 | buffer.WriteString("ooh\n") |
| 133 | buffer.WriteString("aah\n") |
| 134 | s.ExpectSetEventuallyRE("zzz") |
| 135 | if got, want := s.Error(), "expect_test.go:134: found no match for \"zzz\""; got == nil || got.Error() != want { |
| 136 | t.Errorf("got %q, want %q", got, want) |
| 137 | } |
| 138 | s.ExpectEOF() |
| 139 | } |
| 140 | |
Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 141 | func TestRead(t *testing.T) { |
| 142 | buf := []byte{} |
| 143 | buffer := bytes.NewBuffer(buf) |
| 144 | lines := []string{"some words", "bar=baz", "more words"} |
| 145 | for _, l := range lines { |
| 146 | buffer.WriteString(l + "\n") |
| 147 | } |
| 148 | s := expect.NewSession(nil, bufio.NewReader(buffer), time.Minute) |
| 149 | for _, l := range lines { |
| 150 | if got, want := s.ReadLine(), l; got != want { |
| 151 | t.Errorf("got %q, want %q", got, want) |
| 152 | } |
| 153 | } |
| 154 | if s.Failed() { |
| 155 | t.Errorf("unexpected error: %s", s.Error()) |
| 156 | } |
| 157 | want := "" |
| 158 | for i := 0; i < 100; i++ { |
| 159 | m := fmt.Sprintf("%d\n", i) |
| 160 | buffer.WriteString(m) |
| 161 | want += m |
| 162 | } |
| 163 | got, err := s.ReadAll() |
| 164 | if err != nil { |
| 165 | t.Errorf("unexpected error: %s", err) |
| 166 | } |
| 167 | if got != want { |
| 168 | t.Errorf("got %q, want %q", got, want) |
| 169 | } |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 170 | s.ExpectEOF() |
Cosmos Nicolaou | e925f2a | 2014-09-05 14:47:18 -0700 | [diff] [blame] | 171 | } |