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 | |
parent | 7a63996d6eaccbe403c724f40a771e4a48c7ea34 (diff) | |
download | highline-97888f5f91f9675ef2f7809fe1cb423abf6099c8.tar.gz |
Rubocop automatic corrections
57 files changed, 992 insertions, 1011 deletions
@@ -8,7 +8,6 @@ group :development, :test do gem "minitest", require: false end - # Reporting only at one ruby version of travis matrix (no repetition) gem "codeclimate-test-reporter", group: :test, require: false @@ -19,14 +18,14 @@ end group :development do gem 'pronto' + gem 'pronto-poper', require: false gem 'pronto-reek', require: false gem 'pronto-rubocop', require: false - gem 'pronto-poper', require: false # Using strict versions of flay and pronto-flay while # PR https://github.com/mmozuras/pronto-flay/pull/11/files # is not merged gem 'flay', '2.7.0' - gem 'pronto-flay', '0.6.1', require: false gem 'flog' + gem 'pronto-flay', '0.6.1', require: false end @@ -5,10 +5,10 @@ require "code_statistics" require "rubygems" -task :default => [:test] +task default: [:test] Rake::TestTask.new do |test| - test.libs = ["lib", "test"] + test.libs = %w[lib test] test.verbose = true test.warning = true test.test_files = FileList['test/test*.rb'] diff --git a/examples/ansi_colors.rb b/examples/ansi_colors.rb index 6986e72..e39b5cc 100644 --- a/examples/ansi_colors.rb +++ b/examples/ansi_colors.rb @@ -9,7 +9,7 @@ require "rubygems" require "highline/import" # Supported color sequences. -colors = %w{black red green yellow blue magenta cyan white} +colors = %w[black red green yellow blue magenta cyan white] # Using color() with symbols. colors.each_with_index do |c, i| @@ -26,7 +26,7 @@ say("This should be <%= color('underlined', UNDERLINE) %>!") say("This might even <%= BLINK %>blink<%= CLEAR %>!") # It even works with list wrapping. -erb_digits = %w{Zero One Two Three Four} + +erb_digits = %w[Zero One Two Three Four] + ["<%= color('Five', :blue) %%>"] + - %w{Six Seven Eight Nine} + %w[Six Seven Eight Nine] say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>") diff --git a/examples/asking_for_arrays.rb b/examples/asking_for_arrays.rb index 42c3268..85fb74f 100644 --- a/examples/asking_for_arrays.rb +++ b/examples/asking_for_arrays.rb @@ -12,8 +12,8 @@ require "pp" puts "Using: #{HighLine.default_instance.class}" puts -grades = ask( "Enter test scores (or a blank line to quit):", - lambda { |ans| ans =~ /^-?\d+$/ ? Integer(ans) : ans} ) do |q| +grades = ask("Enter test scores (or a blank line to quit):", + ->(ans) { ans =~ /^-?\d+$/ ? Integer(ans) : ans }) do |q| q.gather = "" end diff --git a/examples/basic_usage.rb b/examples/basic_usage.rb index 62e3ccb..3f28b02 100644 --- a/examples/basic_usage.rb +++ b/examples/basic_usage.rb @@ -12,32 +12,33 @@ require "yaml" puts "Using: #{HighLine.default_instance.terminal.class}" puts -contacts = [ ] +contacts = [] class NameClass - def self.parse( string ) + def self.parse(string) if string =~ /^\s*(\w+),\s*(\w+)\s*$/ - self.new($2, $1) + new(Regexp.last_match(2), Regexp.last_match(1)) else raise ArgumentError, "Invalid name format." end end def initialize(first, last) - @first, @last = first, last + @first = first + @last = last end attr_reader :first, :last end begin - entry = Hash.new + entry = {} # basic output say("Enter a contact:") # basic input - entry[:name] = ask("Name? (last, first) ", NameClass) do |q| + entry[:name] = ask("Name? (last, first) ", NameClass) do |q| q.validate = /\A\w+, ?\w+\Z/ end entry[:company] = ask("Company? ") { |q| q.default = "none" } @@ -47,26 +48,28 @@ begin q.case = :up q.validate = /\A[A-Z]{2}\Z/ end - entry[:zip] = ask("Zip? ") do |q| + entry[:zip] = ask("Zip? ") do |q| q.validate = /\A\d{5}(?:-?\d{4})?\Z/ end - entry[:phone] = ask( "Phone? ", - lambda { |p| p.delete("^0-9"). - sub(/\A(\d{3})/, '(\1) '). - sub(/(\d{4})\Z/, '-\1') } ) do |q| - q.validate = lambda { |p| p.delete("^0-9").length == 10 } + entry[:phone] = ask("Phone? ", + lambda { |p| + p.delete("^0-9"). + sub(/\A(\d{3})/, '(\1) '). + sub(/(\d{4})\Z/, '-\1') + }) do |q| + q.validate = ->(p) { p.delete("^0-9").length == 10 } q.responses[:not_valid] = "Enter a phone numer with area code." end entry[:age] = ask("Age? ", Integer) { |q| q.in = 0..105 } entry[:birthday] = ask("Birthday? ", Date) - entry[:interests] = ask( "Interests? (comma separated list) ", - lambda { |str| str.split(/,\s*/) } ) + entry[:interests] = ask("Interests? (comma separated list) ", + ->(str) { str.split(/,\s*/) }) entry[:description] = ask("Enter a description for this contact.") do |q| q.whitespace = :strip_and_collapse end contacts << entry -# shortcut for yes and no questions + # shortcut for yes and no questions end while agree("Enter another contact? ", true) if agree("Save these contacts? ", true) diff --git a/examples/color_scheme.rb b/examples/color_scheme.rb index 1fe8863..1889483 100644 --- a/examples/color_scheme.rb +++ b/examples/color_scheme.rb @@ -10,11 +10,11 @@ require 'highline/import' # Create a color scheme, naming color patterns with symbol names. ft = HighLine::ColorScheme.new do |cs| - cs[:headline] = [ :bold, :yellow, :on_black ] - cs[:horizontal_line] = [ :bold, :white, :on_blue] - cs[:even_row] = [ :green ] - cs[:odd_row] = [ :magenta ] - end + cs[:headline] = %i[bold yellow on_black] + cs[:horizontal_line] = %i[bold white on_blue] + cs[:even_row] = [:green] + cs[:odd_row] = [:magenta] +end # Assign that color scheme to HighLine... HighLine.color_scheme = ft @@ -26,7 +26,7 @@ say("<%= color('-'*20, :horizontal_line) %>") # Setup a toggle for rows. i = true ("A".."D").each do |row| - row_color = i ? :even_row : :odd_row - say("<%= color('#{row}', '#{row_color}') %>") - i = !i + row_color = i ? :even_row : :odd_row + say("<%= color('#{row}', '#{row_color}') %>") + i = !i end diff --git a/examples/get_character.rb b/examples/get_character.rb index c0a666f..0276ca9 100644 --- a/examples/get_character.rb +++ b/examples/get_character.rb @@ -8,8 +8,8 @@ puts choices = "ynaq" answer = ask("Your choice [#{choices}]? ") do |q| - q.echo = false - q.character = true - q.validate = /\A[#{choices}]\Z/ - end + q.echo = false + q.character = true + q.validate = /\A[#{choices}]\Z/ +end say("Your choice: #{answer}") diff --git a/examples/menus.rb b/examples/menus.rb index 258c1ca..5941f17 100644 --- a/examples/menus.rb +++ b/examples/menus.rb @@ -7,7 +7,7 @@ puts "Using: #{HighLine.default_instance.terminal.class}" puts # The old way, using ask() and say()... -choices = %w{ruby python perl} +choices = %w[ruby python perl] say("This is the old way using ask() and say()...") say("Please choose your favorite programming language:") say(choices.map { |c| " #{c}\n" }.join) @@ -25,10 +25,9 @@ choose do |menu| menu.prompt = "Please choose your favorite programming language? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } menu.default = :ruby - end say("\nThis is letter indexing...") @@ -39,7 +38,7 @@ choose do |menu| menu.prompt = "Please choose your favorite programming language? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } end say("\nThis is with a different layout...") @@ -50,7 +49,7 @@ choose do |menu| menu.prompt = "Favorite? " menu.choice :ruby do say("Good choice!") end - menu.choices(:python, :perl) do say("Not from around here, are you?") end + menu.choices(:python, :perl) { say("Not from around here, are you?") } end say("\nYou can even build shells...") @@ -60,10 +59,10 @@ loop do menu.shell = true - menu.choice(:load, "Load a file.") do |command, details| + menu.choice(:load, "Load a file.") do |_command, details| say("Loading file with options: #{details}...") end - menu.choice(:save, "Save a file.") do |command, details| + menu.choice(:save, "Save a file.") do |_command, details| say("Saving file with options: #{details}...") end menu.choice(:quit, "Exit program.") { exit } diff --git a/examples/overwrite.rb b/examples/overwrite.rb index f3c9754..cd02090 100644 --- a/examples/overwrite.rb +++ b/examples/overwrite.rb @@ -15,8 +15,8 @@ prompt = "here is your password:" ask( "#{prompt} <%= color('mypassword', RED, BOLD) %> (Press Any Key to blank) " ) do |q| - q.overwrite = true - q.echo = false # overwrite works best when echo is false. - q.character = true # if this is set to :getc then overwrite does not work + q.overwrite = true + q.echo = false # overwrite works best when echo is false. + q.character = true # if this is set to :getc then overwrite does not work end say("<%= color('Look! blanked out!', GREEN) %>") diff --git a/examples/repeat_entry.rb b/examples/repeat_entry.rb index 5e67f7d..9afd7ec 100644 --- a/examples/repeat_entry.rb +++ b/examples/repeat_entry.rb @@ -17,8 +17,8 @@ puts "Ok, you did it." pass = ask("<%= key %>: ") do |q| q.echo = '*' q.verify_match = true - q.gather = {"Enter a password" => '', - "Please type it again for verification" => ''} + q.gather = { "Enter a password" => '', + "Please type it again for verification" => '' } end puts "Your password is now #{pass}!" diff --git a/examples/trapping_eof.rb b/examples/trapping_eof.rb index a63188c..cd0dfef 100644 --- a/examples/trapping_eof.rb +++ b/examples/trapping_eof.rb @@ -13,7 +13,7 @@ loop do name = ask("What's your name?") break if name == "exit" puts "Hello, #{name}!" - rescue EOFError # HighLine throws this if @input.eof? + rescue EOFError # HighLine throws this if @input.eof? break end end diff --git a/examples/using_readline.rb b/examples/using_readline.rb index d8c072a..13c128b 100644 --- a/examples/using_readline.rb +++ b/examples/using_readline.rb @@ -9,7 +9,7 @@ require "rubygems" require "highline/import" loop do - cmd = ask("Enter command: ", %w{save sample load reset quit}) do |q| + cmd = ask("Enter command: ", %w[save sample load reset quit]) do |q| q.readline = true end say("Executing \"#{cmd}\"...") diff --git a/highline.gemspec b/highline.gemspec index e3e0dda..95b6c60 100644 --- a/highline.gemspec +++ b/highline.gemspec @@ -1,4 +1,5 @@ # coding: utf-8 + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'highline/version' diff --git a/lib/highline.rb b/lib/highline.rb index a94bbed..7612674 100755 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -97,8 +97,8 @@ class HighLine # @param page_at [Integer] page size and paginating. # @param indent_size [Integer] indentation size in spaces. # @param indent_level [Integer] how deep is indentated. - def initialize( input = $stdin, output = $stdout, - wrap_at = nil, page_at = nil, indent_size=3, indent_level=0 ) + def initialize(input = $stdin, output = $stdout, + wrap_at = nil, page_at = nil, indent_size = 3, indent_level = 0) @input = input @output = output @@ -185,8 +185,8 @@ class HighLine # @param yes_or_no_question [String] a question that accepts yes and no as answers # @param character [Boolean, :getc] character mode to be passed to Question#character # @see Question#character - def agree( yes_or_no_question, character = nil ) - ask(yes_or_no_question, lambda { |yn| yn.downcase[0] == ?y}) do |q| + def agree(yes_or_no_question, character = nil) + ask(yes_or_no_question, ->(yn) { yn.downcase[0] == 'y' }) do |q| q.validate = /\A(?:y(?:es)?|no?)\Z/i q.responses[:not_valid] = 'Please enter "yes" or "no".' q.responses[:ask_on_error] = :question @@ -236,7 +236,7 @@ class HighLine # @param items [Array<String>] # @param details [Proc] to be passed to Menu.new # @return [String] answer - def choose( *items, &details ) + def choose(*items, &details) menu = Menu.new(&details) menu.choices(*items) unless items.empty? @@ -268,7 +268,7 @@ class HighLine # @return [lambda] lambda to be used in autocompletion operations def shell_style_lambda(menu) - lambda do |command| # shell-style selection + lambda do |command| # shell-style selection first_word = command.to_s.split.first || "" options = menu.options @@ -359,7 +359,7 @@ class HighLine statement = render_statement(statement) return if statement.empty? - statement = (indentation+statement) + statement = (indentation + statement) # Don't add a newline if statement ends with whitespace, OR # if statement ends with whitespace before a color escape code. @@ -384,7 +384,7 @@ class HighLine # set to <tt>:auto</tt>, HighLine will attempt to determine the columns # available for the <tt>@output</tt> or use a sensible default. # - def wrap_at=( setting ) + def wrap_at=(setting) @wrap_at = setting == :auto ? output_cols : setting end @@ -394,7 +394,7 @@ class HighLine # set to <tt>:auto</tt>, HighLine will attempt to determine the rows available # for the <tt>@output</tt> or use a sensible default. # - def page_at=( setting ) + def page_at=(setting) @page_at = setting == :auto ? output_rows - 2 : setting end @@ -402,7 +402,7 @@ class HighLine # Outputs indentation with current settings # def indentation - ' '*@indent_size*@indent_level + ' ' * @indent_size * @indent_level end # @@ -413,7 +413,7 @@ class HighLine # @param multiline [Boolean] # @return [void] # @see #multi_indent - def indent(increase=1, statement=nil, multiline=nil) + def indent(increase = 1, statement = nil, multiline = nil) @indent_level += increase multi = @multi_indent @multi_indent ||= multiline @@ -505,7 +505,7 @@ class HighLine # @param question [Question] # @return [String] response def get_response_line_mode(question) - if question.echo == true and !question.limit + if question.echo == true && !question.limit get_line(question) else get_line_raw_no_echo_mode(question) @@ -535,7 +535,7 @@ class HighLine # honor backspace and delete if character == "\b" chopped = line.chop! - output_erase_char if chopped and question.echo + output_erase_char if chopped && question.echo else line << character say_last_char_or_echo_char(line, question) @@ -563,11 +563,11 @@ class HighLine def say_last_char_or_echo_char(line, question) @output.print(line[-1]) if question.echo == true - @output.print(question.echo) if question.echo and question.echo != true + @output.print(question.echo) if question.echo && question.echo != true end def line_overflow_for_question?(line, question) - question.limit and line.size == question.limit + question.limit && line.size == question.limit end def output_erase_char diff --git a/lib/highline/builtin_styles.rb b/lib/highline/builtin_styles.rb index c829f60..175497f 100644 --- a/lib/highline/builtin_styles.rb +++ b/lib/highline/builtin_styles.rb @@ -1,4 +1,4 @@ -#coding: utf-8 +# coding: utf-8 class HighLine # Builtin Styles that are included at HighLine initialization. @@ -23,17 +23,17 @@ class HighLine blink: "\e[5m", reverse: "\e[7m", concealed: "\e[8m" - } + }.freeze STYLE_LIST.each do |style_name, code| - style = String(style_name).upcase + style = String(style_name).upcase - const_set style, code - const_set style + "_STYLE", Style.new(name: style_name, code: code, builtin: true) + const_set style, code + const_set style + "_STYLE", Style.new(name: style_name, code: code, builtin: true) end # Basic Style names like CLEAR, BOLD, UNDERLINE - STYLES = %w{CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE CONCEALED} + STYLES = %w[CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE CONCEALED].freeze # A Hash with the basic colors an their ANSI escape codes. COLOR_LIST = { @@ -48,23 +48,23 @@ class HighLine gray: { code: "\e[37m", rgb: [192, 192, 192] }, grey: { code: "\e[37m", rgb: [192, 192, 192] }, none: { code: "\e[38m", rgb: [0, 0, 0] } - } + }.freeze COLOR_LIST.each do |color_name, attributes| - color = String(color_name).upcase + color = String(color_name).upcase - style = Style.new( - name: color_name, - code: attributes[:code], - rgb: attributes[:rgb], - builtin: true - ) + style = Style.new( + name: color_name, + code: attributes[:code], + rgb: attributes[:rgb], + builtin: true + ) - const_set color + "_STYLE", style + const_set color + "_STYLE", style end # The builtin styles basic colors like black, red, green. - BASIC_COLORS = %w{BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY GREY NONE} + BASIC_COLORS = %w[BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY GREY NONE].freeze colors = BASIC_COLORS.dup BASIC_COLORS.each do |color| @@ -86,11 +86,10 @@ class HighLine const_set "ON_#{color}", const_get("ON_#{color}_STYLE").code end - ON_NONE_STYLE.rgb = [255,255,255] # Override; white background + ON_NONE_STYLE.rgb = [255, 255, 255] # Override; white background # BuiltinStyles class methods to be extended. module ClassMethods - # Regexp to match against RGB style constant names. RGB_COLOR_PATTERN = /^(ON_)?(RGB_)([A-F0-9]{6})(_STYLE)?$/ @@ -99,17 +98,17 @@ class HighLine # @param name [Symbol] missing constant name def const_missing(name) if name.to_s =~ RGB_COLOR_PATTERN - on = $1 - suffix = $4 + on = Regexp.last_match(1) + suffix = Regexp.last_match(4) - if suffix - code_name = $1.to_s + $2 + $3 - else - code_name = name.to_s - end + code_name = if suffix + Regexp.last_match(1).to_s + Regexp.last_match(2) + Regexp.last_match(3) + else + name.to_s + end style_name = code_name + '_STYLE' - style = Style.rgb($3) + style = Style.rgb(Regexp.last_match(3)) style = style.on if on const_set(style_name, style) diff --git a/lib/highline/color_scheme.rb b/lib/highline/color_scheme.rb index 11cd687..a776fbf 100644 --- a/lib/highline/color_scheme.rb +++ b/lib/highline/color_scheme.rb @@ -8,7 +8,6 @@ # # This is Free Software. See LICENSE and COPYING for details - class HighLine # # ColorScheme objects encapsulate a named set of colors to be used in the @@ -49,15 +48,15 @@ class HighLine # constants. # # @param h [Hash] - def initialize( h = nil ) - @scheme = Hash.new + def initialize(h = nil) + @scheme = {} load_from_hash(h) if h yield self if block_given? end # Load multiple colors from key/value pairs. # @param h [Hash] - def load_from_hash( h ) + def load_from_hash(h) h.each_pair do |color_tag, constants| self[color_tag] = constants end @@ -66,20 +65,20 @@ class HighLine # Does this color scheme include the given tag name? # @param color_tag [#to_sym] # @return [Boolean] - def include?( color_tag ) + def include?(color_tag) @scheme.keys.include?(to_symbol(color_tag)) end # Allow the scheme to be accessed like a Hash. # @param color_tag [#to_sym] # @return [Style] - def []( color_tag ) + def [](color_tag) @scheme[to_symbol(color_tag)] end # Retrieve the original form of the scheme # @param color_tag [#to_sym] - def definition( color_tag ) + def definition(color_tag) style = @scheme[to_symbol(color_tag)] style && style.list end @@ -93,29 +92,28 @@ class HighLine # Allow the scheme to be set like a Hash. # @param color_tag [#to_sym] # @param constants [Array<Symbol>] Array of Style symbols - def []=( color_tag, constants ) - @scheme[to_symbol(color_tag)] = HighLine::Style.new(:name=>color_tag.to_s.downcase.to_sym, - :list=>constants, :no_index=>true) + def []=(color_tag, constants) + @scheme[to_symbol(color_tag)] = HighLine::Style.new(name: color_tag.to_s.downcase.to_sym, + list: constants, no_index: true) end # Retrieve the color scheme hash (in original definition format) # @return [Hash] scheme as Hash. It may be reused in a new ColorScheme. def to_hash - @scheme.inject({}) { |hsh, pair| key, value = pair; hsh[key] = value.list; hsh } + @scheme.each_with_object({}) { |pair, hsh| key, value = pair; hsh[key] = value.list; } end - private # Return a normalized representation of a color name. - def to_symbol( t ) + def to_symbol(t) t.to_s.downcase end # Return a normalized representation of a color setting. - def to_constant( v ) + def to_constant(v) v = v.to_s if v.is_a?(Symbol) - if v.is_a?(::String) then + if v.is_a?(::String) HighLine.const_get(v.upcase) else v @@ -130,16 +128,16 @@ class HighLine # <tt>:error</tt>, <tt>:warning</tt>, <tt>:notice</tt>, <tt>:info</tt>, # <tt>:debug</tt>, <tt>:row_even</tt>, and <tt>:row_odd</tt> colors. # - def initialize( h = nil ) + def initialize(_h = nil) scheme = { - :critical => [ :yellow, :on_red ], - :error => [ :bold, :red ], - :warning => [ :bold, :yellow ], - :notice => [ :bold, :magenta ], - :info => [ :bold, :cyan ], - :debug => [ :bold, :green ], - :row_even => [ :cyan ], - :row_odd => [ :magenta ] + critical: %i[yellow on_red], + error: %i[bold red], + warning: %i[bold yellow], + notice: %i[bold magenta], + info: %i[bold cyan], + debug: %i[bold green], + row_even: [:cyan], + row_odd: [:magenta] } super(scheme) end diff --git a/lib/highline/compatibility.rb b/lib/highline/compatibility.rb index 33c889c..ee13fd6 100644 --- a/lib/highline/compatibility.rb +++ b/lib/highline/compatibility.rb @@ -4,13 +4,13 @@ unless STDIN.respond_to? :getbyte # HighLine adds #getbyte alias to #getc when #getbyte is not available. class IO # alias to #getc when #getbyte is not available - alias_method :getbyte, :getc + alias getbyte getc end # HighLine adds #getbyte alias to #getc when #getbyte is not available. class StringIO # alias to #getc when #getbyte is not available - alias_method :getbyte, :getc + alias getbyte getc end end @@ -18,6 +18,6 @@ unless "".respond_to? :each_line # HighLine adds #each_line alias to #each when each_line is not available. class String # alias to #each when each_line is not available. - alias_method :each_line, :each + alias each_line each end end diff --git a/lib/highline/custom_errors.rb b/lib/highline/custom_errors.rb index 2d45687..3f57395 100644 --- a/lib/highline/custom_errors.rb +++ b/lib/highline/custom_errors.rb @@ -1,5 +1,4 @@ class HighLine - # Internal HighLine errors. module CustomErrors # An error that responds to :explanation_key diff --git a/lib/highline/import.rb b/lib/highline/import.rb index c2e6084..46cb3db 100644 --- a/lib/highline/import.rb +++ b/lib/highline/import.rb @@ -38,11 +38,11 @@ class Object # @param details [lambda] block to be called with the question # instance as argument. # @return [String] answer - def or_ask( *args, &details ) + def or_ask(*args, &details) ask(*args) do |question| question.first_answer = String(self) - details.call(question) if details + yield(question) if details end end end diff --git a/lib/highline/list.rb b/lib/highline/list.rb index 1f2af2c..8e0a0b0 100644 --- a/lib/highline/list.rb +++ b/lib/highline/list.rb @@ -1,10 +1,8 @@ # coding: utf-8 class HighLine - # List class with some convenience methods like {#col_down}. class List - # Original given *items* argument. # It's frozen at initialization time and # all later transformations will happen on {#list}. @@ -33,7 +31,6 @@ class HighLine attr_reader :transpose_mode - # Content are distributed first by column in col down mode. # @return [Boolean] # @@ -141,9 +138,7 @@ class HighLine # Set the {#row_join_string}. # @see #row_join_string - def row_join_string=(string) - @row_join_string = string - end + attr_writer :row_join_string # Returns the row join string size. # Useful for calculating the actual size of @@ -178,4 +173,4 @@ class HighLine row.compact.join(row_join_string) + "\n" end end -end
\ No newline at end of file +end diff --git a/lib/highline/list_renderer.rb b/lib/highline/list_renderer.rb index fde6ad4..8052d71 100644 --- a/lib/highline/list_renderer.rb +++ b/lib/highline/list_renderer.rb @@ -118,7 +118,7 @@ class HighLine::ListRenderer HighLine::List.new(right_padded_items, cols: col_count, col_down: true).to_s end - def list_uneven_columns_mode(list=nil) + def list_uneven_columns_mode(list = nil) list ||= HighLine::List.new(items) col_max = option || items.size @@ -126,9 +126,9 @@ class HighLine::ListRenderer list.cols = column_count widths = get_col_widths(list) - if column_count == 1 or # last guess - inside_line_size_limit?(widths) or # good guess - option # defined by user + if column_count == 1 || # last guess + inside_line_size_limit?(widths) || # good guess + option # defined by user return pad_uneven_rows(list, widths) end end @@ -194,7 +194,7 @@ class HighLine::ListRenderer end def inside_line_size_limit?(widths) - line_size = widths.inject(0) { |sum, n| sum + n + row_join_str_size } + line_size = widths.reduce(0) { |sum, n| sum + n + row_join_str_size } line_size <= line_size_limit + row_join_str_size end @@ -211,16 +211,14 @@ class HighLine::ListRenderer end def line_size_limit - @line_size_limit ||= ( highline.wrap_at || 80 ) + @line_size_limit ||= (highline.wrap_at || 80) end def row_join_string @row_join_string ||= " " end - def row_join_string=(string) - @row_join_string = string - end + attr_writer :row_join_string def row_join_str_size row_join_string.size @@ -248,4 +246,4 @@ class HighLine::ListRenderer def row_count (items.count / col_count.to_f).ceil end -end
\ No newline at end of file +end diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index e1834ef..a743c4d 100644 --- a/lib/highline/menu.rb +++ b/lib/highline/menu.rb @@ -21,16 +21,16 @@ class HighLine # Pass +false+ to _color_ to turn off HighLine::Menu's # index coloring. # Pass a color and the Menu's indices will be colored. - def self.index_color=(color = :rgb_77bbff) - @index_color = color + class << self + attr_writer :index_color end # Initialize it self.index_color = false # Returns color used for coloring Menu's indices - def self.index_color - @index_color + class << self + attr_reader :index_color end # @@ -53,10 +53,10 @@ class HighLine # Initialize Question objects with ignored values, we'll # adjust ours as needed. # - super("Ignored", [ ], &nil) # avoiding passing the block along + super("Ignored", [], &nil) # avoiding passing the block along - @items = [ ] - @hidden_items = [ ] + @items = [] + @hidden_items = [] @help = Hash.new("There's no help for that topic.") @index = :number @@ -75,13 +75,13 @@ class HighLine @index_color = self.class.index_color # Override Questions responses, we'll set our own. - @responses = { } + @responses = {} # Context for action code. @highline = nil yield self if block_given? - init_help if @shell and not @help.empty? + init_help if @shell && !@help.empty? end # @@ -186,11 +186,11 @@ class HighLine # menu.help("rules", "The rules of this system are as follows...") # end - def choice( name, help = nil, text = nil, &action ) + def choice(name, help = nil, text = nil, &action) item = Menu::Item.new(name, text: text, help: help, action: action) @items << item @help.merge!(item.item_help) - update_responses # rebuild responses based on our settings + update_responses # rebuild responses based on our settings end # @@ -231,7 +231,7 @@ class HighLine # choice has more options available to you, like longer text or help (and # of course, individual actions) # - def choices( *names, &action ) + def choices(*names, &action) names.each { |n| choice(n, &action) } end @@ -242,7 +242,7 @@ class HighLine # @param action (see #choice) # @return (see #choice) - def hidden( name, help = nil, &action ) + def hidden(name, help = nil, &action) item = Menu::Item.new(name, text: name, help: help, action: action) @hidden_items << item @help.merge!(item.item_help) @@ -263,11 +263,11 @@ class HighLine # _index_suffix_ to a single space and _select_by_ to <tt>:name</tt>. # Because of this, you should make a habit of setting the _index_ first. # - def index=( style ) + def index=(style) @index = style # Default settings. - if @index == :none or @index.is_a?(::String) + if @index == :none || @index.is_a?(::String) @index_suffix = " " @select_by = :name end @@ -277,17 +277,17 @@ class HighLine # Initializes the help system by adding a <tt>:help</tt> choice, some # action code, and the default help listing. # - def init_help( ) + def init_help return if @items.include?(:help) topics = @help.keys.sort help_help = @help.include?("help") ? @help["help"] : - "This command will display helpful messages about " + - "functionality, like this one. To see the help for " + - "a specific topic enter:\n\thelp [TOPIC]\nTry asking " + - "for help on any of the following:\n\n" + - "<%= list(#{topics.inspect}, :columns_across) %>" - choice(:help, help_help) do |command, topic| + "This command will display helpful messages about " \ + "functionality, like this one. To see the help for " \ + "a specific topic enter:\n\thelp [TOPIC]\nTry asking " \ + "for help on any of the following:\n\n" \ + "<%= list(#{topics.inspect}, :columns_across) %>" + choice(:help, help_help) do |_command, topic| topic.strip! topic.downcase! if topic.empty? @@ -306,7 +306,7 @@ class HighLine # a help message. # @param help [String] the help message to be associated with the menu # item/title/name. - def help( topic, help ) + def help(topic, help) @help[topic] = help end @@ -339,14 +339,14 @@ class HighLine # will default to <tt>:none</tt> and _flow_ will default to # <tt>:inline</tt>. # - def layout=( new_layout ) + def layout=(new_layout) @layout = new_layout # Default settings. case @layout when :one_line, :menu_only self.index = :none - @flow = :inline + @flow = :inline end end @@ -392,7 +392,7 @@ class HighLine # @param details additional parameter to be passed when in shell mode. # @return [nil, Object] if @nil_on_handled is set it returns +nil+, # else it returns the action return value. - def select( highline_context, selection, details = nil ) + def select(highline_context, selection, details = nil) # add in any hidden menu commands items = all_items @@ -424,17 +424,17 @@ class HighLine item = items.find { |i| i.name == selection } return item if item l_index = "`" # character before the letter "a" - index = items.map { "#{l_index.succ!}" }.index(selection) + index = items.map { l_index.succ!.to_s }.index(selection) items[index] end def value_for_selected_item(item, details) if item.action - if @shell - result = item.action.call(item.name, details) - else - result = item.action.call(item.name) - end + result = if @shell + item.action.call(item.name, details) + else + item.action.call(item.name) + end @nil_on_handled ? nil : result else item.name @@ -451,7 +451,7 @@ class HighLine elsif selections.is_a?(Hash) value_for_hash_selections(items, selections, details) else - fail ArgumentError, 'selections must be either Array or Hash' + raise ArgumentError, 'selections must be either Array or Hash' end end @@ -512,23 +512,23 @@ class HighLine # Allows Menu to behave as a String, just like Question. Returns the # _layout_ to be rendered, which is used by HighLine.say(). # - def to_s( ) + def to_s case @layout when :list %(<%= header ? "#{header}:\n" : '' %>) + - parse_list + - show_default_if_any + - "<%= prompt %>" + parse_list + + show_default_if_any + + "<%= prompt %>" when :one_line %(<%= header ? "#{header}: " : '' %>) + - "<%= prompt %>" + - "(" + parse_list + ")" + - show_default_if_any + - "<%= prompt[/\s*$/] %>" + "<%= prompt %>" \ + "(" + parse_list + ")" + + show_default_if_any + + "<%= prompt[/\s*$/] %>" when :menu_only parse_list + - show_default_if_any + - "<%= prompt %>" + show_default_if_any + + "<%= prompt %>" else @layout end @@ -536,11 +536,11 @@ class HighLine def parse_list "<%= list( menu, #{@flow.inspect}, - #{@list_option.inspect} ) %>" + #{@list_option.inspect} ) %>" end def show_default_if_any - return default.to_s.empty? ? "" : "(#{default}) " + default.to_s.empty? ? "" : "(#{default}) " end # diff --git a/lib/highline/paginator.rb b/lib/highline/paginator.rb index 48792af..9be3821 100644 --- a/lib/highline/paginator.rb +++ b/lib/highline/paginator.rb @@ -3,7 +3,6 @@ class HighLine # Take the task of paginating some piece of text given a HighLine context class Paginator - # @return [HighLine] HighLine context attr_reader :highline @@ -36,7 +35,7 @@ class HighLine # Return last line if user wants to abort paging return "...\n#{lines.last}" unless continue_paging? end - return lines.join + lines.join end # @@ -47,7 +46,7 @@ class HighLine command = highline.new_scope.ask( "-- press enter/return to continue or q to stop -- " ) { |q| q.character = true } - command !~ /\A[qQ]\Z/ # Only continue paging if Q was not hit. + command !~ /\A[qQ]\Z/ # Only continue paging if Q was not hit. end end -end
\ No newline at end of file +end diff --git a/lib/highline/question.rb b/lib/highline/question.rb index 4f9560c..4d390dc 100755 --- a/lib/highline/question.rb +++ b/lib/highline/question.rb @@ -59,11 +59,11 @@ class HighLine @case = nil @in = nil @first_answer = nil - @directory = Pathname.new(File.expand_path(File.dirname($0))) + @directory = Pathname.new(File.expand_path(File.dirname($PROGRAM_NAME))) @glob = "*" - @user_responses = Hash.new + @user_responses = {} @internal_responses = default_responses_hash - @overwrite = false + @overwrite = false # allow block to override settings yield self if block_given? @@ -148,11 +148,11 @@ class HighLine # # Asks a yes or no confirmation question, to ensure a user knows what # they have just agreed to. The confirm attribute can be set to : - # +true+ : In this case the question will be, "Are you sure?". - # Proc : The Proc is yielded the answer given. The Proc must - # output a string which is then used as the confirm - # question. - # String : The String must use ERB syntax. The String is + # +true+ : In this case the question will be, "Are you sure?". + # Proc : The Proc is yielded the answer given. The Proc must + # output a string which is then used as the confirm + # question. + # String : The String must use ERB syntax. The String is # evaluated with access to question and answer and # is then used as the confirm question. # When set to +false+ or +nil+ (the default), answers are not confirmed. @@ -261,8 +261,8 @@ class HighLine def default_responses_hash { - :ask_on_error => "? ", - :mismatch => "Your entries didn't match." + ask_on_error: "? ", + mismatch: "Your entries didn't match." } end @@ -270,15 +270,15 @@ class HighLine # @param message_source (see #build_responses) # @return [Hash] responses hash def build_responses_new_hash(message_source) - { :ambiguous_completion => "Ambiguous choice. Please choose one of " + - choice_error_str(message_source) + '.', - :invalid_type => "You must enter a valid #{message_source}.", - :no_completion => "You must choose one of " + - choice_error_str(message_source) + '.', - :not_in_range => "Your answer isn't within the expected range " + - "(#{expected_range}).", - :not_valid => "Your answer isn't valid (must match " + - "#{validate.inspect})." } + { ambiguous_completion: "Ambiguous choice. Please choose one of " + + choice_error_str(message_source) + '.', + invalid_type: "You must enter a valid #{message_source}.", + no_completion: "You must choose one of " + + choice_error_str(message_source) + '.', + not_in_range: "Your answer isn't within the expected range " \ + "(#{expected_range}).", + not_valid: "Your answer isn't valid (must match " \ + "#{validate.inspect})." } end # This is the actual responses hash that gets used in determining output @@ -306,9 +306,9 @@ class HighLine # @return [String] upcased, downcased, capitalized # or unchanged answer String. def change_case(answer_string) - if [:up, :upcase].include?(@case) + if %i[up upcase].include?(@case) answer_string.upcase - elsif [:down, :downcase].include?(@case) + elsif %i[down downcase].include?(@case) answer_string.downcase elsif @case == :capitalize answer_string.capitalize @@ -369,7 +369,7 @@ class HighLine # Returns an English explanation of the current range settings. def expected_range - expected = [ ] + expected = [] expected << "above #{above}" if above expected << "below #{below}" if below @@ -402,9 +402,9 @@ class HighLine # are not checked. # def in_range? - (!above or answer > above) and - (!below or answer < below) and - (!@in or @in.include?(answer)) + (!above || answer > above) && + (!below || answer < below) && + (!@in || @in.include?(answer)) end # @@ -431,11 +431,11 @@ class HighLine def remove_whitespace(answer_string) if !whitespace answer_string - elsif [:strip, :chomp].include?(whitespace) + elsif %i[strip chomp].include?(whitespace) answer_string.send(whitespace) elsif whitespace == :collapse answer_string.gsub(/\s+/, " ") - elsif [:strip_and_collapse, :chomp_and_collapse].include?(whitespace) + elsif %i[strip_and_collapse chomp_and_collapse].include?(whitespace) result = answer_string.send(whitespace.to_s[/^[a-z]+/]) result.gsub(/\s+/, " ") elsif whitespace == :remove @@ -468,7 +468,7 @@ class HighLine File.basename(file) end else - [ ] + [] end end @@ -485,9 +485,9 @@ class HighLine # and case handling. # def valid_answer? - !validate or - (validate.is_a?(Regexp) and answer =~ validate) or - (validate.is_a?(Proc) and validate[answer]) + !validate || + (validate.is_a?(Regexp) && answer =~ validate) || + (validate.is_a?(Proc) && validate[answer]) end # @@ -534,11 +534,11 @@ class HighLine if confirm == true "Are you sure? " elsif confirm.is_a?(Proc) - confirm.call(self.answer) + confirm.call(answer) else # evaluate ERb under initial scope, so it will have # access to question and answer - template = ERB.new(confirm, nil, "%") + template = ERB.new(confirm, nil, "%") template_renderer = TemplateRenderer.new(template, self, highline) template_renderer.render end @@ -564,7 +564,7 @@ class HighLine # @param highline [HighLine] context # @return [void] def show_question(highline) - highline.say(self) unless (readline && (echo == true && !limit)) + highline.say(self) unless readline && (echo == true && !limit) end # Returns an echo string that is adequate for this Question settings. @@ -594,11 +594,11 @@ class HighLine # def append_default if template =~ /([\t ]+)\Z/ - template << "|#{default}|#{$1}" + template << "|#{default}|#{Regexp.last_match(1)}" elsif template == "" template << "|#{default}| " elsif template[-1, 1] == "\n" - template[-2, 0] = " |#{default}|" + template[-2, 0] = " |#{default}|" else template << " |#{default}|" end @@ -606,7 +606,7 @@ class HighLine def choice_error_str(message_source) if message_source.is_a? Array - '[' + message_source.join(', ') + ']' + '[' + message_source.join(', ') + ']' else message_source.inspect end diff --git a/lib/highline/question/answer_converter.rb b/lib/highline/question/answer_converter.rb index d4067f1..0bd0a78 100644 --- a/lib/highline/question/answer_converter.rb +++ b/lib/highline/question/answer_converter.rb @@ -100,4 +100,4 @@ class HighLine end end end -end
\ No newline at end of file +end diff --git a/lib/highline/question_asker.rb b/lib/highline/question_asker.rb index 06e6647..845914a 100644 --- a/lib/highline/question_asker.rb +++ b/lib/highline/question_asker.rb @@ -33,11 +33,9 @@ class HighLine if question.confirm raise NoConfirmationQuestionError unless @highline.send(:confirm, question) end - rescue ExplainableError => e explain_error(e.explanation_key) retry - rescue ArgumentError => error case error.message when /ambiguous/ @@ -68,7 +66,7 @@ class HighLine original_question_template = question.template verify_match = question.verify_match - begin # when verify_match is set this loop will repeat until unique_answers == 1 + begin # when verify_match is set this loop will repeat until unique_answers == 1 question.template = original_question_template answers = gather_answers_based_on_type @@ -87,7 +85,7 @@ class HighLine # @return [Array] answers def gather_integer gather_with_array do |answers| - (question.gather-1).times { answers << ask_once } + (question.gather - 1).times { answers << ask_once } end end @@ -112,7 +110,6 @@ class HighLine end end - private ## Delegate to Highline @@ -134,7 +131,7 @@ class HighLine if question.gather.is_a?(::String) || question.gather.is_a?(Symbol) answer.to_s == question.gather.to_s else question.gather.is_a?(Regexp) - answer.to_s =~ question.gather + answer.to_s =~ question.gather end end diff --git a/lib/highline/simulate.rb b/lib/highline/simulate.rb index c1ee6df..c0bb585 100644 --- a/lib/highline/simulate.rb +++ b/lib/highline/simulate.rb @@ -10,12 +10,9 @@ # # adapted from https://gist.github.com/194554 - class HighLine - # Simulates Highline input for use in tests. class Simulate - # Creates a simulator with an array of Strings as a script # @param strings [Array<String>] preloaded string to be used # as input buffer when simulating. @@ -31,7 +28,7 @@ class HighLine # Simulate StringIO#getbyte by shifting a single character off of the next line of the script def getbyte line = gets - if line.length > 0 + unless line.empty? char = line.slice! 0 @strings.unshift line char diff --git a/lib/highline/statement.rb b/lib/highline/statement.rb index 5e6e2f2..b5938a9 100644 --- a/lib/highline/statement.rb +++ b/lib/highline/statement.rb @@ -52,14 +52,14 @@ class HighLine end def format_statement - return template_string unless template_string.length > 0 + return template_string if template_string.empty? statement = render_template statement = HighLine::Wrapper.wrap(statement, highline.wrap_at) statement = HighLine::Paginator.new(highline).page_print(statement) - statement = statement.gsub(/\n(?!$)/,"\n#{highline.indentation}") if highline.multi_indent + statement = statement.gsub(/\n(?!$)/, "\n#{highline.indentation}") if highline.multi_indent statement end @@ -79,4 +79,4 @@ class HighLine HighLine.const_get(constant) end end -end
\ No newline at end of file +end diff --git a/lib/highline/string.rb b/lib/highline/string.rb index 162cfeb..7ceb244 100644 --- a/lib/highline/string.rb +++ b/lib/highline/string.rb @@ -3,7 +3,6 @@ require "highline/string_extensions" class HighLine - # # HighLine::String is a subclass of String with convenience methods added for colorization. # @@ -32,4 +31,4 @@ class HighLine class String < ::String include StringExtensions end -end
\ No newline at end of file +end diff --git a/lib/highline/string_extensions.rb b/lib/highline/string_extensions.rb index d2f2de4..95c8bd5 100644 --- a/lib/highline/string_extensions.rb +++ b/lib/highline/string_extensions.rb @@ -57,18 +57,18 @@ class HighLine # @todo Chain existing method_missing? undef :method_missing if method_defined? :method_missing - def method_missing(method, *args, &blk) + def method_missing(method, *_args) if method.to_s =~ /^(on_)?rgb_([0-9a-fA-F]{6})$/ color(method) else - raise NoMethodError, "undefined method `#{method}' for #<#{self.class}:#{'%#x'%self.object_id}>" + raise NoMethodError, "undefined method `#{method}' for #<#{self.class}:#{format('%#x', object_id)}>" end end private def setup_color_code(*colors) - color_code = colors.map{|color| color.is_a?(Numeric) ? '%02x'%color : color.to_s}.join + color_code = colors.map { |color| color.is_a?(Numeric) ? format('%02x', color) : color.to_s }.join raise "Bad RGB color #{colors.inspect}" unless color_code =~ /^[a-fA-F0-9]{6}/ color_code end diff --git a/lib/highline/style.rb b/lib/highline/style.rb index c53024a..8d6cebc 100755 --- a/lib/highline/style.rb +++ b/lib/highline/style.rb @@ -8,16 +8,14 @@ # # This is Free Software. See LICENSE and COPYING for details - class HighLine - # Creates a style using {.find_or_create_style} or # {.find_or_create_style_list} # @param args [Array<Style, Hash, String>] style properties # @return [Style] def self.Style(*args) args = args.compact.flatten - if args.size==1 + if args.size == 1 find_or_create_style(args.first) else find_or_create_style_list(*args) @@ -36,7 +34,7 @@ class HighLine if styles = Style.code_index[arg] styles.first else - Style.new(:code=>arg) + Style.new(code: arg) end elsif style = Style.list[arg] style @@ -45,9 +43,9 @@ class HighLine elsif arg.is_a?(Hash) Style.new(arg) elsif arg.to_s.downcase =~ /^rgb_([a-f0-9]{6})$/ - Style.rgb($1) + Style.rgb(Regexp.last_match(1)) elsif arg.to_s.downcase =~ /^on_rgb_([a-f0-9]{6})$/ - Style.rgb($1).on + Style.rgb(Regexp.last_match(1)).on else raise NameError, "#{arg.inspect} is not a defined Style" end @@ -62,12 +60,11 @@ class HighLine def self.find_or_create_style_list(*args) name = args - Style.list[name] || Style.new(:list=>args) + Style.list[name] || Style.new(list: args) end # ANSI styles to be used by HighLine. class Style - # Index the given style. # Uses @code_index (Hash) as repository. # @param style [Style] @@ -77,10 +74,10 @@ class HighLine @styles ||= {} @styles[style.name] = style end - if !style.list + unless style.list @code_index ||= {} @code_index[style.code] ||= [] - @code_index[style.code].reject!{|indexed_style| indexed_style.name == style.name} + @code_index[style.code].reject! { |indexed_style| indexed_style.name == style.name } @code_index[style.code] << style end style @@ -91,9 +88,9 @@ class HighLine # @return [void] def self.clear_index # reset to builtin only styles - @styles = list.select { |name, style| style.builtin } + @styles = list.select { |_name, style| style.builtin } @code_index = {} - @styles.each { |name, style| index(style) } + @styles.each { |_name, style| index(style) } end # Converts all given color codes to hexadecimal and @@ -106,7 +103,7 @@ class HighLine # HighLine::Style.rgb_hex(9, 10, "11") # => "090a11" def self.rgb_hex(*colors) colors.map do |color| - color.is_a?(Numeric) ? '%02x'%color : color.to_s + color.is_a?(Numeric) ? format('%02x', color) : color.to_s end.join end @@ -117,7 +114,7 @@ class HighLine # HighLine::Style.rgb_parts("090A0B") # => [9, 10, 11] def self.rgb_parts(hex) - hex.scan(/../).map{|part| part.to_i(16)} + hex.scan(/../).map { |part| part.to_i(16) } end # Search for or create a new Style from the colors provided. @@ -137,7 +134,7 @@ class HighLine style else parts = rgb_parts(hex) - new(:name=>name, :code=>"\e[38;5;#{rgb_number(parts)}m", :rgb=>parts) + new(name: name, code: "\e[38;5;#{rgb_number(parts)}m", rgb: parts) end end @@ -146,15 +143,15 @@ class HighLine # @return [Numeric] to be used as escape code on ANSI terminals def self.rgb_number(*parts) parts = parts.flatten - 16 + parts.inject(0) {|kode, part| kode*6 + (part/256.0*6.0).floor} + 16 + parts.reduce(0) { |kode, part| kode * 6 + (part / 256.0 * 6.0).floor } end # From an ANSI number (color escape code), craft an 'rgb_hex' code of it # @param ansi_number [Integer] ANSI escape code # @return [String] all color codes joined as {.rgb_hex} def self.ansi_rgb_to_hex(ansi_number) - raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).include?(ansi_number) - parts = (ansi_number-16).to_s(6).rjust(3,'0').scan(/./).map{|d| (d.to_i*255.0/6.0).ceil} + raise "Invalid ANSI rgb code #{ansi_number}" unless (16..231).cover?(ansi_number) + parts = (ansi_number - 16).to_s(6).rjust(3, '0').scan(/./).map { |d| (d.to_i * 255.0 / 6.0).ceil } rgb_hex(*parts) end @@ -170,7 +167,7 @@ class HighLine # Remove any ANSI color escape sequence of the given String. # @param string [String] - # @return [String] + # @return [String] def self.uncolor(string) string.gsub(/\e\[\d+(;\d+)*m/, '') end @@ -232,7 +229,7 @@ class HighLine # @return [String] the Style code def code if @list - @list.map{|element| HighLine.Style(element).code}.join + @list.map { |element| HighLine.Style(element).code }.join else @code end @@ -257,22 +254,22 @@ class HighLine # @param new_name [Symbol] # @param options [Hash] Style attributes to be changed # @return [Style] new Style with changed attributes - def variant(new_name, options={}) + def variant(new_name, options = {}) raise "Cannot create a variant of a style list (#{inspect})" if @list new_code = options[:code] || code if options[:increment] raise "Unexpected code in #{inspect}" unless new_code =~ /^(.*?)(\d+)(.*)/ - new_code = $1 + ($2.to_i + options[:increment]).to_s + $3 + new_code = Regexp.last_match(1) + (Regexp.last_match(2).to_i + options[:increment]).to_s + Regexp.last_match(3) end new_rgb = options[:rgb] || @rgb - self.class.new(self.to_hash.merge(:name=>new_name, :code=>new_code, :rgb=>new_rgb)) + self.class.new(to_hash.merge(name: new_name, code: new_code, rgb: new_rgb)) end # Uses the color as background and return a new style. # @return [Style] def on - new_name = ('on_'+@name.to_s).to_sym - self.class.list[new_name] ||= variant(new_name, :increment=>10) + new_name = ('on_' + @name.to_s).to_sym + self.class.list[new_name] ||= variant(new_name, increment: 10) end # @return [Style] a brighter version of this Style @@ -289,10 +286,10 @@ class HighLine def create_bright_variant(variant_name) raise "Cannot create a #{name} variant of a style list (#{inspect})" if @list - new_name = ("#{variant_name}_"+@name.to_s).to_sym - new_rgb = @rgb == [0,0,0] ? [128, 128, 128] : @rgb.map {|color| color==0 ? 0 : [color+128,255].min } + new_name = ("#{variant_name}_" + @name.to_s).to_sym + new_rgb = @rgb == [0, 0, 0] ? [128, 128, 128] : @rgb.map { |color| color == 0 ? 0 : [color + 128, 255].min } - find_style(new_name) or variant(new_name, :increment=>60, :rgb=>new_rgb) + find_style(new_name) || variant(new_name, increment: 60, rgb: new_rgb) end def find_style(name) diff --git a/lib/highline/template_renderer.rb b/lib/highline/template_renderer.rb index 9ac3970..e77f51a 100644 --- a/lib/highline/template_renderer.rb +++ b/lib/highline/template_renderer.rb @@ -42,9 +42,9 @@ class HighLine # is not available. # @return [String] error message. def method_missing(method, *args) - "Method #{method} with args #{args.inspect} " + - "is not available on #{self.inspect}. " + - "Try #{methods(false).sort.inspect}" + "Method #{method} with args #{args.inspect} " \ + "is not available on #{inspect}. " \ + "Try #{methods(false).sort.inspect}" end # @return [Question, Menu] {#source} attribute. @@ -59,4 +59,4 @@ class HighLine HighLine.const_get(name) end end -end
\ No newline at end of file +end diff --git a/lib/highline/terminal.rb b/lib/highline/terminal.rb index a76a136..d2b61a0 100755 --- a/lib/highline/terminal.rb +++ b/lib/highline/terminal.rb @@ -16,7 +16,6 @@ class HighLine # input and output to. # The specialized Terminals all decend from this HighLine::Terminal class class Terminal - # Probe for and return a suitable Terminal instance # @param input [IO] input stream # @param output [IO] output stream @@ -57,8 +56,7 @@ class HighLine # An initialization callback. # It is called by {.get_terminal}. - def initialize_system_extensions - end + def initialize_system_extensions; end # @return [Array<Integer, Integer>] two value terminal # size like [columns, lines] @@ -67,8 +65,7 @@ class HighLine end # Enter Raw No Echo mode. - def raw_no_echo_mode - end + def raw_no_echo_mode; end # Yieds a block to be executed in Raw No Echo mode and # then restore the terminal state. @@ -80,40 +77,38 @@ class HighLine end # Restore terminal to its default mode - def restore_mode - end + def restore_mode; end # Get one character from the terminal # @return [String] one character - def get_character - end + def get_character; end # Get one line from the terminal and format accordling. # Use readline if question has readline mode set. # @param question [HighLine::Question] # @param highline [HighLine] # @param options [Hash] - def get_line(question, highline, options={}) + def get_line(question, highline, _options = {}) raw_answer = - if question.readline - get_line_with_readline(question, highline, options={}) - else - get_line_default(highline) - end + if question.readline + get_line_with_readline(question, highline, options = {}) + else + get_line_default(highline) + end question.format_answer(raw_answer) end # Get one line using #readline_read # @param (see #get_line) - def get_line_with_readline(question, highline, options={}) - require "readline" # load only if needed + def get_line_with_readline(question, highline, _options = {}) + require "readline" # load only if needed question_string = highline.render_statement(question) raw_answer = readline_read(question_string, question) - if !raw_answer and highline.track_eof? + if !raw_answer && highline.track_eof? raise EOFError, "The input stream is exhausted." end @@ -140,7 +135,7 @@ class HighLine Readline.readline(prompt, true) end - $VERBOSE = old_verbose + $VERBOSE = old_verbose raw_answer end @@ -148,8 +143,8 @@ class HighLine # Get one line from terminal using default #gets method. # @param highline (see #get_line) def get_line_default(highline) - raise EOFError, "The input stream is exhausted." if highline.track_eof? and - highline.input.eof? + raise EOFError, "The input stream is exhausted." if highline.track_eof? && + highline.input.eof? highline.input.gets end @@ -190,7 +185,11 @@ class HighLine # Saves terminal state using shell stty command. def save_stty - @stty_save = `stty -g`.chomp rescue nil + @stty_save = begin + `stty -g`.chomp + rescue + nil + end end # Restores terminal state using shell stty command. diff --git a/lib/highline/terminal/io_console.rb b/lib/highline/terminal/io_console.rb index eee152f..699db91 100644 --- a/lib/highline/terminal/io_console.rb +++ b/lib/highline/terminal/io_console.rb @@ -26,4 +26,4 @@ class HighLine end end end -end
\ No newline at end of file +end diff --git a/lib/highline/terminal/ncurses.rb b/lib/highline/terminal/ncurses.rb index 817cc23..3f399e0 100644 --- a/lib/highline/terminal/ncurses.rb +++ b/lib/highline/terminal/ncurses.rb @@ -34,4 +34,4 @@ class HighLine end end end -end
\ No newline at end of file +end diff --git a/lib/highline/terminal/unix_stty.rb b/lib/highline/terminal/unix_stty.rb index 3b9668a..1b6f761 100644 --- a/lib/highline/terminal/unix_stty.rb +++ b/lib/highline/terminal/unix_stty.rb @@ -5,25 +5,28 @@ class HighLine # HighLine::Terminal option that uses external "stty" program # to control terminal options. class UnixStty < Terminal - # A Unix savvy method using stty to fetch the console columns, and rows. # ... stty does not work in JRuby # @return (see Terminal#terminal_size) def terminal_size begin require "io/console" - winsize = IO.console.winsize.reverse rescue nil + winsize = begin + IO.console.winsize.reverse + rescue + nil + end return winsize if winsize rescue LoadError end - if /solaris/ =~ RUBY_PLATFORM and - `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/ - [$2, $1].map { |x| x.to_i } + if /solaris/ =~ RUBY_PLATFORM && + `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/ + [Regexp.last_match(2), Regexp.last_match(1)].map(&:to_i) elsif `stty size` =~ /^(\d+)\s(\d+)$/ - [$2.to_i, $1.to_i] + [Regexp.last_match(2).to_i, Regexp.last_match(1).to_i] else - [ 80, 24 ] + [80, 24] end end @@ -40,9 +43,9 @@ class HighLine end # (see Terminal#get_character) - def get_character( input = STDIN ) + def get_character(input = STDIN) input.getc end end end -end
\ No newline at end of file +end diff --git a/lib/highline/wrapper.rb b/lib/highline/wrapper.rb index ae93db6..eb3c138 100644 --- a/lib/highline/wrapper.rb +++ b/lib/highline/wrapper.rb @@ -1,12 +1,10 @@ # coding: utf-8 class HighLine - # A simple Wrapper module that is aware of ANSI escape codes. # It compensates for the ANSI escape codes so it works on the # actual (visual) line length. module Wrapper - # # Wrap a sequence of _lines_ at _wrap_at_ characters per line. Existing # newlines will not be affected by this process, but additional newlines @@ -18,24 +16,24 @@ class HighLine return text unless wrap_at wrap_at = Integer(wrap_at) - wrapped = [ ] + wrapped = [] text.each_line do |line| # take into account color escape sequences when wrapping - wrap_at = wrap_at + (line.length - actual_length(line)) + wrap_at += (line.length - actual_length(line)) while line =~ /([^\n]{#{wrap_at + 1},})/ - search = $1.dup - replace = $1.dup + search = Regexp.last_match(1).dup + replace = Regexp.last_match(1).dup if index = replace.rindex(" ", wrap_at) replace[index, 1] = "\n" replace.sub!(/\n[ \t]+/, "\n") line.sub!(search, replace) else - line[$~.begin(1) + wrap_at, 0] = "\n" + line[$LAST_MATCH_INFO.begin(1) + wrap_at, 0] = "\n" end end wrapped << line end - return wrapped.join + wrapped.join end # @@ -45,8 +43,8 @@ class HighLine # @param string_with_escapes [String] any ANSI colored String # @return [Integer] length based on the visual size of the String # (without the escape codes) - def self.actual_length( string_with_escapes ) + def self.actual_length(string_with_escapes) string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length end end -end
\ No newline at end of file +end diff --git a/test/acceptance/acceptance.rb b/test/acceptance/acceptance.rb index 5ba6784..2aa299f 100644 --- a/test/acceptance/acceptance.rb +++ b/test/acceptance/acceptance.rb @@ -36,8 +36,16 @@ james@grayproductions.net Date: #{Time.now.utc} HighLine::VERSION: #{HighLine::VERSION} Terminal: #{HighLine.default_instance.terminal.class} -RUBY_DESCRIPTION: #{RUBY_DESCRIPTION rescue 'not available'} -Readline::VERSION: #{Readline::VERSION rescue 'not availabe'} +RUBY_DESCRIPTION: #{begin + RUBY_DESCRIPTION + rescue + 'not available' + end} +Readline::VERSION: #{begin + Readline::VERSION + rescue + 'not availabe' + end} ENV['SHELL']: #{ENV['SHELL']} ENV['TERM']: #{ENV['TERM']} ENV['TERM_PROGRAM']: #{ENV['TERM_PROGRAM']} @@ -51,7 +59,7 @@ puts report timestamp = Time.now.strftime('%Y%m%d%H%M%S') filename = "highlinetests-#{timestamp}.log" -File.open "#{filename}", 'w+' do |f| +File.open filename.to_s, 'w+' do |f| f.puts report end diff --git a/test/acceptance/acceptance_test.rb b/test/acceptance/acceptance_test.rb index 19f6dea..30aceaa 100644 --- a/test/acceptance/acceptance_test.rb +++ b/test/acceptance/acceptance_test.rb @@ -5,7 +5,7 @@ require 'highline/import' class HighLine::AcceptanceTest @@answers ||= {} - def self.check(&block) + def self.check caller_file = File.basename(caller[0].split(":")[-3]) test = new diff --git a/test/acceptance/at_color_output_using_erb_templates.rb b/test/acceptance/at_color_output_using_erb_templates.rb index c45f264..0af103b 100644 --- a/test/acceptance/at_color_output_using_erb_templates.rb +++ b/test/acceptance/at_color_output_using_erb_templates.rb @@ -9,7 +9,7 @@ HighLine::AcceptanceTest.check do |t| "You should see the word _grass_ " \ "colored in green color" - t.action = Proc.new do + t.action = proc do say "The <%= color('grass', :green) %> should be green!" end diff --git a/test/acceptance/at_echo_false.rb b/test/acceptance/at_echo_false.rb index 2466b8d..0f0226c 100644 --- a/test/acceptance/at_echo_false.rb +++ b/test/acceptance/at_echo_false.rb @@ -12,7 +12,7 @@ HighLine::AcceptanceTest.check do |t| "When typing the characters you should not " \ "see any of them on the screen." - t.action = Proc.new do + t.action = proc do answer = ask "Enter some characters and press <enter>: " do |q| q.echo = false end diff --git a/test/acceptance/at_readline.rb b/test/acceptance/at_readline.rb index 8a66f09..93fd652 100644 --- a/test/acceptance/at_readline.rb +++ b/test/acceptance/at_readline.rb @@ -22,12 +22,12 @@ HighLine::AcceptanceTest.check do |t| "When ready, just type 'exit' and the loop will finish.\n\n" \ "Don't forget to answer 'y' (yes) or 'n' (no) to the question at the end." - t.action = Proc.new do + t.action = proc do loop do cmd = - ask "Enter command: ", %w{ save sample exec exit load } do |q| - q.readline = true - end + ask "Enter command: ", %w[save sample exec exit load] do |q| + q.readline = true + end say("Executing \"#{cmd}\"...") break if cmd == "exit" end diff --git a/test/io_console_compatible.rb b/test/io_console_compatible.rb index c8e2e70..83817f3 100644 --- a/test/io_console_compatible.rb +++ b/test/io_console_compatible.rb @@ -34,4 +34,4 @@ end class StringIO include IOConsoleCompatible -end
\ No newline at end of file +end diff --git a/test/string_methods.rb b/test/string_methods.rb index 9c61359..90f92e7 100644 --- a/test/string_methods.rb +++ b/test/string_methods.rb @@ -9,25 +9,25 @@ # # String class convenience methods -module StringMethods +module StringMethods def test_color assert_equal("\e[34mstring\e[0m", @string.color(:blue)) - assert_equal("\e[1m\e[47mstring\e[0m", @string.color(:bold,:on_white)) + assert_equal("\e[1m\e[47mstring\e[0m", @string.color(:bold, :on_white)) assert_equal("\e[45mstring\e[0m", @string.on(:magenta)) assert_equal("\e[36mstring\e[0m", @string.cyan) assert_equal("\e[41m\e[5mstring\e[0m\e[0m", @string.blink.on_red) assert_equal("\e[38;5;137mstring\e[0m", @string.color(:rgb_906030)) assert_equal("\e[38;5;101mstring\e[0m", @string.rgb('606030')) - assert_equal("\e[38;5;107mstring\e[0m", @string.rgb('60','90','30')) - assert_equal("\e[38;5;107mstring\e[0m", @string.rgb(96,144,48)) + assert_equal("\e[38;5;107mstring\e[0m", @string.rgb('60', '90', '30')) + assert_equal("\e[38;5;107mstring\e[0m", @string.rgb(96, 144, 48)) assert_equal("\e[38;5;173mstring\e[0m", @string.rgb_c06030) assert_equal("\e[48;5;137mstring\e[0m", @string.color(:on_rgb_906030)) assert_equal("\e[48;5;101mstring\e[0m", @string.on_rgb('606030')) - assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb('60','90','30')) - assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb(96,144,48)) + assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb('60', '90', '30')) + assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb(96, 144, 48)) assert_equal("\e[48;5;173mstring\e[0m", @string.on_rgb_c06030) end - + def test_uncolor colored_string = HighLine::String("\e[38;5;137mstring\e[0m") assert_equal "string", colored_string.uncolor diff --git a/test/test_answer_converter.rb b/test/test_answer_converter.rb index 5a4497d..47bf33c 100644 --- a/test/test_answer_converter.rb +++ b/test/test_answer_converter.rb @@ -12,7 +12,7 @@ class TestAnswerConverter < Minitest::Test answer_converter = HighLine::Question::AnswerConverter.new(question) refute_equal "18", answer_converter.convert - assert_equal 18, answer_converter.convert + assert_equal 18, answer_converter.convert end def test_float_convertion @@ -21,6 +21,6 @@ class TestAnswerConverter < Minitest::Test answer_converter = HighLine::Question::AnswerConverter.new(question) refute_equal "3.14159", answer_converter.convert - assert_equal 3.14159, answer_converter.convert + assert_equal 3.14159, answer_converter.convert end -end
\ No newline at end of file +end diff --git a/test/test_color_scheme.rb b/test/test_color_scheme.rb index 9b4d61b..b4b898a 100644 --- a/test/test_color_scheme.rb +++ b/test/test_color_scheme.rb @@ -3,7 +3,7 @@ # tc_color_scheme.rb # -# Created by Jeremy Hinegardner on 2007-01-24. +# Created by Jeremy Hinegardner on 2007-01-24. # Copyright 2007 Jeremy Hinegardner. All rights reserved. # # This is Free Software. See LICENSE and COPYING for details. @@ -22,31 +22,31 @@ class TestColorScheme < Minitest::Test end def test_using_color_scheme - assert_equal(false,HighLine.using_color_scheme?) + assert_equal(false, HighLine.using_color_scheme?) HighLine.color_scheme = HighLine::ColorScheme.new - assert_equal(true,HighLine.using_color_scheme?) + assert_equal(true, HighLine.using_color_scheme?) end def test_scheme HighLine.color_scheme = HighLine::SampleColorScheme.new @terminal.say("This should be <%= color('warning yellow', :warning) %>.") - assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string) + assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n", @output.string) @output.rewind - + @terminal.say("This should be <%= color('warning yellow', 'warning') %>.") - assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string) + assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n", @output.string) @output.rewind @terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.") - assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string) + assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n", @output.string) @output.rewind - + # Check that keys are available, and as expected - assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort, + assert_equal %w[critical error warning notice info debug row_even row_odd].sort, HighLine.color_scheme.keys.sort - + # Color scheme doesn't care if we use symbols or strings, and is case-insensitive warning1 = HighLine.color_scheme[:warning] warning2 = HighLine.color_scheme["warning"] @@ -62,7 +62,7 @@ class TestColorScheme < Minitest::Test # Nonexistent keys return nil assert_nil HighLine.color_scheme[:nonexistent] - + # Same as above, for definitions defn1 = HighLine.color_scheme.definition(:warning) defn2 = HighLine.color_scheme.definition("warning") @@ -72,23 +72,23 @@ class TestColorScheme < Minitest::Test assert_instance_of Array, defn2 assert_instance_of Array, defn3 assert_instance_of Array, defn4 - assert_equal [:bold, :yellow], defn1 - assert_equal [:bold, :yellow], defn2 - assert_equal [:bold, :yellow], defn3 - assert_equal [:bold, :yellow], defn4 + assert_equal %i[bold yellow], defn1 + assert_equal %i[bold yellow], defn2 + assert_equal %i[bold yellow], defn3 + assert_equal %i[bold yellow], defn4 assert_nil HighLine.color_scheme.definition(:nonexistent) - + color_scheme_hash = HighLine.color_scheme.to_hash assert_instance_of Hash, color_scheme_hash - assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort, + assert_equal %w[critical error warning notice info debug row_even row_odd].sort, color_scheme_hash.keys.sort assert_instance_of Array, HighLine.color_scheme.definition(:warning) - assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning) + assert_equal %i[bold yellow], HighLine.color_scheme.definition(:warning) # turn it back off, should raise an exception HighLine.color_scheme = nil - assert_raises(NameError) { + assert_raises(NameError) do @terminal.say("This should be <%= color('nothing at all', :error) %>.") - } + end end -end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 65c7646..48c0ff0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,9 +13,7 @@ debug_message = "Tests will be run under:\n" debug_message << " - #{HighLine.new.terminal.class}\n" debug_message << " - HighLine::VERSION #{HighLine::VERSION}\n" -if defined? RUBY_DESCRIPTION - debug_message << " - #{RUBY_DESCRIPTION}\n" -end +debug_message << " - #{RUBY_DESCRIPTION}\n" if defined? RUBY_DESCRIPTION puts debug_message 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) diff --git a/test/test_import.rb b/test/test_import.rb index 40ff56d..c84c5fd 100644 --- a/test/test_import.rb +++ b/test/test_import.rb @@ -28,14 +28,14 @@ class TestImport < Minitest::Test # If correctly initialized, it will contain several ins vars. refute_empty HighLine.default_instance.instance_variables end - + def test_or_ask old_instance = HighLine.default_instance - + input = StringIO.new output = StringIO.new - HighLine.default_instance = HighLine.new(input, output) - + HighLine.default_instance = HighLine.new(input, output) + input << "10\n" input.rewind @@ -45,15 +45,15 @@ class TestImport < Minitest::Test assert_equal(20, "20".or_ask("How much? ", Integer)) assert_equal(20, 20.or_ask("How much? ", Integer)) - + assert_equal(10, 20.or_ask("How much? ", Integer) { |q| q.in = 1..10 }) ensure HighLine.default_instance = old_instance end - + def test_redirection old_instance = HighLine.default_instance - + HighLine.default_instance = HighLine.new(nil, (output = StringIO.new)) say("Testing...") assert_equal("Testing...\n", output.string) diff --git a/test/test_list.rb b/test/test_list.rb index 6e4c1de..ce982a8 100644 --- a/test/test_list.rb +++ b/test/test_list.rb @@ -7,16 +7,16 @@ require "highline/list" class TestHighLineList < Minitest::Test def setup - @items = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" ] + @items = %w[a b c d e f g h i j] end def test_in_2_cols list_in_two_cols = - [ [ "a", "b" ], - [ "c", "d" ], - [ "e", "f" ], - [ "g", "h" ], - [ "i", "j" ] ] + [%w[a b], + %w[c d], + %w[e f], + %w[g h], + %w[i j]] highline_list = HighLine::List.new(@items, cols: 2) @@ -25,11 +25,11 @@ class TestHighLineList < Minitest::Test def test_in_2_cols_col_down col_down_list = - [ [ "a", "f"], - [ "b", "g"], - [ "c", "h"], - [ "d", "i"], - [ "e", "j"] ] + [%w[a f], + %w[b g], + %w[c h], + %w[d i], + %w[e j]] highline_list = HighLine::List.new(@items, cols: 2, col_down: true) @@ -38,8 +38,8 @@ class TestHighLineList < Minitest::Test def test_in_2_cols_transposed transposed_list = - [ [ "a", "c", "e", "g", "i" ], - [ "b", "d", "f", "h", "j" ] ] + [%w[a c e g i], + %w[b d f h j]] highline_list = HighLine::List.new(@items, cols: 2, transpose: true) @@ -48,13 +48,13 @@ class TestHighLineList < Minitest::Test def test_in_3_cols list_in_three_cols = - [ [ "a", "b", "c" ], - [ "d", "e", "f" ], - [ "g", "h", "i" ], - [ "j" ] ] + [%w[a b c], + %w[d e f], + %w[g h i], + ["j"]] highline_list = HighLine::List.new(@items, cols: 3) assert_equal list_in_three_cols, highline_list.list end -end
\ No newline at end of file +end diff --git a/test/test_menu.rb b/test/test_menu.rb index 402b47d..514ff6b 100644 --- a/test/test_menu.rb +++ b/test/test_menu.rb @@ -90,7 +90,7 @@ class TestMenu < Minitest::Test # Default: menu.flow = :rows menu.choice "Unicode right single quotation mark: ’" end - assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, { :undef => :replace }), @output.string) + assert_equal("1. Unicode right single quotation mark: ’\n? ".encode(@output.external_encoding, undef: :replace), @output.string) end def test_text_override_index_selects_name @@ -102,8 +102,8 @@ class TestMenu < Minitest::Test menu.choice("Sample2", nil, "Sample1") end assert_equal(selected, "Sample1") - assert_equal("1. Sample2\n" + - "2. Sample1\n" + + assert_equal("1. Sample2\n" \ + "2. Sample1\n" \ "? ", @output.string) end @@ -116,8 +116,8 @@ class TestMenu < Minitest::Test menu.choice("Sample2", nil, "Sample1") end assert_equal(selected, "Sample2") - assert_equal("1. Sample2\n" + - "2. Sample1\n" + + assert_equal("1. Sample2\n" \ + "2. Sample1\n" \ "? ", @output.string) end @@ -130,8 +130,8 @@ class TestMenu < Minitest::Test menu.add_item(HighLine::Menu::Item.new("Sample2", text: "Sample1")) end assert_equal(selected, "Sample1") - assert_equal("1. Sample2\n" + - "2. Sample1\n" + + assert_equal("1. Sample2\n" \ + "2. Sample1\n" \ "? ", @output.string) end @@ -144,8 +144,8 @@ class TestMenu < Minitest::Test menu.add_item(HighLine::Menu::Item.new("Sample2", text: "Sample1")) end assert_equal(selected, "Sample2") - assert_equal("1. Sample2\n" + - "2. Sample1\n" + + assert_equal("1. Sample2\n" \ + "2. Sample1\n" \ "? ", @output.string) end @@ -158,8 +158,8 @@ class TestMenu < Minitest::Test menu.add_item(menu.build_item("Sample2", text: "Sample1")) end assert_equal(selected, "Sample2") - assert_equal("1. Sample2\n" + - "2. Sample1\n" + + assert_equal("1. Sample2\n" \ + "2. Sample1\n" \ "? ", @output.string) end @@ -178,22 +178,22 @@ class TestMenu < Minitest::Test menu.help("rules", "The rules of this system are as follows...") end end - assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " + - "This command will display helpful messages about " + - "functionality, like this one. To see the help for a " + - "specific topic enter:\n" + - "\thelp [TOPIC]\n" + - "Try asking for help on any of the following:\n" + - "\nload quit rules save \n" + - "1. load\n2. save\n3. quit\n4. help\n? " + - "= load\n\n" + - "Load a file.\n" + - "1. load\n2. save\n3. quit\n4. help\n? " + - "= rules\n\n" + - "The rules of this system are as follows...\n" + - "1. load\n2. save\n3. quit\n4. help\n? " + - "= missing\n\n" + - "There's no help for that topic.\n", @output.string ) + assert_equal("1. load\n2. save\n3. quit\n4. help\n? " \ + "This command will display helpful messages about " \ + "functionality, like this one. To see the help for a " \ + "specific topic enter:\n" \ + "\thelp [TOPIC]\n" \ + "Try asking for help on any of the following:\n" \ + "\nload quit rules save \n" \ + "1. load\n2. save\n3. quit\n4. help\n? " \ + "= load\n\n" \ + "Load a file.\n" \ + "1. load\n2. save\n3. quit\n4. help\n? " \ + "= rules\n\n" \ + "The rules of this system are as follows...\n" \ + "1. load\n2. save\n3. quit\n4. help\n? " \ + "= missing\n\n" \ + "There's no help for that topic.\n", @output.string) end def test_index @@ -238,7 +238,7 @@ class TestMenu < Minitest::Test @input.rewind @terminal.choose do |menu| - menu.index = "*" + menu.index = "*" menu.choice "Sample1" menu.choice "Sample2" @@ -265,7 +265,7 @@ class TestMenu < Minitest::Test assert_equal( HighLine.color("1. ", index_color) + "Sample1\n" + HighLine.color("2. ", index_color) + "Sample2\n" + - HighLine.color("3. ", index_color) + "Sample3\n" + + HighLine.color("3. ", index_color) + "Sample3\n" \ "? ", @output.string ) @@ -323,8 +323,8 @@ class TestMenu < Minitest::Test colored_asterix = HighLine.color("* ", index_color) assert_equal( - "#{colored_asterix}Sample1\n" + - "#{colored_asterix}Sample2\n" + + "#{colored_asterix}Sample1\n" \ + "#{colored_asterix}Sample2\n" \ "#{colored_asterix}Sample3\n? ", @output.string ) @@ -343,8 +343,8 @@ class TestMenu < Minitest::Test @terminal.choose(:load, :save, :quit) do |menu| menu.header = "File Menu" end - assert_equal( "File Menu:\n" + - "1. load\n2. save\n3. quit\n? ", @output.string ) + assert_equal("File Menu:\n" \ + "1. load\n2. save\n3. quit\n? ", @output.string) @input.rewind @output.truncate(@output.rewind) @@ -354,14 +354,14 @@ class TestMenu < Minitest::Test menu.header = "File Menu" menu.prompt = "Operation? " end - assert_equal( "File Menu: Operation? " + - "(load, save or quit) ", @output.string ) + assert_equal("File Menu: Operation? " \ + "(load, save or quit) ", @output.string) @input.rewind @output.truncate(@output.rewind) @terminal.choose(:load, :save, :quit) do |menu| - menu.layout = :menu_only + menu.layout = :menu_only end assert_equal("load, save or quit? ", @output.string) @@ -391,9 +391,9 @@ class TestMenu < Minitest::Test # Shows that by default proc results are returned. output = @terminal.choose do |menu| - menu.choice "Sample1" do "output1" end - menu.choice "Sample2" do "output2" end - menu.choice "Sample3" do "output3" end + menu.choice "Sample1" do "output1" end + menu.choice "Sample2" do "output2" end + menu.choice "Sample3" do "output3" end end assert_equal("output3", output) @@ -402,10 +402,10 @@ class TestMenu < Minitest::Test # _nil_on_handled to +true+. # output = @terminal.choose do |menu| - menu.nil_on_handled = true - menu.choice "Sample1" do "output1" end - menu.choice "Sample2" do "output2" end - menu.choice "Sample3" do "output3" end + menu.nil_on_handled = true + menu.choice "Sample1" do "output1" end + menu.choice "Sample2" do "output2" end + menu.choice "Sample3" do "output3" end end assert_equal(nil, output) @@ -445,7 +445,7 @@ class TestMenu < Minitest::Test menu.character = :getc end assert_equal(:Save, answer) - assert_equal(?a, @input.getc) + assert_equal('a', @input.getc) end def test_select_by @@ -527,7 +527,7 @@ class TestMenu < Minitest::Test @input.rewind selected = @terminal.choose do |menu| - menu.index = :letter + menu.index = :letter menu.choice :save menu.choice :load menu.choice :quit @@ -596,12 +596,12 @@ class TestMenu < Minitest::Test assert_equal(selected, 3) # Make sure paging message appeared - assert( @output.string.index('press enter/return to continue or q to stop'), - "Paging message did not appear." ) + assert(@output.string.index('press enter/return to continue or q to stop'), + "Paging message did not appear.") # Make sure it only appeared once - assert( @output.string !~ /q to stop.*q to stop/m, - "Paging message appeared more than once." ) + assert(@output.string !~ /q to stop.*q to stop/m, + "Paging message appeared more than once.") end def test_autocomplete_prompt @@ -614,13 +614,13 @@ class TestMenu < Minitest::Test menu.choice(:Ruby) menu.prompt = "What is your favorite programming language? " end - languages = [:Perl, :Python, :Ruby] - assert_equal("1. Perl\n" + - "2. Python\n" + - "3. Ruby\n" + - "What is your favorite programming language? " + - "You must choose one of [1, 2, 3, Perl, Python, Ruby].\n" + - "? ", @output.string ) + languages = %i[Perl Python Ruby] + assert_equal("1. Perl\n" \ + "2. Python\n" \ + "3. Ruby\n" \ + "What is your favorite programming language? " \ + "You must choose one of [1, 2, 3, Perl, Python, Ruby].\n" \ + "? ", @output.string) end # Issue #180 - https://github.com/JEG2/highline/issues/180 @@ -652,14 +652,14 @@ class TestMenu < Minitest::Test menu.choice "Sample2" menu.choice "last" end - assert_equal(["Sample1", "last"], selected) + assert_equal(%w[Sample1 last], selected) - assert_equal("1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + - "? 1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + + assert_equal("1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ + "? 1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ "? ", @output.string) end @@ -675,12 +675,12 @@ class TestMenu < Minitest::Test end assert_equal(["Sample1"], selected) - assert_equal("1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + - "? 1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + + assert_equal("1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ + "? 1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ "? ", @output.string) end @@ -696,12 +696,12 @@ class TestMenu < Minitest::Test end assert_equal(["Sample1"], selected) - assert_equal("1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + - "? 1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + + assert_equal("1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ + "? 1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ "? ", @output.string) end @@ -717,12 +717,12 @@ class TestMenu < Minitest::Test end assert_equal(["Sample1"], selected) - assert_equal("1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + - "? 1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + + assert_equal("1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ + "? 1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ "? ", @output.string) end @@ -738,12 +738,12 @@ class TestMenu < Minitest::Test end assert_equal({ "First" => "Sample1", second: "last" }, selected) - assert_equal("1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + - "? 1. Sample1\n" + - "2. Sample2\n" + - "3. last\n" + + assert_equal("1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ + "? 1. Sample1\n" \ + "2. Sample2\n" \ + "3. last\n" \ "? ", @output.string) end end diff --git a/test/test_paginator.rb b/test/test_paginator.rb index 6dfa15b..39036ee 100644 --- a/test/test_paginator.rb +++ b/test/test_paginator.rb @@ -19,13 +19,13 @@ class TestHighLinePaginator < Minitest::Test @input << "\n\n" @input.rewind - @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join) - assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join + + @terminal.say((1..50).map { |n| "This is line #{n}.\n" }.join) + assert_equal((1..22).map { |n| "This is line #{n}.\n" }.join + "\n-- press enter/return to continue or q to stop -- \n\n" + - (23..44).map { |n| "This is line #{n}.\n"}.join + + (23..44).map { |n| "This is line #{n}.\n" }.join + "\n-- press enter/return to continue or q to stop -- \n\n" + - (45..50).map { |n| "This is line #{n}.\n"}.join, - @output.string ) + (45..50).map { |n| "This is line #{n}.\n" }.join, + @output.string) end def test_statement_lines_count_equal_to_page_at_shouldnt_paginate @@ -70,4 +70,4 @@ class TestHighLinePaginator < Minitest::Test # expect not to raise an error on next line @terminal.say(list) end -end
\ No newline at end of file +end diff --git a/test/test_simulator.rb b/test/test_simulator.rb index 5f1f305..f22abe6 100644 --- a/test/test_simulator.rb +++ b/test/test_simulator.rb @@ -21,4 +21,4 @@ class SimulatorTest < Minitest::Test assert_equal "18", age end end -end
\ No newline at end of file +end diff --git a/test/test_string_extension.rb b/test/test_string_extension.rb index 9f87294..fb4b9ff 100644 --- a/test/test_string_extension.rb +++ b/test/test_string_extension.rb @@ -13,7 +13,6 @@ require "highline" require "stringio" require "string_methods" - # FakeString is here just to avoid # using HighLine.colorize_strings # on tests @@ -39,7 +38,7 @@ class TestStringExtension < Minitest::Test unless Gem::Version.new(YAML::VERSION) < Gem::Version.new("2.0.2") highline_string = HighLine::String.new("Yaml didn't messed with HighLine::String") yaml_highline_string = highline_string.to_yaml - yaml_loaded_string = YAML.load(yaml_highline_string) + yaml_loaded_string = YAML.safe_load(yaml_highline_string) assert_equal "Yaml didn't messed with HighLine::String", yaml_loaded_string assert_equal highline_string, yaml_loaded_string @@ -63,7 +62,7 @@ class TestStringExtension < Minitest::Test def test_String_includes_StringExtension_when_receives_colorize_strings @include_received = 0 - caller = Proc.new { @include_received += 1 } + caller = proc { @include_received += 1 } ::String.stub :include, caller do HighLine.colorize_strings end diff --git a/test/test_string_highline.rb b/test/test_string_highline.rb index 87db9c6..040e1a2 100644 --- a/test/test_string_highline.rb +++ b/test/test_string_highline.rb @@ -23,19 +23,19 @@ class TestHighLineString < Minitest::Test # Basic constructor assert_equal HighLine::String, @string.class assert_equal "string", @string - + # Alternative constructor method new_string = HighLine::String("string") assert_equal HighLine::String, new_string.class assert_equal @string, new_string - + # String methods work assert_equal 6, @string.size assert_equal "STRING", @string.upcase end include StringMethods - + def test_string_class_is_unchanged assert_raises(::NoMethodError) { "string".color(:blue) } end diff --git a/test/test_style.rb b/test/test_style.rb index 2c44401..a73a35b 100755 --- a/test/test_style.rb +++ b/test/test_style.rb @@ -13,15 +13,14 @@ require "highline" require "stringio" class TestStyle < Minitest::Test - def setup HighLine.reset @input = StringIO.new @output = StringIO.new - @terminal = HighLine.new(@input, @output) - @style1 = HighLine::Style.new(:name=>:foo, :code=>"\e[99m", :rgb=>[1,2,3]) - @style2 = HighLine::Style.new(:name=>:lando, :code=>"\e[98m") - @style3 = HighLine::Style.new(:name=>[:foo, :lando], :list=>[:foo, :lando]) + @terminal = HighLine.new(@input, @output) + @style1 = HighLine::Style.new(name: :foo, code: "\e[99m", rgb: [1, 2, 3]) + @style2 = HighLine::Style.new(name: :lando, code: "\e[98m") + @style3 = HighLine::Style.new(name: %i[foo lando], list: %i[foo lando]) @style4 = HighLine::Style(:rgb_654321) @added_styles_on_setup = 4 # update here if added more styles @added_codes_to_index = 3 # :foo, :lando and :rgb_654321 @@ -39,15 +38,15 @@ class TestStyle < Minitest::Test def test_clear_index_reset_code_index_to_builtin code_index = HighLine::Style.code_index - code_index_array = code_index.map { |code, style_array| style_array }.flatten + code_index_array = code_index.map { |_code, style_array| style_array }.flatten expected_code_index_array_size = code_index_array.size - @added_codes_to_index HighLine::Style.clear_index cleared_code_index = HighLine::Style.code_index - cleared_code_index_array = cleared_code_index.map { |code, style_array| style_array }.flatten + cleared_code_index_array = cleared_code_index.map { |_code, style_array| style_array }.flatten - assert_equal expected_code_index_array_size, cleared_code_index_array.size + assert_equal expected_code_index_array_size, cleared_code_index_array.size end def test_style_method @@ -58,7 +57,7 @@ class TestStyle < Minitest::Test assert_same new_style, s # i.e. s===the latest style created, but not the one searched for # Retrieve a style from a new Style (no new Style created) - s2 = HighLine::Style.new(:name=>:bar, :code=>"\e[97m") + s2 = HighLine::Style.new(name: :bar, code: "\e[97m") s = HighLine.Style(s2) assert_instance_of HighLine::Style, s assert_same s2, s @@ -96,35 +95,35 @@ class TestStyle < Minitest::Test s = HighLine.Style(@style2.name) assert_instance_of HighLine::Style, s assert_same @style2, s - + # See below for color scheme tests - + # Create style from a Hash - s = HighLine.Style(:name=>:han, :code=>"blah", :rgb=>'phooey') + s = HighLine.Style(name: :han, code: "blah", rgb: 'phooey') assert_instance_of HighLine::Style, s assert_equal :han, s.name assert_equal "blah", s.code assert_equal "phooey", s.rgb - + # Create style from an RGB foreground color code s = HighLine.Style(:rgb_1f2e3d) assert_instance_of HighLine::Style, s assert_equal :rgb_1f2e3d, s.name assert_equal "\e[38;5;23m", s.code # Trust me; more testing below - assert_equal [31,46,61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61 - + assert_equal [31, 46, 61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61 + # Create style from an RGB background color code s = HighLine.Style(:on_rgb_1f2e3d) assert_instance_of HighLine::Style, s assert_equal :on_rgb_1f2e3d, s.name assert_equal "\e[48;5;23m", s.code # Trust me; more testing below - assert_equal [31,46,61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61 + assert_equal [31, 46, 61], s.rgb # 0x1f==31, 0x2e==46, 0x3d=61 # Create a style list s1 = HighLine.Style(:bold, :red) assert_instance_of HighLine::Style, s1 - assert_equal [:bold, :red], s1.list - + assert_equal %i[bold red], s1.list + # Find an existing style list s2 = HighLine.Style(:bold, :red) assert_instance_of HighLine::Style, s2 @@ -133,28 +132,28 @@ class TestStyle < Minitest::Test # Create a style list with nils s1 = HighLine.Style(:underline, nil, :blue) assert_instance_of HighLine::Style, s1 - assert_equal [:underline, :blue], s1.list - + assert_equal %i[underline blue], s1.list + # Raise an error for an undefined style assert_raises(::NameError) { HighLine.Style(:fubar) } end - + def test_no_color_scheme HighLine.color_scheme = nil assert_raises(::NameError) { HighLine.Style(:critical) } end - + def test_with_color_scheme HighLine.color_scheme = HighLine::SampleColorScheme.new s = HighLine.Style(:critical) assert_instance_of HighLine::Style, s assert_equal :critical, s.name - assert_equal [:yellow, :on_red], s.list + assert_equal %i[yellow on_red], s.list end - + def test_builtin_foreground_colors_defined HighLine::COLORS.each do |color| - style = HighLine.const_get(color+'_STYLE') + style = HighLine.const_get(color + '_STYLE') assert_instance_of HighLine::Style, style assert_equal color.downcase.to_sym, style.name assert style.builtin @@ -162,10 +161,10 @@ class TestStyle < Minitest::Test assert_instance_of String, code, "Bad code for #{color}" end end - + def test_builtin_background_colors_defined HighLine::COLORS.each do |color| - style = HighLine.const_get('ON_' + color+'_STYLE') + style = HighLine.const_get('ON_' + color + '_STYLE') assert_instance_of HighLine::Style, style assert_equal "ON_#{color}".downcase.to_sym, style.name assert style.builtin @@ -173,10 +172,10 @@ class TestStyle < Minitest::Test assert_instance_of String, code, "Bad code for ON_#{color}" end end - + def test_builtin_styles_defined HighLine::STYLES.each do |style_constant| - style = HighLine.const_get(style_constant+'_STYLE') + style = HighLine.const_get(style_constant + '_STYLE') assert_instance_of HighLine::Style, style assert_equal style_constant.downcase.to_sym, style.name assert style.builtin @@ -184,12 +183,12 @@ class TestStyle < Minitest::Test assert_instance_of String, code, "Bad code for #{style_constant}" end end - + def test_index # Add a Style with a new name and code assert_nil HighLine::Style.list[:s1] assert_nil HighLine::Style.code_index['foo'] - s1 = HighLine::Style.new(:name=>:s1, :code=>'foo') + s1 = HighLine::Style.new(name: :s1, code: 'foo') refute_nil HighLine::Style.list[:s1] assert_same s1, HighLine::Style.list[:s1] assert_equal :s1, HighLine::Style.list[:s1].name @@ -205,9 +204,9 @@ class TestStyle < Minitest::Test # Add another Style with a new name and code assert_nil HighLine::Style.list[:s2] assert_nil HighLine::Style.code_index['bar'] - s2 = HighLine::Style.new(:name=>:s2, :code=>'bar') - assert_equal styles+1, HighLine::Style.list.size - assert_equal codes+1, HighLine::Style.code_index.size + s2 = HighLine::Style.new(name: :s2, code: 'bar') + assert_equal styles + 1, HighLine::Style.list.size + assert_equal codes + 1, HighLine::Style.code_index.size refute_nil HighLine::Style.list[:s2] assert_same s2, HighLine::Style.list[:s2] assert_equal :s2, HighLine::Style.list[:s2].name @@ -217,16 +216,16 @@ class TestStyle < Minitest::Test assert_same s2, HighLine::Style.code_index['bar'].last assert_equal :s2, HighLine::Style.code_index['bar'].last.name assert_equal 'bar', HighLine::Style.code_index['bar'].last.code - + # Add a Style with an existing name s3_before = HighLine::Style.list[:s2] refute_nil HighLine::Style.list[:s2] assert_nil HighLine::Style.code_index['baz'] - s3 = HighLine::Style.new(:name=>:s2, :code=>'baz') + s3 = HighLine::Style.new(name: :s2, code: 'baz') refute_same s2, s3 refute_same s3_before, s3 - assert_equal styles+1, HighLine::Style.list.size - assert_equal codes+2, HighLine::Style.code_index.size + assert_equal styles + 1, HighLine::Style.list.size + assert_equal codes + 2, HighLine::Style.code_index.size refute_nil HighLine::Style.list[:s2] assert_same s3, HighLine::Style.list[:s2] refute_same s2, HighLine::Style.list[:s2] @@ -240,9 +239,9 @@ class TestStyle < Minitest::Test # Add a Style with an existing code assert_equal 1, HighLine::Style.code_index['baz'].size - s4 = HighLine::Style.new(:name=>:s4, :code=>'baz') - assert_equal styles+2, HighLine::Style.list.size - assert_equal codes+2, HighLine::Style.code_index.size + s4 = HighLine::Style.new(name: :s4, code: 'baz') + assert_equal styles + 2, HighLine::Style.list.size + assert_equal codes + 2, HighLine::Style.code_index.size refute_nil HighLine::Style.list[:s4] assert_same s4, HighLine::Style.list[:s4] assert_equal :s4, HighLine::Style.list[:s4].name @@ -255,27 +254,27 @@ class TestStyle < Minitest::Test assert_equal :s4, HighLine::Style.code_index['baz'].last.name assert_equal 'baz', HighLine::Style.code_index['baz'].last.code end - + def test_rgb_hex assert_equal "abcdef", HighLine::Style.rgb_hex("abcdef") - assert_equal "ABCDEF", HighLine::Style.rgb_hex("AB","CD","EF") - assert_equal "010203", HighLine::Style.rgb_hex(1,2,3) - assert_equal "123456", HighLine::Style.rgb_hex(18,52,86) + assert_equal "ABCDEF", HighLine::Style.rgb_hex("AB", "CD", "EF") + assert_equal "010203", HighLine::Style.rgb_hex(1, 2, 3) + assert_equal "123456", HighLine::Style.rgb_hex(18, 52, 86) end - + def test_rgb_parts - assert_equal [1,2,3], HighLine::Style.rgb_parts("010203") - assert_equal [18,52,86], HighLine::Style.rgb_parts("123456") + assert_equal [1, 2, 3], HighLine::Style.rgb_parts("010203") + assert_equal [18, 52, 86], HighLine::Style.rgb_parts("123456") end - + def test_rgb s = HighLine::Style.rgb(1, 2, 3) assert_instance_of HighLine::Style, s assert_equal :rgb_010203, s.name - assert_equal [1,2,3], s.rgb + assert_equal [1, 2, 3], s.rgb assert_equal "\e[38;5;16m", s.code - s = HighLine::Style.rgb("12", "34","56") + s = HighLine::Style.rgb("12", "34", "56") assert_instance_of HighLine::Style, s assert_equal :rgb_123456, s.name assert_equal [0x12, 0x34, 0x56], s.rgb @@ -287,104 +286,104 @@ class TestStyle < Minitest::Test assert_equal [0xab, 0xcd, 0xef], s.rgb assert_equal "\e[38;5;189m", s.code end - + def test_rgb_number - # ANSI RGB coding splits 0..255 into equal sixths, and then the + # ANSI RGB coding splits 0..255 into equal sixths, and then the # red green and blue are encoded in base 6, plus 16, i.e. # 16 + 36*(red_level) + 6*(green_level) + blue_level, # where each of red_level, green_level, and blue_level are in # the range 0..5 - + # This test logic works because 42 is just below 1/6 of 255, # and 43 is just above - - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 0, 0) - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 0, 42) - assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 0, 0, 43) - - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 42, 0) - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 0, 42, 42) - assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 0, 42, 43) - - assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 0, 43, 0) - assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 0, 43, 42) - assert_equal 16 + 0*36 + 1*6 + 1, HighLine::Style.rgb_number( 0, 43, 43) - - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 0, 0) - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 0, 42) - assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 42, 0, 43) - - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 42, 0) - assert_equal 16 + 0*36 + 0*6 + 0, HighLine::Style.rgb_number( 42, 42, 42) - assert_equal 16 + 0*36 + 0*6 + 1, HighLine::Style.rgb_number( 42, 42, 43) - - assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 42, 43, 0) - assert_equal 16 + 0*36 + 1*6 + 0, HighLine::Style.rgb_number( 42, 43, 42) - assert_equal 16 + 0*36 + 1*6 + 1, HighLine::Style.rgb_number( 42, 43, 43) - - assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 0, 0) - assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 0, 42) - assert_equal 16 + 1*36 + 0*6 + 1, HighLine::Style.rgb_number( 43, 0, 43) - - assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 42, 0) - assert_equal 16 + 1*36 + 0*6 + 0, HighLine::Style.rgb_number( 43, 42, 42) - assert_equal 16 + 1*36 + 0*6 + 1, HighLine::Style.rgb_number( 43, 42, 43) - - assert_equal 16 + 1*36 + 1*6 + 0, HighLine::Style.rgb_number( 43, 43, 0) - assert_equal 16 + 1*36 + 1*6 + 0, HighLine::Style.rgb_number( 43, 43, 42) - assert_equal 16 + 1*36 + 1*6 + 1, HighLine::Style.rgb_number( 43, 43, 43) - - assert_equal 16 + 5*36 + 5*6 + 5, HighLine::Style.rgb_number(255,255,255) + + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(0, 0, 0) + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(0, 0, 42) + assert_equal 16 + 0 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(0, 0, 43) + + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(0, 42, 0) + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(0, 42, 42) + assert_equal 16 + 0 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(0, 42, 43) + + assert_equal 16 + 0 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(0, 43, 0) + assert_equal 16 + 0 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(0, 43, 42) + assert_equal 16 + 0 * 36 + 1 * 6 + 1, HighLine::Style.rgb_number(0, 43, 43) + + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(42, 0, 0) + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(42, 0, 42) + assert_equal 16 + 0 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(42, 0, 43) + + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(42, 42, 0) + assert_equal 16 + 0 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(42, 42, 42) + assert_equal 16 + 0 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(42, 42, 43) + + assert_equal 16 + 0 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(42, 43, 0) + assert_equal 16 + 0 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(42, 43, 42) + assert_equal 16 + 0 * 36 + 1 * 6 + 1, HighLine::Style.rgb_number(42, 43, 43) + + assert_equal 16 + 1 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(43, 0, 0) + assert_equal 16 + 1 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(43, 0, 42) + assert_equal 16 + 1 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(43, 0, 43) + + assert_equal 16 + 1 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(43, 42, 0) + assert_equal 16 + 1 * 36 + 0 * 6 + 0, HighLine::Style.rgb_number(43, 42, 42) + assert_equal 16 + 1 * 36 + 0 * 6 + 1, HighLine::Style.rgb_number(43, 42, 43) + + assert_equal 16 + 1 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(43, 43, 0) + assert_equal 16 + 1 * 36 + 1 * 6 + 0, HighLine::Style.rgb_number(43, 43, 42) + assert_equal 16 + 1 * 36 + 1 * 6 + 1, HighLine::Style.rgb_number(43, 43, 43) + + assert_equal 16 + 5 * 36 + 5 * 6 + 5, HighLine::Style.rgb_number(255, 255, 255) end - + def test_ansi_rgb_to_hex - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1) - - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1) - - assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0) - assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0) - assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1) - - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1) - - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 0) - assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 0*6 + 1) - - assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0) - assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 0) - assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0*36 + 1*6 + 1) - - assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0) - assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0) - assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1) - - assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0) - assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 0) - assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 0*6 + 1) - - assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0) - assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 0) - assert_equal "2b2b2b", HighLine::Style.ansi_rgb_to_hex(16 + 1*36 + 1*6 + 1) - + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 1) + + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 1) + + assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 0) + assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 0) + assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 1) + + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 1) + + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "000000", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 0) + assert_equal "00002b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 0 * 6 + 1) + + assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 0) + assert_equal "002b00", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 0) + assert_equal "002b2b", HighLine::Style.ansi_rgb_to_hex(16 + 0 * 36 + 1 * 6 + 1) + + assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 0) + assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 0) + assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 1) + + assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 0) + assert_equal "2b0000", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 0) + assert_equal "2b002b", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 0 * 6 + 1) + + assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 1 * 6 + 0) + assert_equal "2b2b00", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 1 * 6 + 0) + assert_equal "2b2b2b", HighLine::Style.ansi_rgb_to_hex(16 + 1 * 36 + 1 * 6 + 1) + # 0xd5 is the smallest number where n/255.0*6.0 > 5 - assert_equal "d5d5d5", HighLine::Style.ansi_rgb_to_hex(16 + 5*36 + 5*6 + 5) + assert_equal "d5d5d5", HighLine::Style.ansi_rgb_to_hex(16 + 5 * 36 + 5 * 6 + 5) end - + def test_list list_size = HighLine::Style.list.size # Add a Style with a new name and code assert_nil HighLine::Style.list[:s5] - s5 = HighLine::Style.new(:name=>:s5, :code=>'foo') + s5 = HighLine::Style.new(name: :s5, code: 'foo') refute_nil HighLine::Style.list[:s5] - assert_equal list_size+1, HighLine::Style.list.size + assert_equal list_size + 1, HighLine::Style.list.size refute_nil HighLine::Style.list[:s5] assert_same s5, HighLine::Style.list[:s5] assert_equal :s5, HighLine::Style.list[:s5].name @@ -392,30 +391,30 @@ class TestStyle < Minitest::Test # Add another Style with a new name and code assert_nil HighLine::Style.list[:s6] - s6 = HighLine::Style.new(:name=>:s6, :code=>'bar') - assert_equal list_size+2, HighLine::Style.list.size + s6 = HighLine::Style.new(name: :s6, code: 'bar') + assert_equal list_size + 2, HighLine::Style.list.size refute_nil HighLine::Style.list[:s6] assert_same s6, HighLine::Style.list[:s6] assert_equal :s6, HighLine::Style.list[:s6].name assert_equal 'bar', HighLine::Style.list[:s6].code - + # Add a Style with an existing name - s7 = HighLine::Style.new(:name=>:s6, :code=>'baz') - assert_equal list_size+2, HighLine::Style.list.size # No net addition to list + s7 = HighLine::Style.new(name: :s6, code: 'baz') + assert_equal list_size + 2, HighLine::Style.list.size # No net addition to list refute_nil HighLine::Style.list[:s6] assert_same s7, HighLine::Style.list[:s6] # New one replaces old one refute_same s6, HighLine::Style.list[:s6] assert_equal :s6, HighLine::Style.list[:s6].name assert_equal 'baz', HighLine::Style.list[:s6].code end - + def test_code_index list_size = HighLine::Style.code_index.size - + # Add a Style with a new name and code assert_nil HighLine::Style.code_index['chewie'] - HighLine::Style.new(:name=>:s8, :code=>'chewie') - assert_equal list_size+1, HighLine::Style.code_index.size + HighLine::Style.new(name: :s8, code: 'chewie') + assert_equal list_size + 1, HighLine::Style.code_index.size assert_instance_of Array, HighLine::Style.code_index['chewie'] assert_equal 1, HighLine::Style.code_index['chewie'].size assert_equal :s8, HighLine::Style.code_index['chewie'].last.name @@ -423,8 +422,8 @@ class TestStyle < Minitest::Test # Add another Style with a new name and code assert_nil HighLine::Style.code_index['c3po'] - HighLine::Style.new(:name=>:s9, :code=>'c3po') - assert_equal list_size+2, HighLine::Style.code_index.size + HighLine::Style.new(name: :s9, code: 'c3po') + assert_equal list_size + 2, HighLine::Style.code_index.size assert_instance_of Array, HighLine::Style.code_index['c3po'] assert_equal 1, HighLine::Style.code_index['c3po'].size assert_equal :s9, HighLine::Style.code_index['c3po'].last.name @@ -432,33 +431,33 @@ class TestStyle < Minitest::Test # Add a Style with an existing code assert_equal 1, HighLine::Style.code_index['c3po'].size - HighLine::Style.new(:name=>:s10, :code=>'c3po') - assert_equal list_size+2, HighLine::Style.code_index.size + HighLine::Style.new(name: :s10, code: 'c3po') + assert_equal list_size + 2, HighLine::Style.code_index.size assert_equal 2, HighLine::Style.code_index['c3po'].size assert_equal :s10, HighLine::Style.code_index['c3po'].last.name assert_equal 'c3po', HighLine::Style.code_index['c3po'].last.code end - + def test_uncolor # Normal color assert_equal "This should be reverse underlined magenta!\n", - HighLine::Style.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n" ) + HighLine::Style.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n") # RGB color assert_equal "This should be rgb_906030!\n", - HighLine::Style.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n" ) + HighLine::Style.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n") end - + def test_color assert_equal "\e[99mstring\e[0m", @style1.color("string") # simple style assert_equal "\e[99m\e[98mstring\e[0m", @style3.color("string") # Style list end - + def test_code assert_equal "\e[99m", @style1.code # simple style assert_equal "\e[99m\e[98m", @style3.code # Style list end - + def test_red assert_equal 0x65, @style4.red assert_equal 0, HighLine::Style(:none).red # Probably reliable @@ -466,15 +465,15 @@ class TestStyle < Minitest::Test assert_equal 255, HighLine::Style(:bright_magenta).red # Seems to be reliable assert_equal 255, HighLine::Style(:on_none).red # Probably reliable end - + def test_green assert_equal 0x43, @style4.green assert_equal 0, HighLine::Style(:none).green # Probably reliable assert_equal 0, HighLine::Style(:black).green # Probably reliable - assert 240 <= HighLine::Style(:bright_cyan).green # Probably reliable + assert HighLine::Style(:bright_cyan).green >= 240 # Probably reliable assert_equal 255, HighLine::Style(:on_none).green # Probably reliable end - + def test_blue assert_equal 0x21, @style4.blue assert_equal 0, HighLine::Style(:none).blue # Probably reliable @@ -482,121 +481,121 @@ class TestStyle < Minitest::Test assert_equal 255, HighLine::Style(:bright_blue).blue # Probably reliable assert_equal 255, HighLine::Style(:on_none).blue # Probably reliable end - + def test_builtin assert HighLine::Style(:red).builtin assert !@style1.builtin end - + def test_variant style1_name = @style1.name style1_code = @style1.code style1_rgb = @style1.rgb - - s1 = @style1.variant(:new_foo1, :code=>'abracadabra') + + s1 = @style1.variant(:new_foo1, code: 'abracadabra') assert_instance_of HighLine::Style, s1 refute_same @style1, s1 # This is a copy assert_equal :new_foo1, s1.name # Changed assert_equal 'abracadabra', s1.code # Changed - assert_equal [1,2,3], s1.rgb # Unchanged - - s2 = @style1.variant(:new_foo2, :increment=>-15) + assert_equal [1, 2, 3], s1.rgb # Unchanged + + s2 = @style1.variant(:new_foo2, increment: -15) assert_instance_of HighLine::Style, s2 - refute_same @style1, s2 # This is a copy + refute_same @style1, s2 # This is a copy assert_equal :new_foo2, s2.name # Changed assert_equal "\e[84m", s2.code # 99 (original code) - 15 - assert_equal [1,2,3], s2.rgb # Unchanged - - s3 = @style1.variant(:new_foo3, :code=>"\e[55m", :increment=>15) + assert_equal [1, 2, 3], s2.rgb # Unchanged + + s3 = @style1.variant(:new_foo3, code: "\e[55m", increment: 15) assert_instance_of HighLine::Style, s3 - refute_same @style1, s3 # This is a copy + refute_same @style1, s3 # This is a copy assert_equal :new_foo3, s3.name # Changed assert_equal "\e[70m", s3.code # 99 (new code) + 15 - assert_equal [1,2,3], s3.rgb # Unchanged - - s4 = @style1.variant(:new_foo4, :code=>"\e[55m", :increment=>15, :rgb=>"blah") + assert_equal [1, 2, 3], s3.rgb # Unchanged + + s4 = @style1.variant(:new_foo4, code: "\e[55m", increment: 15, rgb: "blah") assert_instance_of HighLine::Style, s4 - refute_same @style1, s4 # This is a copy + refute_same @style1, s4 # This is a copy assert_equal :new_foo4, s4.name # Changed assert_equal "\e[70m", s4.code # 99 (new code) + 15 assert_equal 'blah', s4.rgb # Changed s5 = @style1.variant(:new_foo5) assert_instance_of HighLine::Style, s5 - refute_same @style1, s5 # This is a copy + refute_same @style1, s5 # This is a copy assert_equal :new_foo5, s5.name # Changed assert_equal "\e[99m", s5.code # Unchanged - assert_equal [1,2,3], s5.rgb # Unchanged + assert_equal [1, 2, 3], s5.rgb # Unchanged # No @style1's have been harmed in the running of this test assert_equal style1_name, @style1.name assert_equal style1_code, @style1.code assert_equal style1_rgb, @style1.rgb - + assert_raises(::RuntimeError) { @style3.variant(:new_foo6) } # Can't create a variant of a list style end - + def test_on style1_name = @style1.name style1_code = @style1.code style1_rgb = @style1.rgb - + s1 = @style1.on assert_instance_of HighLine::Style, s1 - refute_same @style1, s1 # This is a copy + refute_same @style1, s1 # This is a copy assert_equal :on_foo, s1.name # Changed assert_equal "\e[109m", s1.code # Changed - assert_equal [1,2,3], s1.rgb # Unchanged + assert_equal [1, 2, 3], s1.rgb # Unchanged # No @style1's have been harmed in the running of this test assert_equal style1_name, @style1.name assert_equal style1_code, @style1.code assert_equal style1_rgb, @style1.rgb - + assert_raises(::RuntimeError) { @style3.on } # Can't create a variant of a list style end - + def test_bright style1_name = @style1.name style1_code = @style1.code style1_rgb = @style1.rgb - + s1 = @style1.bright assert_instance_of HighLine::Style, s1 - refute_same @style1, s1 # This is a copy + refute_same @style1, s1 # This is a copy assert_equal :bright_foo, s1.name # Changed assert_equal "\e[159m", s1.code # Changed - assert_equal [129,130,131], s1.rgb # Changed + assert_equal [129, 130, 131], s1.rgb # Changed # No @style1's have been harmed in the running of this test assert_equal style1_name, @style1.name assert_equal style1_code, @style1.code assert_equal style1_rgb, @style1.rgb - - s2_base = HighLine::Style.new(:name=>:leia, :code=>"\e[92m", :rgb=>[0,0,14]) + + s2_base = HighLine::Style.new(name: :leia, code: "\e[92m", rgb: [0, 0, 14]) s2 = s2_base.bright assert_instance_of HighLine::Style, s2 - refute_same s2_base, s2 # This is a copy + refute_same s2_base, s2 # This is a copy assert_equal :bright_leia, s2.name # Changed assert_equal "\e[152m", s2.code # Changed - assert_equal [0,0,142], s2.rgb # Changed - - s3_base = HighLine::Style.new(:name=>:luke, :code=>"\e[93m", :rgb=>[20,21,0]) + assert_equal [0, 0, 142], s2.rgb # Changed + + s3_base = HighLine::Style.new(name: :luke, code: "\e[93m", rgb: [20, 21, 0]) s3 = s3_base.bright assert_instance_of HighLine::Style, s3 - refute_same s3_base, s3 # This is a copy + refute_same s3_base, s3 # This is a copy assert_equal :bright_luke, s3.name # Changed assert_equal "\e[153m", s3.code # Changed - assert_equal [148,149,0], s3.rgb # Changed - - s4_base = HighLine::Style.new(:name=>:r2d2, :code=>"\e[94m", :rgb=>[0,0,0]) + assert_equal [148, 149, 0], s3.rgb # Changed + + s4_base = HighLine::Style.new(name: :r2d2, code: "\e[94m", rgb: [0, 0, 0]) s4 = s4_base.bright assert_instance_of HighLine::Style, s4 - refute_same s4_base, s4 # This is a copy + refute_same s4_base, s4 # This is a copy assert_equal :bright_r2d2, s4.name # Changed assert_equal "\e[154m", s4.code # Changed - assert_equal [128,128,128], s4.rgb # Changed; special case - + assert_equal [128, 128, 128], s4.rgb # Changed; special case + assert_raises(::RuntimeError) { @style3.bright } # Can't create a variant of a list style end diff --git a/test/test_wrapper.rb b/test/test_wrapper.rb index f7e4871..1db89fc 100644 --- a/test/test_wrapper.rb +++ b/test/test_wrapper.rb @@ -21,19 +21,19 @@ class TestHighLineWrapper < Minitest::Test def test_wrap_long_lines_correctly long_line = - "This is a long flowing paragraph meant to span " + - "several lines. This text should definitely be " + - "wrapped at the set limit, in the result. Your code " + + "This is a long flowing paragraph meant to span " \ + "several lines. This text should definitely be " \ + "wrapped at the set limit, in the result. Your code " \ "does well with things like this.\n\n" wrapped_long_line = - "This is a long flowing paragraph meant to span " + - "several lines. This text should\n" + + "This is a long flowing paragraph meant to span " \ + "several lines. This text should\n" \ - "definitely be wrapped at the set limit, in the " + - "result. Your code does well with\n" + + "definitely be wrapped at the set limit, in the " \ + "result. Your code does well with\n" \ - "things like this.\n\n" + "things like this.\n\n" wrapped = wrap(long_line) assert_equal wrapped_long_line, wrapped @@ -41,13 +41,13 @@ class TestHighLineWrapper < Minitest::Test def test_dont_wrap_already_well_wrapped_text well_formatted_text = - " * This is a simple embedded list.\n" + - " * You're code should not mess with this...\n" + - " * Because it's already formatted correctly and does not\n" + + " * This is a simple embedded list.\n" \ + " * You're code should not mess with this...\n" \ + " * Because it's already formatted correctly and does not\n" \ " exceed the limit!\n" wrapped = wrap(well_formatted_text) - assert_equal well_formatted_text, wrapped + assert_equal well_formatted_text, wrapped end def test_wrap_single_word_longer_than_wrap_at @@ -185,4 +185,4 @@ class TestHighLineWrapper < Minitest::Test end end end -end
\ No newline at end of file +end |