while i < 10
i += 1
next if (i % 2) == 0
- result << i
+ result << i
end
assert_equal __, result
end
def test_proxy_method_returns_wrapped_object
# NOTE: The Television class is defined below
tv = Proxy.new(Television.new)
-
+
+ # HINT: Proxy class is defined above, may need tweaking...
+
assert tv.instance_of?(Proxy)
end
-
+
def test_tv_methods_still_perform_their_function
tv = Proxy.new(Television.new)
- # HINT Proxy class is defined above, may need tweaking...
tv.channel = 10
tv.power
-
+
assert_equal 10, tv.channel
assert tv.on?
end
def test_proxy_records_messages_sent_to_tv
tv = Proxy.new(Television.new)
-
+
tv.power
tv.channel = 10
-
+
assert_equal [:power, :channel=], tv.messages
end
-
+
def test_proxy_handles_invalid_messages
tv = Proxy.new(Television.new)
-
+
assert_raise(NoMethodError) do
tv.no_such_method
end
end
-
+
def test_proxy_reports_methods_have_been_called
tv = Proxy.new(Television.new)
-
+
tv.power
tv.power
-
+
assert tv.called?(:power)
assert ! tv.called?(:channel)
end
-
+
def test_proxy_counts_method_calls
tv = Proxy.new(Television.new)
-
+
tv.power
tv.channel = 48
tv.power
# Example class using in the proxy testing above.
class Television
attr_accessor :channel
-
+
def power
if @power == :on
@power = :off
@power = :on
end
end
-
+
def on?
@power == :on
end
class TelevisionTest < EdgeCase::Koan
def test_it_turns_on
tv = Television.new
-
+
tv.power
assert tv.on?
end
-
+
def test_it_also_turns_off
tv = Television.new
-
+
tv.power
tv.power
-
+
assert ! tv.on?
end
-
+
def test_edge_case_on_off
tv = Television.new
-
+
tv.power
tv.power
tv.power
-
+
assert tv.on?
-
+
tv.power
-
+
assert ! tv.on?
end
assert_equal __, string[7..9]
end
+ def test_you_can_get_a_single_character_from_a_string
+ string = "Bacon, lettuce and tomato"
+ assert_equal __, string[1]
+
+ # Surprised?
+ end
+
in_ruby_version("1.8") do
def test_in_ruby_1_8_single_characters_are_represented_by_integers
assert_equal __, ?a
end
end
-in_ruby_version("1.8") do
- def test_in_ruby_1_8_you_can_get_a_single_character_from_a_string
- string = "Bacon, lettuce and tomato"
- assert_equal __, string[1]
-
- # Surprised?
- end
- end
-
- in_ruby_version("1.9") do
-
- def test_in_ruby_1_9_you_can_get_a_single_character_from_a_string
- string = "Bacon, lettuce and tomato"
- assert_equal "__", string[1]
-
- # Surprised?
- end
- end
-
-
def test_strings_can_be_split
string = "Sausage Egg Cheese"
words = string.split
# interesting string operations are available on symbols.
def test_symbols_cannot_be_concatenated
- # Exceptions will be pondered further farther down the path
+ # Exceptions will be pondered further down the path
assert_raise(___) do
:cats + :dogs
end
assert_raise(TriangleError) do triangle(3, 4, -5) end
assert_raise(TriangleError) do triangle(1, 1, 3) end
assert_raise(TriangleError) do triangle(2, 4, 2) end
- #HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
+ # HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
end
end
# -*- ruby -*-
require 'test/unit/assertions'
-begin
+begin
require 'win32console'
rescue LoadError
end
+
# --------------------------------------------------------------------
# Support code for the Ruby Koans.
# --------------------------------------------------------------------
def using_windows?
File::ALT_SEPARATOR
end
+
def using_win32console
defined? Win32::Console
end
# Why is this so?
end
- def test_different_objects_have_difference_instance_variables
+ def test_different_objects_have_different_instance_variables
fido = Dog6.new("Fido")
rover = Dog6.new("Rover")
end
# QUESTION: Which has precedence: The constant in the lexical scope,
- # or the constant from the inheritance heirarachy?
+ # or the constant from the inheritance hierarchy?
# ------------------------------------------------------------------
assert_equal __(4), MyAnimals::Oyster.new.legs_in_oyster
end
- # QUESTION: Now Which has precedence: The constant in the lexical
- # scope, or the constant from the inheritance heirarachy? Why is it
+ # QUESTION: Now which has precedence: The constant in the lexical
+ # scope, or the constant from the inheritance hierarchy? Why is it
# different than the previous answer?
end
def test_unless_statement
result = :default_value
- unless false
+ unless false # same as saying 'if !false', which evaluates as 'if true'
result = :false_value
end
assert_equal __(:false_value), result
end
+ def test_unless_statement_evaluate_true
+ result = :default_value
+ unless true # same as saying 'if !true', which evaluates as 'if false'
+ result = :true_value
+ end
+ assert_equal __(:default_value), result
+ end
+
def test_unless_statement_modifier
result = :default_value
result = :false_value unless false
while i < 10
i += 1
next if (i % 2) == 0
- result << i
+ result << i
end
assert_equal __([1, 3, 5, 7, 9]), result
end
assert_equal __(nil), hash[:doesnt_exist]
end
+ def test_accessing_hashes_with_fetch
+ hash = { :one => "uno" }
+ assert_equal "uno", hash.fetch(:one)
+ assert_raise(___(IndexError)) do
+ hash.fetch(:doesnt_exist)
+ end
+
+ # THINK ABOUT IT:
+ #
+ # Why might you want to use #fetch instead of #[] when accessing hash keys?
+ end
+
def test_changing_hashes
hash = { :one => "uno", :two => "dos" }
hash[:one] = "eins"
expected = { "jim" => __(54), "amy" => 20, "dan" => 23, "jenny" => __(26) }
assert_equal __(true), expected == new_hash
end
+
+ def test_default_value
+ hash1 = Hash.new
+ hash1[:one] = 1
+
+ assert_equal __(1), hash1[:one]
+ assert_equal __(nil), hash1[:two]
+
+ hash2 = Hash.new("dos")
+ hash2[:one] = 1
+
+ assert_equal __(1), hash2[:one]
+ assert_equal __("dos"), hash2[:two]
+ end
+
+ def test_default_value_is_the_same_object
+ hash = Hash.new([])
+
+ hash[:one] << "uno"
+ hash[:two] << "dos"
+
+ assert_equal __(["uno", "dos"]), hash[:one]
+ assert_equal __(["uno", "dos"]), hash[:two]
+ assert_equal __(["uno", "dos"]), hash[:three]
+
+ assert_equal __(true), hash[:one].object_id == hash[:two].object_id
+ end
+
+ def test_default_value_with_block
+ hash = Hash.new {|hash, key| hash[key] = [] }
+
+ hash[:one] << "uno"
+ hash[:two] << "dos"
+
+ assert_equal __(["uno"]), hash[:one]
+ assert_equal __(["dos"]), hash[:two]
+ assert_equal __([]), hash[:three]
+ end
end
end
def test_calling_with_variable_arguments
+ assert_equal __(Array), method_with_var_args.class
assert_equal __([]), method_with_var_args
assert_equal __([:one]), method_with_var_args(:one)
assert_equal __([:one, :two]), method_with_var_args(:one, :two)
@messages << sym
@object.send(sym, *args, &block)
end
-
+
def called?(method)
@messages.include?(method)
end
def test_proxy_method_returns_wrapped_object
# NOTE: The Television class is defined below
tv = Proxy.new(Television.new)
-
+
+ # HINT: Proxy class is defined above, may need tweaking...
+
assert tv.instance_of?(Proxy)
end
-
+
def test_tv_methods_still_perform_their_function
tv = Proxy.new(Television.new)
-
+
tv.channel = 10
tv.power
-
+
assert_equal 10, tv.channel
assert tv.on?
end
def test_proxy_records_messages_sent_to_tv
tv = Proxy.new(Television.new)
-
+
tv.power
tv.channel = 10
-
+
assert_equal [:power, :channel=], tv.messages
end
-
+
def test_proxy_handles_invalid_messages
tv = Proxy.new(Television.new)
-
+
assert_raise(NoMethodError) do
tv.no_such_method
end
end
-
+
def test_proxy_reports_methods_have_been_called
tv = Proxy.new(Television.new)
-
+
tv.power
tv.power
-
+
assert tv.called?(:power)
assert ! tv.called?(:channel)
end
-
+
def test_proxy_counts_method_calls
tv = Proxy.new(Television.new)
-
+
tv.power
tv.channel = 48
tv.power
# Example class using in the proxy testing above.
class Television
attr_accessor :channel
-
+
def power
if @power == :on
@power = :off
@power = :on
end
end
-
+
def on?
@power == :on
end
class TelevisionTest < EdgeCase::Koan
def test_it_turns_on
tv = Television.new
-
+
tv.power
assert tv.on?
end
-
+
def test_it_also_turns_off
tv = Television.new
-
+
tv.power
tv.power
-
+
assert ! tv.on?
end
-
+
def test_edge_case_on_off
tv = Television.new
-
+
tv.power
tv.power
tv.power
-
+
assert tv.on?
-
+
tv.power
-
+
assert ! tv.on?
end
def test_shortcut_character_classes_are_negated_with_capitals
assert_equal __("the number is "), "the number is 42"[/\D+/]
assert_equal __("space:"), "space: \t\n"[/\S+/]
+ # ... a programmer would most likely do
+ assert_equal __(" = "), "variable_1 = 42"[/[^a-zA-Z0-9_]+/]
assert_equal __(" = "), "variable_1 = 42"[/\W+/]
end
assert_raise(TriangleError) do triangle(3, 4, -5) end
assert_raise(TriangleError) do triangle(1, 1, 3) end
assert_raise(TriangleError) do triangle(2, 4, 2) end
+ # HINT: for tips, see http://stackoverflow.com/questions/3834203/ruby-koan-151-raising-exceptions
end
end
end
def test_true_is_treated_as_true
- assert_equal __(:true_stuff), truth_value(true)
+ assert_equal __(:true_stuff), truth_value(true)
end
def test_false_is_treated_as_false
# -*- ruby -*-
require 'test/unit/assertions'
+begin
+ require 'win32console'
+rescue LoadError
+end
# --------------------------------------------------------------------
# Support code for the Ruby Koans.
def use_colors?
return false if ENV['NO_COLOR']
if ENV['ANSI_COLOR'].nil?
- ! using_windows?
+ if using_windows?
+ using_win32console
+ end
else
ENV['ANSI_COLOR'] =~ /^(t|y)/i
end
def using_windows?
File::ALT_SEPARATOR
end
+
+ def using_win32console
+ defined? Win32::Console
+ end
end
class Sensei