require 'spec_helper' module Landable describe TidyService do let(:service) { TidyService } describe '.tidyable?' do after(:each) { service.class_variable_set :@@is_tidyable, nil } context 'when tidyable' do it 'should check on the availability of a `tidy` command' do Kernel.should_receive(:system).with('which tidy > /dev/null') { true } service.should be_tidyable service.class_variable_get(:@@is_tidyable).should be_true end end context 'not tidyable' do it 'should return false' do Kernel.should_receive(:system).with('which tidy > /dev/null') { false } service.should_not be_tidyable service.class_variable_get(:@@is_tidyable).should be_false end end end describe '.call!' do it 'should call #call with raise_on_error: true' do input = double output = double service.should_receive(:call).with(input, raise_on_error: true) { output } service.call!(input).should == output end end describe '.call' do context 'when not tidyable' do it 'should raise an exception' do service.should_receive(:tidyable?) { false } expect { service.call 'foo' }.to raise_error(StandardError) end end context 'when tidyable' do before(:each) do service.should_receive(:tidyable?) { true } end it 'should invoke tidy and return a Result' do input = double('input') output = double('output') result = double('result') service.should_receive(:wrap_liquid) { |input| input }.ordered # passthrough; will test later mock_io = double('io') mock_io.should_receive(:puts).with(input).ordered mock_io.should_receive(:close_write).ordered mock_io.should_receive(:read) { output } service.should_receive(:unwrap_liquid) { |input| input }.ordered # passthrough; will test later # other setup service.should_receive(:options) { ['one', 'two', 'three'] } IO.should_receive(:popen).with('tidy one two three', 'r+').and_yield(mock_io) TidyService::Result.should_receive(:new).with(output) { result } service.call(input).should == result end it 'should wrap known liquid tags before sending to tidy, and unwrap them after' do original_string = '