spec/wikilink/converter/site_spec.rb in wikilink-converter-0.1.0 vs spec/wikilink/converter/site_spec.rb in wikilink-converter-0.2.1

- old
+ new

@@ -2,55 +2,19 @@ describe Wikilink::Converter::Site do DEFAULT_NAMESPACE = Wikilink::Converter::Site::DEFAULT_NAMESPACE CURRENT_SITE_NAME = Wikilink::Converter::Site::CURRENT_SITE_NAME - # { subject.foo }.should forward(:bar).to(handler).with(any_args) - RSpec::Matchers.define :forward do |message| - match do |block| - block.call - @handler.received_message?(message, *@arguments) - end - - description do - "forward to message #{message} to #{@handler.inspect}" - end - - failure_message_for_should do |block| - <<MESSAGE -expected forward #{message} - to #{block} - with #{@arguments.inspect} -MESSAGE - end - - failure_message_for_should_not do |block| - <<MESSAGE -expected not forward #{message} - to #{block} - with #{@arguments.inspect} -MESSAGE - end - - chain :with do |*arguments| - @arguments = arguments - end - - chain :to do |handler| - @handler = handler - end - end - shared_examples 'configuring a new instance of class as namespace handler' do |namespace| # require setting converter to an instance of Site context 'with converter class' do class SpecNamespace; end define_method :call do |*args| if namespace.to_s.empty? - converter.on_namespace SpecNamespace, *args + converter.namespace SpecNamespace, *args else - converter.on_namespace namespace, SpecNamespace, *args + converter.namespace namespace, SpecNamespace, *args end end let(:namespace_converter) { double(:namespace_converter) } it 'creates a new instance of the specified class' do @@ -60,11 +24,11 @@ it 'uses the instance of the class as new namespace converter' do SpecNamespace.should_receive(:new).once.and_return(namespace_converter) call namespace_converter.should_receive(:run).and_return('it works') - converter.run(':', namespace, 'Home', 'Name', '/').should eq('it works') + converter.run(namespace, path: 'Home', name: 'Name').should eq('it works') end context 'and options' do let(:options) { { foo: :bar} } it 'creates a new instance of the specified class and options' do @@ -80,10 +44,11 @@ end end let(:default_namespace) { double(:default_namespace).as_null_object } let(:namespace) { double(:namespace).as_null_object } + let(:run_options) { { path: 'Home', name: 'Name' } } before { Wikilink::Converter::Namespace::Default.stub(:new) { default_namespace } } shared_examples '' context 'current site' do @@ -131,39 +96,39 @@ end end describe '#run' do it 'delegates default namespace to Namespace::Default instance' do - ->{ converter.run(':', DEFAULT_NAMESPACE, 'Home', 'Name', '/') }. - should forward(:run).to(default_namespace).with(':', 'Home', 'Name', '/') + default_namespace.should_receive(:run).with(hash_including(run_options)) + converter.run(DEFAULT_NAMESPACE, run_options) end end - describe '#on_default_namespace' do - it 'is a shortcut of #on_namespace' do - converter.should_receive(:on_namespace).with(DEFAULT_NAMESPACE, 'arg') - converter.on_default_namespace 'arg' + describe '#default_namespace' do + it 'is a shortcut of #namespace' do + converter.should_receive(:namespace).with(DEFAULT_NAMESPACE, 'arg') + converter.default_namespace 'arg' end end - describe '#on_namespace' do + describe '#namespace' do context 'without any arguments nor block' do let(:default_namespace) { double(:default_namespace) } it 'does not change the default namespace handler' do Wikilink::Converter::Namespace::Default.should_receive(:new). once.and_return(default_namespace) - converter.on_namespace + converter.namespace end end context 'with block' do it 'configures default namespace with the block' do yielded = double('yielded') yielded.should_receive(:poke) default_namespace.should_receive(:config).and_yield - converter.on_namespace do + converter.namespace do yielded.poke end end end @@ -171,64 +136,64 @@ let(:default_namespace) { double(:default_namespace) } it 'does not change the default namespace handler' do Wikilink::Converter::Namespace::Default.should_receive(:new). once.and_return(default_namespace) - converter.on_namespace foo: :bar + converter.namespace foo: :bar end end context 'with default namespace name' do let(:default_namespace) { double(:default_namespace) } it 'does not change the default namespace handler' do Wikilink::Converter::Namespace::Default.should_receive(:new). once.and_return(default_namespace) - converter.on_namespace(DEFAULT_NAMESPACE) + converter.namespace(DEFAULT_NAMESPACE) end end context 'with other namespace name' do - let(:name) { 'topics' } + let(:namespace_name) { 'topics' } let(:klass) { Wikilink::Converter::Namespace } it 'creates a new Wikilink::Converter::Namespace instance' do klass.should_receive(:new) - converter.on_namespace name + converter.namespace namespace_name end it 'initializes Wikilink::Converter::Namespace instance with option :site_name' do klass.should_receive(:new).with(hash_including(site_name: site_name)) - converter.on_namespace name + converter.namespace namespace_name end it 'initializes Wikilink::Converter::Namespace instance with option :name' do - klass.should_receive(:new).with(hash_including(name: name)) - converter.on_namespace name + klass.should_receive(:new).with(hash_including(name: namespace_name)) + converter.namespace namespace_name end it 'uses the instance of Wikilink::Converter::Namespace as new namespace converter' do klass.should_receive(:new).and_return(namespace) - converter.on_namespace name + converter.namespace namespace_name namespace.should_receive(:run).and_return('it works') - converter.run(':', name, 'Home', 'Name', '/').should eq('it works') + converter.run(namespace_name, run_options).should eq('it works') end context 'with object' do it 'uses that object as new namespace converter' do - converter.on_namespace name, namespace + converter.namespace namespace_name, namespace namespace.should_receive(:run).and_return('it works') - converter.run(':', name, 'Home', 'Name', '/').should eq('it works') + converter.run(namespace_name, run_options).should eq('it works') end end it_behaves_like 'configuring a new instance of class as namespace handler', 'topics' end it_behaves_like 'configuring a new instance of class as namespace handler' context 'with object' do it 'uses that object as new namespace converter' do - converter.on_namespace namespace + converter.namespace namespace namespace.should_receive(:run).and_return('it works') - converter.run(':', DEFAULT_NAMESPACE, 'Home', 'Name', '/').should eq('it works') + converter.run(DEFAULT_NAMESPACE, run_options).should eq('it works') end end context 'with namespace, classname and options' do end @@ -274,59 +239,59 @@ end end describe '#run' do it 'delegates default namespace to Namespace::Default instance' do - ->{ converter.run(':', DEFAULT_NAMESPACE, 'Home', 'Name', '/') }. - should forward(:run).to(default_namespace).with(':', 'Home', 'Name', '/') + default_namespace.should_receive(:run).with(hash_including(run_options)) + converter.run(DEFAULT_NAMESPACE, run_options) end - context 'with method #on_namespace_topics defined' do + context 'with method #run_namespace_topics defined' do before { Wikilink::Converter::Namespace::Default.unstub(:new) } class SpecSite < Wikilink::Converter::Site - def on_namespace_topics(colon, path, name, current_page) + def run_namespace_topics(run_options) 'it works' end end let(:converter) { SpecSite.new } - it 'invokes #on_namespace_topics to handle namespace topics' do - converter.run(':', 'topics', 'Home', 'Name', '/').should eq('it works') + it 'invokes #run_namespace_topics to handle namespace topics' do + converter.run('topics', run_options).should eq('it works') end context 'but user has override it' do before { converter.namespace('topics') do 'use this version' end } it 'uses user version' do - converter.run(':', 'topics', 'Home', 'Name', '/').should eq('use this version') + converter.run('topics', run_options).should eq('use this version') end end end - context 'with method #on_namespace_ defined' do + context 'with method #run_default_namespace defined' do before { Wikilink::Converter::Namespace::Default.unstub(:new) } class SpecSite < Wikilink::Converter::Site - def on_namespace_(colon, path, name, current_page) + def run_default_namespace(run_options) 'it works' end end let(:converter) { SpecSite.new } - it 'invokes #on_namespace_ to handle default namespace' do - converter.run(':', DEFAULT_NAMESPACE, 'Home', 'Name', '/').should eq('it works') + it 'invokes #run_default_namespace to handle default namespace' do + converter.run(DEFAULT_NAMESPACE, run_options).should eq('it works') end context 'but user has override it' do before { converter.namespace do 'use this version' end } it 'uses user version' do - converter.run(':', DEFAULT_NAMESPACE, 'Home', 'Name', '/').should eq('use this version') + converter.run(DEFAULT_NAMESPACE, run_options).should eq('use this version') end end end end end