spec/signore/executable_spec.rb in signore-0.0.0 vs spec/signore/executable_spec.rb in signore-0.1.0
- old
+ new
@@ -4,56 +4,43 @@
module Signore describe Executable do
describe '#initialize' do
- before do
- $stderr = StringIO.new
- end
-
- after do
- $stderr = STDERR
- end
-
- def stderr
- $stderr.rewind
- $stderr.read
- end
-
it 'prints usage if no command is given' do
- lambda { Executable.new([]) }.must_raise SystemExit
- stderr.must_match /usage: signore prego\|pronto \[label, …\]/
+ stderr = capture_io { -> { Executable.new [] }.must_raise SystemExit }.last
+ stderr.must_include 'usage: signore prego|pronto [label, …]'
end
it 'prints usage if a bogus command is given' do
- lambda { Executable.new(['bogus']) }.must_raise SystemExit
- stderr.must_match /usage: signore prego\|pronto \[label, …\]/
+ stderr = capture_io { -> { Executable.new ['bogus'] }.must_raise SystemExit }.last
+ stderr.must_include 'usage: signore prego|pronto [label, …]'
end
it 'loads the signature database from the specified location' do
- db = MiniTest::Mock.new
- db.expect :load, nil, ['signatures.yml']
- Executable.new ['-d', 'signatures.yml', 'prego'], db
- db.verify
+ db_class = MiniTest::Mock.new
+ db_class.expect :new, nil, ['signatures.yml']
+ Executable.new ['-d', 'signatures.yml', 'prego'], db_class
+ db_class.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 = MiniTest::Mock.new
- db.expect :load, nil, [File.expand_path('~/.local/share/signore/signatures.yml')]
- Executable.new ['prego'], db
- db.verify
+ 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
end
it 'loads the signature database from $XDG_DATA_HOME/signore/signatures.yml if $XDG_DATA_HOME is set' do
begin
orig_data_home = ENV.delete 'XDG_DATA_HOME'
ENV['XDG_DATA_HOME'] = Dir.tmpdir
- db = MiniTest::Mock.new
- db.expect :load, nil, ["#{ENV['XDG_DATA_HOME']}/signore/signatures.yml"]
- Executable.new ['prego'], db
- db.verify
+ 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
ensure
orig_data_home ? ENV['XDG_DATA_HOME'] = orig_data_home : ENV.delete('XDG_DATA_HOME')
end
end
@@ -62,47 +49,71 @@
describe '#run' do
describe 'prego' do
it 'prints a signature tagged with the provided tags' do
- Executable.new(['-d', 'spec/fixtures/signatures.yml', 'prego', 'tech', 'programming']).run output = StringIO.new
- output.rewind
- output.read.must_equal "// sometimes I believe compiler ignores all my comments\n"
+ 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
end
it 'prints a signature based on allowed and forbidden tags' do
- Executable.new(['-d', 'spec/fixtures/signatures.yml', 'prego', '~programming', 'tech', '~security']).run output = StringIO.new
- output.rewind
- output.read.must_equal "You do have to be mad to work here, but it doesn’t help.\n [Gary Barnes, asr]\n"
+ stdout = capture_io { Executable.new(['-d', 'spec/fixtures/signatures.yml', 'prego', '~programming', 'tech', '~security']).run }.first
+ stdout.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
- @path = "#{Dir.tmpdir}/#{rand}/signatures.yml"
+ @file = Tempfile.new ''
end
- after do
- FileUtils.rmtree File.dirname @path
- end
-
it 'asks about signature parts and saves given signature with provided labels' do
- input = StringIO.new "The Wikipedia page on ADHD is like 20 pages long. That’s just cruel.\n\nMark Pilgrim\n\n\n\n"
- Executable.new(['-d', @path, 'pronto', 'Wikipedia', 'ADHD']).run output = StringIO.new, input
- output.rewind
- output.read.must_equal "text?\nauthor?\nsubject?\nsource?\nThe Wikipedia page on ADHD is like 20 pages long. That’s just cruel.\n [Mark Pilgrim]\n"
- Executable.new(['-d', @path, 'prego', 'Wikipedia', 'ADHD']).run output = StringIO.new
- output.rewind
- output.read.must_equal "The Wikipedia page on ADHD is like 20 pages long. That’s just cruel.\n [Mark Pilgrim]\n"
+ input = StringIO.new <<-END.dedent
+ The Wikipedia page on ADHD is like 20 pages long. That’s just cruel.\n
+ 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
+ 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
+ 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 "‘I’ve gone through over-stressed to physical exhaustion – what’s next?’\n‘Tuesday.’\n\nSimon Burr, Kyle Hearn\n\n\n\n"
- Executable.new(['-d', @path, 'pronto']).run output = StringIO.new, input
- output.rewind
- output.read.must_equal "text?\nauthor?\nsubject?\nsource?\n‘I’ve gone through over-stressed to physical exhaustion – what’s next?’\n‘Tuesday.’\n [Simon Burr, Kyle Hearn]\n"
+ 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
+ END
+
+ stdout = capture_io { Executable.new(['-d', @file.path, 'pronto']).run input }.first
+ stdout.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]
+ END
end
end
end