Project

General

Profile

Download (1.88 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / test / helpers / command.rb @ ebb00443

1

    
2
module CommandTestHelper
3

    
4
  def with_params(params, &block)
5
    context "with params " + params.to_s do
6
      let(:with_params) { params }
7
      self.instance_eval(&block)
8
    end
9
  end
10

    
11
  def it_should_call_action(action, params)
12
    it "should call action " + action.to_s do
13
      arguments ||= respond_to?(:with_params) ? with_params : []
14
      cmd.resource.resource_class.expects_with(action, params)
15
      cmd.run(arguments)
16
    end
17
  end
18

    
19
  def it_should_fail_with(message, arguments=[])
20
    it "should fail with " + message.to_s do
21
      proc { cmd.run(arguments) }.must_raise Clamp::UsageError
22
    end
23
  end
24

    
25
  def it_should_accept(message, arguments=[])
26
    it "should accept " + message.to_s do
27
      cmd.run(arguments).must_equal HammerCLI::EX_OK
28
    end
29
  end
30

    
31
  def it_should_print_column(column_name, arguments=nil)
32
    it "should print column " + column_name do
33
      arguments ||= respond_to?(:with_params) ? with_params : []
34

    
35
      cmd.stubs(:context).returns({ :adapter => :test })
36
      proc { cmd.run(arguments) }.must_output(/.*##{column_name}#.*/)
37
    end
38
  end
39

    
40
  def it_should_print_columns(column_names, arguments=nil)
41
    column_names.each do |name|
42
      it_should_print_column name, arguments
43
    end
44
  end
45

    
46
  def it_should_print_n_records(count=nil, arguments=nil)
47
    it "should print correct count of records" do
48
      arguments ||= respond_to?(:with_params) ? with_params : []
49

    
50
      cmd.stubs(:context).returns({ :adapter => :test })
51
      count ||= expected_record_count rescue 0
52
      out, err = capture_io do
53
        cmd.run(arguments)
54
      end
55

    
56
      out.split(/\n/).length.must_equal count+1 # plus 1 for line with column headers
57
    end
58
  end
59

    
60
  def it_should_accept_search_params
61
    it_should_accept "search", ["--search=some_search"]
62
    it_should_accept "per page", ["--per-page=1"]
63
    it_should_accept "page", ["--page=2"]
64
    it_should_accept "order", ["--order=order"]
65
  end
66

    
67
end