ghaki-match 2011.11.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +19 -0
- data/README +23 -0
- data/lib/ghaki/match/finder.rb +31 -0
- data/lib/ghaki/match/mixin/auto_boolean.rb +32 -0
- data/lib/ghaki/match/mixin/auto_rename.rb +49 -0
- data/lib/ghaki/match/parser/base.rb +66 -0
- data/lib/ghaki/match/parser/boolean.rb +51 -0
- data/spec/ghaki/match/finder_spec.rb +32 -0
- data/spec/ghaki/match/mixin/auto_boolean_spec.rb +86 -0
- data/spec/ghaki/match/mixin/auto_rename_spec.rb +131 -0
- data/spec/ghaki/match/parser/base_spec.rb +101 -0
- data/spec/ghaki/match/parser/boolean_spec.rb +136 -0
- data/spec/spec_helper.rb +6 -0
- metadata +108 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2010 Gerald Kalafut
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
= Ghaki Match - Regexp helpers
|
|
2
|
+
|
|
3
|
+
Ghaki Match is a collection of regular expression automation helpers.
|
|
4
|
+
|
|
5
|
+
== Download
|
|
6
|
+
|
|
7
|
+
The latest version of Ghaki Match can be found at
|
|
8
|
+
|
|
9
|
+
* git@github.com:ghaki/ghaki-match.git
|
|
10
|
+
|
|
11
|
+
== Installation
|
|
12
|
+
|
|
13
|
+
The preferred method of installing Ghaki Match is through its GEM file.
|
|
14
|
+
|
|
15
|
+
% [sudo] gem install ghaki-match-0.0.1.gem
|
|
16
|
+
|
|
17
|
+
== License
|
|
18
|
+
|
|
19
|
+
Ghaki App is released under the MIT license.
|
|
20
|
+
|
|
21
|
+
== Support
|
|
22
|
+
|
|
23
|
+
Contact mailto:gerald@kalafut.org
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Ghaki #:nodoc:
|
|
2
|
+
module Match #:nodoc:
|
|
3
|
+
|
|
4
|
+
# Match against list of regular expressions.
|
|
5
|
+
class Finder
|
|
6
|
+
|
|
7
|
+
attr_accessor :lookups
|
|
8
|
+
|
|
9
|
+
# Given list of regular expressions
|
|
10
|
+
def initialize arg_rx_list
|
|
11
|
+
@lookups = arg_rx_list
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Matches string against saved regexps.
|
|
15
|
+
def match_text text
|
|
16
|
+
@lookups.each do |rx_curr|
|
|
17
|
+
return true if text =~ rx_curr
|
|
18
|
+
end
|
|
19
|
+
false
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Matches any line against saved regexps.
|
|
23
|
+
def match_lines lines
|
|
24
|
+
lines.each do |text|
|
|
25
|
+
return true if self.match_text( text )
|
|
26
|
+
end
|
|
27
|
+
false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
31
|
+
end end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'ghaki/match/parser/boolean'
|
|
2
|
+
|
|
3
|
+
module Ghaki #:nodoc:
|
|
4
|
+
module Match #:nodoc:
|
|
5
|
+
module Mixin #:nodoc:
|
|
6
|
+
|
|
7
|
+
module AutoBoolean
|
|
8
|
+
|
|
9
|
+
attr_writer :auto_boolean_matcher
|
|
10
|
+
|
|
11
|
+
def auto_boolean_matcher
|
|
12
|
+
@auto_boolean_matcher ||= Ghaki::Match::Parser::Boolean.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def auto_boolean_fields
|
|
16
|
+
auto_boolean_matcher.boolean_fields
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def auto_boolean_fields= val
|
|
20
|
+
auto_boolean_matcher.boolean_fields = val
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def auto_boolean_value val, opts={}, &block
|
|
24
|
+
auto_boolean_matcher.parse_value( val, opts, &block )
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def auto_boolean_field key, val, opts={}, &block
|
|
28
|
+
auto_boolean_matcher.parse_field( key, val, opts, &block )
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
end end end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'ghaki/bool/accessors'
|
|
2
|
+
|
|
3
|
+
module Ghaki #:nodoc:
|
|
4
|
+
module Match #:nodoc:
|
|
5
|
+
module Mixin #:nodoc:
|
|
6
|
+
|
|
7
|
+
module AutoRename
|
|
8
|
+
extend Ghaki::Bool::Accessors
|
|
9
|
+
|
|
10
|
+
bool_accessor :auto_rename_to_token
|
|
11
|
+
attr_writer :auto_rename_fields
|
|
12
|
+
|
|
13
|
+
def auto_rename_fields
|
|
14
|
+
@auto_rename_fields || {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Converts 'HTTP Compression' into :http_compression
|
|
18
|
+
def auto_field_name key
|
|
19
|
+
key.downcase.gsub(%r{[^A-Z0-9]+}xoi,' ').strip.gsub(' ','_')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Given a string, convert it using #easy_name
|
|
23
|
+
# - if the rename table does not exist, return the converted string
|
|
24
|
+
# - if the rename table exists, and has an entry for that converted value
|
|
25
|
+
# - if the entry is a token, return the table's token entry
|
|
26
|
+
# - if the entry is true, then just tokenize the converted string
|
|
27
|
+
# - if the entry is false, then pass through the converted string
|
|
28
|
+
# - otherwise, complain about the invalid value
|
|
29
|
+
# - if the rename table exists, and does NOT have an entry
|
|
30
|
+
# - return the string
|
|
31
|
+
|
|
32
|
+
def auto_rename_field dirty_key, opts={}
|
|
33
|
+
do_token = opts[:auto_rename_to_token]
|
|
34
|
+
do_token = self.auto_rename_to_token? if do_token.nil?
|
|
35
|
+
to_field = opts[:auto_rename_fields] || self.auto_rename_fields
|
|
36
|
+
clean_key = auto_field_name(dirty_key)
|
|
37
|
+
field_key = to_field[clean_key]
|
|
38
|
+
field_key = do_token if field_key.nil? and (not do_token.nil?)
|
|
39
|
+
case field_key
|
|
40
|
+
when Symbol then field_key
|
|
41
|
+
when true then clean_key.to_sym
|
|
42
|
+
when false then clean_key
|
|
43
|
+
else
|
|
44
|
+
raise ArgumentError, "Unknown Auto Rename Field Value: #{clean_key}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
end end end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module Ghaki #:nodoc:
|
|
2
|
+
module Match #:nodoc:
|
|
3
|
+
module Parser #:nodoc:
|
|
4
|
+
|
|
5
|
+
# Matches against paired regular expressions and returned values.
|
|
6
|
+
class Base
|
|
7
|
+
|
|
8
|
+
attr_accessor :lookups, :default_value
|
|
9
|
+
|
|
10
|
+
def initialize pairs={}, opts={}
|
|
11
|
+
@lookups = pairs
|
|
12
|
+
@default_value = opts[:default_value]
|
|
13
|
+
add_words( opts[:match_words] ) if opts.has_key?(:match_words)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Add simple words with return values to match pairs.
|
|
17
|
+
# - Keys are return values, values are words to turn into regexps.
|
|
18
|
+
#
|
|
19
|
+
# Given:
|
|
20
|
+
#
|
|
21
|
+
# add_words :up => %w{ ON UP }, :down => %w{ DOWN OFF }
|
|
22
|
+
#
|
|
23
|
+
# This will match against 'ON' and 'UP' and return :up
|
|
24
|
+
# and match against 'DOWN' and 'OFF' and return :down
|
|
25
|
+
def add_words pairs
|
|
26
|
+
pairs.each_pair do |val,keyz|
|
|
27
|
+
keyz.each do |key|
|
|
28
|
+
@lookups[ %r{\b#{key}\b}i ] = val
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Matches lines against regexp keys returning paired value.
|
|
34
|
+
# - Yields on not found or returns default value.
|
|
35
|
+
def match_lines lines, opts={}
|
|
36
|
+
@lookups.each_pair do |rx_curr,ret_val|
|
|
37
|
+
lines.each do |text|
|
|
38
|
+
return ret_val if text =~ rx_curr
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
if block_given?
|
|
42
|
+
yield
|
|
43
|
+
elsif opts.has_key?(:default_value)
|
|
44
|
+
opts[:default_value]
|
|
45
|
+
else
|
|
46
|
+
@default_value
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Matches string against regexp keys returning paired value.
|
|
51
|
+
# - Yields on not found or returns default value.
|
|
52
|
+
def match_text text, opts={}
|
|
53
|
+
@lookups.each_pair do |rx_curr,ret_val|
|
|
54
|
+
return ret_val if text =~ rx_curr
|
|
55
|
+
end
|
|
56
|
+
if block_given?
|
|
57
|
+
yield
|
|
58
|
+
elsif opts.has_key?(:default_value)
|
|
59
|
+
opts[:default_value]
|
|
60
|
+
else
|
|
61
|
+
@default_value
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
end end end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'ghaki/match/parser/base'
|
|
2
|
+
|
|
3
|
+
module Ghaki #:nodoc:
|
|
4
|
+
module Match #:nodoc:
|
|
5
|
+
module Parser #:nodoc:
|
|
6
|
+
|
|
7
|
+
class Boolean < Base
|
|
8
|
+
|
|
9
|
+
DEFAULT_VALUES = {
|
|
10
|
+
true => %w{ TRUE YES ON ENABLED },
|
|
11
|
+
false => %w{ FALSE NO OFF DISABLED },
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
attr_accessor :boolean_fields
|
|
15
|
+
|
|
16
|
+
def initialize opts={}; super( {}, opts )
|
|
17
|
+
opts_boolean opts
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def opts_boolean opts
|
|
21
|
+
@boolean_fields = opts[:boolean_fields] || []
|
|
22
|
+
add_trues( opts[:boolean_trues ] ) unless opts[:boolean_trues].nil?
|
|
23
|
+
add_falses( opts[:boolean_falses] ) unless opts[:boolean_falses].nil?
|
|
24
|
+
add_words( DEFAULT_VALUES ) unless opts[:skip_boolean_defaults]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def add_trues *list
|
|
28
|
+
add_words( true => [list].flatten )
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def add_falses *list
|
|
32
|
+
add_words( false => [list].flatten )
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Determine truthiness, return if it is.
|
|
36
|
+
# - Otherwise return original value or yield if block is given.
|
|
37
|
+
def parse_value val, opts={}, &block
|
|
38
|
+
match_text( val, { :default_value => val }.merge(opts), &block )
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check whether key is a known boolean field, if so, get truthiness.
|
|
42
|
+
def parse_field key, val, opts={}, &block
|
|
43
|
+
if @boolean_fields.member?(key)
|
|
44
|
+
parse_value( val, opts, &block )
|
|
45
|
+
else
|
|
46
|
+
val
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
end end end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'ghaki/match/finder'
|
|
2
|
+
|
|
3
|
+
module Ghaki module Match module Finder_Testing
|
|
4
|
+
describe Ghaki::Match::Finder do
|
|
5
|
+
|
|
6
|
+
subject do
|
|
7
|
+
Ghaki::Match::Finder.new([
|
|
8
|
+
%r{\bquack\b},
|
|
9
|
+
%r{\bmoo\b},
|
|
10
|
+
])
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe '#match_text' do
|
|
14
|
+
it 'should match' do
|
|
15
|
+
subject.match_text('moo cow').should == true
|
|
16
|
+
end
|
|
17
|
+
it 'should not match' do
|
|
18
|
+
subject.match_text('zip zap').should == false
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#match_lines' do
|
|
23
|
+
it 'should match' do
|
|
24
|
+
subject.match_lines(['quick quack']).should == true
|
|
25
|
+
end
|
|
26
|
+
it 'should not match' do
|
|
27
|
+
subject.match_lines(['zip zap']).should == false
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
end end end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require 'ghaki/match/mixin/auto_boolean'
|
|
2
|
+
|
|
3
|
+
module Ghaki module Match module Mixin module AutoBoolean_Testing
|
|
4
|
+
describe AutoBoolean do
|
|
5
|
+
|
|
6
|
+
GOOD_KEY = 'EXPECTED'
|
|
7
|
+
GOOD_TRUE = 'TRUE'
|
|
8
|
+
MISS_KEY = 'INVALID'
|
|
9
|
+
MISS_VAL = 'BOGUS'
|
|
10
|
+
FIELDS_EXP = [GOOD_KEY]
|
|
11
|
+
|
|
12
|
+
class UsingAuto
|
|
13
|
+
include AutoBoolean
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
subject { UsingAuto.new }
|
|
17
|
+
|
|
18
|
+
it { should respond_to :auto_boolean_matcher= }
|
|
19
|
+
|
|
20
|
+
describe '#auto_boolean_matcher' do
|
|
21
|
+
it 'creates when not initialized' do
|
|
22
|
+
subject.auto_boolean_matcher.should be_kind_of(Parser::Boolean)
|
|
23
|
+
end
|
|
24
|
+
it 'accepts initialized value' do
|
|
25
|
+
parser = Parser::Boolean.new
|
|
26
|
+
subject.auto_boolean_matcher = parser
|
|
27
|
+
subject.auto_boolean_matcher.should == parser
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
describe '#auto_boolean_fields' do
|
|
32
|
+
it 'defaults to empty list' do
|
|
33
|
+
subject.auto_boolean_fields.should be_empty
|
|
34
|
+
end
|
|
35
|
+
it 'returns truthiness when field is present' do
|
|
36
|
+
subject.auto_boolean_fields = FIELDS_EXP
|
|
37
|
+
subject.auto_boolean_fields.should == FIELDS_EXP
|
|
38
|
+
subject.auto_boolean_field( GOOD_KEY, GOOD_TRUE ).should be_true
|
|
39
|
+
end
|
|
40
|
+
it 'returns orginal when field is missing' do
|
|
41
|
+
subject.auto_boolean_fields = FIELDS_EXP
|
|
42
|
+
subject.auto_boolean_fields.should == FIELDS_EXP
|
|
43
|
+
subject.auto_boolean_field( MISS_KEY, GOOD_TRUE ).should == GOOD_TRUE
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe '#auto_boolean_value' do
|
|
48
|
+
|
|
49
|
+
context 'using defaults' do
|
|
50
|
+
context 'with known values' do
|
|
51
|
+
Parser::Boolean::DEFAULT_VALUES[true].each do |item|
|
|
52
|
+
it "returns true for: #{item}" do
|
|
53
|
+
subject.auto_boolean_value(item).should be_true
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
Parser::Boolean::DEFAULT_VALUES[false].each do |item|
|
|
58
|
+
it "returns false for: #{item}" do
|
|
59
|
+
subject.auto_boolean_value(item).should be_false
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
context 'with unknown values' do
|
|
64
|
+
it 'returns original value' do
|
|
65
|
+
subject.auto_boolean_value(MISS_VAL).should == MISS_VAL
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe '#auto_boolean_field' do
|
|
72
|
+
context 'with present field' do
|
|
73
|
+
it 'converts value to boolean' do
|
|
74
|
+
subject.auto_boolean_fields = FIELDS_EXP
|
|
75
|
+
subject.auto_boolean_field(GOOD_KEY,GOOD_TRUE).should be_true
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
context 'with invalid field' do
|
|
79
|
+
it 'returns original value' do
|
|
80
|
+
subject.auto_boolean_field(MISS_KEY,MISS_VAL).should == MISS_VAL
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
end
|
|
86
|
+
end end end end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
require 'ghaki/match/mixin/auto_rename'
|
|
2
|
+
|
|
3
|
+
module Ghaki module Match module Mixin module AutoRename_Testing
|
|
4
|
+
describe AutoRename do
|
|
5
|
+
|
|
6
|
+
FIELD_CLEANUP_EXAMPLES = {
|
|
7
|
+
'accepts numbers and letters' => {
|
|
8
|
+
'phasellus_tortor_elementum' => 'phasellus_tortor_elementum',
|
|
9
|
+
'Nulla Nisl 801 Leo Porta Et' => 'nulla_nisl_801_leo_porta_et',
|
|
10
|
+
},
|
|
11
|
+
'removes garbage' => {
|
|
12
|
+
'Lorem ipsum dolor-sit amet.' => 'lorem_ipsum_dolor_sit_amet',
|
|
13
|
+
'Fermentum / Augue (FRINGILLA)' => 'fermentum_augue_fringilla',
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class UsingAuto
|
|
18
|
+
include AutoRename
|
|
19
|
+
end
|
|
20
|
+
before(:each) do
|
|
21
|
+
@subj = UsingAuto.new
|
|
22
|
+
end
|
|
23
|
+
subject { @subj }
|
|
24
|
+
|
|
25
|
+
it { should respond_to :auto_rename_to_token? }
|
|
26
|
+
it { should respond_to :auto_rename_to_token! }
|
|
27
|
+
|
|
28
|
+
it { should respond_to :auto_rename_fields= }
|
|
29
|
+
describe '#auto_rename_fields' do
|
|
30
|
+
it 'defaults empty hash' do
|
|
31
|
+
subject.auto_rename_fields.should == {}
|
|
32
|
+
end
|
|
33
|
+
it 'returns set value' do
|
|
34
|
+
thing = { 'zip' => 'zap' }
|
|
35
|
+
subject.auto_rename_fields = thing
|
|
36
|
+
subject.auto_rename_fields.should == thing
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe '#auto_field_name' do
|
|
41
|
+
FIELD_CLEANUP_EXAMPLES.each_pair do |reason,examples|
|
|
42
|
+
it reason do
|
|
43
|
+
examples.each_pair do |key,val|
|
|
44
|
+
subject.auto_field_name(key).should == val
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe '#auto_rename_field' do
|
|
51
|
+
|
|
52
|
+
ARF_RULES = {
|
|
53
|
+
'phasellus_tortor_elementum' => true, # clean and token
|
|
54
|
+
'lorem_ipsum_dolor_sit_amet' => :amet_lorem_dolor_sit, # set specific
|
|
55
|
+
'nulla_nisl_801_porta' => false, # just clean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
ARF_EXPECTED = {
|
|
59
|
+
'cleans and tokenizes' => {
|
|
60
|
+
'phasellus_tortor_elementum' => :phasellus_tortor_elementum,
|
|
61
|
+
},
|
|
62
|
+
'renames to specific' => {
|
|
63
|
+
'Lorem ipsum dolor sit amet.' => :amet_lorem_dolor_sit,
|
|
64
|
+
},
|
|
65
|
+
'cleans only' => {
|
|
66
|
+
'Nulla Nisl 801 Porta' => 'nulla_nisl_801_porta', # clean
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
before(:each) do
|
|
71
|
+
subject.auto_rename_fields = ARF_RULES
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
context 'with matched' do
|
|
75
|
+
ARF_EXPECTED.each_pair do |reason,examples|
|
|
76
|
+
it "#{reason} by rule" do
|
|
77
|
+
examples.each_pair do |get,put|
|
|
78
|
+
subject.auto_rename_field(get).should == put
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
context 'with unknown' do
|
|
85
|
+
let(:get_field) { 'UNKNOWN' }
|
|
86
|
+
let(:put_token) { :unknown }
|
|
87
|
+
let(:put_clean) { 'unknown' }
|
|
88
|
+
|
|
89
|
+
context 'using set :auto_rename_to_token' do
|
|
90
|
+
|
|
91
|
+
it 'translates unknown into Symbol' do
|
|
92
|
+
subject.auto_rename_to_token! true
|
|
93
|
+
subject.auto_rename_field(get_field).should == put_token
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it 'translates unknown into String' do
|
|
97
|
+
subject.auto_rename_to_token! false
|
|
98
|
+
subject.auto_rename_field(get_field).should == put_clean
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
it 'fails when not set' do
|
|
102
|
+
lambda do
|
|
103
|
+
subject.auto_rename_field('missing')
|
|
104
|
+
end.should raise_error( ArgumentError, 'Unknown Auto Rename Field Value: missing' )
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
context 'using option :auto_rename_to_token' do
|
|
110
|
+
|
|
111
|
+
it 'accepts :auto_rename_to_token when true' do
|
|
112
|
+
subject.auto_rename_field( get_field , :auto_rename_to_token => true ).should == put_token
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'accepts :auto_rename_to_token when false' do
|
|
116
|
+
subject.auto_rename_field( get_field, :auto_rename_to_token => false ).should == put_clean
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it 'fails when not set' do
|
|
120
|
+
lambda do
|
|
121
|
+
subject.auto_rename_field('missing')
|
|
122
|
+
end.should raise_error( ArgumentError, 'Unknown Auto Rename Field Value: missing' )
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
end
|
|
131
|
+
end end end end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
require 'ghaki/match/parser/base'
|
|
2
|
+
|
|
3
|
+
module Ghaki module Match module Parser module Base_Testing
|
|
4
|
+
describe Base do
|
|
5
|
+
FIND_KEY = 'FOUND'
|
|
6
|
+
FIND_VAL = 'RETURNED'
|
|
7
|
+
GOOD_TEXT = 'SHOULD BE FOUND BY PARSER'
|
|
8
|
+
MISS_TEXT = 'INVALID'
|
|
9
|
+
MISS_DEF = 'PASS_BACK'
|
|
10
|
+
|
|
11
|
+
subject do
|
|
12
|
+
Base.new({
|
|
13
|
+
%r{\b#{FIND_KEY}\b} => FIND_VAL,
|
|
14
|
+
})
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe '#initialize' do
|
|
18
|
+
context 'using option :match_words' do
|
|
19
|
+
it 'accepts' do
|
|
20
|
+
@subj = Base.new( :match_words => { FIND_VAL => [FIND_KEY] } )
|
|
21
|
+
subject.match_text(GOOD_TEXT).should == FIND_VAL
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe '#add_words' do
|
|
27
|
+
subject { Base.new }
|
|
28
|
+
it 'generates match pairs' do
|
|
29
|
+
subject.add_words FIND_VAL => [FIND_KEY]
|
|
30
|
+
subject.match_text(GOOD_TEXT).should == FIND_VAL
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe '#match_text' do
|
|
35
|
+
|
|
36
|
+
context 'when matched' do
|
|
37
|
+
it 'passes specified value' do
|
|
38
|
+
subject.match_text(GOOD_TEXT).should == FIND_VAL
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context 'when unmatched' do
|
|
43
|
+
let(:text) { MISS_TEXT }
|
|
44
|
+
it 'returns nil without default' do
|
|
45
|
+
subject.match_text(text).should == nil
|
|
46
|
+
end
|
|
47
|
+
it 'returns default if present' do
|
|
48
|
+
subject.default_value = MISS_DEF
|
|
49
|
+
subject.match_text(text).should == MISS_DEF
|
|
50
|
+
end
|
|
51
|
+
it 'yields to block if present' do
|
|
52
|
+
yielded_action = false
|
|
53
|
+
subject.match_text(text) do
|
|
54
|
+
yielded_action = true
|
|
55
|
+
MISS_DEF
|
|
56
|
+
end.should == MISS_DEF
|
|
57
|
+
yielded_action.should be_true
|
|
58
|
+
end
|
|
59
|
+
it 'passed default overrides existing' do
|
|
60
|
+
subject.default_value = 'IGNORE'
|
|
61
|
+
subject.match_text( text, :default_value => MISS_DEF ).should == MISS_DEF
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
describe '#match_lines' do
|
|
68
|
+
|
|
69
|
+
context 'when matched' do
|
|
70
|
+
it 'should match' do
|
|
71
|
+
subject.match_lines([GOOD_TEXT]).should == FIND_VAL
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
context 'when unmatched' do
|
|
76
|
+
let(:lines) { [MISS_TEXT] }
|
|
77
|
+
it 'returns nil without default' do
|
|
78
|
+
subject.match_lines(lines).should == nil
|
|
79
|
+
end
|
|
80
|
+
it 'returns default if present' do
|
|
81
|
+
subject.default_value = MISS_DEF
|
|
82
|
+
subject.match_lines(lines).should == MISS_DEF
|
|
83
|
+
end
|
|
84
|
+
it 'passed default overrides existing' do
|
|
85
|
+
subject.default_value = 'IGNORE'
|
|
86
|
+
subject.match_lines( lines, :default_value => MISS_DEF ).should == MISS_DEF
|
|
87
|
+
end
|
|
88
|
+
it 'yields to block if present' do
|
|
89
|
+
yielded_action = false
|
|
90
|
+
subject.match_lines(lines) do
|
|
91
|
+
yielded_action = true
|
|
92
|
+
MISS_DEF
|
|
93
|
+
end.should == MISS_DEF
|
|
94
|
+
yielded_action.should be_true
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end
|
|
101
|
+
end end end end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require 'ghaki/match/parser/boolean'
|
|
2
|
+
|
|
3
|
+
module Ghaki module Match module Parser module Boolean_Testing
|
|
4
|
+
describe Boolean do
|
|
5
|
+
|
|
6
|
+
FIELDS_EXP = ['EXPECTED']
|
|
7
|
+
EXTRA_KEY = 'ZAP'
|
|
8
|
+
EXTRA_TEXT = 'ZIP ZAP ZOOM'
|
|
9
|
+
|
|
10
|
+
it { should respond_to :boolean_fields }
|
|
11
|
+
it { should respond_to :boolean_fields= }
|
|
12
|
+
|
|
13
|
+
describe '#initialize' do
|
|
14
|
+
|
|
15
|
+
context 'using option :boolean_fields' do
|
|
16
|
+
it 'accepts when given' do
|
|
17
|
+
@subj = Boolean.new( :boolean_fields => FIELDS_EXP )
|
|
18
|
+
@subj.boolean_fields.should == FIELDS_EXP
|
|
19
|
+
end
|
|
20
|
+
it 'defaults as empty' do
|
|
21
|
+
@subj = Boolean.new
|
|
22
|
+
@subj.boolean_fields.should be_empty
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context 'using option :skip_boolean_defaults' do
|
|
27
|
+
it 'ignores defaults when specified' do
|
|
28
|
+
@subj = Boolean.new( :skip_boolean_defaults => true )
|
|
29
|
+
@subj.lookups.should be_empty
|
|
30
|
+
end
|
|
31
|
+
it 'imports defaults when missing' do
|
|
32
|
+
@subj = Boolean.new
|
|
33
|
+
@subj.lookups.should_not be_empty
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context 'using option :boolean_trues' do
|
|
38
|
+
it 'accepts when given' do
|
|
39
|
+
@subj = Boolean.new( :boolean_trues => EXTRA_KEY )
|
|
40
|
+
@subj.match_text(EXTRA_TEXT).should be_true
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'using option :boolean_falses' do
|
|
45
|
+
it 'accepts when given' do
|
|
46
|
+
@subj = Boolean.new( :boolean_falses => EXTRA_KEY )
|
|
47
|
+
@subj.match_text(EXTRA_TEXT).should be_false
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe '#parse_value' do
|
|
54
|
+
|
|
55
|
+
subject { Boolean.new }
|
|
56
|
+
|
|
57
|
+
context 'when unmatched' do
|
|
58
|
+
let(:text) { 'INVALID' }
|
|
59
|
+
it 'passes through original value' do
|
|
60
|
+
subject.parse_value(text).should == text
|
|
61
|
+
end
|
|
62
|
+
it 'yields to block if present' do
|
|
63
|
+
yielded = false
|
|
64
|
+
subject.parse_value(text) do
|
|
65
|
+
yielded = true
|
|
66
|
+
'RETURNED'
|
|
67
|
+
end.should == 'RETURNED'
|
|
68
|
+
yielded.should be_true
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
context 'using matched defaults' do
|
|
73
|
+
Boolean::DEFAULT_VALUES[true].each do |item|
|
|
74
|
+
it "returns true given: #{item}" do
|
|
75
|
+
subject.parse_value(item).should be_true
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
Boolean::DEFAULT_VALUES[false].each do |item|
|
|
79
|
+
it "returns false given: #{item}" do
|
|
80
|
+
subject.parse_value(item).should be_false
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
describe '#parse_field' do
|
|
88
|
+
|
|
89
|
+
subject { Boolean.new :boolean_fields => FIELDS_EXP }
|
|
90
|
+
|
|
91
|
+
context 'with present field' do
|
|
92
|
+
|
|
93
|
+
let(:field) { FIELDS_EXP.first }
|
|
94
|
+
|
|
95
|
+
context 'when unmatched' do
|
|
96
|
+
let(:text) { 'NOT_FOUND' }
|
|
97
|
+
it 'passes through original value' do
|
|
98
|
+
subject.parse_field( field, text ).should == text
|
|
99
|
+
end
|
|
100
|
+
it 'yields to block if present' do
|
|
101
|
+
yielded = false
|
|
102
|
+
subject.parse_field( field, text ) do
|
|
103
|
+
yielded = true
|
|
104
|
+
'PASSED_BACK'
|
|
105
|
+
end.should == 'PASSED_BACK'
|
|
106
|
+
yielded.should be_true
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context 'using matched defaults' do
|
|
111
|
+
Boolean::DEFAULT_VALUES[true].each do |item|
|
|
112
|
+
it "returns true given: #{item}" do
|
|
113
|
+
subject.parse_field( field, item ).should be_true
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
Boolean::DEFAULT_VALUES[false].each do |item|
|
|
117
|
+
it "returns false given: #{item}" do
|
|
118
|
+
subject.parse_field( field, item ).should be_false
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
context 'with missing field' do
|
|
126
|
+
let(:field) { 'INVALID' }
|
|
127
|
+
let(:text) { 'PASSED_BACK' }
|
|
128
|
+
it 'passes through original value' do
|
|
129
|
+
subject.parse_field( field, text ).should == text
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
end
|
|
136
|
+
end end end end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ghaki-match
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 2011.11.30.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Gerald Kalafut
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2011-11-30 00:00:00.000000000Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: ghaki-bool
|
|
16
|
+
requirement: &73929750 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 2011.11.29.1
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *73929750
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rspec
|
|
27
|
+
requirement: &73929530 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 2.4.0
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *73929530
|
|
36
|
+
- !ruby/object:Gem::Dependency
|
|
37
|
+
name: rdoc
|
|
38
|
+
requirement: &73929290 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 3.9.4
|
|
44
|
+
type: :development
|
|
45
|
+
prerelease: false
|
|
46
|
+
version_requirements: *73929290
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: mocha
|
|
49
|
+
requirement: &73929030 !ruby/object:Gem::Requirement
|
|
50
|
+
none: false
|
|
51
|
+
requirements:
|
|
52
|
+
- - ! '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.9.12
|
|
55
|
+
type: :development
|
|
56
|
+
prerelease: false
|
|
57
|
+
version_requirements: *73929030
|
|
58
|
+
description: Collection of helpers for regular expression helpers.
|
|
59
|
+
email: gerald@kalafut.org
|
|
60
|
+
executables: []
|
|
61
|
+
extensions: []
|
|
62
|
+
extra_rdoc_files:
|
|
63
|
+
- README
|
|
64
|
+
files:
|
|
65
|
+
- lib/ghaki/match/finder.rb
|
|
66
|
+
- lib/ghaki/match/parser/boolean.rb
|
|
67
|
+
- lib/ghaki/match/parser/base.rb
|
|
68
|
+
- lib/ghaki/match/mixin/auto_rename.rb
|
|
69
|
+
- lib/ghaki/match/mixin/auto_boolean.rb
|
|
70
|
+
- README
|
|
71
|
+
- LICENSE
|
|
72
|
+
- spec/ghaki/match/parser/base_spec.rb
|
|
73
|
+
- spec/ghaki/match/parser/boolean_spec.rb
|
|
74
|
+
- spec/ghaki/match/mixin/auto_rename_spec.rb
|
|
75
|
+
- spec/ghaki/match/mixin/auto_boolean_spec.rb
|
|
76
|
+
- spec/ghaki/match/finder_spec.rb
|
|
77
|
+
- spec/spec_helper.rb
|
|
78
|
+
homepage: http://github.com/ghaki
|
|
79
|
+
licenses: []
|
|
80
|
+
post_install_message:
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
none: false
|
|
86
|
+
requirements:
|
|
87
|
+
- - ! '>='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
none: false
|
|
92
|
+
requirements:
|
|
93
|
+
- - ! '>='
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: 1.3.6
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project: ghaki-match
|
|
98
|
+
rubygems_version: 1.8.10
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 3
|
|
101
|
+
summary: Regular expression automation helpers
|
|
102
|
+
test_files:
|
|
103
|
+
- spec/ghaki/match/parser/base_spec.rb
|
|
104
|
+
- spec/ghaki/match/parser/boolean_spec.rb
|
|
105
|
+
- spec/ghaki/match/mixin/auto_rename_spec.rb
|
|
106
|
+
- spec/ghaki/match/mixin/auto_boolean_spec.rb
|
|
107
|
+
- spec/ghaki/match/finder_spec.rb
|
|
108
|
+
- spec/spec_helper.rb
|