Skip to content

Instantly share code, notes, and snippets.

@Jamedjo
Created October 22, 2014 01:37
Show Gist options
  • Save Jamedjo/75da8e6d13b4cb0e64a3 to your computer and use it in GitHub Desktop.
Save Jamedjo/75da8e6d13b4cb0e64a3 to your computer and use it in GitHub Desktop.
# http://www.codewars.com/kata/batchitemprocessor/ruby
require './batchitemprocessor'
class Test
# This class mimics the one provided in the kata
def self.assert_equals(actual, expected, message="")
if actual!=expected
puts "\nTest '#{message}' failed.\nExpected #{expected}\nGot #{actual}"
else
puts "."
end
end
end
# You can run these tests in the kata by ignoring everything above this line
def test(message)
yield(message)
end
def test_processed(message, items)
bip = BatchItemProcessor.new
yield(bip)
Test.assert_equals(bip.processed_items,items,message)
end
test "processed_items returns an array of items processed" do |message|
bip = BatchItemProcessor.new
bip.process_items([1,2]){}
Test.assert_equals(bip.processed_items,[1,2],message)
end
test "Processes all items if none are already processed" do |message|
processed = []
BatchItemProcessor.new.process_items([1,2,3,4]) do |item|
processed << item
end
Test.assert_equals(processed,[1,2,3,4],message)
end
test "Ignores items already processed" do |message|
processed = []
logItem = ->(item){processed << item}
bip = BatchItemProcessor.new
bip.process_items([1,2,3,4], &logItem)
bip.process_items([3,4,5,6], &logItem)
Test.assert_equals(processed,[1,2,3,4,5,6],message)
end
test_processed "Only processes items when should_process returns true", [2] do |bip|
bip.should_process(&:even?)
bip.process_items([1,2]){}
end
test_processed "reset clears processed item state", [] do |bip|
bip.process_items([1,2]){}
bip.reset
end
[["symbol",:length], ["string","length"]].each do |type, method|
test_processed "uses identify(#{type}) to compare objects", ["a", "aa"] do |bip|
bip.identify(method)
bip.process_items(["a","b","aa","bb"]){}
end
end
test_processed "uses identify to compare hashes", [{id:1,name:"a"}] do |bip|
bip.identify(:id)
bip.process_items([{id:1,name:"a"},{id:1,name:"b"}]){}
end
test_processed "correctly handles nil being processed and not processed again", [nil] do |bip|
bip.process_items([nil,nil]){}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment