Permalink
Browse files

Added specs for Callback and DataCallback.

  • Loading branch information...
1 parent 0b293cc commit 3ba343bc095cec91b7b4ce44e2d3302a0445f9bc @postmodern committed Aug 12, 2012
Showing with 80 additions and 0 deletions.
  1. +36 −0 spec/callback_spec.rb
  2. +44 −0 spec/data_callback_spec.rb
View
@@ -0,0 +1,36 @@
+require 'spec_helper'
+require 'ffi/http/parser/instance'
+
+describe Instance::Callback do
+ let(:parser) { Instance.new }
+
+ describe "yielded arguments" do
+ subject do
+ described_class.new { |*arguments| @yielded_arguments = arguments }
+ end
+
+ before { subject.call(parser) }
+
+ it "should not yield the parser" do
+ @yielded_arguments.should be_empty
+ end
+ end
+
+ context "when :return is thrown" do
+ subject do
+ described_class.new { throw :return, -1 }
+ end
+
+ it "should catch :return" do
+ subject.call(parser).should == -1
+ end
+ end
+
+ describe "return value" do
+ subject { described_class.new { :do_stuff } }
+
+ it "should return 0 by default" do
+ subject.call(parser).should == 0
+ end
+ end
+end
@@ -0,0 +1,44 @@
+require 'spec_helper'
+require 'ffi/http/parser/instance'
+
+describe Instance::DataCallback do
+ let(:parser) { Instance.new }
+ let(:data) { 'GET /' }
+ let(:length) { data.length }
+
+ let(:buffer) do
+ FFI::Buffer.new(length).tap do |buffer|
+ buffer.put_bytes(0,data)
+ end
+ end
+
+ describe "yielded arguments" do
+ subject do
+ described_class.new { |data| @yielded_data = data }
+ end
+
+ before { subject.call(parser,buffer,length) }
+
+ it "should yield the data" do
+ @yielded_data.should == data
+ end
+ end
+
+ context "when :return is thrown" do
+ subject do
+ described_class.new { |data| throw :return, -1 }
+ end
+
+ it "should catch :return" do
+ subject.call(parser,buffer,length).should == -1
+ end
+ end
+
+ describe "return value" do
+ subject { described_class.new { |data| :do_stuff } }
+
+ it "should return 0 by default" do
+ subject.call(parser,buffer,length).should == 0
+ end
+ end
+end

0 comments on commit 3ba343b

Please sign in to comment.