From db0def94e96a82a727de3e371a5e9546e7807ba4 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 6 Dec 2019 14:11:50 +0700 Subject: [PATCH 1/4] Prevent Arithmetic overflow (OverflowError) in hash calculations by using the wrapping arithmetic operators --- src/mocks/registry.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mocks/registry.cr b/src/mocks/registry.cr index e3ec157..3801cd1 100644 --- a/src/mocks/registry.cr +++ b/src/mocks/registry.cr @@ -83,7 +83,7 @@ module Mocks end def hash - object_id.hash * 32 + args.hash + object_id.hash &* 32 &+ args.hash end def inspect(io) @@ -122,7 +122,7 @@ module Mocks end def hash - @registry_name.hash * 32 * 32 + @name.hash * 32 + @object_id.hash + @registry_name.hash &* 32 &* 32 &+ @name.hash &* 32 &+ @object_id.hash end def inspect(io) From 30dc89e1f5d76bd4529dd9b1c3a6d040c8dfe320 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 6 Dec 2019 14:36:22 +0700 Subject: [PATCH 2/4] Fix deprecated Time.new warnings --- README.md | 2 +- spec/mocks_spec.cr | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4ef2a51..4096f70 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Mocks.create_struct_mock Example do end example = Example.new -allow(example).to receive(now).and_return(Time.new(2014, 12, 22)) +allow(example).to receive(now).and_return(Time.local(2014, 12, 22)) ``` ### Double diff --git a/spec/mocks_spec.cr b/spec/mocks_spec.cr index 4297f29..831b86c 100644 --- a/spec/mocks_spec.cr +++ b/spec/mocks_spec.cr @@ -42,7 +42,7 @@ end struct StructTimeExample def self.now - Time.new(2015, 1, 10) + Time.local(2015, 1, 10) end end @@ -147,10 +147,10 @@ describe Mocks do end it "works with struct methods" do - StructTimeExample.now.should eq(Time.new(2015, 1, 10)) + StructTimeExample.now.should eq(Time.local(2015, 1, 10)) - allow(StructTimeExample).to receive(self.now).and_return(Time.new(2014, 12, 22)) - StructTimeExample.now.should eq(Time.new(2014, 12, 22)) + allow(StructTimeExample).to receive(self.now).and_return(Time.local(2014, 12, 22)) + StructTimeExample.now.should eq(Time.local(2014, 12, 22)) end it "affects only the same class" do From b098c6ae8a34509df88baf4d3664471cd223bdfe Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 6 Dec 2019 15:35:49 +0700 Subject: [PATCH 3/4] Fixed File.exists? mock (need to mock Crystal::System::File) --- spec/mocks/create_module_mock_spec.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/mocks/create_module_mock_spec.cr b/spec/mocks/create_module_mock_spec.cr index b1979a8..f225d84 100644 --- a/spec/mocks/create_module_mock_spec.cr +++ b/spec/mocks/create_module_mock_spec.cr @@ -10,8 +10,8 @@ Mocks.create_module_mock MyModule do mock self.exists?(name) end -Mocks.create_mock File do - mock self.exists?(name) +Mocks.create_module_mock Crystal::System::File do + mock self.exists?(path) end describe "create module mock macro" do @@ -22,7 +22,7 @@ describe "create module mock macro" do end it "does not fail with Nil errors for stdlib class" do - allow(File).to receive(self.exists?("hello")).and_return(true) + allow(Crystal::System::File).to receive(self.exists?("hello")).and_return(true) File.exists?("world").should eq(false) File.exists?("hello").should eq(true) end From b0870619a8d63bf155b4d2ac62e140a2e7638dc7 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 6 Dec 2019 16:05:17 +0700 Subject: [PATCH 4/4] Added failing spec for class method that has default value for an argument --- spec/mocks_spec.cr | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/mocks_spec.cr b/spec/mocks_spec.cr index 831b86c..2329676 100644 --- a/spec/mocks_spec.cr +++ b/spec/mocks_spec.cr @@ -9,6 +9,10 @@ class Example "#{greeting} world" end + def self.hello_world_default(greeting, object = "world") + "#{greeting} to the #{object}" + end + def say_hello "hey!" end @@ -49,6 +53,7 @@ end Mocks.create_mock Example do mock self.hello_world mock self.hello_world(greeting) + mock self.hello_world_default(greeting, object = "world") mock instance.say_hello mock instance.say_hello(name) mock instance.greeting = value @@ -139,6 +144,13 @@ describe Mocks do Example.hello_world("halo").should eq("halo there world") end + it "works with class methods that have default values for arguments" do + Example.hello_world_default("hello").should eq("hello to the world") + + allow(Example).to receive(self.hello_world_default("halo", "earth")).and_return("halo there earth") + Example.hello_world_default("halo", "earth").should eq("halo there earth") + end + it "works with module methods" do ModuleExample.hello_world.should eq("what a wonderful world")