pedrozath-mercenary 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe(Mercenary::Command) do
6
+ context "a basic command" do
7
+ let(:command) { Mercenary::Command.new(:my_name) }
8
+ let(:parent) { Mercenary::Command.new(:my_parent) }
9
+ let(:with_sub) do
10
+ c = Mercenary::Command.new(:i_have_subcommand)
11
+ add_sub.call(c)
12
+ c
13
+ end
14
+ let(:command_with_parent) do
15
+ Mercenary::Command.new(
16
+ :i_have_parent,
17
+ parent
18
+ )
19
+ end
20
+ let(:add_sub) do
21
+ proc do |c|
22
+ c.command(:sub_command) { |p| }
23
+ end
24
+ end
25
+
26
+ it "can be created with just a name" do
27
+ expect(command.name).to eql(:my_name)
28
+ end
29
+
30
+ it "can hold a parent command" do
31
+ expect(command_with_parent.parent).to eql(parent)
32
+ end
33
+
34
+ it "can create subcommands" do
35
+ expect(add_sub.call(command)).to be_a(Mercenary::Command)
36
+ expect(add_sub.call(command).parent).to eq(command)
37
+ end
38
+
39
+ it "can set its version" do
40
+ version = "1.4.2"
41
+ command.version version
42
+ expect(command.version).to eq(version)
43
+ end
44
+
45
+ it "can set its syntax" do
46
+ syntax_string = "my_name [options]"
47
+ cmd = described_class.new(:my_name)
48
+ cmd.syntax syntax_string
49
+ expect(cmd.syntax).to eq(syntax_string)
50
+ end
51
+
52
+ it "can set its description" do
53
+ desc = "run all the things"
54
+ command.description desc
55
+ expect(command.description).to eq(desc)
56
+ end
57
+
58
+ it "can set its options" do
59
+ name = "show_drafts"
60
+ opts = ["--drafts", "Render posts in the _drafts folder"]
61
+ option = Mercenary::Option.new(name, opts)
62
+ command.option name, *opts
63
+ expect(command.options).to eql([option])
64
+ expect(command.map.values).to include(name)
65
+ end
66
+
67
+ it "knows its full name" do
68
+ expect(command_with_parent.full_name).to eql("my_parent i_have_parent")
69
+ end
70
+
71
+ it "knows its identity" do
72
+ command_with_parent.version "1.8.7"
73
+ expect(command_with_parent.identity).to eql("my_parent i_have_parent 1.8.7")
74
+ end
75
+
76
+ it "raises an ArgumentError if I specify a default_command that isn't there" do
77
+ c = command # some weird NameError with the block below?
78
+ expect { c.default_command(:nope) }.to raise_error(ArgumentError)
79
+ end
80
+
81
+ it "sets the default_command" do
82
+ expect(with_sub.default_command(:sub_command).name).to eq(:sub_command)
83
+ end
84
+
85
+ context "with an alias" do
86
+ before(:each) do
87
+ command_with_parent.alias(:an_alias)
88
+ end
89
+ it "shows the alias in the summary" do
90
+ expect(command_with_parent.summarize).to eql(" i_have_parent, an_alias ")
91
+ end
92
+
93
+ it "its names_and_aliases method reports both the name and alias" do
94
+ expect(command_with_parent.names_and_aliases).to eql("i_have_parent, an_alias")
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe(Mercenary::Option) do
6
+ let(:config_key) { "largo" }
7
+ let(:description) { "This is a description" }
8
+ let(:switches) { ["-l", "--largo"] }
9
+ let(:option) { described_class.new(config_key, [switches, description].flatten.reject(&:nil?)) }
10
+
11
+ it "knows its config key" do
12
+ expect(option.config_key).to eql(config_key)
13
+ end
14
+
15
+ it "knows its description" do
16
+ expect(option.description).to eql(description)
17
+ end
18
+
19
+ it "knows its switches" do
20
+ expect(option.switches).to eql(switches)
21
+ end
22
+
23
+ it "knows how to present itself" do
24
+ expect(option.to_s).to eql(" -l, --largo #{description}")
25
+ end
26
+
27
+ it "has an OptionParser representation" do
28
+ expect(option.for_option_parser).to eql([switches, description].flatten)
29
+ end
30
+
31
+ it "compares itself with other options well" do
32
+ new_option = described_class.new(config_key, ["-l", "--largo", description])
33
+ expect(option.eql?(new_option)).to be(true)
34
+ expect(option.hash.eql?(new_option.hash)).to be(true)
35
+ end
36
+
37
+ it "has a custom #hash" do
38
+ expect(option.hash.to_s).to match(%r!\d+!)
39
+ end
40
+
41
+ context "with just the long switch" do
42
+ let(:switches) { ["--largo"] }
43
+
44
+ it "adds an empty string in place of the short switch" do
45
+ expect(option.switches).to eql(["", "--largo"])
46
+ end
47
+
48
+ it "sets its description properly" do
49
+ expect(option.description).to eql(description)
50
+ end
51
+
52
+ it "knows how to present the switch" do
53
+ expect(option.formatted_switches).to eql(" --largo ")
54
+ end
55
+ end
56
+
57
+ context "with just the short switch" do
58
+ let(:switches) { ["-l"] }
59
+
60
+ it "adds an empty string in place of the long switch" do
61
+ expect(option.switches).to eql(["-l", ""])
62
+ end
63
+
64
+ it "sets its description properly" do
65
+ expect(option.description).to eql(description)
66
+ end
67
+
68
+ it "knows how to present the switch" do
69
+ expect(option.formatted_switches).to eql(" -l ")
70
+ end
71
+ end
72
+
73
+ context "without a description" do
74
+ let(:description) { nil }
75
+
76
+ it "knows there is no description" do
77
+ expect(option.description).to be(nil)
78
+ end
79
+
80
+ it "knows both inputs are switches" do
81
+ expect(option.switches).to eql(switches)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe(Mercenary::Presenter) do
6
+ let(:supercommand) { Mercenary::Command.new(:script_name) }
7
+ let(:command) { Mercenary::Command.new(:subcommand, supercommand) }
8
+ let(:presenter) { described_class.new(command) }
9
+
10
+ before(:each) do
11
+ command.version "1.4.2"
12
+ command.description "Do all the things."
13
+ command.option "one", "-1", "--one", "The first option"
14
+ command.option "two", "-2", "--two", "The second option"
15
+ command.alias :cmd
16
+ supercommand.commands[command.name] = command
17
+ end
18
+
19
+ it "knows how to present the command" do
20
+ expect(presenter.command_presentation).to eql("script_name subcommand 1.4.2 -- Do all the things.\n\nUsage:\n\n script_name subcommand\n\nOptions:\n -1, --one The first option\n -2, --two The second option")
21
+ end
22
+
23
+ it "knows how to present the subcommands, without duplicates for aliases" do
24
+ expect(described_class.new(supercommand).subcommands_presentation).to eql(" subcommand, cmd Do all the things.")
25
+ end
26
+
27
+ it "knows how to present the usage" do
28
+ expect(presenter.usage_presentation).to eql(" script_name subcommand")
29
+ end
30
+
31
+ it "knows how to present the options" do
32
+ expect(presenter.options_presentation).to eql(" -1, --one The first option\n -2, --two The second option")
33
+ end
34
+
35
+ it "allows you to say print_* instead of *_presentation" do
36
+ expect(presenter.print_usage).to eql(presenter.usage_presentation)
37
+ expect(presenter.print_subcommands).to eql(presenter.subcommands_presentation)
38
+ expect(presenter.print_options).to eql(presenter.options_presentation)
39
+ expect(presenter.print_command).to eql(presenter.command_presentation)
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe(Mercenary::Program) do
6
+ context "a basic program" do
7
+ let(:program) { Mercenary::Program.new(:my_name) }
8
+
9
+ it "can be created with just a name" do
10
+ expect(program.name).to eql(:my_name)
11
+ end
12
+
13
+ it "can set its version" do
14
+ version = Mercenary::VERSION
15
+ program.version version
16
+ expect(program.version).to eq(version)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "mercenary"
6
+
7
+ RSpec.configure do |config|
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = "random"
16
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pedrozath-mercenary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.6
5
+ platform: ruby
6
+ authors:
7
+ - Tom Preston-Werner
8
+ - Parker Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.51'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.51'
70
+ description: Lightweight and flexible library for writing command-line apps in Ruby.
71
+ email:
72
+ - tom@mojombo.com
73
+ - parkrmoore@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - History.markdown
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - examples/help_dialogue.rb
88
+ - examples/logging.rb
89
+ - examples/trace.rb
90
+ - lib/mercenary.rb
91
+ - lib/mercenary/command.rb
92
+ - lib/mercenary/option.rb
93
+ - lib/mercenary/presenter.rb
94
+ - lib/mercenary/program.rb
95
+ - lib/mercenary/version.rb
96
+ - mercenary.gemspec
97
+ - script/bootstrap
98
+ - script/cibuild
99
+ - script/console
100
+ - script/examples
101
+ - script/fmt
102
+ - spec/command_spec.rb
103
+ - spec/option_spec.rb
104
+ - spec/presenter_spec.rb
105
+ - spec/program_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/jekyll/mercenary
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.7.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Lightweight and flexible library for writing command-line apps in Ruby.
131
+ test_files:
132
+ - spec/command_spec.rb
133
+ - spec/option_spec.rb
134
+ - spec/presenter_spec.rb
135
+ - spec/program_spec.rb
136
+ - spec/spec_helper.rb