File: argument_spec.rb

package info (click to toggle)
ruby-thor 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: ruby: 9,250; makefile: 8; sh: 1
file content (76 lines) | stat: -rw-r--r-- 2,220 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
require "helper"
require "thor/parser"

describe Thor::Argument do
  def argument(name, options = {})
    @argument ||= Thor::Argument.new(name, options)
  end

  describe "errors" do
    it "raises an error if name is not supplied" do
      expect do
        argument(nil)
      end.to raise_error(ArgumentError, "Argument name can't be nil.")
    end

    it "raises an error if type is unknown" do
      expect do
        argument(:command, type: :unknown)
      end.to raise_error(ArgumentError, "Type :unknown is not valid for arguments.")
    end

    it "raises an error if argument is required and has default values" do
      expect do
        argument(:command, type: :string, default: "bar", required: true)
      end.to raise_error(ArgumentError, "An argument cannot be required and have default value.")
    end

    it "raises an error if enum isn't enumerable" do
      expect do
        argument(:command, type: :string, enum: "bar")
      end.to raise_error(ArgumentError, "An argument cannot have an enum other than an enumerable.")
    end
  end

  describe "#usage" do
    it "returns usage for string types" do
      expect(argument(:foo, type: :string).usage).to eq("FOO")
    end

    it "returns usage for numeric types" do
      expect(argument(:foo, type: :numeric).usage).to eq("N")
    end

    it "returns usage for array types" do
      expect(argument(:foo, type: :array).usage).to eq("one two three")
    end

    it "returns usage for hash types" do
      expect(argument(:foo, type: :hash).usage).to eq("key:value")
    end
  end

  describe "#print_default" do
    it "prints arrays in a copy pasteable way" do
      expect(argument(:foo, {
        required: false,
        type: :array,
        default: ["one","two"]
      }).print_default).to eq('"one" "two"')
    end
    it "prints arrays with a single string default as before" do
      expect(argument(:foo,    {
        required: false,
        type: :array,
        default: "foobar"
      }).print_default).to eq("foobar")
    end
    it "prints none arrays as default" do
      expect(argument(:foo, {
        required: false,
        type: :numeric,
        default: 13,
      }).print_default).to eq(13)
    end
  end
end