Skip to content

Instantly share code, notes, and snippets.

@Kuz-man
Created November 8, 2016 16:33
Show Gist options
  • Save Kuz-man/414a3b568804dfe1c5a943ee3fcdedfa to your computer and use it in GitHub Desktop.
Save Kuz-man/414a3b568804dfe1c5a943ee3fcdedfa to your computer and use it in GitHub Desktop.
Task 03 tests
describe CommandParser do
describe '#argument' do
it 'adds an argument' do
parser = CommandParser.new('rspec')
parser.argument('FILE') do |runner, value|
runner[:file] = value
end
command_runner = {}
parser.parse(command_runner, ['spec.rb'])
expect(command_runner).to eq({file: 'spec.rb'})
end
it 'adds an argument as in the example' do
parser = CommandParser.new('rspec')
parser.argument('FILE') do |runner, value|
runner[:file] = value
end
command_runner = {}
parser.parse(command_runner, ['spec.rb'])
expect(command_runner).to eq ({file: 'spec.rb'})
parser8 = CommandParser.new('rspec')
parser8.argument('FILE') do |runner, value|
runner[:larodi] = value
end
command_runner = {}
parser8.parse(command_runner, ['spec.rb'])
expect(command_runner).to eq ({larodi: 'spec.rb'})
end
it 'adds a couple of arguments' do
parser2 = CommandParser.new('rspec')
parser2.argument('FIRST') do |runner, value|
runner[:first] = value
end
parser2.argument('SECOND') do |runner, value|
runner[:second] = value
end
command_runner = {}
parser2.parse(command_runner, ['foo', 'larodi'])
expect(command_runner).to eq ({first: 'foo', second: 'larodi'})
end
it 'adds a couple of arguments with valid names' do
parser = CommandParser.new('rspec')
parser.argument('FIRST') do |runner, value|
runner[:first] = value
end
parser.argument('SECOND') do |runner, value|
runner[:second] = value
end
command_runner = {}
parser.parse(command_runner, ['foo', '-b', '--gaga', 'larodi', '--batman'])
expect(command_runner).to eq ({first: 'foo', second: 'larodi'})
end
end
describe '#option' do
it 'adds an option' do
parser = CommandParser.new('rspec')
parser.option('v', 'version', 'show version number') do |runner, value|
runner[:version] = value
end
command_runner = {}
parser.parse(command_runner, ['--version'])
expect(command_runner).to eq({version: true})
end
it 'adds an option as in the example' do
parser3 = CommandParser.new('rspec')
parser3.option('v', 'version', 'show version number') do |runner, value|
runner[:version] = value
end
command_runner = {}
parser3.parse(command_runner, ['--version'])
command_runner #=> {version: true}
command_runner = {}
parser3.parse(command_runner, ['-v'])
expect(command_runner).to eq ({version: true})
end
it 'returns empty Hash if argv contains no valid members' do
parser = CommandParser.new('rspec')
parser.option('v', 'version', 'show version number') do |runner, value|
runner[:version] = value
end
command_runner = {}
parser.parse(command_runner, ['--random_stuff'])
expect(command_runner).to eq Hash.new
end
end
describe '#option_with_parameter' do
it 'adds an option with argument' do
parser = CommandParser.new('rspec')
parser.option_with_parameter('r', 'require', 'require FILE in spec', 'FILE') do |runner, value|
runner[:username] = value
end
command_runner = {}
parser.parse(command_runner, ['--require=solution.rb'])
expect(command_runner).to eq({username: 'solution.rb'})
end
it 'adds an option with parameters as in the example' do
parser3 = CommandParser.new('rspec')
parser3.option_with_parameter('r', 'require', 'require FILE in spec', 'FILE') do |runner, value|
runner[:require] = value
end
command_runner = {}
parser3.parse(command_runner, ['--require=solution.rb'])
command_runner #=> {require: 'solution.rb'}
command_runner = {}
parser3.parse(command_runner, ['-rsolution.rb'])
expect(command_runner).to eq ({require: 'solution.rb'})
end
end
describe '#help' do
it 'returns a properly formatted help message' do
parser = CommandParser.new('rspec')
parser.argument('SPEC FILE') { |_, _| }
parser.option('v', 'verbose', 'Verbose mode') { |_, _| }
parser.option_with_parameter('r', 'require', 'require FILE in spec', 'FILE') { |_, _| }
header = parser.help.lines.first.chomp
options_help_messages = parser.help.lines.map(&:chomp).drop(1)
expect(header).to eq 'Usage: rspec [SPEC FILE]'
expect(options_help_messages).to match_array([
' -v, --verbose Verbose mode',
' -r, --require=FILE require FILE in spec',
])
end
it 'returns a properly formatted help message as in the examples' do
parser = CommandParser.new('rspec')
parser.argument('FIRST') { |_, _| }
parser.argument('SECOND') { |_, _| }
expect(parser.help).to eq 'Usage: rspec [FIRST] [SECOND]'
end
it 'returns a properly formatted argument/option_with_parameters mashup' do
parser = CommandParser.new('rspec')
parser.argument('FILE') do |runner, value|
runner[:file] = value
end
parser.option_with_parameter('r', 'require', 'require FILE in spec', 'FILE') do |runner, value|
runner[:file] = value
end
header = parser.help.lines.first.chomp
options_help_messages = parser.help.lines.map(&:chomp).drop(1)
expect(header).to eq 'Usage: rspec [FILE]'
expect(options_help_messages).to match_array([
' -r, --require=FILE require FILE in spec'
])
end
it 'returns a properly formatted mess of arguments/options/parameters' do
parser = CommandParser.new('rspec')
parser.argument('SPEC FILE') { |_, _| }
parser.option('v', 'verbose', 'Verbose mode') { |_, _| }
parser.option_with_parameter('r', 'require', 'require FILE in spec', 'FILE') { |_, _| }
parser.argument('SOMETHING ELSE') { |_, _| }
parser.option_with_parameter('e', 'eins', 'zwei', 'polizei') { |_, _| }
header = parser.help.lines.first.chomp
options_help_messages = parser.help.lines.map(&:chomp).drop(1)
expect(header).to eq 'Usage: rspec [SPEC FILE] [SOMETHING ELSE]'
expect(options_help_messages).to match_array([
' -v, --verbose Verbose mode',
' -r, --require=FILE require FILE in spec',
' -e, --eins=polizei zwei'
])
end
it 'returns a properly formatted mess of arguments/options/parameters v2' do
parser = CommandParser.new('rspec')
parser.argument('SPEC FILE') do |runner, value|
runner[:file] = value
end
parser.option('v', 'verbose', 'Verbose mode') do |runner, value|
runner[:foo] = value
end
parser.argument('SOMETHING ELSE') do |runner, value|
runner[:bar] = value
end
parser.option_with_parameter('e', 'eins', 'zwei', 'polizei') do |runner, value|
runner[:larodi] = value
end
command_runner = {}
parser.parse(command_runner, ['arg_one', '-v', 'SOMETHING ELSE', '--eins=test'])
expect(command_runner).to eq ({:file=>"arg_one", :foo=>true, :bar=>"SOMETHING ELSE", :larodi=>"test"})
header = parser.help.lines.first.chomp
options_help_messages = parser.help.lines.map(&:chomp).drop(1)
expect(header).to eq 'Usage: rspec [SPEC FILE] [SOMETHING ELSE]'
expect(options_help_messages).to match_array([
' -v, --verbose Verbose mode',
' -e, --eins=polizei zwei'
])
end
it 'returns a properly formatted mess of arguments/options/parameters v3' do
parser = CommandParser.new('rspec')
parser.argument('SPEC FILE') do |runner, value|
runner[:file] = value
end
parser.option('v', 'verbose', 'Verbose mode') do |runner, value|
runner[:foo] = value
end
parser.argument('SOMETHING ELSE') do |runner, value|
runner[:bar] = value
end
parser.option_with_parameter('e', 'eins', 'zwei', 'polizei') do |runner, value|
runner[:larodi] = value
end
command_runner = {}
parser.parse(command_runner, ['arg_one', '--list', '--verbose', '-arg_two', 'neverland', '-abc', '-enothing', 'nothing_at_all'])
expect(command_runner).to eq ({:file=>"arg_one", :foo=>true, :bar=>"neverland", :larodi=>"nothing"})
header = parser.help.lines.first.chomp
options_help_messages = parser.help.lines.map(&:chomp).drop(1)
expect(header).to eq 'Usage: rspec [SPEC FILE] [SOMETHING ELSE]'
expect(options_help_messages).to match_array([
' -v, --verbose Verbose mode',
' -e, --eins=polizei zwei'
])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment