File: number.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 (69 lines) | stat: -rw-r--r-- 2,121 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
require 'grok'
require 'test/unit'

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

  def test_match_number
    @grok.compile("%{NUMBER}")
    # step of a prime number near 100 so we get about 2000 iterations
    #puts @grok.expanded_pattern.inspect
    -100000.step(100000, 97) do |value|
      match = @grok.match(value.to_s)
      assert_not_equal(false, match, "#{value} should not match false")
      assert_equal(value.to_s, match.captures["NUMBER"][0])
    end
  end

  def test_match_number_float
    # generate some random floating point values
    # always seed with the same random number, so the test is always the same
    srand(0)
    @grok.compile("%{NUMBER}")
    0.upto(1000) do |value|
      value = (rand * 100000 - 50000).to_s
      match = @grok.match(value)
      assert_not_equal(false, match)
      assert_equal(value, match.captures["NUMBER"][0])
    end
  end

  def test_match_number_amid_things
    @grok.compile("%{NUMBER}")
    value = "hello 12345 world"
    match = @grok.match(value)
    assert_not_equal(false, match)
    assert_equal("12345", match.captures["NUMBER"][0])

    value = "Something costs $55.4!"
    match = @grok.match(value)
    assert_not_equal(false, match)
    assert_equal("55.4", match.captures["NUMBER"][0])
  end

  def test_no_match_number
    @grok.compile("%{NUMBER}")
    ["foo", "", " ", ".", "hello world", "-abcd"].each do |value|
      match = @grok.match(value.to_s)
      assert_equal(false, match)
    end
  end

  def test_match_base16num
    @grok.compile("%{BASE16NUM}")
    # Ruby represents negative values in a strange way, so only
    # test positive numbers for now.
    # I don't think anyone uses negative values in hex anyway...
    0.upto(1000) do |value|
      [("%x" % value), ("0x%08x" % value), ("%016x" % value)].each do |hexstr|
        match = @grok.match(hexstr)
        assert_not_equal(false, match)
        assert_equal(hexstr, match.captures["BASE16NUM"][0])
      end
    end
  end
end