Skip to content

Commit

Permalink
Merge pull request #146 from abinoam/feat_refac_2
Browse files Browse the repository at this point in the history
Fix SimpleCov configuration and add some tests
  • Loading branch information
abinoam committed Jul 9, 2015
2 parents 80492e2 + 2eca7ea commit bf04da6
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .simplecov
@@ -0,0 +1,5 @@
unless SimpleCov.running
SimpleCov.start do
add_filter "test_"
end
end
2 changes: 1 addition & 1 deletion lib/highline/question.rb
Expand Up @@ -314,7 +314,7 @@ def change_case( answer_string )
# completed for any reason.
#
def convert
Question::AnswerConverter.new(self).convert
AnswerConverter.new(self).convert
end

def check_range
Expand Down
44 changes: 22 additions & 22 deletions lib/highline/question/answer_converter.rb
Expand Up @@ -23,62 +23,62 @@ def convert
answer
end

private

def convert_by_answer_type
if answer_type.respond_to? :parse
answer_type.parse(answer)
elsif answer_type.is_a? Class
send(answer_type.name)
else
send(answer_type.class.name)
end
end

def String
def to_string
HighLine::String(answer)
end

# That's a weird name for a method!
# But it's working ;-)
define_method "HighLine::String" do
define_method "to_highline::string" do
HighLine::String(answer)
end

def Integer
def to_integer
Kernel.send(:Integer, answer)
end

def Float
def to_float
Kernel.send(:Float, answer)
end

def Symbol
def to_symbol
answer.to_sym
end

def Regexp
def to_regexp
Regexp.new(answer)
end

def File
def to_file
self.answer = choices_complete(answer)
File.open(File.join(directory.to_s, answer.last))
end

def Pathname
def to_pathname
self.answer = choices_complete(answer)
Pathname.new(File.join(directory.to_s, answer.last))
end

def Array
def to_array
self.answer = choices_complete(answer)
answer.last
end

def Proc
def to_proc
answer_type.call(answer)
end

private

def convert_by_answer_type
if answer_type.respond_to? :parse
answer_type.parse(answer)
elsif answer_type.is_a? Class
send("to_#{answer_type.name.downcase}")
else
send("to_#{answer_type.class.name.downcase}")
end
end
end
end
end
26 changes: 26 additions & 0 deletions test/test_answer_converter.rb
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
# coding: utf-8

require "test_helper"

require "highline/question"

class TestAnswerConverter < Minitest::Test
def test_integer_convertion
question = HighLine::Question.new("What's your age?", Integer)
question.answer = "18"
answer_converter = HighLine::Question::AnswerConverter.new(question)

refute_equal "18", answer_converter.convert
assert_equal 18, answer_converter.convert
end

def test_float_convertion
question = HighLine::Question.new("Write PI", Float)
question.answer = "3.14159"
answer_converter = HighLine::Question::AnswerConverter.new(question)

refute_equal "3.14159", answer_converter.convert
assert_equal 3.14159, answer_converter.convert
end
end
1 change: 0 additions & 1 deletion test/test_color_scheme.rb
Expand Up @@ -8,7 +8,6 @@
#
# This is Free Software. See LICENSE and COPYING for details.

require "minitest/autorun"
require "test_helper"

require "highline"
Expand Down
10 changes: 5 additions & 5 deletions test/test_helper.rb
Expand Up @@ -2,10 +2,10 @@
# coding: utf-8

require 'simplecov'
SimpleCov.start do
add_filter "test_"
end

require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
if ENV['CODECLIMATE_REPO_TOKEN']
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
end

require "minitest/autorun"
107 changes: 105 additions & 2 deletions test/test_highline.rb
Expand Up @@ -8,7 +8,6 @@
#
# This is Free Software. See LICENSE and COPYING for details.

require "minitest/autorun"
require "test_helper"

require "highline"
Expand Down Expand Up @@ -98,7 +97,6 @@ def test_ask_string_converting
assert_raises(EOFError) { @terminal.ask("Any input left? ", HighLine::String) }
end


def test_indent
text = "Testing...\n"
@terminal.indent_level=1
Expand Down Expand Up @@ -221,6 +219,35 @@ def test_case_changes
assert_equal("crazy", answer)
end

def test_ask_with_overwrite
@input << "Yes, sure!\n"
@input.rewind

answer = @terminal.ask("Do you like Ruby? ") do |q|
q.overwrite = true
q.echo = false
end

assert_equal("Yes, sure!", answer)
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
assert_equal("Do you like Ruby? #{erase_sequence}", @output.string)
end

def test_ask_with_overwrite_and_character_mode
@input << "Y"
@input.rewind

answer = @terminal.ask("Do you like Ruby (Y/N)? ") do |q|
q.overwrite = true
q.echo = false
q.character = true
end

assert_equal("Y", answer)
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
assert_equal("Do you like Ruby (Y/N)? #{erase_sequence}", @output.string)
end

def test_character_echo
@input << "password\r"
@input.rewind
Expand Down Expand Up @@ -408,6 +435,14 @@ def test_color
@terminal.say("This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!")
assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n", @output.string)

# 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 }

# 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?
Expand Down Expand Up @@ -499,6 +534,35 @@ def test_confirm
@output.string )
end

def test_generic_confirm_with_true
@input << "junk.txt\nno\nsave.txt\ny\n"
@input.rewind

answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = true
q.responses[:ask_on_error] = :question
end
assert_equal("save.txt", answer)
assert_equal( "Enter a filename: " +
"Are you sure? " +
"Enter a filename: " +
"Are you sure? ",
@output.string )

@input.truncate(@input.rewind)
@input << "junk.txt\nyes\nsave.txt\nn\n"
@input.rewind
@output.truncate(@output.rewind)

answer = @terminal.ask("Enter a filename: ") do |q|
q.confirm = true
end
assert_equal("junk.txt", answer)
assert_equal( "Enter a filename: " +
"Are you sure? ",
@output.string )
end

def test_defaults
@input << "\nNo Comment\n"
@input.rewind
Expand Down Expand Up @@ -1046,6 +1110,24 @@ def test_range_requirements
"(below 0).\n" +
"? ", @output.string )

@input.truncate(@input.rewind)
@input << "-541\n11\n6\n"
@input.rewind
@output.truncate(@output.rewind)

answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
q.above = 0
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 )

@input.truncate(@input.rewind)
@input << "1\n-541\n6\n"
@input.rewind
Expand Down Expand Up @@ -1296,6 +1378,20 @@ def test_whitespace

@input.rewind

answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :strip
end
assert_equal("A lot\tof \t space\t \there!", answer)

@input.rewind

answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = :collapse
end
assert_equal(" A lot of space here! ", answer)

@input.rewind

answer = @terminal.ask("Enter a whitespace filled string: ")
assert_equal("A lot\tof \t space\t \there!", answer)

Expand All @@ -1319,6 +1415,13 @@ def test_whitespace
q.whitespace = :none
end
assert_equal(" A lot\tof \t space\t \there! \n", answer)

@input.rewind

answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
q.whitespace = nil
end
assert_equal(" A lot\tof \t space\t \there! \n", answer)
end

def test_track_eof
Expand Down
1 change: 0 additions & 1 deletion test/test_import.rb
Expand Up @@ -8,7 +8,6 @@
#
# This is Free Software. See LICENSE and COPYING for details.

require "minitest/autorun"
require "test_helper"

require "highline/import"
Expand Down
1 change: 0 additions & 1 deletion test/test_list.rb
@@ -1,7 +1,6 @@
#!/usr/bin/env ruby
# coding: utf-8

require "minitest/autorun"
require "test_helper"

require "highline/list"
Expand Down
1 change: 0 additions & 1 deletion test/test_menu.rb
Expand Up @@ -8,7 +8,6 @@
#
# This is Free Software. See LICENSE and COPYING for details.

require "minitest/autorun"
require "test_helper"

require "highline"
Expand Down
1 change: 0 additions & 1 deletion test/test_paginator.rb
@@ -1,7 +1,6 @@
#!/usr/bin/env ruby
# coding: utf-8

require "minitest/autorun"
require "test_helper"

require "highline"
Expand Down
1 change: 0 additions & 1 deletion test/test_simulator.rb
@@ -1,4 +1,3 @@
require "minitest/autorun"
require "test_helper"

require "highline/import"
Expand Down
28 changes: 27 additions & 1 deletion test/test_string_extension.rb
Expand Up @@ -7,7 +7,6 @@
#
# This is Free Software. See LICENSE and COPYING for details.

require "minitest/autorun"
require "test_helper"

require "highline"
Expand All @@ -29,6 +28,10 @@ def setup
@string = FakeString.new "string"
end

def teardown
HighLine.reset
end

include StringMethods

def test_Highline_String_is_yaml_serializable
Expand All @@ -43,4 +46,27 @@ def test_Highline_String_is_yaml_serializable
assert_instance_of HighLine::String, yaml_loaded_string
end
end

def test_highline_string_respond_to_color
assert HighLine::String.new("highline string").respond_to? :color
end

def test_normal_string_doesnt_respond_to_color
refute "normal_string".respond_to? :color
end

def test_highline_string_still_raises_for_non_available_messages
assert_raises(NoMethodError) do
@string.unknown_message
end
end

def test_String_includes_StringExtension_when_receives_colorize_strings
@include_received = 0
caller = Proc.new { @include_received += 1 }
::String.stub :include, caller do
HighLine.colorize_strings
end
assert_equal 1, @include_received
end
end

0 comments on commit bf04da6

Please sign in to comment.