spec/wikilink/converter_spec.rb in wikilink-converter-0.1.0 vs spec/wikilink/converter_spec.rb in wikilink-converter-0.2.1
- old
+ new
@@ -1,10 +1,38 @@
require File.expand_path('../../spec_helper', __FILE__)
+shared_examples 'singleton' do |klass|
+ describe '.instance' do
+ subject { klass }
+ its(:instance) { should be_a_kind_of(klass) }
+ it 'returns the same instances for all calls to #instance' do
+ klass.instance.should equal(klass.instance)
+ end
+ end
+end
+
describe Wikilink::Converter do
CURRENT_SITE = Wikilink::Converter::CURRENT_SITE
+ it_behaves_like 'singleton', Wikilink::Converter
+ describe '.config' do
+ it 'yields the singleton instance' do
+ Wikilink::Converter.config do |arg|
+ arg.should equal(Wikilink::Converter.instance)
+ end
+ end
+ end
+
+ %w{run execute}.each do |method|
+ describe ".#{method}" do
+ it "forwards method #{method} to #instance" do
+ Wikilink::Converter.instance.should_receive(method.to_sym).with(:arg)
+ Wikilink::Converter.send method.to_sym, :arg
+ end
+ end
+ end
+
let(:converter) { Wikilink::Converter.new }
describe '#run' do
subject { converter.method(:run) }
it 'does nothing when text does not contain wikilink' do
@@ -37,11 +65,11 @@
end
context 'in namespace topics' do
context 'with default handler' do
before do
- converter.on('topics', Wikilink::Converter::Namespace::Default)
+ converter.namespace('topics', Wikilink::Converter::Namespace::Default)
end
it { should convert('[[topics:World]]').to('<a href="/World">topics:World</a>') }
end
end
@@ -61,81 +89,88 @@
it { should convert('[[Hello/World|]]').to('<a href="/Hello/World">World</a>') }
context 'in namespace topics' do
context 'with default handler' do
before do
- converter.on('topics', Wikilink::Converter::Namespace::Default)
+ converter.namespace('topics', Wikilink::Converter::Namespace::Default)
end
it { should convert('[[topics:World|]]').to('<a href="/World">World</a>') }
it { should convert('[[:topics:World|]]').to('<a href="/World">World</a>') }
end
end
end
+ it 'passes options to namespace converter' do
+ Wikilink::Converter::Site.any_instance.should_receive(:run) { |*args|
+ args.last[:hello].should eq('world')
+ 'result'
+ }
+ converter.run('[[Hello]]', hello: 'world')
+ end
end
- describe '#on_default_site' do
- it 'is a shortcut of #on_site' do
- converter.should_receive(:on_site).with(CURRENT_SITE, 'arg')
- converter.on_current_site 'arg'
+ describe '#default_site' do
+ it 'is a shortcut of #site' do
+ converter.should_receive(:site).with(CURRENT_SITE, 'arg')
+ converter.current_site 'arg'
end
end
- describe '#on_site' do
+ describe '#site' do
let(:default_site) { double(:default_site).as_null_object }
let(:site) { double(:site).as_null_object }
before { Wikilink::Converter::Site.stub(:new) { default_site } }
let(:converter) { Wikilink::Converter.new }
context 'without any arguments' do
it 'does not create new instance' do
converter # call it to initialize first
Wikilink::Converter::Site.should_not_receive :new
- converter.on_site
+ converter.site
end
it 'yields the default site converter' do
- converter.on_site do |site_converter|
+ converter.site do |site_converter|
site_converter.should eq(default_site)
end
end
end
context 'with site name' do
it 'creates a new instance of Wikilink::Converter::Site as the site converter' do
converter # call it to initialize first
Wikilink::Converter::Site.should_receive(:new).once
- converter.on_site 'wikipedia'
+ converter.site 'wikipedia'
end
it 'initializes the instance with option :name' do
converter # call it to initialize first
Wikilink::Converter::Site.should_receive(:new).with(hash_including(name: 'wikipedia')).once
- converter.on_site 'wikipedia'
+ converter.site 'wikipedia'
end
it 'yields the new converter instance' do
converter # call it to initialize first
Wikilink::Converter::Site.should_receive(:new).once.and_return(site)
- converter.on_site('wikipedia') do |site_converter|
+ converter.site('wikipedia') do |site_converter|
site_converter.should eq(site)
end
end
context 'and options' do
let(:options) { { foo: :bar } }
it 'initializes the instance with given options' do
converter # call it to initialize first
Wikilink::Converter::Site.should_receive(:new).with(hash_including(foo: :bar)).once
- converter.on_site 'wikipedia', options
+ converter.site 'wikipedia', options
end
end
end
context 'with object' do
it 'does not create new instance' do
converter # call it to initialize first
Wikilink::Converter::Site.should_not_receive :new
- converter.on_site site
+ converter.site site
end
it 'yields the object' do
- converter.on_site(site) do |site_converter|
+ converter.site(site) do |site_converter|
site_converter.should eq(site)
end
end
end
end
@@ -150,10 +185,10 @@
subject.should convert('\\{{toc}}').to('{{toc}}')
end
context 'with handler registered on action toc' do
before {
- converter.on_action('toc') do |arg|
+ converter.action('toc') do |arg|
"Table of Contents#{arg}"
end
}
it 'uses the handler to process {{toc}}' do