Skip to content

Commit 9741517

Browse files
committed
- inverted BufferedTokenizerExt§IterableAdapterWithEmptyCheck#isEmpty
- specs improved (yaauie/logstash@b524a67) with a custom matcher that validates both `empty?` (which maps to isEmpty) and `entries` (which is provided by the jruby shim extending java-Iterator with RubyEnumerable)
1 parent cd0c8b1 commit 9741517

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

logstash-core/spec/logstash/util/buftok_spec.rb

+19-15
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,37 @@
2020
describe FileWatch::BufferedTokenizer do
2121
subject { FileWatch::BufferedTokenizer.new }
2222

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
2731
end
2832

2933
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"])
3135
end
3236

3337
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"])
3640
end
3741

3842
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"])
4044
end
4145

4246
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"])
4549
end
4650

4751
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(["", "", ""])
5054
end
5155

5256
describe 'flush' do
@@ -89,12 +93,12 @@ def to_list(iterator)
8993
let(:delimiter) { "||" }
9094

9195
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"])
9397
end
9498

9599
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"])
98102
end
99103
end
100104
end

logstash-core/src/main/java/org/logstash/common/BufferedTokenizerExt.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public Iterator<CharSequence> iterator() {
107107
}
108108

109109
public boolean isEmpty() {
110-
return origIterator.hasNext();
110+
return !origIterator.hasNext();
111111
}
112112
}
113113

0 commit comments

Comments
 (0)