spec/spec_helper.rb in amqp-0.7.0.pre vs spec/spec_helper.rb in amqp-0.7.0

- old
+ new

@@ -1,41 +1,96 @@ # encoding: utf-8 $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +require 'bundler' +Bundler.setup +Bundler.require :default, :test + require "mq" -require "bacon" -require "em-spec/bacon" -# Notes about Bacon & EM-spec -# - Subsequent describe blocks don't work. -# - Bacon doesn't support any kind of pending specs. -# - Bacon doesn't support before(:all) hook (see bellow). -EM.spec_backend = EventMachine::Spec::Bacon +amqp_config = File.dirname(__FILE__) + '/amqp.yml' -# Usage with tracer: -# 1) Start tracer on a PORT_NUMBER -# 2) ruby spec/sync_async_spec.rb amqp://localhost:PORT_NUMBER -if ARGV.first && ARGV.first.match(/^amqps?:/) - amqp_url = ARGV.first - puts "Using AMQP URL #{amqp_url}" +if File.exists? amqp_config + class Hash + def symbolize_keys + self.inject({}) do |result, (key, value)| + new_key = key.is_a?(String) ? key.to_sym : key + new_value = value.is_a?(Hash) ? value.symbolize_keys : value + result[new_key] = new_value + result + end + end + end + AMQP_OPTS = YAML::load_file(amqp_config).symbolize_keys[:test] else - amqp_url = "amqp://localhost" + AMQP_OPTS = {:host => 'localhost', :port => 5672} end -# Bacon doesn't seem to have some global before hook -Bacon::Context.class_eval do - alias_method :__initialize__, :initialize - # Let's use Module#define_method, so we can - # access local variables defined in parent scopes. - define_method(:initialize) do |*args, &block| - __initialize__(*args, &block) +if RUBY_VERSION == "1.8.7" + module ArrayExtensions + def sample + self.choice + end # sample + end - # There's no support for before(:all), so we just - # save whatever AMQP.connect returns (we suppose - # it isn't nil) and use value = value || connect(). - self.before do - @connected ||= AMQP.connect(amqp_url) - end + class Array + include ArrayExtensions end +end + + + +# Shorthand for mocking subject's instance variable +def subject_mock(name, as_null = false) + mock = mock(name) + mock.as_null_object if as_null + subject.instance_variable_set(name.to_sym, mock) + mock +end + +# Returns Header that should be correctly parsed +def basic_header(opts = {}) + AMQP::Frame::Header.new( + AMQP::Protocol::Header.new( + AMQP::Protocol::Basic, :priority => 1), opts[:channel] || 0) +end + +# Returns AMQP Frame::Header frame that contains Protocol::Header header +# with (pre-)defined accessors set. +def test_header opts = {} + AMQP::Frame::Header.new( + AMQP::Protocol::Header.new( + opts[:klass] || AMQP::Protocol::Test, + opts[:size] || 4, + opts[:weight] || 2, + opts[:properties] || {:delivery_mode => 1})) +end + +# Returns AMQP Frame::Method frame that contains Protocol::Basic::Deliver +# with (pre-)defined accessors set. +def test_method_deliver opts = {} + AMQP::Frame::Method.new( + AMQP::Protocol::Basic::Deliver.new( + :consumer_tag => opts[:consumer_tag] || 'test_consumer')) +end + +require "stringio" + +def capture_stdout(&block) + $stdout = StringIO.new + block.call + $stdout.rewind + result = $stdout.read + $stdout = STDOUT + return result +end + +def capture_stderr(&block) + $stderr = StringIO.new + block.call + $stderr.rewind + result = $stderr.read + $stderr = STDOUT + return result end