File: quotedstring.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 (54 lines) | stat: -rw-r--r-- 1,481 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
#require 'rubygems'
require 'grok'
require 'test/unit'

class QuotedStringPatternsTest < Test::Unit::TestCase
  def setup
    @grok = Grok.new
    path = "#{File.dirname(__FILE__)}/../../../patterns/base"
    @grok.add_patterns_from_file(path)
  end

  def test_quoted_string_common
    @grok.compile("%{QUOTEDSTRING}")
    inputs = ["hello", ""]
    quotes = %w{" ' `}
    inputs.each do |value|
      quotes.each do |quote|
        str = "#{quote}#{value}#{quote}"
        match = @grok.match(str)
        assert_not_equal(false, match)
        assert_equal(str, match.captures["QUOTEDSTRING"][0])
      end
    end
  end

  def test_quoted_string_inside_escape
    @grok.compile("%{QUOTEDSTRING}")
    quotes = %w{" ' `}
    quotes.each do |quote|
      str = "#{quote}hello \\#{quote}world\\#{quote}#{quote}"
      match = @grok.match(str)
      assert_not_equal(false, match)
      assert_equal(str, match.captures["QUOTEDSTRING"][0])
    end
  end

  def test_escaped_quotes_no_match_quoted_string
    @grok.compile("%{QUOTEDSTRING}")
    inputs = ["\\\"testing\\\"", "\\\'testing\\\'", "\\\`testing\\\`",]
    inputs.each do |value|
      match = @grok.match(value)
      assert_equal(false, match)
    end
  end

  def test_non_quoted_strings_no_match
    @grok.compile("%{QUOTEDSTRING}")
    inputs = ["\\\"testing", "testing", "hello world ' something ` foo"]
    inputs.each do |value|
      match = @grok.match(value)
      assert_equal(false, match)
    end
  end
end