Permalink
Browse files

Executed requests message has requests signatures ordered by the last…

… request time.
  • Loading branch information...
1 parent df682b0 commit 9b4c3f8698ed5e5da49f1c41463b1d3af8ae94fc @bblimke committed Nov 11, 2010
@@ -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
@@ -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
@@ -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
@@ -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.