Files
crystal_cligen/spec/command_spec.cr
Tristan Ancelet 136298e67c Changing to cligen
2026-02-23 13:09:33 -06:00

132 lines
3.7 KiB
Crystal

require "spec"
require "../src/command"
@[CliGen::CommandInfo(description: "Test")]
class CommandSubclass < CliGen::Command
define_argument(testvar,
type: Int32,
long: "--testvar TESTVAR",
description: "Does things"
)
define_argument(testvar_the_return,
type: Int32,
def_getter: true,
long: "--testvar_the_return TESTVAR",
description: "Does things"
)
end
EXAMPLES = [
"Have test"
]
@[CliGen::SubCommand(description: "test", examples: ::EXAMPLES)]
def CommandSubclass.do_thing
puts "HI"
end
@[CliGen::CommandPreRun]
def CommandSubclass.check_things
puts "I was run"
end
## Need to make sure EVERYTHING is generated before testing
macro finished
describe CliGen::Command do
describe "subclassing" do
describe "Creates the arguments" do
it "has testvar" do
{{CommandSubclass.class.has_method?(:testvar)}}.should be_true
end
it "has testvar2 getter" do
{{CommandSubclass.class.has_method?(:get_testvar_the_return)}}.should be_true
end
end
it "has Header" do
CommandSubclass::HEADER.empty?.should be_false
end
it "has examples" do
CommandSubclass::HEADER.includes?("Examples").should be_true
end
{% pre_runs = CommandSubclass.class.methods.select(&.annotation(::CliGen::CommandPreRun)) %}
{% run = CommandSubclass.class.methods.find{|m| m.name.stringify == "run"}.stringify %}
{% if pre_runs.size > 0 %}
describe "when defining pre-run methods" do
{% for pre_run in pre_runs %}
it "generates {{pre_run.name}}" do
{{run}}.includes?({{pre_run.name.stringify}}).should be_true
end
{% end %}
end
{% end %}
{% subcommands = CommandSubclass.class.methods.select(&.annotation(::CliGen::SubCommand)) %}
{% unless subcommands.empty? %}
describe "should have subcommands" do
{% for subcommand in subcommands %}
it "has {{subcommand.name}}" do
{{CommandSubclass.class.has_method?(subcommand.name.symbolize)}}.should be_true
end
{% end %}
end
{% end %}
{% arguments = CommandSubclass.class.methods.select(&.annotation(::CliGen::CommandArgument)) %}
{% unless arguments.empty? %}
describe "should have arguments" do
{% for argument in arguments %}
it "has {{argument.name}}" do
{{CommandSubclass.class.has_method?(argument.name.symbolize)}}.should be_true
end
{% end %}
end
{% end %}
{% if [arguments, subcommands].any?{|i| i.size >= 0 } %}
describe "should have generated OptionParser parser.on" do
{{ make_parser = CommandSubclass.class.methods.find{|m| m.name.stringify == "make_parser"}.stringify}}
{% unless subcommands.empty? %}
describe "for subcommands" do
{% for command in subcommands %}
it "has {{command.name}}" do
{{make_parser}}.includes?("on(" + {{command.name.stringify.stringify}}).should be_true
end
{% end %}
end
{% end %}
{% unless arguments.empty? %}
describe "for arguments" do
{% for argument in arguments %}
{% anno = argument.annotation(::CliGen::CommandArgument) %}
it "has " + {{anno[:long].stringify}} do
{{make_parser}}.includes?({{anno[:long]}}).should be_true
end
{% end %}
end
{% end %}
end
{% end %}
describe "Should have generated macro class methods" do
it "has generated run" do
{{CommandSubclass.class.has_method?(:run)}}.should be_true
end
it "has generated make_parser" do
{{CommandSubclass.class.has_method?(:make_parser)}}.should be_true
end
end
end
end
end