spec/klipp_spec.rb in klipp-0.0.1 vs spec/klipp_spec.rb in klipp-0.2.0

- old
+ new

@@ -1,139 +1,134 @@ # coding: utf-8 require 'spec_helper' describe Klipp do - before do - @output = Klipp.output = StringIO.new - end + context 'capturing stdout' do - describe 'when routing' do - - it 'prints help without any parameters' do - expect { Klipp.route *%w[] }.to raise_error SystemExit - should include 'Xcode templates for the rest of us.' + it 'matches stdout' do + capture_stdout { + puts "I'm out" + }.should eq "I'm out\n" end - it 'prints warnings accordingly' do - expect { Klipp.route *%w[prepare Bullshit] }.to raise_error SystemExit - should include '[!]' + it 'matches formatador stdout' do + capture_stdout { + Formatador.display_line "I'm [green]green[/]" + }.should include "green" end - it 'raises a warning when routing unknown commands' do - expect { Klipp.route *%w[allyourbase] }.to raise_error SystemExit - should include '[!]' - end + end - it 'routes `prepare`' do - Klipp.expects(:prepare) - Klipp.route *%w[prepare Example] + context 'when routing commands' do + + it 'returns exit code 1 without any commands and displays a hint' do + (capture_stdout do + Klipp.route(*%w[]).should eq 1 + end).should include '[?]' end - it 'prints help when preparing without a template name' do - expect { Klipp.route *%w[prepare] }.to raise_error SystemExit - should include 'Add a template name to the `prepare` command.' + it 'returns exit code 1 with an unknown command and displays the error' do + (capture_stdout do + Klipp.route(*%w[magic]).should eq 1 + end).should include '[!]' end - it 'routes `list`' do - Klipp.expects(:list) - Klipp.route *%w[list] + it 'routes prepare' do + Klipp.expects(:cli_prepare).with(['Example']) + Klipp.route(*%w[prepare Example]) end - it 'routes `version`' do - Klipp.expects(:version) - Klipp.route *%w[version] + it 'routes create' do + Klipp.expects(:cli_create).with(['-f']) + Klipp.route(*%w[create -f]) end - it 'routes `create``' do - Klipp.expects(:create) - Klipp.route *%w[create] + it 'routes template with exit code 0' do + Template.expects(:route) + Klipp.route(*%w[template]).should eq 0 end end - describe 'when preparing' do + context 'prepare' do before do - Klipp::Configuration.stubs(:root_dir).returns File.join(__dir__, 'fixtures') + Klipp::Configuration.stubs(:root_dir).returns(File.join(__dir__, 'fixtures')) end - it 'raises help if the template argument is missing' do - expect { Klipp.prepare nil }.to raise_error HelpRequest + it 'without template name raises error' do + expect { Klipp.cli_prepare([]) }.to raise_error Klipp::Hint end - it 'saves a klippfile' do - IO.expects(:write).with('Example.klippfile', anything).once - Klipp.prepare 'Example' - end + context 'without an existing Klippfile' do + before do + File.stubs(:exists?).returns(false) + end - it 'raises an error when the template argument is incorrect' do - expect { Klipp.prepare 'Bullshit' }.to raise_error RuntimeError + it 'write a new Klippfile' do + klippfile = read_fixture 'Klippfile-after-prepare' + File.expects(:write).with('Klippfile', klippfile) + Klipp.cli_prepare(%w[Example]) + end end - it 'raises an error when a .klippfile for the template already exists' do - File.stubs(:exists?).with(anything).returns true - expect { Klipp.prepare 'Example' }.to raise_error RuntimeError - end - end + context 'with an existing Klippfile' do - describe 'when listing' do + before do + File.stubs(:exists?).returns(true) + end - it 'lists templates' do - Klipp::Configuration.stubs(:root_dir).returns File.join(__dir__, 'fixtures') - Klipp.list - should include 'Example' - end + it 'will not overwrite' do + File.expects(:write).never + expect { Klipp.cli_prepare(%w[Example]) }.to raise_error RuntimeError + end - it 'raises an error when there are no templates' do - Klipp.expects(:template_files).returns([]) - expect { Klipp.list }.to raise_error RuntimeError + it 'will overwrite when forced' do + klippfile = read_fixture 'Klippfile-after-prepare' + File.expects(:write).with('Klippfile', klippfile) + Klipp.cli_prepare(%w[Example -f]) + end + end end - describe 'when versioning' do + context 'create' do - it 'displays the current version' do - Klipp.version - should include Klipp::VERSION + before do + Klipp::Configuration.stubs(:root_dir).returns(File.join(__dir__, 'fixtures')) + Dir.stubs(:pwd).returns(Dir.mktmpdir) + + maker = Klipp::Creator.new + maker.eval_string(read_fixture('Klippfile'), fixture_path('Klippfile')) + Klipp::Creator.stubs(:from_file).returns(maker) end - end + it 'creates files from a Klippfile' do - describe 'when creating' do + Klipp.cli_create([]) + File.exists?(File.join Dir.pwd, 'Podfile').should be true + File.exists?(File.join Dir.pwd, '.gitignore').should be true + File.exists?(File.join Dir.pwd, 'AmazingApp').should be true + File.exists?(File.join Dir.pwd, 'Example.klippspec').should be false - before do - Klipp::Configuration.stubs(:root_dir).returns File.join(__dir__, 'fixtures') - @example_klippfile_contents = File.read(File.join(__dir__, 'fixtures', 'klipps', 'Example.klippfile')) end - it 'calls create on a project with a named template' do - mock_project = mock - mock_project.expects(:create) - Klipp::Project.expects(:new).with(is_a(Klipp::Template)).returns(mock_project) - File.stubs(:read).with(anything).returns( @example_klippfile_contents ) - Klipp.create('Example') - end + it 'creates files interactively' do - it 'calls create on a project with an inferred template' do - Dir.expects(:glob).with(anything, anything).returns %w(Example.klippfile) - mock_project = mock - mock_project.expects(:create) - Klipp::Project.expects(:new).with(is_a(Klipp::Template)).returns(mock_project) - File.stubs(:read).with(anything).returns( @example_klippfile_contents ) - Klipp.create nil - end + input = StringIO.new + output = StringIO.new + highline = HighLine.new(input, output) - it 'raises an error when the template name can\'t be inferred' do - Dir.expects(:glob).with(anything, anything).returns [] - expect { Klipp.create nil }.to raise_error RuntimeError - end + input << "KLPObject\nN\n" + input.rewind - end + Klipp.cli_create(%w(Interactive), highline) + File.exists?(File.join Dir.pwd, 'KLPObjectTests.m').should be true - def subject - @output.string + end + end end \ No newline at end of file