the_array_comparator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +21 -21
- data/README.md +2 -2
- data/TODO.md +2 -1
- data/gemfiles/Gemfile.default +1 -1
- data/lib/the_array_comparator/cache.rb +43 -44
- data/lib/the_array_comparator/caching_strategies/anonymous_cache.rb +69 -0
- data/lib/the_array_comparator/caching_strategies/single_value_cache.rb +69 -0
- data/lib/the_array_comparator/comparator.rb +48 -40
- data/lib/the_array_comparator/exceptions.rb +31 -2
- data/lib/the_array_comparator/{strategies → searching_strategies}/base.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_all.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_all_with_substring_search.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_any.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_any_with_substring_search.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_not.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/contains_not_with_substring_search.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/is_equal.rb +1 -1
- data/lib/the_array_comparator/{strategies → searching_strategies}/is_not_equal.rb +1 -1
- data/lib/the_array_comparator/strategy_dispatcher.rb +110 -0
- data/lib/the_array_comparator/version.rb +2 -2
- data/lib/the_array_comparator.rb +12 -20
- data/spec/benchmark/benchmark_spec.rb +50 -9
- data/spec/cache/anonymous_cache_spec.rb +89 -0
- data/spec/cache/single_value_cache_spec.rb +77 -0
- data/spec/cache_spec.rb +21 -70
- data/spec/comparator/base_spec.rb +2 -2
- data/spec/comparator/comparator_spec.rb +7 -37
- data/spec/comparator/contains_all_spec.rb +7 -7
- data/spec/comparator/contains_all_with_substring_search_spec.rb +6 -6
- data/spec/comparator/contains_any_spec.rb +6 -6
- data/spec/comparator/contains_any_with_substring_search_spec.rb +5 -5
- data/spec/comparator/contains_not_spec.rb +6 -6
- data/spec/comparator/contains_not_with_substring_search_spec.rb +4 -4
- data/spec/comparator/is_equal_spec.rb +7 -7
- data/spec/comparator/is_not_equal_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -1
- data/spec/strategy_dispatcher/strategy_dispatcher_spec.rb +159 -0
- metadata +20 -11
@@ -0,0 +1,110 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
# the main module
|
4
|
+
module TheArrayComparator
|
5
|
+
# the main comparator shell class
|
6
|
+
class StrategyDispatcher
|
7
|
+
|
8
|
+
# Create strategy dispatcher
|
9
|
+
def initialize
|
10
|
+
@available_strategies = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# Define a reader for the available strategies
|
15
|
+
#
|
16
|
+
# @param [String, Symbol] name
|
17
|
+
# the name for the reader
|
18
|
+
def strategy_reader(name)
|
19
|
+
raise Exceptions::UsedInternalKeyword, "You tried to define a reader using an internal name , which is forbidden (your reader name: #{name}). Please choose another name. Thank you very much." if internal_keywords.include? name
|
20
|
+
|
21
|
+
define_method name.to_sym do
|
22
|
+
instance_variable_get :@available_strategies
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def internal_keywords
|
29
|
+
[
|
30
|
+
:initialize,
|
31
|
+
:strategy_reader,
|
32
|
+
:register,
|
33
|
+
:exception_to_raise_for_invalid_strategy,
|
34
|
+
:class_must_have_methods,
|
35
|
+
:each,
|
36
|
+
:interal_keywords,
|
37
|
+
:valid_strategy?,
|
38
|
+
:exception_if_not_implemented,
|
39
|
+
]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# Register a new comparator strategy
|
45
|
+
#
|
46
|
+
# @param [String,Symbol] name
|
47
|
+
# The name which can be used to refer to the registered strategy
|
48
|
+
#
|
49
|
+
# @param [Comparator] klass
|
50
|
+
# The strategy class which should be registered
|
51
|
+
#
|
52
|
+
# @raise user defined exception
|
53
|
+
# Raise exception if an incompatible strategy class is given. Please
|
54
|
+
# see #exception_to_raise_for_invalid_strategy for more information
|
55
|
+
def register(name,klass)
|
56
|
+
if valid_strategy? klass
|
57
|
+
available_strategies[name.to_sym] = klass
|
58
|
+
else
|
59
|
+
raise exception_to_raise_for_invalid_strategy, "Registering #{klass} failed. It does not support \"#{class_must_have_methods.join("-, ")}\"-method"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns the exception used in registration check
|
64
|
+
#
|
65
|
+
# @note
|
66
|
+
# has to be implemented by concrete dispatcher -> otherwise exception
|
67
|
+
def exception_to_raise_for_invalid_strategy
|
68
|
+
exception_if_not_implemented __method__
|
69
|
+
end
|
70
|
+
|
71
|
+
# Return all must have methods
|
72
|
+
#
|
73
|
+
# @note
|
74
|
+
# has to be implemented by concrete dispatcher -> otherwise exception
|
75
|
+
def class_must_have_methods
|
76
|
+
exception_if_not_implemented __method__
|
77
|
+
end
|
78
|
+
|
79
|
+
# Iterate over all strategies
|
80
|
+
#
|
81
|
+
# @param [Block] block
|
82
|
+
# the block to be executed for each strategy
|
83
|
+
#
|
84
|
+
# @return [Enumerator]
|
85
|
+
# enumerator over all available strategies
|
86
|
+
def each(&block)
|
87
|
+
available_strategies.each(block)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
#internal reader
|
93
|
+
def available_strategies
|
94
|
+
@available_strategies or raise Exceptions::WrongUsageOfLibrary, "You forgot to call \"super()\" in your \"initialize\"-method of your strategy dispatcher #{self.class.name}"
|
95
|
+
end
|
96
|
+
|
97
|
+
# Check if given klass is a valid
|
98
|
+
def valid_strategy?(klass)
|
99
|
+
class_must_have_methods.all? { |m| klass.new.respond_to?(m) }
|
100
|
+
end
|
101
|
+
|
102
|
+
# There are methods to be
|
103
|
+
# implemented, raise an exception
|
104
|
+
# if one forgot to implement them
|
105
|
+
def exception_if_not_implemented(m)
|
106
|
+
raise Exceptions::MustHaveMethodNotImplemented, "You forgot to implement the must have method \"#{m}\" in your strategy dispatcher #{self.class.name}"
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
data/lib/the_array_comparator.rb
CHANGED
@@ -6,30 +6,22 @@ require 'forwardable'
|
|
6
6
|
|
7
7
|
require 'the_array_comparator/version'
|
8
8
|
require 'the_array_comparator/exceptions'
|
9
|
-
require 'the_array_comparator/cache'
|
10
9
|
require 'the_array_comparator/sample'
|
11
10
|
require 'the_array_comparator/check'
|
12
11
|
require 'the_array_comparator/result'
|
13
|
-
require 'the_array_comparator/
|
14
|
-
require 'the_array_comparator/
|
15
|
-
require 'the_array_comparator/
|
16
|
-
require 'the_array_comparator/
|
17
|
-
require 'the_array_comparator/
|
18
|
-
require 'the_array_comparator/
|
19
|
-
require 'the_array_comparator/
|
20
|
-
require 'the_array_comparator/
|
21
|
-
require 'the_array_comparator/
|
12
|
+
require 'the_array_comparator/strategy_dispatcher'
|
13
|
+
require 'the_array_comparator/cache'
|
14
|
+
require 'the_array_comparator/searching_strategies/base'
|
15
|
+
require 'the_array_comparator/searching_strategies/contains_all_with_substring_search'
|
16
|
+
require 'the_array_comparator/searching_strategies/contains_any_with_substring_search'
|
17
|
+
require 'the_array_comparator/searching_strategies/contains_all'
|
18
|
+
require 'the_array_comparator/searching_strategies/contains_any'
|
19
|
+
require 'the_array_comparator/searching_strategies/contains_not_with_substring_search'
|
20
|
+
require 'the_array_comparator/searching_strategies/contains_not'
|
21
|
+
require 'the_array_comparator/searching_strategies/is_equal'
|
22
|
+
require 'the_array_comparator/searching_strategies/is_not_equal'
|
22
23
|
require 'the_array_comparator/comparator'
|
23
24
|
|
24
25
|
|
25
26
|
# main module
|
26
|
-
module TheArrayComparator
|
27
|
-
Comparator.register :contains_all, Strategies::ContainsAll
|
28
|
-
Comparator.register :contains_any, Strategies::ContainsAny
|
29
|
-
Comparator.register :not_contains, Strategies::ContainsNot
|
30
|
-
Comparator.register :contains_all_as_substring, Strategies::ContainsAllWithSubstringSearch
|
31
|
-
Comparator.register :contains_any_as_substring, Strategies::ContainsAnyWithSubstringSearch
|
32
|
-
Comparator.register :not_contains_substring, Strategies::ContainsNotWithSubstringSearch
|
33
|
-
Comparator.register :is_equal, Strategies::IsEqual
|
34
|
-
Comparator.register :is_not_equal, Strategies::IsNotEqual
|
35
|
-
end
|
27
|
+
module TheArrayComparator; end
|
@@ -1,28 +1,69 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
2
|
+
require 'strategies_helper'
|
3
3
|
|
4
4
|
describe "benchmark for strategies" do
|
5
5
|
let(:count_of_lines) { 100000 }
|
6
|
+
let(:keywords) { %w{a c d} }
|
7
|
+
let(:data) { generate_testdata(keywords: keywords, count_of_data: count_of_lines) }
|
6
8
|
|
7
9
|
describe "strategy contains with substring search" do
|
8
|
-
let(:keywords) { %w{ab c d} }
|
9
|
-
let(:data) { generate_testdata(keywords: keywords, count_of_data: count_of_lines) }
|
10
|
-
|
11
10
|
it "is fast", :bm => true do
|
12
|
-
|
11
|
+
keywords = %w{ab c d}
|
12
|
+
data = generate_testdata(keywords: keywords, count_of_data: count_of_lines)
|
13
|
+
sample = SampleDouble.new(data,keywords)
|
14
|
+
|
15
|
+
comparator = SearchingStrategies::ContainsAllWithSubstringSearch.new(sample)
|
13
16
|
time_taken = Benchmark.realtime { comparator.success? }
|
14
17
|
expect(time_taken).to be < 1
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
18
21
|
describe "strategy contains" do
|
19
|
-
let(:keywords) { %w{a c d} }
|
20
|
-
let(:data) { generate_testdata(keywords: keywords, count_of_data: count_of_lines) }
|
21
|
-
|
22
22
|
it "is fast", :bm => true do
|
23
|
-
|
23
|
+
sample = SampleDouble.new(data,keywords)
|
24
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
24
25
|
time_taken = Benchmark.realtime { comparator.success? }
|
25
26
|
expect(time_taken).to be < 1
|
26
27
|
end
|
27
28
|
end
|
29
|
+
|
30
|
+
describe "caching behaviour" do
|
31
|
+
it "the cache reduces delay", :bm => true do
|
32
|
+
comparator = Comparator.new
|
33
|
+
keyword = %w{ a }
|
34
|
+
|
35
|
+
100.times do
|
36
|
+
comparator.add_check data , :contains_all , keyword
|
37
|
+
end
|
38
|
+
|
39
|
+
time_taken_cold_cache = Benchmark.realtime { comparator.success? }
|
40
|
+
time_taken_warm_cache = Benchmark.realtime { comparator.success? }
|
41
|
+
|
42
|
+
expect(time_taken_warm_cache).to be < time_taken_cold_cache
|
43
|
+
end
|
44
|
+
|
45
|
+
it "will be reseted when a new element was added after first access to cache", :bm => true do
|
46
|
+
comparator = Comparator.new
|
47
|
+
keyword = %w{ a }
|
48
|
+
|
49
|
+
#cold cache 1
|
50
|
+
100.times do
|
51
|
+
comparator.add_check data , :contains_all , keyword
|
52
|
+
end
|
53
|
+
time_taken_cold_cache_1 = Benchmark.realtime { comparator.success? }
|
54
|
+
|
55
|
+
#cold cache 2
|
56
|
+
comparator.add_check data , :contains_all , keyword
|
57
|
+
time_taken_cold_cache_2 = Benchmark.realtime { comparator.success? }
|
58
|
+
|
59
|
+
#there should be no large difference between those values
|
60
|
+
# cold: ~8s
|
61
|
+
# warm: <1s
|
62
|
+
#
|
63
|
+
# therefor all difference larger than 1 should be treated as error
|
64
|
+
difference = time_taken_cold_cache_1 - time_taken_cold_cache_2
|
65
|
+
|
66
|
+
expect(difference).to be < 1
|
67
|
+
end
|
68
|
+
end
|
28
69
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#enconding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe CachingStrategies::AnonymousCache do
|
5
|
+
|
6
|
+
let(:cache) { CachingStrategies::AnonymousCache.new }
|
7
|
+
|
8
|
+
it "adds abitrary objects to cache" do
|
9
|
+
expect {
|
10
|
+
cache.add %w{obj}
|
11
|
+
cache.add('obj')
|
12
|
+
}.to_not raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns all cached objects" do
|
16
|
+
objects = []
|
17
|
+
|
18
|
+
objects << cache.add('obj')
|
19
|
+
objects << cache.add('obj')
|
20
|
+
objects << cache.add('obj')
|
21
|
+
|
22
|
+
expect(cache.stored_objects).to eq(objects)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "clears cache" do
|
26
|
+
cache.add('obj')
|
27
|
+
cache.add('obj')
|
28
|
+
cache.add('obj')
|
29
|
+
expect(cache.stored_objects.size).to eq(3)
|
30
|
+
|
31
|
+
cache.clear
|
32
|
+
expect(cache.stored_objects.size).to eq(0)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "tells the requester if there are new objects" do
|
36
|
+
cache.add('obj')
|
37
|
+
expect(cache.new_objects?).to eq(true)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "tells the requester if there __no__ new objects" do
|
41
|
+
cache.add('obj')
|
42
|
+
cache.stored_objects
|
43
|
+
expect(cache.new_objects?).to eq(false)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "works with sub-sequent requests to the cache as well" do
|
47
|
+
cache.add('obj')
|
48
|
+
cache.stored_objects
|
49
|
+
cache.add('obj')
|
50
|
+
expect(cache.new_objects?).to eq(true)
|
51
|
+
|
52
|
+
cache.add('obj')
|
53
|
+
cache.stored_objects
|
54
|
+
cache.add('obj')
|
55
|
+
cache.stored_objects
|
56
|
+
expect(cache.new_objects?).to eq(false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "returns the same objecs if requested multiple times" do
|
60
|
+
cache.add('obj')
|
61
|
+
cache.add('obj')
|
62
|
+
run1 = cache.stored_objects
|
63
|
+
run2 = cache.stored_objects
|
64
|
+
|
65
|
+
expect(run1).to be(run2)
|
66
|
+
expect(run1).to eq(run2)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "deletes specific objects from cache (by number)" do
|
70
|
+
cache.add('obj')
|
71
|
+
c_created = cache.add('obj')
|
72
|
+
cache.add('obj')
|
73
|
+
|
74
|
+
c_deleted = cache.delete_object(1)
|
75
|
+
|
76
|
+
expect(c_created).to eq(c_deleted)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "gets you a specific object" do
|
80
|
+
cache.add('obj')
|
81
|
+
c_created = cache.add('obj')
|
82
|
+
cache.add('obj')
|
83
|
+
|
84
|
+
c_fetched = cache.fetch_object(1)
|
85
|
+
|
86
|
+
expect(c_created).to eq(c_fetched)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#enconding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe CachingStrategies::SingleValueCache do
|
5
|
+
|
6
|
+
let(:cache) { CachingStrategies::SingleValueCache.new }
|
7
|
+
|
8
|
+
it "adds abitrary objects to cache" do
|
9
|
+
expect {
|
10
|
+
cache.add %w{obj}
|
11
|
+
}.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns one and only object" do
|
15
|
+
objects = []
|
16
|
+
|
17
|
+
objects << cache.add('obj')
|
18
|
+
objects << cache.add('obj')
|
19
|
+
|
20
|
+
expect(cache.stored_objects).to eq([ objects.last ])
|
21
|
+
end
|
22
|
+
|
23
|
+
it "clears cache" do
|
24
|
+
cache.add('obj')
|
25
|
+
expect(cache.stored_objects.size).to eq(1)
|
26
|
+
|
27
|
+
cache.clear
|
28
|
+
expect(cache.stored_objects.size).to eq(0)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "tells the requester if there are new objects" do
|
32
|
+
cache.add('obj')
|
33
|
+
expect(cache.new_objects?).to eq(true)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "tells the requester if there __no__ new objects" do
|
37
|
+
cache.add('obj')
|
38
|
+
cache.stored_objects
|
39
|
+
expect(cache.new_objects?).to eq(false)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "works with sub-sequent requests to the cache as well" do
|
43
|
+
cache.add('obj')
|
44
|
+
cache.stored_objects
|
45
|
+
cache.add('obj')
|
46
|
+
expect(cache.new_objects?).to eq(true)
|
47
|
+
|
48
|
+
cache.add('obj')
|
49
|
+
cache.stored_objects
|
50
|
+
cache.add('obj')
|
51
|
+
cache.stored_objects
|
52
|
+
expect(cache.new_objects?).to eq(false)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns the same objecs if requested multiple times" do
|
56
|
+
cache.add('obj')
|
57
|
+
run1 = cache.stored_objects
|
58
|
+
run2 = cache.stored_objects
|
59
|
+
|
60
|
+
expect(run1).to be(run2)
|
61
|
+
expect(run1).to eq(run2)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "deletes specific objects from cache (by number)" do
|
65
|
+
c_created = cache.add('obj')
|
66
|
+
c_deleted = cache.delete_object
|
67
|
+
|
68
|
+
expect(c_deleted).to eq(c_created)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "gets you a specific object" do
|
72
|
+
c_created = cache.add('obj')
|
73
|
+
c_fetched = cache.fetch_object
|
74
|
+
|
75
|
+
expect(c_fetched).to eq(c_created)
|
76
|
+
end
|
77
|
+
end
|
data/spec/cache_spec.rb
CHANGED
@@ -1,89 +1,40 @@
|
|
1
|
-
#enconding: utf-8
|
2
1
|
require 'spec_helper'
|
3
2
|
|
4
3
|
describe Cache do
|
5
4
|
|
6
|
-
let
|
5
|
+
it "let you add a cache" do
|
6
|
+
cache = Cache.new
|
7
|
+
cache.add(:test, :anonymous_cache)
|
8
|
+
data = %w{ a b c d}
|
7
9
|
|
8
|
-
it "adds abitrary objects to cache" do
|
9
10
|
expect {
|
10
|
-
cache.add
|
11
|
-
cache.add('obj')
|
11
|
+
cache[:test].add data
|
12
12
|
}.to_not raise_error
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
16
|
-
|
15
|
+
it "fails if an unknown cache strategy is given" do
|
16
|
+
cache = Cache.new
|
17
|
+
data = %w{ a b c d}
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
expect(cache.stored_objects).to eq(objects)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "clears cache" do
|
26
|
-
cache.add('obj')
|
27
|
-
cache.add('obj')
|
28
|
-
cache.add('obj')
|
29
|
-
expect(cache.stored_objects.size).to eq(3)
|
30
|
-
|
31
|
-
cache.clear
|
32
|
-
expect(cache.stored_objects.size).to eq(0)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "tells the requester if there are new objects" do
|
36
|
-
cache.add('obj')
|
37
|
-
expect(cache.new_objects?).to eq(true)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "tells the requester if there __no__ new objects" do
|
41
|
-
cache.add('obj')
|
42
|
-
cache.stored_objects
|
43
|
-
expect(cache.new_objects?).to eq(false)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "works with sub-sequent requests to the cache as well" do
|
47
|
-
cache.add('obj')
|
48
|
-
cache.stored_objects
|
49
|
-
cache.add('obj')
|
50
|
-
expect(cache.new_objects?).to eq(true)
|
51
|
-
|
52
|
-
cache.add('obj')
|
53
|
-
cache.stored_objects
|
54
|
-
cache.add('obj')
|
55
|
-
cache.stored_objects
|
56
|
-
expect(cache.new_objects?).to eq(false)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "returns the same objecs if requested multiple times" do
|
60
|
-
cache.add('obj')
|
61
|
-
cache.add('obj')
|
62
|
-
run1 = cache.stored_objects
|
63
|
-
run2 = cache.stored_objects
|
64
|
-
|
65
|
-
expect(run1).to be(run2)
|
66
|
-
expect(run1).to eq(run2)
|
19
|
+
expect {
|
20
|
+
cache.add(:test, :anonymous_cache_abc)
|
21
|
+
}.to raise_error Exceptions::UnknownCachingStrategy
|
67
22
|
end
|
68
23
|
|
69
|
-
it "
|
70
|
-
|
71
|
-
c_created = cache.add('obj')
|
72
|
-
cache.add('obj')
|
73
|
-
|
74
|
-
c_deleted = cache.delete_object(1)
|
24
|
+
it "fails if an unknown cache is given" do
|
25
|
+
data = %w{ a b c d}
|
75
26
|
|
76
|
-
expect
|
27
|
+
expect {
|
28
|
+
cache[:test123].add data
|
29
|
+
}.to raise_error
|
77
30
|
end
|
78
31
|
|
79
|
-
it "
|
80
|
-
cache.
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
c_fetched = cache.fetch_object(1)
|
32
|
+
it "returns the cache after adding it" do
|
33
|
+
cache = Cache.new
|
34
|
+
cache.add(:test, :anonymous_cache)
|
35
|
+
data = %w{ a b c d}
|
85
36
|
|
86
|
-
expect(
|
37
|
+
expect( cache[:test].class ).to be(CachingStrategies::AnonymousCache)
|
87
38
|
end
|
88
39
|
|
89
40
|
end
|
@@ -2,10 +2,10 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'strategies_helper'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe SearchingStrategies::Base do
|
6
6
|
|
7
7
|
it "raise an exception if api is not implemented" do
|
8
|
-
class TestStrategy <
|
8
|
+
class TestStrategy < SearchingStrategies::Base
|
9
9
|
end
|
10
10
|
|
11
11
|
expect {
|
@@ -2,29 +2,15 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Comparator do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
cache_klass = Class.new do
|
6
|
+
def success?
|
7
|
+
true
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
expect {
|
13
|
-
Comparator.register(:is_eqal_new, comparator_klass)
|
14
|
-
}.to_not raise_error Exceptions::IncompatibleComparator
|
10
|
+
def initialize(sample=nil)
|
11
|
+
end
|
15
12
|
end
|
16
13
|
|
17
|
-
it "fails when registering a not suitable class" do
|
18
|
-
comparator_instance = double('TestComparatorInstance')
|
19
|
-
comparator_instance.stub(:successasdf?).and_return(true)
|
20
|
-
|
21
|
-
comparator_klass = double('TestComparatorClass')
|
22
|
-
comparator_klass.stub(:new).and_return(comparator_instance)
|
23
|
-
expect {
|
24
|
-
Comparator.register(:is_eqal_new, comparator_klass)
|
25
|
-
}.to raise_error Exceptions::IncompatibleComparator
|
26
|
-
end
|
27
|
-
|
28
14
|
it "let you add check to check for" do
|
29
15
|
testrun = Comparator.new
|
30
16
|
data = %w{ a b c d}
|
@@ -45,22 +31,6 @@ describe Comparator do
|
|
45
31
|
}.to raise_error Exceptions::UnknownCheckType
|
46
32
|
end
|
47
33
|
|
48
|
-
it "let you register and use classes" do
|
49
|
-
comparator_instance = double('TestComparatorInstance')
|
50
|
-
comparator_instance.stub(:success?).and_return(true)
|
51
|
-
|
52
|
-
comparator_klass = double('TestComparatorClass')
|
53
|
-
comparator_klass.stub(:new).and_return(comparator_instance)
|
54
|
-
|
55
|
-
Comparator.register(:new_comp, comparator_klass)
|
56
|
-
|
57
|
-
comparator = Comparator.new
|
58
|
-
comparator.add_check %w{a}, :new_comp , %{a}
|
59
|
-
result = comparator.success?
|
60
|
-
|
61
|
-
expect(result).to eq(true)
|
62
|
-
end
|
63
|
-
|
64
34
|
it "support some comparators by default" do
|
65
35
|
testrun = Comparator.new
|
66
36
|
data = %w{ a b c d}
|
@@ -182,7 +152,7 @@ describe Comparator do
|
|
182
152
|
expect(result.of_checks).to eq(true)
|
183
153
|
end
|
184
154
|
|
185
|
-
it "tells you which check has failed and made the whole thing failed"
|
155
|
+
it "tells you which check has failed and made the whole thing failed" do
|
186
156
|
comparator = Comparator.new
|
187
157
|
data = %w{ a b c d }
|
188
158
|
keyword_successfull = %w{ a b }
|
@@ -2,7 +2,7 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
require 'strategies_helper'
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe SearchingStrategies::ContainsAll do
|
6
6
|
let(:data) { %w{ a b c e} }
|
7
7
|
let(:keywords_overlap) { %w{ a } }
|
8
8
|
let(:keywords_no_overlap) { %w{ d } }
|
@@ -10,37 +10,37 @@ describe Strategies::ContainsAll do
|
|
10
10
|
|
11
11
|
it "fails if keywords are empty" do
|
12
12
|
sample = SampleDouble.new(data,[])
|
13
|
-
comparator =
|
13
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
14
14
|
expect(comparator.success?).to eq(false)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "fails if data is empty" do
|
18
18
|
sample = SampleDouble.new([],keywords_no_overlap)
|
19
|
-
comparator =
|
19
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
20
20
|
expect(comparator.success?).to eq(false)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "is successfull if both keywords and data are empty" do
|
24
24
|
sample = SampleDouble.new([],[])
|
25
|
-
comparator =
|
25
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
26
26
|
expect(comparator.success?).to eq(true)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "is successfull when there's a data overlap" do
|
30
30
|
sample = SampleDouble.new(data,keywords_overlap)
|
31
|
-
comparator =
|
31
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
32
32
|
expect(comparator.success?).to eq(true)
|
33
33
|
end
|
34
34
|
|
35
35
|
it "doesn't find something if there's no overlap" do
|
36
36
|
sample = SampleDouble.new(data,keywords_no_overlap)
|
37
|
-
comparator =
|
37
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
38
38
|
expect(comparator.success?).to eq(false)
|
39
39
|
end
|
40
40
|
|
41
41
|
it "fails if not all keywords can be found within the data" do
|
42
42
|
sample = SampleDouble.new(data,multiple_keywords_with_one_no_overlap)
|
43
|
-
comparator =
|
43
|
+
comparator = SearchingStrategies::ContainsAll.new(sample)
|
44
44
|
expect(comparator.success?).to eq(false)
|
45
45
|
end
|
46
46
|
|