crafting_table 0.3.0
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.asc +11 -0
- data.tar.gz.asc +11 -0
- data/.gitignore +0 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +41 -0
- data/LICENSE +14 -0
- data/Rakefile +0 -0
- data/crafting_table.gemspec +19 -0
- data/data/items/thermal_expansion.yml +64 -0
- data/data/items/vanilla.yml +1152 -0
- data/data/recipes/thermal_expansion.yml +97 -0
- data/data/recipes/vanilla.yml +25 -0
- data/data/test/items/vanilla.yml +1149 -0
- data/data/test/recipes/vanilla.yml +25 -0
- data/examples/interactive_table.rb +98 -0
- data/features/item_differentiation.feature +18 -0
- data/features/loading_items.feature +10 -0
- data/features/resolving_recipes.feature +26 -0
- data/features/searching_items.feature +83 -0
- data/features/searching_recipes.feature +42 -0
- data/features/step_definitions/item_manager_steps.rb +88 -0
- data/features/step_definitions/item_steps.rb +23 -0
- data/features/step_definitions/recipe_manager_steps.rb +42 -0
- data/features/support/env.rb +29 -0
- data/lib/crafting_table.rb +19 -0
- data/lib/crafting_table/item.rb +61 -0
- data/lib/crafting_table/item_manager.rb +171 -0
- data/lib/crafting_table/recipe.rb +36 -0
- data/lib/crafting_table/recipe_manager.rb +216 -0
- data/lib/crafting_table/search/damage_search.rb +51 -0
- data/lib/crafting_table/search/fuzzy_name_search.rb +39 -0
- data/lib/crafting_table/search/input_search.rb +47 -0
- data/lib/crafting_table/search/item_id_search.rb +51 -0
- data/lib/crafting_table/search/name_search.rb +71 -0
- data/lib/crafting_table/search/output_search.rb +47 -0
- data/lib/crafting_table/search/search_builder.rb +85 -0
- data/spec/crafting_table/item_manager_spec.rb +421 -0
- data/spec/crafting_table/item_spec.rb +67 -0
- data/spec/crafting_table/recipe_manager_spec.rb +353 -0
- data/spec/crafting_table/recipe_spec.rb +25 -0
- data/spec/crafting_table/search/search_builder.rb +105 -0
- data/spec/crafting_table/search/search_spec.rb +383 -0
- data/spec/spec_helper.rb +6 -0
- metadata +145 -0
- metadata.gz.asc +11 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes and items by their damage value.
|
8
|
+
#
|
9
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
10
|
+
# @since 0.3
|
11
|
+
class DamageSearch
|
12
|
+
attr_reader :damage_value
|
13
|
+
|
14
|
+
# Create a new DamageSearch
|
15
|
+
#
|
16
|
+
# @param [Fixnum, Range] damage_value Damage value for which to filter.
|
17
|
+
def initialize(damage_value)
|
18
|
+
@damage_value = damage_value
|
19
|
+
end
|
20
|
+
|
21
|
+
# Apply this filter to a collection of items.
|
22
|
+
#
|
23
|
+
# @param [Array<Item>] items
|
24
|
+
# Collection of items which to filter.
|
25
|
+
# @return [Array<Item>]
|
26
|
+
# Items which matched the search criteria
|
27
|
+
def apply_to(items)
|
28
|
+
if damage_value.respond_to?(:include?)
|
29
|
+
items.select { |item| damage_value.include? item.damage_value }
|
30
|
+
else
|
31
|
+
items.select { |item| damage_value == item.damage_value }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Compare two searches for equality.
|
36
|
+
#
|
37
|
+
# They are considered equal if the damage value for which they filter is equal.
|
38
|
+
#
|
39
|
+
# @param [DamageSearch] other DamageSearch which to compare for equality.
|
40
|
+
# @return [Boolean] Whether two searches are equal.
|
41
|
+
def ==(other)
|
42
|
+
other.damage_value == damage_value
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :eq?, :==
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes and items by their name.
|
8
|
+
#
|
9
|
+
# Unlike NameSearch, this class does not filter for exact matches,
|
10
|
+
# but for inclusions. E.g. searching for "Ore" will return "Iron
|
11
|
+
# Ore" as well as "Gold Ore".
|
12
|
+
#
|
13
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
14
|
+
# @since 0.3
|
15
|
+
class FuzzyNameSearch < NameSearch
|
16
|
+
|
17
|
+
def exact?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
# Apply this filter to a collection of items or recipes.
|
22
|
+
#
|
23
|
+
# @param [Array<Item, Recipe>] collection
|
24
|
+
# Collection of items and recipes which to filter.
|
25
|
+
# @return [Array<Item, Recipe>]
|
26
|
+
# Items and recipes which matched the search criteria.
|
27
|
+
def apply_to(collection)
|
28
|
+
if case_sensitive?
|
29
|
+
collection.select { |item| item.name.include? name }
|
30
|
+
else
|
31
|
+
collection.select { |item| item.name.downcase.include? name.downcase }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes by their input.
|
8
|
+
#
|
9
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
10
|
+
# @since 0.3
|
11
|
+
class InputSearch
|
12
|
+
attr_reader :item
|
13
|
+
|
14
|
+
# Create a new InputSearch
|
15
|
+
#
|
16
|
+
# @param [Item] item Item for which to filter.
|
17
|
+
def initialize(item)
|
18
|
+
@item = item
|
19
|
+
end
|
20
|
+
|
21
|
+
# Apply this filter to a collection of recipes.
|
22
|
+
#
|
23
|
+
# @param [Array<Recipe>] recipes
|
24
|
+
# Collection of recipes which to filter.
|
25
|
+
# @return [Array<Recipe>]
|
26
|
+
# Recipes which matched the search criteria
|
27
|
+
def apply_to(recipes)
|
28
|
+
recipes.select { |recipe| recipe.input.include? item }
|
29
|
+
end
|
30
|
+
|
31
|
+
# Compare two searches for equality.
|
32
|
+
#
|
33
|
+
# They are considered equal if the item for which they filter is equal.
|
34
|
+
#
|
35
|
+
# @param [InputSearch] other InputSearch which to compare for equality.
|
36
|
+
# @return [Boolean] Whether two searches are equal.
|
37
|
+
def ==(other)
|
38
|
+
other.item == item
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :eq?, :==
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes and items by their item ID.
|
8
|
+
#
|
9
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
10
|
+
# @since 0.3
|
11
|
+
class ItemIDSearch
|
12
|
+
attr_reader :item_id
|
13
|
+
|
14
|
+
# Create a new ItemIDSearch
|
15
|
+
#
|
16
|
+
# @param [Fixnum, Range] item_id Item ID for which to search.
|
17
|
+
def initialize(item_id)
|
18
|
+
@item_id = item_id
|
19
|
+
end
|
20
|
+
|
21
|
+
# Apply this filter to a collection of items.
|
22
|
+
#
|
23
|
+
# @param [Array<Item>] items
|
24
|
+
# Collection of items which to filter.
|
25
|
+
# @return [Array<Item>]
|
26
|
+
# Items which matched the search criteria
|
27
|
+
def apply_to(items)
|
28
|
+
if item_id.respond_to?(:include?)
|
29
|
+
items.select { |item| item_id.include? item.item_id }
|
30
|
+
else
|
31
|
+
items.select { |item| item_id == item.item_id }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Compare two searches for equality.
|
36
|
+
#
|
37
|
+
# They are considered equal if the item ID for which they filter is equal.
|
38
|
+
#
|
39
|
+
# @param [ItemIDSearch] other ItemIDSearch which to compare for equality.
|
40
|
+
# @return [Boolean] Whether two searches are equal.
|
41
|
+
def ==(other)
|
42
|
+
other.item_id == item_id
|
43
|
+
end
|
44
|
+
|
45
|
+
alias_method :eq?, :==
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes and items by their name.
|
8
|
+
#
|
9
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
10
|
+
# @since 0.3
|
11
|
+
class NameSearch
|
12
|
+
attr_reader :name, :case_sensitive
|
13
|
+
|
14
|
+
# Create a new NameSearch
|
15
|
+
#
|
16
|
+
# @param [String] name Name for which to filter.
|
17
|
+
# @param [Hash] options Options hash which influences the filtering.
|
18
|
+
# @option options [Boolean] :case_sensitive (true)
|
19
|
+
# Whether to filter case-sensitively.
|
20
|
+
def initialize(name, options = {})
|
21
|
+
@name = name
|
22
|
+
@case_sensitive = options.fetch(:case_sensitive, true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def exact?
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def case_sensitive?
|
30
|
+
@case_sensitive
|
31
|
+
end
|
32
|
+
|
33
|
+
# Apply this filter to a collection of items or recipes.
|
34
|
+
#
|
35
|
+
# @param [Array<Item, Recipe>] collection
|
36
|
+
# Collection of items and recipes which to filter.
|
37
|
+
# @return [Array<Item, Recipe>]
|
38
|
+
# Items and recipes which matched the search criteria.
|
39
|
+
def apply_to(collection)
|
40
|
+
if case_sensitive?
|
41
|
+
if exact?
|
42
|
+
collection.select { |item| item.name == name }
|
43
|
+
else
|
44
|
+
collection.select { |item| item.name.include? name }
|
45
|
+
end
|
46
|
+
else
|
47
|
+
if exact?
|
48
|
+
collection.select { |item| item.name.downcase == name.downcase }
|
49
|
+
else
|
50
|
+
collection.select { |item| item.name.downcase.include? name.downcase }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Compare two searches for equality.
|
56
|
+
#
|
57
|
+
# They are considered equal if the name for which they filter is equal.
|
58
|
+
#
|
59
|
+
# @param [NameSearch] other NameSearch which to compare for equality.
|
60
|
+
# @return [Boolean] Whether two searches are equal.
|
61
|
+
def ==(other)
|
62
|
+
other.name == name && other.case_sensitive == case_sensitive
|
63
|
+
end
|
64
|
+
|
65
|
+
alias_method :eq?, :==
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which allows to filter recipes by their output.
|
8
|
+
#
|
9
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
10
|
+
# @since 0.3
|
11
|
+
class OutputSearch
|
12
|
+
attr_reader :item
|
13
|
+
|
14
|
+
# Create a new OutputSearch.
|
15
|
+
#
|
16
|
+
# @param [Item] item Item for which to filter.
|
17
|
+
def initialize(item)
|
18
|
+
@item = item
|
19
|
+
end
|
20
|
+
|
21
|
+
# Apply this filter to a collection of recipes.
|
22
|
+
#
|
23
|
+
# @param [Array<Recipe>] recipes
|
24
|
+
# Collection of recipes which to filter.
|
25
|
+
# @return [Array<Recipe>]
|
26
|
+
# Recipes which matched the search criteria
|
27
|
+
def apply_to(recipes)
|
28
|
+
recipes.select { |recipe| recipe.output.include? item }
|
29
|
+
end
|
30
|
+
|
31
|
+
# Compare two searches for equality.
|
32
|
+
#
|
33
|
+
# They are considered equal if the item for which they filter is equal.
|
34
|
+
#
|
35
|
+
# @param [InputSearch] other InputSearch which to compare for equality.
|
36
|
+
# @return [Boolean] Whether two searches are equal.
|
37
|
+
def ==(other)
|
38
|
+
other.item == item
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :eq?, :==
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
module CraftingTable
|
4
|
+
|
5
|
+
module Search
|
6
|
+
|
7
|
+
# A class which provides a basic API to construct searches.
|
8
|
+
#
|
9
|
+
# This allows to easily create multiple searches without having to
|
10
|
+
# know about the underlying classes.
|
11
|
+
#
|
12
|
+
# @example Searching for name and damage value
|
13
|
+
# builder = SearchBuilder.new
|
14
|
+
# builder.name = 'Wood'
|
15
|
+
# builder.exact = false
|
16
|
+
# builder.case_sensitive = false
|
17
|
+
# builder.damage_value = 3
|
18
|
+
#
|
19
|
+
# builder.searches #=> [FuzzyNameSearch(...), DamageSearch(...)]
|
20
|
+
#
|
21
|
+
# @author Michael Senn <morrolan@morrolan.ch>
|
22
|
+
# @since 0.3
|
23
|
+
class SearchBuilder
|
24
|
+
# @!attribute [rw] name
|
25
|
+
# @return [String] The name for which to search.
|
26
|
+
#
|
27
|
+
# @!attribute [rw] exact
|
28
|
+
# @return [Boolean] Whether to search for an exact match.
|
29
|
+
#
|
30
|
+
# @!attribute [rw] case_sensitive
|
31
|
+
# @return [Boolean] Whether to search case-sensitively.
|
32
|
+
#
|
33
|
+
# @!attribute [rw] damage_value
|
34
|
+
# @return [#include?, Fixnum]
|
35
|
+
# The damage value(s) for which to search.
|
36
|
+
#
|
37
|
+
# @!attribute [rw] item_id
|
38
|
+
# @return [#include?, Fixnum]
|
39
|
+
# The item ID(s) for which to search.
|
40
|
+
#
|
41
|
+
# @!attribute [rw] input
|
42
|
+
# @return [Item] The input for which to search.
|
43
|
+
#
|
44
|
+
# @!attribute [rw] output
|
45
|
+
# @return [Item] The output for which to search.
|
46
|
+
|
47
|
+
attr_accessor :name, :exact, :case_sensitive
|
48
|
+
attr_accessor :damage_value
|
49
|
+
attr_accessor :item_id
|
50
|
+
attr_accessor :input, :output
|
51
|
+
|
52
|
+
|
53
|
+
# Create a collection of searches based on the instance
|
54
|
+
# variables which were set.
|
55
|
+
#
|
56
|
+
# @return [Array<*Search>]
|
57
|
+
def searches
|
58
|
+
searches = []
|
59
|
+
|
60
|
+
if name
|
61
|
+
c_sens = case_sensitive.nil? ? true : case_sensitive
|
62
|
+
if exact
|
63
|
+
searches << NameSearch.new(name, case_sensitive: c_sens)
|
64
|
+
else
|
65
|
+
searches << FuzzyNameSearch.new(name, case_sensitive: c_sens)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
searches << DamageSearch.new(damage_value) if damage_value
|
70
|
+
|
71
|
+
searches << ItemIDSearch.new(item_id) if item_id
|
72
|
+
|
73
|
+
searches << InputSearch.new(input) if input
|
74
|
+
|
75
|
+
searches << OutputSearch.new(output) if output
|
76
|
+
|
77
|
+
searches
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,421 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module CraftingTable
|
6
|
+
|
7
|
+
describe ItemManager do
|
8
|
+
let(:manager) { ItemManager.new }
|
9
|
+
let(:item_log) { Item.new('Log', 17) }
|
10
|
+
let(:item_gold_ore) { Item.new('Gold Ore', 14) }
|
11
|
+
let(:item_iron_ore) { Item.new('Iron Ore', 15) }
|
12
|
+
let(:item_stone) { Item.new('Stone', 1) }
|
13
|
+
|
14
|
+
let(:item_wood_oak) { Item.new('Oak Wood', 17, 0) }
|
15
|
+
let(:item_wood_spruce) { Item.new('Spruce Wood', 17, 1) }
|
16
|
+
let(:item_wood_birch) { Item.new('Birch Wood', 17, 2) }
|
17
|
+
let(:item_wood_jungle) { Item.new('Jungle Wood', 17, 3) }
|
18
|
+
|
19
|
+
|
20
|
+
describe '#initialize' do
|
21
|
+
|
22
|
+
it 'should set the appropriate instance variables' do
|
23
|
+
expect(manager.items).to eq []
|
24
|
+
|
25
|
+
expect(ItemManager.new([item_log, item_gold_ore]).items).to eq [item_log, item_gold_ore]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#add' do
|
31
|
+
|
32
|
+
it 'should add the item to the internal collection' do
|
33
|
+
expect { manager.add(item_log) }.to change { manager.items.count }.by 1
|
34
|
+
expect(manager.items).to include item_log
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#add_from_file' do
|
40
|
+
|
41
|
+
it 'should add all items from file' do
|
42
|
+
# Todo: This test is very brittle. A separate data file for
|
43
|
+
# testing purposes which does not get modified might be a good idea.
|
44
|
+
|
45
|
+
manager.add_from_file(ITEM_FILE)
|
46
|
+
expect(manager.items).to have(371).items
|
47
|
+
|
48
|
+
item_names = manager.items.map(&:name)
|
49
|
+
['Stone', 'Gold Ore', 'Piston', 'Lapis Lazuli Ore'].each do |name|
|
50
|
+
expect(item_names).to include name
|
51
|
+
end
|
52
|
+
|
53
|
+
expect(manager.find_by_identifier([1, 0]).first.name).to eq 'Stone'
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#clear' do
|
59
|
+
|
60
|
+
it 'should remove all items from the internal collection' do
|
61
|
+
manager.add(item_gold_ore)
|
62
|
+
expect { manager.clear }.to change { manager.items.count }.to 0
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#find' do
|
68
|
+
before(:each) do
|
69
|
+
manager.clear
|
70
|
+
manager.add(item_log)
|
71
|
+
manager.add(item_gold_ore)
|
72
|
+
manager.add(item_iron_ore)
|
73
|
+
manager.add(item_stone)
|
74
|
+
|
75
|
+
manager.add(item_wood_oak)
|
76
|
+
manager.add(item_wood_spruce)
|
77
|
+
manager.add(item_wood_birch)
|
78
|
+
manager.add(item_wood_jungle)
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when searching for the item's name" do
|
82
|
+
|
83
|
+
context 'with case sensitivity' do
|
84
|
+
|
85
|
+
it 'should return items with matching case' do
|
86
|
+
results = manager.find do |search|
|
87
|
+
search.case_sensitive = true
|
88
|
+
search.name = 'Gold Ore'
|
89
|
+
end
|
90
|
+
expect(results).to eq [item_gold_ore]
|
91
|
+
|
92
|
+
results = manager.find do |search|
|
93
|
+
search.case_sensitive = true
|
94
|
+
search.name = 'Log'
|
95
|
+
end
|
96
|
+
expect(results).to eq [item_log]
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not return items where the case does not match' do
|
100
|
+
results = manager.find do |search|
|
101
|
+
search.case_sensitive = true
|
102
|
+
search.name = 'gold ore'
|
103
|
+
end
|
104
|
+
expect(results).to be_empty
|
105
|
+
|
106
|
+
results = manager.find do |search|
|
107
|
+
search.case_sensitive = true
|
108
|
+
search.name = 'LoG'
|
109
|
+
end
|
110
|
+
expect(results).to be_empty
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'with case-insensitivity' do
|
116
|
+
|
117
|
+
it 'should return items no matter their case' do
|
118
|
+
results = manager.find do |search|
|
119
|
+
search.case_sensitive = false
|
120
|
+
search.name = 'Gold Ore'
|
121
|
+
end
|
122
|
+
expect(results).to eq [item_gold_ore]
|
123
|
+
|
124
|
+
results = manager.find do |search|
|
125
|
+
search.case_sensitive = false
|
126
|
+
search.name = 'LOG'
|
127
|
+
end
|
128
|
+
expect(results).to eq [item_log]
|
129
|
+
|
130
|
+
results = manager.find do |search|
|
131
|
+
search.case_sensitive = false
|
132
|
+
search.name = 'iron ore'
|
133
|
+
end
|
134
|
+
expect(results).to eq [item_iron_ore]
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'with exact matching' do
|
140
|
+
|
141
|
+
it 'should return exact matches' do
|
142
|
+
results = manager.find do |search|
|
143
|
+
search.exact = true
|
144
|
+
search.name = 'Gold Ore'
|
145
|
+
end
|
146
|
+
expect(results).to eq [item_gold_ore]
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should not return non-exact matches' do
|
150
|
+
results = manager.find do |search|
|
151
|
+
search.exact = true
|
152
|
+
search.name = 'Gold'
|
153
|
+
end
|
154
|
+
expect(results).to be_empty
|
155
|
+
|
156
|
+
results = manager.find do |search|
|
157
|
+
search.exact = true
|
158
|
+
search.name = 'Ore'
|
159
|
+
end
|
160
|
+
expect(results).to be_empty
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'with fuzzy matching' do
|
166
|
+
|
167
|
+
it 'should return non-exact matches too' do
|
168
|
+
results = manager.find do |search|
|
169
|
+
search.exact = false
|
170
|
+
search.name = 'Gold Ore'
|
171
|
+
end
|
172
|
+
expect(results).to eq [item_gold_ore]
|
173
|
+
|
174
|
+
results = manager.find do |search|
|
175
|
+
search.exact = false
|
176
|
+
search.name = 'Ore'
|
177
|
+
end
|
178
|
+
expect(results).to eq [item_gold_ore, item_iron_ore]
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
context "when searching for the item's damage value" do
|
186
|
+
|
187
|
+
context 'with an integer' do
|
188
|
+
|
189
|
+
it 'should return items with matching damage value' do
|
190
|
+
results = manager.find do |search|
|
191
|
+
search.damage_value = 3
|
192
|
+
end
|
193
|
+
expect(results).to eq [item_wood_jungle]
|
194
|
+
|
195
|
+
results = manager.find do |search|
|
196
|
+
search.damage_value = 1
|
197
|
+
end
|
198
|
+
expect(results).to eq [item_wood_spruce]
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'with a range or array' do
|
204
|
+
|
205
|
+
it 'should return items the damage value of which is included in the range or array' do
|
206
|
+
results = manager.find do |search|
|
207
|
+
search.damage_value = 1..3
|
208
|
+
end
|
209
|
+
expect(results).to eq [item_wood_spruce,
|
210
|
+
item_wood_birch,
|
211
|
+
item_wood_jungle]
|
212
|
+
|
213
|
+
results = manager.find do |search|
|
214
|
+
search.damage_value = [1, 3]
|
215
|
+
end
|
216
|
+
expect(results).to eq [item_wood_spruce,
|
217
|
+
item_wood_jungle]
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
|
224
|
+
context "when searching for the item's ID" do
|
225
|
+
# let(:item_stone { Item.new('Stone', 1) }
|
226
|
+
# let(:item_log) { Item.new('Log', 17) }
|
227
|
+
# let(:item_gold_ore) { Item.new('Gold Ore', 14) }
|
228
|
+
# let(:item_iron_ore) { Item.new('Iron Ore', 15) }
|
229
|
+
|
230
|
+
|
231
|
+
context 'with an integer' do
|
232
|
+
|
233
|
+
it 'should return items with matching ID' do
|
234
|
+
results = manager.find do |search|
|
235
|
+
search.item_id = 15
|
236
|
+
end
|
237
|
+
expect(results).to eq [item_iron_ore]
|
238
|
+
|
239
|
+
results = manager.find do |search|
|
240
|
+
search.item_id = 1
|
241
|
+
end
|
242
|
+
expect(results).to eq [item_stone]
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
context 'with a range or array' do
|
248
|
+
|
249
|
+
it 'should return items the item ID of which is included in the range or array' do
|
250
|
+
results = manager.find do |search|
|
251
|
+
search.item_id = 14..15
|
252
|
+
end
|
253
|
+
expect(results.sort_by { |item| item.name }).to eq [item_gold_ore,
|
254
|
+
item_iron_ore]
|
255
|
+
|
256
|
+
results = manager.find do |search|
|
257
|
+
search.item_id = [1, 15]
|
258
|
+
end
|
259
|
+
expect(results.sort_by { |item| item.name }).to eq [item_iron_ore,
|
260
|
+
item_stone]
|
261
|
+
end
|
262
|
+
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
context 'with multiple searches' do
|
268
|
+
|
269
|
+
it 'should return items which matched all criteria' do
|
270
|
+
results = manager.find do |search|
|
271
|
+
search.name = 'Wood'
|
272
|
+
search.exact = false
|
273
|
+
|
274
|
+
search.damage_value = 3
|
275
|
+
end
|
276
|
+
expect(results).to eq [item_wood_jungle]
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
describe '#find_by_name' do
|
287
|
+
before(:each) do
|
288
|
+
manager.clear
|
289
|
+
manager.add(item_log)
|
290
|
+
manager.add(item_gold_ore)
|
291
|
+
manager.add(item_iron_ore)
|
292
|
+
end
|
293
|
+
|
294
|
+
context 'with case sensitivity' do
|
295
|
+
let(:args) { { case_sensitive: true } }
|
296
|
+
|
297
|
+
it 'should return items with matching case' do
|
298
|
+
expect(manager.find_by_name('Gold Ore', args).first).to eq item_gold_ore
|
299
|
+
expect(manager.find_by_name('Log', args).first).to eq item_log
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'should not return items where the case does not match' do
|
303
|
+
expect(manager.find_by_name('gold ore', args)).to be_empty
|
304
|
+
expect(manager.find_by_name('LoG', args)).to be_empty
|
305
|
+
end
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
context 'with case-insensitivity' do
|
310
|
+
let(:args) { { case_sensitive: false } }
|
311
|
+
|
312
|
+
it 'should return items no matter their case' do
|
313
|
+
expect(manager.find_by_name('Gold Ore', args).first).to eq item_gold_ore
|
314
|
+
expect(manager.find_by_name('LOG', args).first).to eq item_log
|
315
|
+
expect(manager.find_by_name('iron ore', args).first).to eq item_iron_ore
|
316
|
+
end
|
317
|
+
|
318
|
+
end
|
319
|
+
|
320
|
+
context 'with exact matching' do
|
321
|
+
let(:args) { { exact: true } }
|
322
|
+
|
323
|
+
it 'should return exact matches' do
|
324
|
+
expect(manager.find_by_name('Gold Ore', args).first).to eq item_gold_ore
|
325
|
+
end
|
326
|
+
|
327
|
+
it 'should not return non-exact matches' do
|
328
|
+
expect(manager.find_by_name('Gold', args)).to be_empty
|
329
|
+
expect(manager.find_by_name('Ore', args)).to be_empty
|
330
|
+
end
|
331
|
+
|
332
|
+
end
|
333
|
+
|
334
|
+
context 'with fuzzy matching' do
|
335
|
+
let(:args) { { exact: false } }
|
336
|
+
|
337
|
+
it 'should return non-exact matches too' do
|
338
|
+
expect(manager.find_by_name('Gold Ore', args).first).to eq item_gold_ore
|
339
|
+
|
340
|
+
expect(manager.find_by_name('Gold', args).first).to eq item_gold_ore
|
341
|
+
expect(manager.find_by_name('Ore', args).sort { |i1, i2| i1.name <=> i2.name } ) .to eq [item_gold_ore, item_iron_ore]
|
342
|
+
end
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
end
|
347
|
+
|
348
|
+
describe '#find_by_item_id' do
|
349
|
+
before(:each) do
|
350
|
+
manager.clear
|
351
|
+
manager.add(item_log)
|
352
|
+
manager.add(item_gold_ore)
|
353
|
+
manager.add(item_iron_ore)
|
354
|
+
end
|
355
|
+
|
356
|
+
context 'when supplied an integer' do
|
357
|
+
|
358
|
+
it 'should return all items with the same item id' do
|
359
|
+
expect(manager.find_by_item_id(17).first).to eq item_log
|
360
|
+
expect(manager.find_by_item_id(0)).to be_empty
|
361
|
+
end
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
context 'when supplied something which provides #include?' do
|
366
|
+
|
367
|
+
it 'should return all items the id of which was included in the collection' do
|
368
|
+
expect(manager.find_by_item_id(14..15).sort { |i1, i2| i1.name <=> i2.name }).to eq [item_gold_ore, item_iron_ore]
|
369
|
+
expect(manager.find_by_item_id([15, 17]).sort { |i1, i2| i1.name <=> i2.name }).to eq [item_iron_ore, item_log]
|
370
|
+
end
|
371
|
+
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
|
376
|
+
describe '#find_by_damage_value' do
|
377
|
+
before(:each) do
|
378
|
+
manager.clear
|
379
|
+
manager.add(item_wood_oak)
|
380
|
+
manager.add(item_wood_spruce)
|
381
|
+
manager.add(item_wood_birch)
|
382
|
+
manager.add(item_wood_jungle)
|
383
|
+
end
|
384
|
+
|
385
|
+
context 'when supplied an integer' do
|
386
|
+
|
387
|
+
it 'should return all items with the same damage value' do
|
388
|
+
expect(manager.find_by_damage_value(2).first).to eq item_wood_birch
|
389
|
+
end
|
390
|
+
|
391
|
+
end
|
392
|
+
|
393
|
+
context 'when supplied something which provides #include?' do
|
394
|
+
|
395
|
+
it 'should return all items the damage value of which was included in the collection' do
|
396
|
+
expect(manager.find_by_damage_value(0..2).sort { |i1, i2| i1.damage_value <=> i2.damage_value }).to eq [item_wood_oak, item_wood_spruce, item_wood_birch]
|
397
|
+
expect(manager.find_by_damage_value([1, 3]).sort { |i1, i2| i1.damage_value <=> i2.damage_value }).to eq [item_wood_spruce, item_wood_jungle]
|
398
|
+
end
|
399
|
+
|
400
|
+
end
|
401
|
+
|
402
|
+
end
|
403
|
+
|
404
|
+
describe '#find_by_identifier' do
|
405
|
+
before(:each) do
|
406
|
+
manager.clear
|
407
|
+
manager.add(item_wood_oak)
|
408
|
+
manager.add(item_wood_spruce)
|
409
|
+
manager.add(item_wood_birch)
|
410
|
+
manager.add(item_wood_jungle)
|
411
|
+
end
|
412
|
+
|
413
|
+
it 'should return all items with the specified identifier' do
|
414
|
+
expect(manager.find_by_identifier([17, 1]).first).to eq item_wood_spruce
|
415
|
+
end
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
end
|
420
|
+
|
421
|
+
end
|