Module: AeEasy::Test::Helper
- Defined in:
- lib/ae_easy/test/helper.rb
Class Method Summary collapse
-
.collection_diff(items_a, items_b, opts = {}) ⇒ Hash
Generate a diff over 2 collections.
-
.collection_match?(fragment, universe, opts = {}) ⇒ Boolean
Validate when an item collection match universe item collection.
-
.delete_keys_from!(hash, keys) ⇒ Hash
Delete keys from a hash.
-
.load_file(file_path, should_exists = false) ⇒ String?
Load and return file contents when exists.
-
.load_json_file(file_path, should_exists = false) ⇒ Hash?
Load and return file contents as json when exists.
-
.match?(element, filter, opts = {}) ⇒ Boolean
Check if an hash element match the filter.
-
.match_collections(items_a, items_b, opts = {}) ⇒ Hash
Match two collections and calculate diff.
-
.sanitize(raw_hash, opts) ⇒ Hash
Sanitize a copy of the hash provided.
Class Method Details
.collection_diff(items_a, items_b, opts = {}) ⇒ Hash
Generate a diff over 2 collections.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ae_easy/test/helper.rb', line 121 def self.collection_diff items_a, items_b, opts = {} # TODO: Improve this function #raise NotImplementedError.new('Current status WIP, don\'t use it for now.') opts = { exact_match: true, deep_stringify: true, sanitize: true, skip_keys: nil, compare_way: :both }.merge opts # Match collections items match = nil compare_right = opts[:compare_way] == :right || opts[:compare_way] == :both compare_left = opts[:compare_way] == :left || opts[:compare_way] == :both items_a = items_a.sort{|a,b|b.keys.count <=> a.keys.count} items_b = items_b.sort{|a,b|b.keys.count <=> a.keys.count} remaining_items = items_b + [] not_found = [] items_a.each do |item_a| found = remaining_items.find do |item_b| match = false match ||= match?(item_a, item_b, opts) if compare_left match ||= match?(item_b, item_a, opts) if compare_right match end # Save diff not_found << item_a if found.nil? remaining_items.delete found end # Send diff results { items_a: not_found, items_b: remaining_items, match: (not_found.count < 1 && remaining_items.count < 1) } end |
.collection_match?(fragment, universe, opts = {}) ⇒ Boolean
Validate when an item collection match universe item collection.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/ae_easy/test/helper.rb', line 181 def self.collection_match? fragment, universe, opts = {} opts = { exact_match: true, same_count: true, deep_stringify: true, sanitize: true, skip_keys: nil, compare_way: :both }.merge opts # False when item collections count are different return false if (opts[:match_quantity]) && fragment.count != universe.count diff = collection_diff fragment, universe, opts match = diff[:items_a].count < 1 && diff[:items_b].count < 1 match end |
.delete_keys_from!(hash, keys) ⇒ Hash
Delete keys from a hash.
33 34 35 36 37 |
# File 'lib/ae_easy/test/helper.rb', line 33 def self.delete_keys_from! hash, keys return hash if keys.nil? keys.each{|k|hash.delete k} hash end |
.load_file(file_path, should_exists = false) ⇒ String?
Load and return file contents when exists.
10 11 12 13 |
# File 'lib/ae_easy/test/helper.rb', line 10 def self.load_file file_path, should_exists = false return nil unless should_exists || File.exists?(file_path) File.open(file_path, 'r', encoding: 'UTF-8').read end |
.load_json_file(file_path, should_exists = false) ⇒ Hash?
Load and return file contents as json when exists.
21 22 23 24 25 |
# File 'lib/ae_easy/test/helper.rb', line 21 def self.load_json_file file_path, should_exists = false file_content = load_file file_path, should_exists return nil if file_content.nil? || file_content.to_s.strip == '' JSON.parse(file_content) end |
.match?(element, filter, opts = {}) ⇒ Boolean
Check if an hash element match the filter.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ae_easy/test/helper.rb', line 74 def self.match? element, filter, opts = {} opts = { sanitize: true, deep_stringify: true, exact_match: true, skip_keys: nil }.merge opts # Sanitize element and filter when need if opts[:sanitize] element = sanitize element, opts filter = sanitize filter, opts end # Validate exact match when need exact_match = opts[:exact_match] return false if exact_match && element.keys.count != filter.keys.count # Match element filter filter.each do |k,v| return false if exact_match && !element.has_key?(k) return false if element[k] != v end true end |
.match_collections(items_a, items_b, opts = {}) ⇒ Hash
Match two collections and calculate diff.
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ae_easy/test/helper.rb', line 213 def self.match_collections items_a, items_b, opts = {} diff = collection_diff( items_a, items_b, skip_keys: opts[:skip], compare_way: :both ) match = (diff[:items_a].count < 1 && diff[:items_b].count < 1) {diff: diff, match: diff[:match]} end |
.sanitize(raw_hash, opts) ⇒ Hash
Sanitize a copy of the hash provided.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ae_easy/test/helper.rb', line 49 def self.sanitize raw_hash, opts opts = { deep_stringify: true, skip_keys: nil }.merge opts hash = (opts[:deep_stringify]) ? AeEasy::Core.deep_stringify_keys(raw_hash) : AeEasy::Core.deep_clone(raw_hash) delete_keys_from! hash, opts[:skip_keys] end |