Skip to content

Instantly share code, notes, and snippets.

@avgerin0s
Last active December 25, 2015 20:49
Show Gist options
  • Save avgerin0s/7038491 to your computer and use it in GitHub Desktop.
Save avgerin0s/7038491 to your computer and use it in GitHub Desktop.
module FuzzyMatch
def self.equals?(obj1, obj2)
if obj1.is_a?(Array) && obj2.is_a?(Array)
obj1.sort == obj2.sort
elsif obj1.is_a?(Hash) && obj2.is_a?(Hash)
return false if !equals?(obj1.keys, obj2.keys)
obj1.keys.each do |key|
return false if !equals?(obj1[key], obj2[key])
end
true
else
obj1 == obj2
end
end
end
require "fuzzy_match"
describe FuzzyMatch do
describe "self.equals?" do
context "when checking for objects from different classes" do
let(:obj1) { { a: 1} }
let(:obj2) { "Skroutz" }
it "returns false" do
FuzzyMatch.equals?(obj1, obj2).should eq(false)
end
end
context "when checking for strings" do
context "that are equal" do
let(:string1) { "Skroutz" }
let(:string2) { "Skroutz" }
it "returns true" do
FuzzyMatch.equals?(string1, string2).should eq(true)
end
end
context "that are different" do
let(:string1) { "Skroutz" }
let(:string2) { "Alve" }
it "returns false" do
FuzzyMatch.equals?(string1, string2).should eq(false)
end
end
end
context "when checking for numbers" do
context "that are equal" do
let(:num1) { 42 }
let(:num2) { 42 }
it "returns true" do
FuzzyMatch.equals?(num1, num2).should eq(true)
end
context "but the one is Fixnum and the other Float" do
let(:num1) { 1 }
let(:num2) { 1.0 }
it "returns true" do
FuzzyMatch.equals?(num1, num2).should eq(true)
end
end
end
context "that are not equal" do
let(:num1) { 42 }
let(:num2) { 4242 }
it "returns false" do
FuzzyMatch.equals?(num1, num2).should eq(false)
end
end
end
context "when checking for arrays" do
context "that are equal" do
context "and in order" do
let(:array1) { [1, 2] }
let(:array2) { [1, 2] }
it "returns true" do
FuzzyMatch.equals?(array1, array2).should eq(true)
end
end
context "but not in order" do
let(:array1) { [1, 2] }
let(:array2) { [2, 1] }
it "returns true" do
FuzzyMatch.equals?(array1, array2).should eq(true)
end
end
context "that are not equal" do
let(:array1) { [1, 1, 2] }
let(:array2) { [1, 2] }
it "returns false" do
FuzzyMatch.equals?(array1, array2).should eq(false)
end
end
end
end
context "when checking for hashes" do
context "that are equal" do
let(:hash1) { { a: 1, b: { c: [1, 2] } } }
let(:hash2) { { a: 1, b: { c: [2, 1] } } }
it "returns true" do
FuzzyMatch.equals?(hash1, hash2).should eq(true)
end
end
context "that are not equal" do
let(:hash1) { { a: 1, b: { c: [3, 2] } } }
let(:hash2) { { a: 1, b: [1, 2] } }
it "returns false" do
FuzzyMatch.equals?(hash1, hash2).should eq(false)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment