Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 218 lines (178 sloc) 7.255 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
require 'spec_helper'
require 'guard/guard'

describe Guard::Interactor do
  subject { Guard::Interactor.new }

  describe '#readline' do
    it 'must be implemented by a subclass' do
      expect { subject.read_line }.to raise_error(NotImplementedError)
    end
  end

  describe '.fabricate' do
    it 'returns the Readline interactor for a :readline symbol' do
      Guard::Interactor.interactor = :readline
      Guard::Interactor.fabricate.should be_an_instance_of(Guard::ReadlineInteractor)
    end

    it 'returns the Gets interactor for a :simple symbol' do
      Guard::Interactor.interactor = :simple
      Guard::Interactor.fabricate.should be_an_instance_of(Guard::SimpleInteractor)
    end

    it 'returns nil for an :off symbol' do
      Guard::Interactor.interactor = :off
      Guard::Interactor.fabricate.should be_nil
    end

    it 'auto detects the interactor when unspecified' do
      Guard::Interactor.interactor = nil
      Guard::Interactor.should_receive(:auto_detect)
      Guard::Interactor.fabricate
    end
  end

  describe '#process_input' do
    it 'shows the help on help action' do
      subject.should_receive(:extract_scopes_and_action).with('help').and_return [{ }, :help]
      subject.should_receive(:help)
      subject.process_input 'help'
    end

    it 'stops Guard on stop action and exit' do
      subject.should_receive(:extract_scopes_and_action).with('stop').and_return [{ }, :stop]
      ::Guard.should_receive(:stop)

      begin
        subject.process_input 'stop'
        raise 'Guard did not exit!'
      rescue SystemExit => e
        e.status.should eq(0)
      end
    end

    it 'pauses Guard on pause action' do
      subject.should_receive(:extract_scopes_and_action).with('pause').and_return [{ }, :pause]
      ::Guard.should_receive(:pause)
      subject.process_input 'pause'
    end

    it 'reloads Guard on reload action' do
      subject.should_receive(:extract_scopes_and_action).with('reload').and_return [{ }, :reload]
      subject.should_receive(:reload).with({ })
      subject.process_input 'reload'
    end

    it 'runs all Guard on run_all action' do
      subject.should_receive(:extract_scopes_and_action).with('').and_return [{ }, :run_all]
      ::Guard.should_receive(:run_all).with({ })
      subject.process_input ''
    end

    it 'toggles the notifications on notification action' do
      subject.should_receive(:extract_scopes_and_action).with('notification').and_return [{ }, :notification]
      subject.should_receive(:toggle_notification)
      subject.process_input 'notification'
    end

    it 'shows an error on unknown action' do
      subject.should_receive(:extract_scopes_and_action).with('foo').and_return [{ }, :unknown]
      ::Guard::UI.should_receive(:error).with 'Unknown command foo'
      subject.process_input 'foo'
    end
  end

  describe '#reload' do
    before do
      ::Guard.stub(:reload)
      ::Guard::Dsl.stub(:reevaluate_guardfile)
      ::Guard::UI.stub(:info)
    end

    context 'with a scope' do
      it 'does not re-evaluate the Guardfile' do
        ::Guard::Dsl.should_not_receive(:reevaluate_guardfile)
        subject.reload({ :group => :frontend })
      end

      it 'reloads Guard' do
        ::Guard.should_receive(:reload).with({ :group => :frontend })
        subject.reload({ :group => :frontend })
      end
    end

    context 'with an empty scope' do
      it 'does re-evaluate the Guardfile' do
        ::Guard::Dsl.should_receive(:reevaluate_guardfile)
        subject.reload({ })
      end

      it 'reloads Guard' do
        ::Guard.should_receive(:reload).with({ })
        subject.reload({ })
      end
    end
  end

  describe 'toggle_notification' do
    before { ::Guard::UI.stub(:info) }

    it 'disables the notifications when enabled' do
      ENV['GUARD_NOTIFY'] = 'true'
      ::Guard::Notifier.should_receive(:turn_off)
      subject.toggle_notification
    end

    it 'enables the notifications when disabled' do
      ENV['GUARD_NOTIFY'] = 'false'
      ::Guard::Notifier.should_receive(:turn_on)
      subject.toggle_notification
    end
  end

  describe '#extract_scopes_and_action' do

    class Guard::Foo < Guard::Guard;
    end
    class Guard::FooBar < Guard::Guard;
    end

    before(:each) do
      guard = ::Guard.setup
      @backend_group = guard.add_group(:backend)
      @frontend_group = guard.add_group(:frontend)
      @foo_guard = guard.add_guard(:foo, [], [], { :group => :backend })
      @foo_bar_guard = guard.add_guard('foo-bar', [], [], { :group => :frontend })
    end

    it 'returns :run_all action if entry is blank' do
      subject.extract_scopes_and_action('').should eql([{ }, :run_all])
    end

    it 'returns action if entry is only a action' do
      subject.extract_scopes_and_action('exit').should eql([{ }, :stop])
    end

    it 'returns guard scope and run_all action if entry is only a guard scope' do
      subject.extract_scopes_and_action('foo-bar').should eql([{ :guard => @foo_bar_guard }, :run_all])
    end

    it 'returns group scope and run_all action if entry is only a group scope' do
      subject.extract_scopes_and_action('backend').should eql([{ :group => @backend_group }, :run_all])
    end

    it 'returns no action if entry is not a scope or action' do
      subject.extract_scopes_and_action('x').should eql([{ }, nil])
    end

    it 'returns guard scope and action if entry is a guard scope and a action' do
      subject.extract_scopes_and_action('foo r').should eql([{ :guard => @foo_guard }, :reload])
    end

    it 'returns group scope and action if entry is a group scope and a action' do
      subject.extract_scopes_and_action('frontend r').should eql([{ :group => @frontend_group }, :reload])
    end

    it 'returns group scope and run_all action if entry is a group scope and not a action' do
      subject.extract_scopes_and_action('frontend x').should eql([{ :group => @frontend_group }, :run_all])
    end

    it 'returns no action if entry is not a scope and not a action' do
      subject.extract_scopes_and_action('x x').should eql([{ }, nil])
    end

    describe 'extracting actions' do
      it 'returns :help action for the help entry and for its shortcut' do
        %w{help h}.each do |e|
          subject.extract_scopes_and_action(e).should eql([{ }, :help])
        end
      end

      it 'returns :reload action for the reload entry and for its shortcut' do
        %w{reload r}.each do |e|
          subject.extract_scopes_and_action(e).should eql([{ }, :reload])
        end
      end

      it 'returns :stop action for exit or quit entry and for their shortcuts' do
        %w{exit e quit q}.each do |e|
          subject.extract_scopes_and_action(e).should eql([{ }, :stop])
        end
      end

      it 'returns :pause action for the pause entry and for its shortcut' do
        %w{pause p}.each do |e|
          subject.extract_scopes_and_action(e).should eql([{ }, :pause])
        end
      end

      it 'returns :notification action for the notification entry and for its shortcut' do
        %w{notification n}.each do |e|
          subject.extract_scopes_and_action(e).should eql([{ }, :notification])
        end
      end
    end
  end

end
Something went wrong with that request. Please try again.