Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

raise a PatternError when an undefined pattern is encountered

  • Loading branch information...
commit 8997788e5b9e81c4984bebadd537459b78dfa47a 1 parent 3c3d21a
@fetep fetep authored
View
6 lib/grok-pure.rb
@@ -5,6 +5,8 @@
# TODO(sissel): Check if 'grok' c-ext has been loaded and abort?
class Grok
+ class PatternError < StandardError; end
+
# The pattern input
attr_accessor :pattern
@@ -88,7 +90,7 @@ def compile(pattern)
# Replace any instances of '%{FOO}' with that pattern.
loop do
if iterations_left == 0
- raise "Deep recursion pattern compilation of #{pattern.inspect} - expanded: #{@expanded_pattern.inspect}"
+ raise PatternError, "Deep recursion pattern compilation of #{pattern.inspect} - expanded: #{@expanded_pattern.inspect}"
end
iterations_left -= 1
m = PATTERN_RE.match(@expanded_pattern)
@@ -125,6 +127,8 @@ def compile(pattern)
#puts "m[0]: #{m[0]}"
#puts "replacement_pattern => #{replacement_pattern}"
index += 1
+ else
+ raise PatternError, "pattern #{m[0]} not defined"
end
end
View
13 test/pure-ruby/general/basic_test.rb
@@ -55,4 +55,17 @@ def test_grok_load_patterns_from_file
@grok.compile("%{TEST}")
assert_equal("(?<a0>\\d+)", @grok.expanded_pattern)
end
+
+ def test_grok_expanded_unknown_pattern
+ assert_raise(Grok::PatternError, "unknown pattern %{foo}") do
+ @grok.compile("%{foo}")
+ end
+ end
+
+ def test_grok_expanded_unknown_pattern_embedded
+ @grok.add_pattern("test", "hello world")
+ assert_raise(Grok::PatternError, "unknown pattern %{foo}") do
+ @grok.compile("%{test} bar %{foo} baz")
+ end
+ end
end
View
2  test/pure-ruby/general/captures_test.rb
@@ -86,7 +86,7 @@ def test_nested_captures
def test_nesting_recursion
@grok.add_pattern("foo", "%{foo}")
- assert_raises(RuntimeError) do
+ assert_raises(Grok::PatternError) do
@grok.compile("%{foo}")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.