File: parser_constraint_test.rb

package info (click to toggle)
ruby-optimist 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: ruby: 2,912; makefile: 4
file content (141 lines) | stat: -rw-r--r-- 5,171 bytes parent folder | download
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
require_relative '../test_helper'

module Optimist

class ParserConstraintTest < ::Minitest::Test
  def setup
    @p = Parser.new
  end

  def parser
    @p ||= Parser.new
  end

  def test_conflicts
    @p.opt :one
    err_regex = /unknown option 'two'/
    assert_raises_errmatch(ArgumentError, err_regex) { @p.conflicts :one, :two }
    @p.opt :two
    @p.conflicts :one, :two
    @p.parse %w(--one)
    @p.parse %w(--two)
    err_regex = /only one of --one, --two can be given/

    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }

    @p.opt :hello
    @p.opt :yellow
    @p.opt :mellow
    @p.opt :jello
    @p.conflicts :hello, :yellow, :mellow, :jello
    err_regex = /only one of --hello, --yellow, --mellow, --jello can be given/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello --yellow --mellow --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello --mellow --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello --jello) }

    @p.parse %w(--hello)
    @p.parse %w(--jello)
    @p.parse %w(--yellow)
    @p.parse %w(--mellow)

    @p.parse %w(--mellow --one)
    @p.parse %w(--mellow --two)

    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--mellow --two --jello) }
    err_regex = /only one of --one, --two can be given/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --mellow --two --jello) }
  end

  def test_conflict_error_messages
    @p.opt :one
    @p.opt "two"
    @p.conflicts :one, "two"
    err_regex = %r/only one of --one, --two can be given/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }
  end

  def test_either
    @p.opt :one
    err_regex = /unknown option 'two'/
    assert_raises_errmatch(ArgumentError, err_regex) { @p.either :one, :two }
    @p.opt :two
    @p.either :one, :two
    @p.parse %w(--one)
    @p.parse %w(--two)
    err_regex = /one and only one of --one, --two is required/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w() }

    @p.opt :hello
    @p.opt :yellow
    @p.opt :mellow
    @p.opt :jello
    @p.either :hello, :yellow, :mellow, :jello
    err_regex = /one and only one of --hello, --yellow, --mellow, --jello is required/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --hello --yellow --mellow --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--two --hello --mellow --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --hello --jello) }

    @p.parse %w(--hello --one)
    @p.parse %w(--jello --two)
    @p.parse %w(--mellow --one)
    @p.parse %w(--mellow --two)

    err_regex = /one and only one of/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--mellow --two --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --mellow --two --jello) }
  end

  def test_either_error_messages
    @p.opt :one
    @p.opt :two
    @p.opt :three
    @p.either :one, :two, :three
    err_regex = %r/one and only one of --one, --two, --three is required/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one --two) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--three --two --one) }
  end

  def test_depends
    @p.opt :one
    err_regex = /unknown option 'two'/
    assert_raises_errmatch(ArgumentError, err_regex) { @p.depends :one, :two }
    @p.opt :two
    @p.depends :one, :two
    @p.parse %w(--one --two)
    err_regex = /--one, --two have a dependency and must be given together/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--two) }

    @p.opt :hello
    @p.opt :yellow
    @p.opt :mellow
    @p.opt :jello
    @p.depends :hello, :yellow, :mellow, :jello
    @p.parse %w(--hello --yellow --mellow --jello)
    err_regex = /-hello, --yellow, --mellow, --jello have a dependency and must be given together/

    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello --mellow --jello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello --jello) }

    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--hello) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--mellow) }

    @p.parse %w(--hello --yellow --mellow --jello --one --two)
    @p.parse %w(--hello --yellow --mellow --jello --one --two a b c)

    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--mellow --two --jello --one) }
  end

  def test_depends_error_messages
    @p.opt :one
    @p.opt "two"
    @p.depends :one, "two"

    @p.parse %w(--one --two)
    err_regex = %r/--one, --two have a dependency and must be given together/
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--one) }
    assert_raises_errmatch(CommandlineError, err_regex) { @p.parse %w(--two) }
  end
end
end