File: captures_test.rb

package info (click to toggle)
grok 1.20110708.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,388 kB
  • sloc: ansic: 3,469; ruby: 987; makefile: 276; sh: 124; yacc: 106
file content (152 lines) | stat: -rw-r--r-- 5,572 bytes parent folder | download | duplicates (8)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#require 'rubygems'
require 'grok'
require 'test/unit'

class GrokPatternCapturingTests < Test::Unit::TestCase
  def setup
    @grok = Grok.new
  end

  def test_capture_methods
    @grok.add_pattern("foo", ".*")
    @grok.compile("%{foo}")
    match = @grok.match("hello world")
    assert_respond_to(match, :captures)
    assert_respond_to(match, :start)
    assert_respond_to(match, :end)
    assert_respond_to(match, :subject)
    assert_respond_to(match, :each_capture)
  end

  def test_basic_capture
    @grok.add_pattern("foo", ".*")
    @grok.compile("%{foo}")
    input = "hello world"
    match = @grok.match(input)
    assert_equal("(?<0000>.*)", @grok.expanded_pattern)
    assert_kind_of(Grok::Match, match)
    assert_kind_of(Hash, match.captures)
    assert_equal(match.captures.length, 1)
    assert_kind_of(Array, match.captures["foo"])
    assert_equal(1, match.captures["foo"].length)
    assert_kind_of(String, match.captures["foo"][0])
    assert_equal(input, match.captures["foo"][0])

    match.each_capture do |key, val|
      assert(key.is_a?(String), "Grok::Match::each_capture should yield string,string, got #{key.class.name} as first argument.")
      assert(val.is_a?(String), "Grok::Match::each_capture should yield string,string, got #{key.class.name} as first argument.")
    end

    assert_kind_of(Fixnum, match.start)
    assert_kind_of(Fixnum, match.end)
    assert_kind_of(String, match.subject)
    assert_equal(0, match.start,
                 "Match of /.*/, start should equal 0")
    assert_equal(input.length, match.end,
                 "Match of /.*/, end should equal input string length")
    assert_equal(input, match.subject)
  end

  def test_multiple_captures_with_same_name
    @grok.add_pattern("foo", "\\w+")
    @grok.compile("%{foo} %{foo}")
    match = @grok.match("hello world")
    assert_not_equal(false, match)
    assert_equal(1, match.captures.length)
    assert_equal(2, match.captures["foo"].length)
    assert_equal("hello", match.captures["foo"][0])
    assert_equal("world", match.captures["foo"][1])
  end

  def test_multiple_captures
    @grok.add_pattern("foo", "\\w+")
    @grok.add_pattern("bar", "\\w+")
    @grok.compile("%{foo} %{bar}")
    match = @grok.match("hello world")
    assert_not_equal(false, match)
    assert_equal(2, match.captures.length)
    assert_equal(1, match.captures["foo"].length)
    assert_equal(1, match.captures["bar"].length)
    assert_equal("hello", match.captures["foo"][0])
    assert_equal("world", match.captures["bar"][0])
  end

  def test_nested_captures
    @grok.add_pattern("foo", "\\w+ %{bar}")
    @grok.add_pattern("bar", "\\w+")
    @grok.compile("%{foo}")
    match = @grok.match("hello world")
    assert_not_equal(false, match)
    assert_equal(2, match.captures.length)
    assert_equal(1, match.captures["foo"].length)
    assert_equal(1, match.captures["bar"].length)
    assert_equal("hello world", match.captures["foo"][0])
    assert_equal("world", match.captures["bar"][0])
  end

  def test_nesting_recursion
    @grok.add_pattern("foo", "%{foo}")
    assert_raises(ArgumentError) do
      @grok.compile("%{foo}")
    end
  end

  def test_valid_capture_subnames
    name = "foo"
    @grok.add_pattern(name, "\\w+")
    subname = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_abc:def"
    @grok.compile("%{#{name}:#{subname}}")
    match = @grok.match("hello")
    assert_not_equal(false, match)
    assert_equal(1, match.captures.length)
    assert_equal(1, match.captures["#{name}:#{subname}"].length)
    assert_equal("hello", match.captures["#{name}:#{subname}"][0])
  end

  def test_grok_inline_definition
    input = "key=123"
    @grok.compile("key=%{VALUE=\\d+}")
    match = @grok.match(input)
    assert_not_equal(false, match, "Expected '#{input}' to match '#{@grok.expanded_pattern}'")
    assert_equal(1, match.captures.length)
    assert_equal(1, match.captures["VALUE"].length)
    assert_equal("123", match.captures["VALUE"].first)
  end

  def test_grok_crazy_nested_inline_definition
    email = "something@some.host.name"
    input = "HELLO #{email}"
    #@grok[:logmask] = 0xffffff
    @grok.compile("HELLO %{EMAIL:email=[A-Za-z_+-]+@%{MYDOMAIN=some\\.host\\.name}}")
    match = @grok.match(input)
    assert_not_equal(false, match, "Expected '#{input}' to match '#{@grok.expanded_pattern}'")
    assert_equal(2, match.captures.length)
    assert_equal(1, match.captures["EMAIL:email"].length)
    assert_equal(email, match.captures["EMAIL:email"].first)
  end

  def test_grok_nested_inline_definition
    email = "something@some.host.name"
    input = "HELLO #{email}"
    @grok.add_pattern("MYDOMAIN", "some\\.host\\.name")
    #@grok[:logmask] = 0xffffff
    @grok.compile("HELLO %{EMAIL:email=[A-Za-z_+-]+@%{MYDOMAIN}}")
    match = @grok.match(input)
    assert_not_equal(false, match, "Expected '#{input}' to match '#{@grok.expanded_pattern}'")
    assert_equal(2, match.captures.length)
    assert_equal(1, match.captures["EMAIL:email"].length)
    assert_equal(email, match.captures["EMAIL:email"].first)
  end

  # TODO(sissel): This doesn't work yet.
  def __test_grok_inline_definition_with_predicate
    input = "key=123"
    @grok[:logmask] = 0xffffff
    @grok.compile("key=%{VALUE=\\d+ == 123}")
    match = @grok.match(input)
    assert_not_equal(false, match, "Expected '#{input}' to match '#{@grok.expanded_pattern}'")
    assert_equal(1, match.captures.length)
    assert_equal(1, match.captures["VALUE"].length)
    assert_equal("123", match.captures["VALUE"].first)
  end
end