spec/signore/executable_spec.rb in signore-0.1.2 vs spec/signore/executable_spec.rb in signore-0.2.0

- old
+ new

@@ -1,122 +1,125 @@ -# encoding: UTF-8 - require_relative '../spec_helper' module Signore describe Executable do - describe '#initialize' do - it 'prints usage if no command is given' do - stderr = capture_io { -> { Executable.new [] }.must_raise SystemExit }.last - stderr.must_include 'usage: signore prego|pronto [label, …]' + capture_io do + -> { Executable.new [] }.must_raise SystemExit + end.last.must_include 'usage: signore prego|pronto [tag, …]' end it 'prints usage if a bogus command is given' do - stderr = capture_io { -> { Executable.new ['bogus'] }.must_raise SystemExit }.last - stderr.must_include 'usage: signore prego|pronto [label, …]' + capture_io do + -> { Executable.new ['bogus'] }.must_raise SystemExit + end.last.must_include 'usage: signore prego|pronto [tag, …]' end it 'loads the signature database from the specified location' do - db_class = MiniTest::Mock.new - db_class.expect :new, nil, ['signatures.yml'] - Executable.new ['-d', 'signatures.yml', 'prego'], db_class - db_class.verify + db_factory = MiniTest::Mock.new.expect :new, nil, ['signatures.yml'] + Executable.new %w[-d signatures.yml prego], db_factory: db_factory + db_factory.verify end - it 'loads the signature database from ~/.local/share/signore/signatures.yml if no location specified' do - pending if ENV['XDG_DATA_HOME'] - db_class = MiniTest::Mock.new - db_class.expect :new, nil, [File.expand_path('~/.local/share/signore/signatures.yml')] - Executable.new ['prego'], db_class - db_class.verify + it 'defaults to ~/.local/share/signore/signatures.yml' do + begin + orig = ENV.delete 'XDG_DATA_HOME' + default_path = File.expand_path '~/.local/share/signore/signatures.yml' + db_factory = MiniTest::Mock.new + db_factory.expect :new, nil, [default_path] + Executable.new ['prego'], db_factory: db_factory + db_factory.verify + ensure + ENV['XDG_DATA_HOME'] = orig if orig + end end - it 'loads the signature database from $XDG_DATA_HOME/signore/signatures.yml if $XDG_DATA_HOME is set' do + it 'defaults to $XDG_DATA_HOME/signore/signatures.yml' do begin - orig_data_home = ENV.delete 'XDG_DATA_HOME' + orig = ENV.delete 'XDG_DATA_HOME' ENV['XDG_DATA_HOME'] = Dir.tmpdir - db_class = MiniTest::Mock.new - db_class.expect :new, nil, ["#{ENV['XDG_DATA_HOME']}/signore/signatures.yml"] - Executable.new ['prego'], db_class - db_class.verify + default_path = "#{ENV['XDG_DATA_HOME']}/signore/signatures.yml" + db_factory = MiniTest::Mock.new + db_factory.expect :new, nil, [default_path] + Executable.new ['prego'], db_factory: db_factory + db_factory.verify ensure - orig_data_home ? ENV['XDG_DATA_HOME'] = orig_data_home : ENV.delete('XDG_DATA_HOME') + orig ? ENV['XDG_DATA_HOME'] = orig : ENV.delete('XDG_DATA_HOME') end end - end describe '#run' do - describe 'prego' do - it 'prints a signature tagged with the provided tags' do - stdout = capture_io { Executable.new(['-d', 'spec/fixtures/signatures.yml', 'prego', 'tech', 'programming']).run }.first - stdout.must_equal <<-end.dedent - // sometimes I believe compiler ignores all my comments - end + args = %w[-d spec/fixtures/signatures.yml prego tech programming] + output = "// sometimes I believe compiler ignores all my comments\n" + stdout = capture_io { Executable.new(args).run }.first + stdout.must_equal output end it 'prints a signature based on allowed and forbidden tags' do - stdout = capture_io { Executable.new(['-d', 'spec/fixtures/signatures.yml', 'prego', '~programming', 'tech', '~security']).run }.first - stdout.must_equal <<-end.dedent + path = 'spec/fixtures/signatures.yml' + args = %W[-d #{path} prego ~programming tech ~security] + out = capture_io { Executable.new(args).run }.first + out.must_equal <<-end.dedent You do have to be mad to work here, but it doesn’t help. [Gary Barnes, asr] end end - end describe 'pronto' do - before do @file = Tempfile.new '' end - it 'asks about signature parts and saves given signature with provided labels' do + it 'asks about signature parts and saves resulting signature' do input = StringIO.new <<-end.dedent - The Wikipedia page on ADHD is like 20 pages long. That’s just cruel.\n + The Wikipedia page on ADHD is like 20 pages long. That’s just cruel. + Mark Pilgrim\n\n\n end - stdout = capture_io { Executable.new(['-d', @file.path, 'pronto', 'Wikipedia', 'ADHD']).run input }.first - stdout.must_equal <<-end.dedent + capture_io do + args = %W[-d #{@file.path} pronto Wikipedia ADHD] + Executable.new(args).run input: input + end.first.must_equal <<-end.dedent text? author? subject? source? The Wikipedia page on ADHD is like 20 pages long. That’s just cruel. [Mark Pilgrim] end - stdout = capture_io { Executable.new(['-d', @file.path, 'prego', 'Wikipedia', 'ADHD']).run }.first - stdout.must_equal <<-end.dedent + capture_io do + Executable.new(%W[-d #{@file.path} prego Wikipedia ADHD]).run + end.first.must_equal <<-end.dedent The Wikipedia page on ADHD is like 20 pages long. That’s just cruel. [Mark Pilgrim] end end it 'handles multi-line signatures' do input = StringIO.new <<-end.dedent - ‘I’ve gone through over-stressed to physical exhaustion – what’s next?’ - ‘Tuesday.’\n - Simon Burr, Kyle Hearn\n\n\n + ‘You’ve got an interesting accent. Subtle. I can’t place it.’ + ‘It’s text-to-speech… I was raised by smartphones.’ + + Patrick Ewing\n\n\n end - stdout = capture_io { Executable.new(['-d', @file.path, 'pronto']).run input }.first - stdout.must_equal <<-end.dedent + capture_io do + Executable.new(['-d', @file.path, 'pronto']).run input: input + end.first.must_equal <<-end.dedent text? author? subject? source? - ‘I’ve gone through over-stressed to physical exhaustion – what’s next?’ - ‘Tuesday.’ - [Simon Burr, Kyle Hearn] + ‘You’ve got an interesting accent. Subtle. I can’t place it.’ + ‘It’s text-to-speech… I was raised by smartphones.’ + [Patrick Ewing] end end - end - end - end end