Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved some tests around

  • Loading branch information...
commit d096735131697a40bb9f1a2dcfc7aef67693d65f 1 parent 05e7bc1
Amos Elliston authored
View
125 lib/amqp/buffer.rb
@@ -274,128 +274,3 @@ def _write data, pack = nil
end
end
end
-
-if $0 =~ /bacon/ or $0 == __FILE__
- require 'bacon'
- include AMQP
-
- describe Buffer do
- before do
- @buf = Buffer.new
- end
-
- should 'have contents' do
- @buf.contents.should == ''
- end
-
- should 'initialize with data' do
- @buf = Buffer.new('abc')
- @buf.contents.should == 'abc'
- end
-
- should 'append raw data' do
- @buf << 'abc'
- @buf << 'def'
- @buf.contents.should == 'abcdef'
- end
-
- should 'append other buffers' do
- @buf << Buffer.new('abc')
- @buf.data.should == 'abc'
- end
-
- should 'have a position' do
- @buf.pos.should == 0
- end
-
- should 'have a length' do
- @buf.length.should == 0
- @buf << 'abc'
- @buf.length.should == 3
- end
-
- should 'know the end' do
- @buf.empty?.should == true
- end
-
- should 'read and write data' do
- @buf._write('abc')
- @buf.rewind
- @buf._read(2).should == 'ab'
- @buf._read(1).should == 'c'
- end
-
- should 'raise on overflow' do
- lambda{ @buf._read(1) }.should.raise Buffer::Overflow
- end
-
- should 'raise on invalid types' do
- lambda{ @buf.read(:junk) }.should.raise Buffer::InvalidType
- lambda{ @buf.write(:junk, 1) }.should.raise Buffer::InvalidType
- end
-
- { :octet => 0b10101010,
- :short => 100,
- :long => 100_000_000,
- :longlong => 666_555_444_333_222_111,
- :shortstr => 'hello',
- :longstr => 'bye'*500,
- :timestamp => time = Time.at(Time.now.to_i),
- :table => { :this => 'is', :a => 'hash', :with => {:nested => 123, :and => time, :also => 123.456} },
- :bit => true
- }.each do |type, value|
-
- should "read and write a #{type}" do
- @buf.write(type, value)
- @buf.rewind
- @buf.read(type).should == value
- @buf.should.be.empty
- end
-
- end
-
- should 'read and write multiple bits' do
- bits = [true, false, false, true, true, false, false, true, true, false]
- @buf.write(:bit, bits)
- @buf.write(:octet, 100)
-
- @buf.rewind
-
- bits.map do
- @buf.read(:bit)
- end.should == bits
- @buf.read(:octet).should == 100
- end
-
- should 'read and write properties' do
- properties = ([
- [:octet, 1],
- [:shortstr, 'abc'],
- [:bit, true],
- [:bit, false],
- [:shortstr, nil],
- [:timestamp, nil],
- [:table, { :a => 'hash' }],
- ]*5).sort_by{rand}
-
- @buf.write(:properties, properties)
- @buf.rewind
- @buf.read(:properties, *properties.map{|type,_| type }).should == properties.map{|_,value| value }
- @buf.should.be.empty
- end
-
- should 'do transactional reads with #extract' do
- @buf.write :octet, 8
- orig = @buf.to_s
-
- @buf.rewind
- @buf.extract do |b|
- b.read :octet
- b.read :short
- end
-
- @buf.pos.should == 0
- @buf.data.should == orig
- end
- end
-end
View
59 lib/amqp/frame.rb
@@ -60,62 +60,3 @@ def self.parse(buf)
end
end
end
-
-if $0 =~ /bacon/ or $0 == __FILE__
- require 'rubygems'
- require 'bacon'
- include AMQP
-
- describe Frame do
- should 'handle basic frame types' do
- Frame::Method.new.id.should == 1
- Frame::Header.new.id.should == 2
- Frame::Body.new.id.should == 3
- end
-
- should 'convert method frames to binary' do
- meth = Protocol::Connection::Secure.new :challenge => 'secret'
-
- frame = Frame::Method.new(meth)
- frame.to_binary.should.be.kind_of? Buffer
- frame.to_s.should == [ 1, 0, meth.to_s.length, meth.to_s, 206 ].pack('CnNa*C')
- end
-
- should 'convert binary to method frames' do
- orig = Frame::Method.new Protocol::Connection::Secure.new(:challenge => 'secret')
-
- copy = Frame.parse(orig.to_binary)
- copy.should == orig
- end
-
- should 'ignore partial frames until ready' do
- frame = Frame::Method.new Protocol::Connection::Secure.new(:challenge => 'secret')
- data = frame.to_s
-
- buf = Buffer.new
- Frame.parse(buf).should == nil
-
- buf << data[0..5]
- Frame.parse(buf).should == nil
-
- buf << data[6..-1]
- Frame.parse(buf).should == frame
-
- Frame.parse(buf).should == nil
- end
-
- should 'convert header frames to binary' do
- head = Protocol::Header.new(Protocol::Basic, :priority => 1)
-
- frame = Frame::Header.new(head)
- frame.to_s.should == [ 2, 0, head.to_s.length, head.to_s, 206 ].pack('CnNa*C')
- end
-
- should 'convert binary to header frame' do
- orig = Frame::Header.new Protocol::Header.new(Protocol::Basic, :priority => 1)
-
- copy = Frame.parse(orig.to_binary)
- copy.should == orig
- end
- end
-end
View
51 lib/amqp/protocol.rb
@@ -156,54 +156,3 @@ def self.parse buf
#:stopdoc:
end
end
-
-if $0 =~ /bacon/ or $0 == __FILE__
- require 'bacon'
- include AMQP
-
- describe Protocol do
- should 'instantiate methods with arguments' do
- meth = Protocol::Connection::StartOk.new nil, 'PLAIN', nil, 'en_US'
- meth.locale.should == 'en_US'
- end
-
- should 'instantiate methods with named parameters' do
- meth = Protocol::Connection::StartOk.new :locale => 'en_US',
- :mechanism => 'PLAIN'
- meth.locale.should == 'en_US'
- end
-
- should 'convert methods to binary' do
- meth = Protocol::Connection::Secure.new :challenge => 'secret'
- meth.to_binary.should.be.kind_of? Buffer
-
- meth.to_s.should == [ 10, 20, 6, 'secret' ].pack('nnNa*')
- end
-
- should 'convert binary to method' do
- orig = Protocol::Connection::Secure.new :challenge => 'secret'
- copy = Protocol.parse orig.to_binary
- orig.should == copy
- end
-
- should 'convert headers to binary' do
- head = Protocol::Header.new Protocol::Basic,
- size = 5,
- weight = 0,
- :content_type => 'text/json',
- :delivery_mode => 1,
- :priority => 1
- head.to_s.should == [ 60, weight, 0, size, 0b1001_1000_0000_0000, 9, 'text/json', 1, 1 ].pack('nnNNnCa*CC')
- end
-
- should 'convert binary to header' do
- orig = Protocol::Header.new Protocol::Basic,
- size = 5,
- weight = 0,
- :content_type => 'text/json',
- :delivery_mode => 1,
- :priority => 1
- Protocol::Header.new(orig.to_binary).should == orig
- end
- end
-end
View
122 test/buffer_test.rb
@@ -0,0 +1,122 @@
+require 'test_helper'
+require 'amqp/buffer'
+
+module Carrot::AMQP
+ context 'Buffer' do
+ setup do
+ @buf = Buffer.new
+ end
+
+ test 'have contents' do
+ assert_equal '', @buf.contents
+ end
+
+ test 'initialize with data' do
+ @buf = Buffer.new('abc')
+ assert_equal 'abc', @buf.contents
+ end
+
+ test 'append raw data' do
+ @buf << 'abc'
+ @buf << 'def'
+ assert_equal 'abcdef', @buf.contents
+ end
+
+ test 'append other buffers' do
+ @buf << Buffer.new('abc')
+ assert_equal 'abc', @buf.contents
+ end
+
+ test 'have a position' do
+ assert_equal 0, @buf.pos
+ end
+
+ test 'have a length' do
+ assert_equal 0, @buf.length
+ @buf << 'abc'
+ assert_equal 3, @buf.length
+ end
+
+ test 'know the end' do
+ assert_equal true, @buf.empty?
+ end
+
+ test 'read and write data' do
+ @buf._write('abc')
+ @buf.rewind
+ assert_equal 'ab', @buf._read(2)
+ assert_equal 'c', @buf._read(1)
+ end
+
+ test 'raise on overflow' do
+ assert_raise(Buffer::Overflow) { @buf._read(1) }
+ end
+
+ test 'raise on invalid types' do
+ assert_raise(Buffer::InvalidType) { @buf.read(:junk) }
+ assert_raise(Buffer::InvalidType) { @buf.write(:junk, 1) }
+ end
+
+ { :octet => 0b10101010,
+ :short => 100,
+ :long => 100_000_000,
+ :longlong => 666_555_444_333_222_111,
+ :shortstr => 'hello',
+ :longstr => 'bye'*500,
+ :timestamp => time = Time.at(Time.now.to_i),
+ :table => { :this => 'is', :a => 'hash', :with => {:nested => 123, :and => time, :also => 123.456} },
+ :bit => true
+ }.each do |type, value|
+
+ test "read and write a #{type}" do
+ @buf.write(type, value)
+ @buf.rewind
+ assert_equal value, @buf.read(type)
+ assert_equal true, @buf.empty?
+ end
+
+ end
+
+ test 'read and write multiple bits' do
+ bits = [true, false, false, true, true, false, false, true, true, false]
+ @buf.write(:bit, bits)
+ @buf.write(:octet, 100)
+
+ @buf.rewind
+
+ assert_equal bits, bits.collect{ @buf.read(:bit) }
+ assert_equal 100, @buf.read(:octet)
+ end
+
+ test 'read and write properties' do
+ properties = ([
+ [:octet, 1],
+ [:shortstr, 'abc'],
+ [:bit, true],
+ [:bit, false],
+ [:shortstr, nil],
+ [:timestamp, nil],
+ [:table, { :a => 'hash' }],
+ ]*5).sort_by{rand}
+
+ @buf.write(:properties, properties)
+ @buf.rewind
+ assert_equal properties.map{|_,value| value }, @buf.read(:properties, *properties.map{|type,_| type })
+ assert_equal true, @buf.empty?
+ end
+
+ test 'do transactional reads with #extract' do
+ @buf.write :octet, 8
+ orig = @buf.to_s
+
+ @buf.rewind
+ @buf.extract do |b|
+ b.read :octet
+ b.read :short
+ end
+
+ assert_equal 0, @buf.pos
+ assert_equal orig, @buf.data
+ end
+ end
+end
View
24 test/carrot_test.rb
@@ -1,25 +1,23 @@
require 'test_helper'
-class CarrotTest < Test::Unit::TestCase
+context 'test queue' do
TEST_QUEUE = '_carrot_test'
-
- test "simple server connection" do
- c = Carrot.new
+ setup do
+ @carrot = Carrot.new
+ @q = @carrot.queue(TEST_QUEUE)
+ @q.purge
end
test "large messages" do
msg = 'a' * 1024 * 1024
- q = Carrot.queue(TEST_QUEUE)
- q.publish(msg)
- assert_equal msg, q.pop
+ @q.publish(msg)
+ assert_equal msg, @q.pop
end
test "reset" do
- c = Carrot.new
- q = c.queue(TEST_QUEUE)
- count = q.message_count
- q.publish('test')
- c.reset
- assert_equal count + 1, q.message_count
+ count = @q.message_count
+ @q.publish('test')
+ @carrot.reset
+ assert_equal count + 1, @q.message_count
end
end
View
56 test/frame_test.rb
@@ -0,0 +1,56 @@
+require 'test_helper'
+
+module Carrot::AMQP
+ context 'Frame' do
+ test 'handle basic frame types' do
+ assert_equal 1, Frame::Method.new.id
+ assert_equal 2, Frame::Header.new.id
+ assert_equal 3, Frame::Body.new.id
+ end
+
+ test 'convert method frames to binary' do
+ meth = Protocol::Connection::Secure.new :challenge => 'secret'
+
+ frame = Frame::Method.new(meth)
+ assert frame.to_binary.kind_of?(Buffer)
+ assert_equal [ 1, 0, meth.to_s.length, meth.to_s, 206 ].pack('CnNa*C'), frame.to_s
+ end
+
+ test 'convert binary to method frames' do
+ orig = Frame::Method.new Protocol::Connection::Secure.new(:challenge => 'secret')
+
+ copy = Frame.parse(orig.to_binary)
+ assert_equal orig, copy
+ end
+
+ test 'ignore partial frames until ready' do
+ frame = Frame::Method.new Protocol::Connection::Secure.new(:challenge => 'secret')
+ data = frame.to_s
+
+ buf = Buffer.new
+ assert_equal nil, Frame.parse(buf)
+
+ buf << data[0..5]
+ assert_equal nil, Frame.parse(buf)
+
+ buf << data[6..-1]
+ assert_equal frame, Frame.parse(buf)
+
+ assert_equal nil, Frame.parse(buf)
+ end
+
+ test 'convert header frames to binary' do
+ head = Protocol::Header.new(Protocol::Basic, :priority => 1)
+
+ frame = Frame::Header.new(head)
+ assert_equal [ 2, 0, head.to_s.length, head.to_s, 206 ].pack('CnNa*C'), frame.to_s
+ end
+
+ test 'convert binary to header frame' do
+ orig = Frame::Header.new Protocol::Header.new(Protocol::Basic, :priority => 1)
+
+ copy = Frame.parse(orig.to_binary)
+ assert_equal orig, copy
+ end
+ end
+end
View
52 test/protocol_test.rb
@@ -0,0 +1,52 @@
+require 'test_helper'
+
+module Carrot::AMQP
+ context 'Protocol' do
+ test 'instantiate methods with arguments' do
+ meth = Protocol::Connection::StartOk.new(nil, 'PLAIN', nil, 'en_US')
+ assert_equal 'en_US', meth.locale
+ end
+
+ test 'instantiate methods with named parameters' do
+ meth = Protocol::Connection::StartOk.new(:locale => 'en_US', :mechanism => 'PLAIN')
+ assert_equal 'en_US', meth.locale
+ end
+
+ test 'convert methods to binary' do
+ meth = Protocol::Connection::Secure.new(:challenge => 'secret')
+ assert meth.to_binary.kind_of?(Buffer)
+
+ assert_equal [ 10, 20, 6, 'secret' ].pack('nnNa*'), meth.to_s
+ end
+
+ test 'convert binary to method' do
+ orig = Protocol::Connection::Secure.new(:challenge => 'secret')
+ copy = Protocol.parse orig.to_binary
+ assert_equal copy, orig
+ end
+
+ test 'convert headers to binary' do
+ head = Protocol::Header.new(
+ Protocol::Basic,
+ size = 5,
+ weight = 0,
+ :content_type => 'text/json',
+ :delivery_mode => 1,
+ :priority => 1
+ )
+ assert_equal [ 60, weight, 0, size, 0b1001_1000_0000_0000, 9, 'text/json', 1, 1 ].pack('nnNNnCa*CC'), head.to_s
+ end
+
+ test 'convert binary to header' do
+ orig = Protocol::Header.new(
+ Protocol::Basic,
+ size = 5,
+ weight = 0,
+ :content_type => 'text/json',
+ :delivery_mode => 1,
+ :priority => 1
+ )
+ assert_equal orig, Protocol::Header.new(orig.to_binary)
+ end
+ end
+end
View
37 test/test_helper.rb
@@ -1,18 +1,27 @@
-require 'rubygems'
+dir = File.dirname(File.expand_path(__FILE__))
+$LOAD_PATH.unshift dir + '/../lib'
+require 'carrot'
require 'test/unit'
-#require 'shoulda'
-require 'mocha'
-require File.dirname(__FILE__) + '/../lib/carrot'
-
-class << Test::Unit::TestCase
- def test(name, &block)
- test_name = "test_#{name.gsub(/[\s\W]/,'_')}"
- raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name
- define_method test_name, &block
- end
+require 'rubygems'
+require 'pp'
- def xtest(name, &block)
- # no-op, an empty test method is defined to prevent "no tests in testcase" errors when all tests are disabled
- define_method(:test_disabled) { assert true }
+##
+# test/spec/mini 3
+# http://gist.github.com/25455
+# chris@ozmm.org
+# file:lib/test/spec/mini.rb
+#
+def context(*args, &block)
+ return super unless (name = args.first) && block
+ require 'test/unit'
+ klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
+ def self.test(name, &block)
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
+ end
+ def self.xtest(*args) end
+ def self.setup(&block) define_method(:setup, &block) end
+ def self.teardown(&block) define_method(:teardown, &block) end
end
+ (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
+ klass.class_eval &block
end
Please sign in to comment.
Something went wrong with that request. Please try again.