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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
# frozen_string_literal: true
require 'helper'
module Cri
class CommandDSLTestCase < Cri::TestCase
def test_create_command
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
option :a, :aaa, 'opt a', argument: :optional, multiple: true
required :b, :bbb, 'opt b'
optional :c, :ccc, 'opt c'
flag :d, :ddd, 'opt d'
forbidden :e, :eee, 'opt e'
flag :f, :fff, 'opt f', hidden: true
run do |_opts, _args|
$did_it_work = :probably
end
end
command = dsl.command
# Run
$did_it_work = :sadly_not
command.run(%w[-a x -b y -c -d -e])
assert_equal :probably, $did_it_work
# Check
assert_equal 'moo', command.name
assert_equal 'dunno whatever', command.usage
assert_equal 'does stuff', command.summary
assert_equal 'This command does a lot of stuff.', command.description
# Check options
expected_option_definitions =
Set.new(
[
{ short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
{ short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
{ short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil, transform: nil },
{ short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
{ short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
{ short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: false, transform: nil },
],
)
actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
assert_equal expected_option_definitions, actual_option_definitions
end
def test_optional_options
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
flag :s, nil, 'short'
flag nil, :long, 'long'
run do |_opts, _args|
$did_it_work = :probably
end
end
command = dsl.command
# Run
$did_it_work = :sadly_not
command.run(%w[-s --long])
assert_equal :probably, $did_it_work
# Check options
expected_option_definitions =
Set.new(
[
{ short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
{ short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: false, transform: nil },
],
)
actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
assert_equal expected_option_definitions, actual_option_definitions
end
def test_multiple
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
flag :f, :flag, 'flag', multiple: true
required :r, :required, 'req', multiple: true
optional :o, :optional, 'opt', multiple: true
run { |_opts, _args| }
end
command = dsl.command
# Check options
expected_option_definitions =
Set.new(
[
{ short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: false, transform: nil },
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil, transform: nil },
],
)
actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
assert_equal expected_option_definitions, actual_option_definitions
end
def test_hidden
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
flag :f, :flag, 'flag', hidden: true
required :r, :required, 'req', hidden: true
optional :o, :optional, 'opt', hidden: true
run { |_opts, _args| }
end
command = dsl.command
# Check options
expected_option_definitions =
Set.new(
[
{ short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: false, transform: nil },
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil, transform: nil },
],
)
actual_option_definitions = Set.new(command.option_definitions.map(&:to_h))
assert_equal expected_option_definitions, actual_option_definitions
end
def test_raises_on_unrecognized_option
# Define
dsl = Cri::CommandDSL.new
assert_raises ArgumentError do
dsl.option :s, :long, 'desc', unrecognized: true
end
end
def test_required_short_and_long
# Define
dsl = Cri::CommandDSL.new
assert_raises ArgumentError do
dsl.instance_eval do
option nil, nil, 'meh'
end
end
assert_raises ArgumentError do
dsl.instance_eval do
flag nil, nil, 'meh'
end
end
assert_raises ArgumentError do
dsl.instance_eval do
required nil, nil, 'meh'
end
end
assert_raises ArgumentError do
dsl.instance_eval do
optional nil, nil, 'meh'
end
end
end
def test_default_value_with_equiredness_is_required
dsl = Cri::CommandDSL.new
dsl.instance_eval do
required 'a', 'animal', 'Specify animal', default: 'giraffe'
end
end
def test_default_value_errors_when_requiredness_is_forbidden
dsl = Cri::CommandDSL.new
err = assert_raises ArgumentError do
dsl.instance_eval do
flag 'a', 'animal', 'Allow animal', default: 'giraffe'
end
end
assert_equal('a default value cannot be specified for flag options', err.message)
end
def test_subcommand
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'super'
subcommand do |c|
c.name 'sub'
end
end
command = dsl.command
# Check
assert_equal 'super', command.name
assert_equal 1, command.subcommands.size
assert_equal 'sub', command.subcommands.to_a[0].name
end
def test_aliases
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
aliases :moo, :aah
end
command = dsl.command
# Check
assert_equal %w[aah moo], command.aliases.sort
end
def test_run_arity
dsl = Cri::CommandDSL.new
assert_raises ArgumentError do
dsl.instance_eval do
run do |_a, _b, _c, _d, _e|
end
end
end
end
def test_runner
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval(<<-CMD, __FILE__, __LINE__ + 1)
class Cri::CommandDSLTestCaseCommandRunner < Cri::CommandRunner
def run
$did_it_work = arguments[0]
end
end
runner Cri::CommandDSLTestCaseCommandRunner
CMD
command = dsl.command
# Check
$did_it_work = false
command.run(%w[certainly])
assert_equal 'certainly', $did_it_work
end
def test_params
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
param :foo
param :bar
param :qux
run do |_opts, args|
$args_num = { foo: args[0], bar: args[1], qux: args[2] }
$args_sym = { foo: args[:foo], bar: args[:bar], qux: args[:qux] }
end
end
command = dsl.command
# Run
$args_num = '???'
$args_sym = '???'
command.run(%w[a b c])
assert_equal({ foo: 'a', bar: 'b', qux: 'c' }, $args_num)
assert_equal({ foo: 'a', bar: 'b', qux: 'c' }, $args_sym)
end
def test_params_transform
# Define
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
param :foo, transform: lambda(&:upcase)
run do |_opts, args|
$args_num = { foo: args[0] }
$args_sym = { foo: args[:foo] }
end
end
command = dsl.command
# Run
$args_num = '???'
$args_sym = '???'
command.run(%w[abc])
assert_equal({ foo: 'ABC' }, $args_num)
assert_equal({ foo: 'ABC' }, $args_sym)
end
def test_no_params_with_one_param_specified
dsl = Cri::CommandDSL.new
err = assert_raises Cri::CommandDSL::AlreadySpecifiedWithParams do
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
param :oink
no_params
end
end
assert_equal('Attempted to declare the command "moo" as taking no parameters, but some parameters are already declared for this command. Suggestion: remove the #no_params call.', err.message)
end
def test_one_param_with_no_params_specified
dsl = Cri::CommandDSL.new
err = assert_raises Cri::CommandDSL::AlreadySpecifiedAsNoParams do
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
param :oink
end
end
assert_equal('Attempted to specify a parameter :oink to the command "moo", which is already specified as taking no params. Suggestion: remove the #no_params call.', err.message)
end
end
end
|