File: test_argument_list.rb

package info (click to toggle)
ruby-cri 2.15.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: ruby: 2,776; makefile: 11
file content (110 lines) | stat: -rw-r--r-- 3,329 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'helper'

module Cri
  class ArgumentListTestCase < Cri::TestCase
    def test_empty
      args = Cri::ArgumentList.new([], false, [])

      assert_equal([], args.to_a)
      assert(args.empty?)
      assert_equal(0, args.size)
      assert_equal(nil, args[0])
      assert_equal(nil, args[:abc])
    end

    def test_no_param_defns
      args = Cri::ArgumentList.new(%w[a b c], false, [])

      assert_equal(%w[a b c], args.to_a)
      refute(args.empty?)
      assert_equal(3, args.size)
      assert_equal('a', args[0])
      assert_equal('b', args[1])
      assert_equal('c', args[2])
      assert_equal(nil, args[3])
      assert_equal(nil, args[:abc])
    end

    def test_enum
      args = Cri::ArgumentList.new(%w[a b c], false, [])

      assert_equal(%w[A B C], args.map(&:upcase))
    end

    def test_enum_without_block
      args = Cri::ArgumentList.new(%w[a b c], false, [])

      assert_equal(%w[A B C], args.each.map(&:upcase))
    end

    def test_no_method_error
      args = Cri::ArgumentList.new(%w[a b c], false, [])

      refute args.respond_to?(:oink)
      assert_raises(NoMethodError, 'x') do
        args.oink
      end
    end

    def test_dash_dash
      args = Cri::ArgumentList.new(%w[a -- b -- c], false, [])

      assert_equal(%w[a b c], args.to_a)
    end

    def test_one_param_defn_matched
      param_defns = [Cri::ParamDefinition.new(name: 'filename', transform: nil)]
      args = Cri::ArgumentList.new(%w[notbad.jpg], false, param_defns)

      assert_equal(['notbad.jpg'], args.to_a)
      assert_equal(1, args.size)
      assert_equal('notbad.jpg', args[0])
      assert_equal('notbad.jpg', args[:filename])
    end

    def test_one_param_defn_too_many
      param_defns = [Cri::ParamDefinition.new(name: 'filename', transform: nil)]

      exception = assert_raises(Cri::ArgumentList::ArgumentCountMismatchError) do
        Cri::ArgumentList.new(%w[notbad.jpg verybad.jpg], false, param_defns)
      end
      assert_equal('incorrect number of arguments given: expected 1, but got 2', exception.message)
    end

    def test_one_param_defn_too_few
      param_defns = [Cri::ParamDefinition.new(name: 'filename', transform: nil)]

      exception = assert_raises(Cri::ArgumentList::ArgumentCountMismatchError) do
        Cri::ArgumentList.new(%w[], false, param_defns)
      end
      assert_equal('incorrect number of arguments given: expected 1, but got 0', exception.message)
    end

    def test_zero_params_zero_args
      args = Cri::ArgumentList.new(%w[], false, [])

      assert_equal([], args.to_a)
      assert args.empty?
      assert_equal(0, args.size)
    end

    def test_zero_params_one_arg
      exception = assert_raises(Cri::ArgumentList::ArgumentCountMismatchError) do
        Cri::ArgumentList.new(%w[a], true, [])
      end
      assert_equal('incorrect number of arguments given: expected 0, but got 1', exception.message)
    end

    def test_transform
      param_defns = [Cri::ParamDefinition.new(name: 'filename', transform: lambda(&:upcase))]
      args = Cri::ArgumentList.new(%w[notbad.jpg], false, param_defns)

      assert_equal(['NOTBAD.JPG'], args.to_a)
      assert_equal(1, args.size)
      assert_equal('NOTBAD.JPG', args[0])
      assert_equal('NOTBAD.JPG', args[:filename])
    end
  end
end