diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2017-06-30 23:53:15 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2017-06-30 23:53:15 -0300 |
commit | 97888f5f91f9675ef2f7809fe1cb423abf6099c8 (patch) | |
tree | d26f2898b93308ba8a3e12b5fd3b363a46a0ef59 /test/test_highline.rb | |
parent | 7a63996d6eaccbe403c724f40a771e4a48c7ea34 (diff) | |
download | highline-97888f5f91f9675ef2f7809fe1cb423abf6099c8.tar.gz |
Rubocop automatic corrections
Diffstat (limited to 'test/test_highline.rb')
-rwxr-xr-x | test/test_highline.rb | 623 |
1 files changed, 310 insertions, 313 deletions
diff --git a/test/test_highline.rb b/test/test_highline.rb index 76ed572..a58d412 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -15,27 +15,25 @@ require "stringio" require "readline" require "tempfile" -=begin -if HighLine::CHARACTER_MODE == "Win32API" - class HighLine - # Override Windows' character reading so it's not tied to STDIN. - def get_character( input = STDIN ) - input.getc - end - end -end -=end +# if HighLine::CHARACTER_MODE == "Win32API" +# class HighLine +# # Override Windows' character reading so it's not tied to STDIN. +# def get_character( input = STDIN ) +# input.getc +# end +# end +# end class TestHighLine < Minitest::Test def setup HighLine.reset @input = StringIO.new @output = StringIO.new - @terminal = HighLine.new(@input, @output) + @terminal = HighLine.new(@input, @output) end - + def test_agree_valid_yes_answers - valid_yes_answers = %w{ y yes Y YES } + valid_yes_answers = %w[y yes Y YES] valid_yes_answers.each do |user_input| @input << "#{user_input}\n" @@ -50,7 +48,7 @@ class TestHighLine < Minitest::Test end def test_agree_valid_no_answers - valid_no_answers = %w{ n no N NO } + valid_no_answers = %w[n no N NO] valid_no_answers.each do |user_input| @input << "#{user_input}\n" @@ -65,7 +63,7 @@ class TestHighLine < Minitest::Test end def test_agree_invalid_answers - invalid_answers = [ "ye", "yuk", "nope", "Oh yes", "Oh no", "Hell no!"] + invalid_answers = ["ye", "yuk", "nope", "Oh yes", "Oh no", "Hell no!"] invalid_answers.each do |user_input| # Each invalid answer, should be followed by a 'y' (as the question is reasked) @@ -89,7 +87,7 @@ class TestHighLine < Minitest::Test assert_equal(true, @terminal.agree("Yes or no? ", :getc)) end - + def test_agree_with_block @input << "\n\n" @input.rewind @@ -97,17 +95,17 @@ class TestHighLine < Minitest::Test assert_equal(true, @terminal.agree("Yes or no? ") { |q| q.default = "y" }) assert_equal(false, @terminal.agree("Yes or no? ") { |q| q.default = "n" }) end - + def test_ask name = "James Edward Gray II" @input << name << "\n" @input.rewind assert_equal(name, @terminal.ask("What is your name? ")) - + assert_raises(EOFError) { @terminal.ask("Any input left? ") } end - + def test_ask_string name = "James Edward Gray II" @input << name << "\n" @@ -138,51 +136,51 @@ class TestHighLine < Minitest::Test def test_indent text = "Testing...\n" - @terminal.indent_level=1 + @terminal.indent_level = 1 @terminal.say(text) - assert_equal(' '*3+text, @output.string) + assert_equal(' ' * 3 + text, @output.string) @output.truncate(@output.rewind) - @terminal.indent_level=3 + @terminal.indent_level = 3 @terminal.say(text) - assert_equal(' '*9+text, @output.string) + assert_equal(' ' * 9 + text, @output.string) @output.truncate(@output.rewind) - @terminal.indent_level=0 - @terminal.indent_size=5 + @terminal.indent_level = 0 + @terminal.indent_size = 5 @terminal.indent(2, text) - assert_equal(' '*10+text, @output.string) + assert_equal(' ' * 10 + text, @output.string) @output.truncate(@output.rewind) - @terminal.indent_level=0 - @terminal.indent_size=4 - @terminal.indent { + @terminal.indent_level = 0 + @terminal.indent_size = 4 + @terminal.indent do @terminal.say(text) - } - assert_equal(' '*4+text, @output.string) + end + assert_equal(' ' * 4 + text, @output.string) @output.truncate(@output.rewind) - @terminal.indent_size=2 - @terminal.indent(3) { |t| + @terminal.indent_size = 2 + @terminal.indent(3) do |t| t.say(text) - } - assert_equal(' '*6+text, @output.string) + end + assert_equal(' ' * 6 + text, @output.string) @output.truncate(@output.rewind) - @terminal.indent { |t| - t.indent { - t.indent { - t.indent { |tt| + @terminal.indent do |t| + t.indent do + t.indent do + t.indent do |tt| tt.say(text) - } - } - } - } - assert_equal(' '*8+text, @output.string) + end + end + end + end + assert_equal(' ' * 8 + text, @output.string) text = "Multi\nLine\nIndentation\n" - indent = ' '*4 - @terminal.indent_level=2 + indent = ' ' * 4 + @terminal.indent_level = 2 @output.truncate(@output.rewind) @terminal.say(text) assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string) @@ -196,7 +194,7 @@ class TestHighLine < Minitest::Test @terminal.indent(0, text, true) assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string) end - + def test_newline @terminal.newline @terminal.newline @@ -208,37 +206,37 @@ class TestHighLine < Minitest::Test @input << "ruby\nRuby\n" @input.rewind - languages = [:Perl, :Python, :Ruby] - answer = @terminal.ask( "What is your favorite programming language? ", - languages ) + languages = %i[Perl Python Ruby] + answer = @terminal.ask("What is your favorite programming language? ", + languages) assert_equal(languages.last, answer) @input.truncate(@input.rewind) @input << "ruby\n" @input.rewind - answer = @terminal.ask( "What is your favorite programming language? ", - languages ) do |q| + answer = @terminal.ask("What is your favorite programming language? ", + languages) do |q| q.case = :capitalize end assert_equal(languages.last, answer) - + # poor auto-complete error message @input.truncate(@input.rewind) @input << "lisp\nruby\n" @input.rewind @output.truncate(@output.rewind) - answer = @terminal.ask( "What is your favorite programming language? ", - languages ) do |q| + answer = @terminal.ask("What is your favorite programming language? ", + languages) do |q| q.case = :capitalize end assert_equal(languages.last, answer) - assert_equal( "What is your favorite programming language? " + - "You must choose one of [Perl, Python, Ruby].\n" + - "? ", @output.string ) + assert_equal("What is your favorite programming language? " \ + "You must choose one of [Perl, Python, Ruby].\n" \ + "? ", @output.string) end - + def test_case_changes @input << "jeg2\n" @input.rewind @@ -302,8 +300,8 @@ class TestHighLine < Minitest::Test @input.rewind @output.truncate(@output.rewind) - answer = @terminal.ask( "Select an option (1, 2 or 3): ", - Integer ) do |q| + answer = @terminal.ask("Select an option (1, 2 or 3): ", + Integer) do |q| q.echo = "*" q.character = true end @@ -312,33 +310,33 @@ class TestHighLine < Minitest::Test end def test_backspace_does_not_enter_prompt - @input << "\b\b" - @input.rewind - answer = @terminal.ask("Please enter your password: ") do |q| - q.echo = "*" - end - assert_equal("", answer) - assert_equal("Please enter your password: \n", @output.string) + @input << "\b\b" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + assert_equal("Please enter your password: \n", @output.string) end def test_after_some_chars_backspace_does_not_enter_prompt_when_ascii - @input << "apple\b\b\b\b\b\b\b\b\b\b" - @input.rewind - answer = @terminal.ask("Please enter your password: ") do |q| - q.echo = "*" - end - assert_equal("", answer) - assert_equal("apple".size, @output.string.count("\b")) + @input << "apple\b\b\b\b\b\b\b\b\b\b" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + assert_equal("apple".size, @output.string.count("\b")) end def test_after_some_chars_backspace_does_not_enter_prompt_when_utf8 - @input << "maçã\b\b\b\b\b\b\b\b" - @input.rewind - answer = @terminal.ask("Please enter your password: ") do |q| - q.echo = "*" - end - assert_equal("", answer) - assert_equal("maçã".size, @output.string.count("\b")) + @input << "maçã\b\b\b\b\b\b\b\b" + @input.rewind + answer = @terminal.ask("Please enter your password: ") do |q| + q.echo = "*" + end + assert_equal("", answer) + assert_equal("maçã".size, @output.string.count("\b")) end def test_readline_mode @@ -353,7 +351,7 @@ class TestHighLine < Minitest::Test terminal = @terminal.terminal - if terminal.jruby? or terminal.rubinius? or terminal.windows? + if terminal.jruby? || terminal.rubinius? || terminal.windows? skip "We can't test Readline on JRuby, Rubinius and Windows yet" end @@ -427,7 +425,7 @@ class TestHighLine < Minitest::Test assert_equal("you can't see me", answer) assert_equal("Please enter some hidden text: ****************\n", @output.string) end - + def test_character_reading # WARNING: This method does NOT cover Unix and Windows savvy testing! @input << "12345" @@ -450,10 +448,10 @@ class TestHighLine < Minitest::Test @output.truncate(@output.rewind) - @terminal.say( "This should be " + - "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" ) - assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n", - @output.string ) + @terminal.say("This should be " \ + "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!") + assert_equal("This should be \e[1m\e[47mbold on white\e[0m!\n", + @output.string) @output.truncate(@output.rewind) @@ -462,10 +460,10 @@ class TestHighLine < Minitest::Test @output.truncate(@output.rewind) - @terminal.say( "This should be " + - "<%= color('blinking on red', :blink, :on_red) %>!" ) - assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n", - @output.string ) + @terminal.say("This should be " \ + "<%= color('blinking on red', :blink, :on_red) %>!") + assert_equal("This should be \e[5m\e[41mblinking on red\e[0m!\n", + @output.string) @output.truncate(@output.rewind) @@ -484,18 +482,18 @@ class TestHighLine < Minitest::Test # Relying on const_missing assert_instance_of HighLine::Style, HighLine::ON_RGB_C06031_STYLE - assert_instance_of String , HighLine::ON_RGB_C06032 - assert_raises(NameError) { HighLine::ON_RGB_ZZZZZZ } + assert_instance_of String, HighLine::ON_RGB_C06032 + assert_raises(NameError) { HighLine::ON_RGB_ZZZZZZ } # Retrieving color_code from a style assert_equal "\e[41m", @terminal.color_code([HighLine::ON_RED_STYLE]) @output.truncate(@output.rewind) - + # Does class method work, too? @terminal.say("This should be <%= HighLine.color('reverse underlined magenta', :reverse, :underline, :magenta) %>!") - assert_equal( "This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n", - @output.string ) + assert_equal("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n", + @output.string) @output.truncate(@output.rewind) @@ -592,7 +590,7 @@ class TestHighLine < Minitest::Test assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string) cli.say("This should be <%= color('cyan', CYAN) %>!") - assert_equal("This should be cyan!\n", cli_output.string ) + assert_equal("This should be cyan!\n", cli_output.string) gterm_output.truncate(gterm_output.rewind) @output.truncate(@output.rewind) @@ -619,23 +617,20 @@ class TestHighLine < Minitest::Test def test_uncolor # instance method - assert_equal( "This should be reverse underlined magenta!\n", - @terminal.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n") - ) + assert_equal("This should be reverse underlined magenta!\n", + @terminal.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")) @output.truncate(@output.rewind) # class method - assert_equal( "This should be reverse underlined magenta!\n", - HighLine.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n") - ) + assert_equal("This should be reverse underlined magenta!\n", + HighLine.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")) @output.truncate(@output.rewind) # RGB color - assert_equal( "This should be rgb_906030!\n", - @terminal.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n") - ) + assert_equal("This should be rgb_906030!\n", + @terminal.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n")) end def test_grey_is_the_same_of_gray @@ -661,7 +656,7 @@ class TestHighLine < Minitest::Test assert_equal bright_blue_code, light_blue_code end - + def test_confirm @input << "junk.txt\nno\nsave.txt\ny\n" @input.rewind @@ -671,11 +666,11 @@ class TestHighLine < Minitest::Test q.responses[:ask_on_error] = :question end assert_equal("save.txt", answer) - assert_equal( "Enter a filename: " + - "Are you sure you want to overwrite junk.txt? " + - "Enter a filename: " + + assert_equal("Enter a filename: " \ + "Are you sure you want to overwrite junk.txt? " \ + "Enter a filename: " \ "Are you sure you want to overwrite save.txt? ", - @output.string ) + @output.string) @input.truncate(@input.rewind) @input << "junk.txt\nyes\nsave.txt\nn\n" @@ -686,9 +681,9 @@ class TestHighLine < Minitest::Test q.confirm = "Are you sure you want to overwrite <%= answer %>? " end assert_equal("junk.txt", answer) - assert_equal( "Enter a filename: " + + assert_equal("Enter a filename: " \ "Are you sure you want to overwrite junk.txt? ", - @output.string ) + @output.string) @input.truncate(@input.rewind) @input << "junk.txt\nyes\nsave.txt\nn\n" @@ -697,18 +692,18 @@ class TestHighLine < Minitest::Test scoped_variable = { "junk.txt" => '20mb' } answer = @terminal.ask("Enter a filename: ") do |q| - q.confirm = Proc.new do |answer| - "Are you sure you want to overwrite #{answer} with size " + - "of #{scoped_variable[answer]}? " + q.confirm = proc do |answer| + "Are you sure you want to overwrite #{answer} with size " \ + "of #{scoped_variable[answer]}? " end end assert_equal("junk.txt", answer) - assert_equal( "Enter a filename: " + - "Are you sure you want to overwrite junk.txt " + + assert_equal("Enter a filename: " \ + "Are you sure you want to overwrite junk.txt " \ "with size of 20mb? ", - @output.string ) + @output.string) end - + def test_generic_confirm_with_true @input << "junk.txt\nno\nsave.txt\ny\n" @input.rewind @@ -718,11 +713,11 @@ class TestHighLine < Minitest::Test q.responses[:ask_on_error] = :question end assert_equal("save.txt", answer) - assert_equal( "Enter a filename: " + - "Are you sure? " + - "Enter a filename: " + + assert_equal("Enter a filename: " \ + "Are you sure? " \ + "Enter a filename: " \ "Are you sure? ", - @output.string ) + @output.string) @input.truncate(@input.rewind) @input << "junk.txt\nyes\nsave.txt\nn\n" @@ -733,9 +728,9 @@ class TestHighLine < Minitest::Test q.confirm = true end assert_equal("junk.txt", answer) - assert_equal( "Enter a filename: " + + assert_equal("Enter a filename: " \ "Are you sure? ", - @output.string ) + @output.string) end def test_defaults @@ -757,8 +752,8 @@ class TestHighLine < Minitest::Test q.validate = /\Ay(?:es)?|no?|no comment\Z/i end assert_equal("No Comment", answer) - assert_equal( "Are you sexually active? |No Comment| ", - @output.string ) + assert_equal("Are you sexually active? |No Comment| ", + @output.string) end def test_default_with_String @@ -828,24 +823,24 @@ class TestHighLine < Minitest::Test end assert_equal("yes", answer) end - + def test_erb - @terminal.say( "The integers from 1 to 10 are:\n" + - "% (1...10).each do |n|\n" + - "\t<%= n %>,\n" + - "% end\n" + - "\tand 10" ) - assert_equal( "The integers from 1 to 10 are:\n" + - "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" + + @terminal.say("The integers from 1 to 10 are:\n" \ + "% (1...10).each do |n|\n" \ + "\t<%= n %>,\n" \ + "% end\n" \ + "\tand 10") + assert_equal("The integers from 1 to 10 are:\n" \ + "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" \ "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n", - @output.string ) + @output.string) end - + def test_files @input << "#{File.basename(__FILE__)[0, 7]}\n" @input.rewind - - assert_equal "test_hi\n",@input.read + + assert_equal "test_hi\n", @input.read @input.rewind file = @terminal.ask("Select a file: ", File) do |q| @@ -865,7 +860,7 @@ class TestHighLine < Minitest::Test assert_instance_of(Pathname, pathname) assert_equal(File.size(__FILE__), pathname.size) end - + def test_gather_with_integer @input << "James\nDana\nStorm\nGypsy\n\n" @input.rewind @@ -873,7 +868,7 @@ class TestHighLine < Minitest::Test answers = @terminal.ask("Enter four names:") do |q| q.gather = 4 end - assert_equal(%w{James Dana Storm Gypsy}, answers) + assert_equal(%w[James Dana Storm Gypsy], answers) assert_equal("\n", @input.gets) assert_equal("Enter four names:\n", @output.string) end @@ -885,7 +880,7 @@ class TestHighLine < Minitest::Test answers = @terminal.ask("Enter four names:") do |q| q.gather = "" end - assert_equal(%w{James Dana Storm Gypsy}, answers) + assert_equal(%w[James Dana Storm Gypsy], answers) end def test_gather_with_regexp @@ -895,7 +890,7 @@ class TestHighLine < Minitest::Test answers = @terminal.ask("Enter four names:") do |q| q.gather = /^\s*$/ end - assert_equal(%w{James Dana Storm Gypsy}, answers) + assert_equal(%w[James Dana Storm Gypsy], answers) end def test_gather_with_hash @@ -903,10 +898,10 @@ class TestHighLine < Minitest::Test @input.rewind answers = @terminal.ask("<%= key %>: ", Integer) do |q| - q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0} + q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0 } end - assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49}, - answers ) + assert_equal({ "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49 }, + answers) assert_equal("Age: Father's Age: Wife's Age: ", @output.string) end @@ -943,7 +938,7 @@ class TestHighLine < Minitest::Test answer = @terminal.ask("<%= key %>: ") do |q| q.verify_match = true - q.gather = {"Enter a password" => '', "Please type it again" => ''} + q.gather = { "Enter a password" => '', "Please type it again" => '' } end assert_equal("Password", answer) @@ -956,19 +951,19 @@ class TestHighLine < Minitest::Test q.verify_match = true q.responses[:mismatch] = 'Typing mismatch!' q.responses[:ask_on_error] = '' - q.gather = {"Enter a password" => '', "Please type it again" => ''} + q.gather = { "Enter a password" => '', "Please type it again" => '' } end assert_equal("Password", answer) - assert_equal( "Enter a password: " + - "Please type it again: " + - "Typing mismatch!\n" + - "Enter a password: " + - "Please type it again: ", @output.string ) + assert_equal("Enter a password: " \ + "Please type it again: " \ + "Typing mismatch!\n" \ + "Enter a password: " \ + "Please type it again: ", @output.string) end def test_lists - digits = %w{Zero One Two Three Four Five Six Seven Eight Nine} + digits = %w[Zero One Two Three Four Five Six Seven Eight Nine] erb_digits = digits.dup erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>" @@ -978,96 +973,96 @@ class TestHighLine < Minitest::Test @output.truncate(@output.rewind) @terminal.say("<%= list(#{digits.inspect}, :inline) %>") - assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n", - @output.string ) + assert_equal(digits[0..-2].join(", ") + " or #{digits.last}\n", + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>") - assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n", - @output.string ) + assert_equal(digits[0..-2].join(", ") + " and #{digits.last}\n", + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>") - assert_equal( "Zero Four Eight\n" + - "One Five Nine \n" + - "Two Six \n" + + assert_equal("Zero Four Eight\n" \ + "One Five Nine \n" \ + "Two Six \n" \ "Three Seven\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>") - assert_equal( "Zero Four Eight\n" + - "One \e[34mFive\e[0m Nine \n" + - "Two Six \n" + + assert_equal("Zero Four Eight\n" \ + "One \e[34mFive\e[0m Nine \n" \ + "Two Six \n" \ "Three Seven\n", - @output.string ) + @output.string) colums_of_twenty = ["12345678901234567890"] * 5 - + @output.truncate(@output.rewind) @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>") - assert_equal( "12345678901234567890 12345678901234567890 " + - "12345678901234567890\n" + + assert_equal("12345678901234567890 12345678901234567890 " \ + "12345678901234567890\n" \ "12345678901234567890 12345678901234567890\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>") - assert_equal( "Zero One Two \n" + - "Three Four Five \n" + - "Six Seven Eight\n" + + assert_equal("Zero One Two \n" \ + "Three Four Five \n" \ + "Six Seven Eight\n" \ "Nine \n", - @output.string ) - + @output.string) + colums_of_twenty.pop @output.truncate(@output.rewind) @terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>") - assert_equal( "12345678901234567890 12345678901234567890 " + - "12345678901234567890\n" + + assert_equal("12345678901234567890 12345678901234567890 " \ + "12345678901234567890\n" \ "12345678901234567890\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) - + wide = %w[0123456789 a b c d e f g h i j k l m n o p q r s t u v w x y z] @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across ) %>") - assert_equal( "0123456789 a b c d e f g h i j k l m n o " + - "p q r s t u v w\n" + + assert_equal("0123456789 a b c d e f g h i j k l m n o " \ + "p q r s t u v w\n" \ "x y z\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across, 10 ) %>") - assert_equal( "0123456789 a b c d e f g h i\n" + - "j k l m n o p q r s\n" + + assert_equal("0123456789 a b c d e f g h i\n" \ + "j k l m n o p q r s\n" \ "t u v w x y z\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down ) %>") - assert_equal( "0123456789 b d f h j l n p r t v x z\n" + + assert_equal("0123456789 b d f h j l n p r t v x z\n" \ "a c e g i k m o q s u w y\n", - @output.string ) + @output.string) @output.truncate(@output.rewind) @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down, 10 ) %>") - assert_equal( "0123456789 c f i l o r u x\n" + - "a d g j m p s v y\n" + + assert_equal("0123456789 c f i l o r u x\n" \ + "a d g j m p s v y\n" \ "b e h k n q t w z\n", - @output.string ) + @output.string) end - + def test_lists_with_zero_items modes = [nil, :rows, :inline, :columns_across, :columns_down] modes.each do |mode| @@ -1083,70 +1078,72 @@ class TestHighLine < Minitest::Test assert_equal("\n", result) end end - + def test_lists_with_one_item items = ['Zero'] - modes = { nil => "Zero\n", - :rows => "Zero\n", - :inline => "Zero", - :columns_across => "Zero\n", - :columns_down => "Zero\n" } - + modes = { nil => "Zero\n", + :rows => "Zero\n", + :inline => "Zero", + :columns_across => "Zero\n", + :columns_down => "Zero\n" } + modes.each do |mode, expected| result = @terminal.list(items, mode) assert_equal(expected, result) end end - + def test_lists_with_two_items - items = ['Zero', 'One'] - modes = { nil => "Zero\nOne\n", - :rows => "Zero\nOne\n", - :inline => "Zero or One", - :columns_across => "Zero One \n", - :columns_down => "Zero One \n" } - + items = %w[Zero One] + modes = { nil => "Zero\nOne\n", + :rows => "Zero\nOne\n", + :inline => "Zero or One", + :columns_across => "Zero One \n", + :columns_down => "Zero One \n" } + modes.each do |mode, expected| result = @terminal.list(items, mode) assert_equal(expected, result) end end - + def test_lists_with_three_items - items = ['Zero', 'One', 'Two'] - modes = { nil => "Zero\nOne\nTwo\n", - :rows => "Zero\nOne\nTwo\n", - :inline => "Zero, One or Two", - :columns_across => "Zero One Two \n", - :columns_down => "Zero One Two \n" } + items = %w[Zero One Two] + modes = { nil => "Zero\nOne\nTwo\n", + :rows => "Zero\nOne\nTwo\n", + :inline => "Zero, One or Two", + :columns_across => "Zero One Two \n", + :columns_down => "Zero One Two \n" } modes.each do |mode, expected| result = @terminal.list(items, mode) assert_equal(expected, result) end end - + def test_mode assert(%w[HighLine::Terminal::IOConsole HighLine::Terminal::NCurses HighLine::Terminal::UnixStty].include?(@terminal.terminal.character_mode), "#{@terminal.terminal.character_mode} not in list") end - + class NameClass - def self.parse( string ) + def self.parse(string) if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/ - self.new($2, $3, $1) + new(Regexp.last_match(2), Regexp.last_match(3), Regexp.last_match(1)) else raise ArgumentError, "Invalid name format." end end def initialize(first, middle, last) - @first, @middle, @last = first, middle, last + @first = first + @middle = middle + @last = last end - + attr_reader :first, :middle, :last end - + def test_my_class_conversion @input << "Gray, James Edward\n" @input.rewind @@ -1164,7 +1161,7 @@ class TestHighLine < Minitest::Test assert_equal("James", answer.first) assert_equal("Edward", answer.middle) end - + def test_no_echo @input << "password\r" @input.rewind @@ -1177,7 +1174,7 @@ class TestHighLine < Minitest::Test @input.rewind @output.truncate(@output.rewind) - + answer = @terminal.ask("Pick a letter or number: ") do |q| q.character = true q.echo = false @@ -1186,7 +1183,7 @@ class TestHighLine < Minitest::Test assert_equal("a", @input.getc.chr) assert_equal("Pick a letter or number: \n", @output.string) end - + def test_correct_string_encoding_when_echo_false @input << "ação\r" # An UTF-8 portuguese word for 'action' @input.rewind @@ -1234,7 +1231,7 @@ class TestHighLine < Minitest::Test assert_equal("Type: ****\n", @output.string) assert_equal("maçã", answer) end - + def test_range_requirements @input << "112\n-541\n28\n" @input.rewind @@ -1243,13 +1240,13 @@ class TestHighLine < Minitest::Test q.in = 0..105 end assert_equal(28, answer) - assert_equal( "Tell me your age.\n" + - "Your answer isn't within the expected range " + - "(included in 0..105).\n" + - "? " + - "Your answer isn't within the expected range " + - "(included in 0..105).\n" + - "? ", @output.string ) + assert_equal("Tell me your age.\n" \ + "Your answer isn't within the expected range " \ + "(included in 0..105).\n" \ + "? " \ + "Your answer isn't within the expected range " \ + "(included in 0..105).\n" \ + "? ", @output.string) @input.truncate(@input.rewind) @input << "1\n-541\n28\n" @@ -1260,13 +1257,13 @@ class TestHighLine < Minitest::Test q.above = 3 end assert_equal(28, answer) - assert_equal( "Tell me your age.\n" + - "Your answer isn't within the expected range " + - "(above 3).\n" + - "? " + - "Your answer isn't within the expected range " + - "(above 3).\n" + - "? ", @output.string ) + assert_equal("Tell me your age.\n" \ + "Your answer isn't within the expected range " \ + "(above 3).\n" \ + "? " \ + "Your answer isn't within the expected range " \ + "(above 3).\n" \ + "? ", @output.string) @input.truncate(@input.rewind) @input << "1\n28\n-541\n" @@ -1277,13 +1274,13 @@ class TestHighLine < Minitest::Test q.below = 0 end assert_equal(-541, answer) - assert_equal( "Lowest numer you can think of?\n" + - "Your answer isn't within the expected range " + - "(below 0).\n" + - "? " + - "Your answer isn't within the expected range " + - "(below 0).\n" + - "? ", @output.string ) + assert_equal("Lowest numer you can think of?\n" \ + "Your answer isn't within the expected range " \ + "(below 0).\n" \ + "? " \ + "Your answer isn't within the expected range " \ + "(below 0).\n" \ + "? ", @output.string) @input.truncate(@input.rewind) @input << "-541\n11\n6\n" @@ -1295,13 +1292,13 @@ class TestHighLine < Minitest::Test q.below = 10 end assert_equal(6, answer) - assert_equal( "Enter a low even number: " + - "Your answer isn't within the expected range " + - "(above 0 and below 10).\n" + - "? " + - "Your answer isn't within the expected range " + - "(above 0 and below 10).\n" + - "? ", @output.string ) + assert_equal("Enter a low even number: " \ + "Your answer isn't within the expected range " \ + "(above 0 and below 10).\n" \ + "? " \ + "Your answer isn't within the expected range " \ + "(above 0 and below 10).\n" \ + "? ", @output.string) @input.truncate(@input.rewind) @input << "1\n-541\n6\n" @@ -1314,27 +1311,27 @@ class TestHighLine < Minitest::Test q.in = [2, 4, 6, 8] end assert_equal(6, answer) - assert_equal( "Enter a low even number: " + - "Your answer isn't within the expected range " + - "(above 0, below 10, and included in [2, 4, 6, 8]).\n" + - "? " + - "Your answer isn't within the expected range " + - "(above 0, below 10, and included in [2, 4, 6, 8]).\n" + - "? ", @output.string ) - end - + assert_equal("Enter a low even number: " \ + "Your answer isn't within the expected range " \ + "(above 0, below 10, and included in [2, 4, 6, 8]).\n" \ + "? " \ + "Your answer isn't within the expected range " \ + "(above 0, below 10, and included in [2, 4, 6, 8]).\n" \ + "? ", @output.string) + end + def test_reask - number = 61676 + number = 61_676 @input << "Junk!\n" << number << "\n" @input.rewind answer = @terminal.ask("Favorite number? ", Integer) assert_kind_of(Integer, number) - assert_instance_of(Fixnum, number) + assert_instance_of(Integer, number) assert_equal(number, answer) - assert_equal( "Favorite number? " + - "You must enter a valid Integer.\n" + - "? ", @output.string ) + assert_equal("Favorite number? " \ + "You must enter a valid Integer.\n" \ + "? ", @output.string) @input.rewind @output.truncate(@output.rewind) @@ -1344,43 +1341,43 @@ class TestHighLine < Minitest::Test q.responses[:invalid_type] = "Not a valid number!" end assert_kind_of(Integer, number) - assert_instance_of(Fixnum, number) + assert_instance_of(Integer, number) assert_equal(number, answer) - assert_equal( "Favorite number? " + - "Not a valid number!\n" + - "Favorite number? ", @output.string ) + assert_equal("Favorite number? " \ + "Not a valid number!\n" \ + "Favorite number? ", @output.string) @input.truncate(@input.rewind) @input << "gen\ngene\n" @input.rewind @output.truncate(@output.rewind) - answer = @terminal.ask("Select a mode: ", [:generate, :gentle]) + answer = @terminal.ask("Select a mode: ", %i[generate gentle]) assert_instance_of(Symbol, answer) assert_equal(:generate, answer) - assert_equal( "Select a mode: " + - "Ambiguous choice. " + - "Please choose one of [generate, gentle].\n" + - "? ", @output.string ) + assert_equal("Select a mode: " \ + "Ambiguous choice. " \ + "Please choose one of [generate, gentle].\n" \ + "? ", @output.string) end - + def test_response_embedding @input << "112\n-541\n28\n" @input.rewind answer = @terminal.ask("Tell me your age.", Integer) do |q| q.in = 0..105 - q.responses[:not_in_range] = "Need a #{q.answer_type}" + + q.responses[:not_in_range] = "Need a #{q.answer_type}" \ " #{q.expected_range}." end assert_equal(28, answer) - assert_equal( "Tell me your age.\n" + - "Need a Integer included in 0..105.\n" + - "? " + - "Need a Integer included in 0..105.\n" + - "? ", @output.string ) + assert_equal("Tell me your age.\n" \ + "Need a Integer included in 0..105.\n" \ + "? " \ + "Need a Integer included in 0..105.\n" \ + "? ", @output.string) end - + def test_say @terminal.say("This will have a newline.") assert_equal("This will have a newline.\n", @output.string) @@ -1401,7 +1398,7 @@ class TestHighLine < Minitest::Test assert_equal("This will not have a newline.\t", @output.string) @output.truncate(@output.rewind) - + @terminal.say("This will not\n end with a newline. ") assert_equal("This will not\n end with a newline. ", @output.string) @@ -1430,7 +1427,7 @@ class TestHighLine < Minitest::Test def test_say_handles_non_string_argument integer = 10 - hash = { :a => 20 } + hash = { a: 20 } @terminal.say(integer) assert_equal String(integer), @output.string.chomp @@ -1442,20 +1439,20 @@ class TestHighLine < Minitest::Test end def test_terminal_size - assert_instance_of(Fixnum, @terminal.terminal.terminal_size[0]) - assert_instance_of(Fixnum, @terminal.terminal.terminal_size[1]) + assert_instance_of(Integer, @terminal.terminal.terminal_size[0]) + assert_instance_of(Integer, @terminal.terminal.terminal_size[1]) end def test_type_conversion - number = 61676 + number = 61_676 @input << number << "\n" @input.rewind answer = @terminal.ask("Favorite number? ", Integer) assert_kind_of(Integer, answer) - assert_instance_of(Fixnum, answer) + assert_instance_of(Integer, answer) assert_equal(number, answer) - + @input.truncate(@input.rewind) number = 1_000_000_000_000_000_000_000_000_000_000 @input << number << "\n" @@ -1463,7 +1460,7 @@ class TestHighLine < Minitest::Test answer = @terminal.ask("Favorite number? ", Integer) assert_kind_of(Integer, answer) - assert_instance_of(Bignum, answer) + assert_instance_of(Integer, answer) assert_equal(number, answer) @input.truncate(@input.rewind) @@ -1471,10 +1468,10 @@ class TestHighLine < Minitest::Test @input << number << "\n" @input.rewind - answer = @terminal.ask( "Favorite number? ", - lambda { |n| n.to_f.abs.round } ) + answer = @terminal.ask("Favorite number? ", + ->(n) { n.to_f.abs.round }) assert_kind_of(Integer, answer) - assert_instance_of(Fixnum, answer) + assert_instance_of(Integer, answer) assert_equal(11, answer) @input.truncate(@input.rewind) @@ -1509,11 +1506,11 @@ class TestHighLine < Minitest::Test @input << "gen\n" @input.rewind - answer = @terminal.ask("Select a mode: ", [:generate, :run]) + answer = @terminal.ask("Select a mode: ", %i[generate run]) assert_instance_of(Symbol, answer) assert_equal(:generate, answer) end - + def test_validation @input << "system 'rm -rf /'\n105\n0b101_001\n" @input.rewind @@ -1522,17 +1519,17 @@ class TestHighLine < Minitest::Test q.validate = /\A(?:0b)?[01_]+\Z/ end assert_equal("0b101_001", answer) - assert_equal( "Enter a binary number: " + - "Your answer isn't valid " + - "(must match /\\A(?:0b)?[01_]+\\Z/).\n" + - "? " + - "Your answer isn't valid " + - "(must match /\\A(?:0b)?[01_]+\\Z/).\n" + - "? ", @output.string ) + assert_equal("Enter a binary number: " \ + "Your answer isn't valid " \ + "(must match /\\A(?:0b)?[01_]+\\Z/).\n" \ + "? " \ + "Your answer isn't valid " \ + "(must match /\\A(?:0b)?[01_]+\\Z/).\n" \ + "? ", @output.string) @input.truncate(@input.rewind) - @input << "Gray II, James Edward\n" + - "Gray, Dana Ann Leslie\n" + + @input << "Gray II, James Edward\n" \ + "Gray, Dana Ann Leslie\n" \ "Gray, James Edward\n" @input.rewind @@ -1546,11 +1543,11 @@ class TestHighLine < Minitest::Test end assert_equal("Gray, James Edward", answer) end - + def test_whitespace @input << " A lot\tof \t space\t \there! \n" @input.rewind - + answer = @terminal.ask("Enter a whitespace filled string: ") do |q| q.whitespace = :chomp end @@ -1603,7 +1600,7 @@ class TestHighLine < Minitest::Test end assert_equal(" A lot\tof \t space\t \there! \n", answer) end - + def test_track_eof assert_raises(EOFError) { @terminal.ask("Any input left? ") } @@ -1613,13 +1610,13 @@ class TestHighLine < Minitest::Test HighLine.track_eof = false begin require 'highline/import' - ask("And now? ") # this will still blow up, nothing available + ask("And now? ") # this will still blow up, nothing available rescue - refute_equal(EOFError, $!.class) # but HighLine's safe guards are off + refute_equal(EOFError, $ERROR_INFO.class) # but HighLine's safe guards are off end HighLine.default_instance = old_instance end - + def test_version refute_nil(HighLine::VERSION) assert_instance_of(String, HighLine::VERSION) |