|
20 | 20 | describe FileWatch::BufferedTokenizer do
|
21 | 21 | subject { FileWatch::BufferedTokenizer.new }
|
22 | 22 |
|
23 |
| - def to_list(iterator) |
24 |
| - a = [] |
25 |
| - iterator.each { |v| a << v } |
26 |
| - return a |
| 23 | + |
| 24 | + # A matcher that ensures the result of BufferedTokenizer#extract "quacks like" an expected ruby Array in two respects: |
| 25 | + # - #empty? -> boolean: true indicates that the _next_ Enumerable#each will emit zero items. |
| 26 | + # - #entries -> Array: the ordered entries |
| 27 | + def emit_exactly(expected_array) |
| 28 | + # note: order matters; Iterator#each and the methods that delegate to it consume the iterator |
| 29 | + have_attributes(:empty? => expected_array.empty?, |
| 30 | + :entries => expected_array.entries) # consumes iterator, must be done last |
27 | 31 | end
|
28 | 32 |
|
29 | 33 | it "should tokenize a single token" do
|
30 |
| - expect(to_list(subject.extract("foo\n"))).to eq(["foo"]) |
| 34 | + expect(subject.extract("foo\n")).to emit_exactly(["foo"]) |
31 | 35 | end
|
32 | 36 |
|
33 | 37 | it "should merge multiple token" do
|
34 |
| - expect(to_list(subject.extract("foo"))).to eq([]) |
35 |
| - expect(to_list(subject.extract("bar\n"))).to eq(["foobar"]) |
| 38 | + expect(subject.extract("foo")).to emit_exactly([]) |
| 39 | + expect(subject.extract("bar\n")).to emit_exactly(["foobar"]) |
36 | 40 | end
|
37 | 41 |
|
38 | 42 | it "should tokenize multiple token" do
|
39 |
| - expect(to_list(subject.extract("foo\nbar\n"))).to eq(["foo", "bar"]) |
| 43 | + expect(subject.extract("foo\nbar\n")).to emit_exactly(["foo", "bar"]) |
40 | 44 | end
|
41 | 45 |
|
42 | 46 | it "should ignore empty payload" do
|
43 |
| - expect(to_list(subject.extract(""))).to eq([]) |
44 |
| - expect(to_list(subject.extract("foo\nbar"))).to eq(["foo"]) |
| 47 | + expect(subject.extract("")).to emit_exactly([]) |
| 48 | + expect(subject.extract("foo\nbar")).to emit_exactly(["foo"]) |
45 | 49 | end
|
46 | 50 |
|
47 | 51 | it "should tokenize empty payload with newline" do
|
48 |
| - expect(to_list(subject.extract("\n"))).to eq([""]) |
49 |
| - expect(to_list(subject.extract("\n\n\n"))).to eq(["", "", ""]) |
| 52 | + expect(subject.extract("\n")).to emit_exactly([""]) |
| 53 | + expect(subject.extract("\n\n\n")).to emit_exactly(["", "", ""]) |
50 | 54 | end
|
51 | 55 |
|
52 | 56 | describe 'flush' do
|
@@ -89,12 +93,12 @@ def to_list(iterator)
|
89 | 93 | let(:delimiter) { "||" }
|
90 | 94 |
|
91 | 95 | it "should tokenize multiple token" do
|
92 |
| - expect(to_list(subject.extract("foo||b|r||"))).to eq(["foo", "b|r"]) |
| 96 | + expect(subject.extract("foo||b|r||")).to emit_exactly(["foo", "b|r"]) |
93 | 97 | end
|
94 | 98 |
|
95 | 99 | it "should ignore empty payload" do
|
96 |
| - expect(to_list(subject.extract(""))).to eq([]) |
97 |
| - expect(to_list(subject.extract("foo||bar"))).to eq(["foo"]) |
| 100 | + expect(subject.extract("")).to emit_exactly([]) |
| 101 | + expect(subject.extract("foo||bar")).to emit_exactly(["foo"]) |
98 | 102 | end
|
99 | 103 | end
|
100 | 104 | end
|
0 commit comments