Skip to content

Commit

Permalink
Executed requests message has requests signatures ordered by the last…
Browse files Browse the repository at this point in the history
… request time.
  • Loading branch information
bblimke committed Nov 11, 2010
1 parent df682b0 commit 9b4c3f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/webmock/request_registry.rb
Expand Up @@ -24,7 +24,7 @@ def to_s
"No requests were made."
else
text = ""
self.requested_signatures.hash.each do |request_signature, times_executed|
self.requested_signatures.each do |request_signature, times_executed|
text << "#{request_signature} was made #{times_executed} time#{times_executed == 1 ? '' : 's' }\n"
end
text
Expand Down
13 changes: 9 additions & 4 deletions lib/webmock/util/hash_counter.rb
@@ -1,20 +1,25 @@
module WebMock

module Util

class Util::HashCounter
attr_accessor :hash
def initialize
self.hash = {}
@order = {}
@max = 0
end
def put key, num=1
hash[key] = (hash[key] || 0) + num
@order[key] = @max = @max + 1
end
def get key
hash[key] || 0
end
end

def each(&block)
@order.to_a.sort {|a, b| a[1] <=> b[1]}.each do |a|
block.call(a[0], hash[a[0]])
end
end
end
end

end
2 changes: 1 addition & 1 deletion spec/request_registry_spec.rb
Expand Up @@ -64,7 +64,7 @@
WebMock::RequestRegistry.instance.requested_signatures.put(s)
end
WebMock::RequestRegistry.instance.to_s.should ==
"PUT http://www.example.org/ was made 1 time\nGET http://www.example.com/ was made 2 times\n"
"GET http://www.example.com/ was made 2 times\nPUT http://www.example.org/ was made 1 time\n"
end

it "should output info if no requests were executed" do
Expand Down
15 changes: 15 additions & 0 deletions spec/util/hash_counter_spec.rb
Expand Up @@ -21,4 +21,19 @@
counter.get(:def).should == 0
end

describe "each" do
it "should provide elements in order of the last modified" do
counter = WebMock::Util::HashCounter.new
counter.put(:a)
counter.put(:b)
counter.put(:c)
counter.put(:b)
counter.put(:a)
counter.put(:d)

elements = []
counter.each {|k,v| elements << [k,v]}
elements.should == [[:c, 1], [:b, 2], [:a, 2], [:d, 1]]
end
end
end

0 comments on commit 9b4c3f8

Please sign in to comment.