Back ported a lot of changes made to the Koans directory.
authorJim Weirich <jim.weirich@gmail.com>
Sun, 4 Dec 2011 07:00:22 +0000 (02:00 -0500)
committerJim Weirich <jim.weirich@gmail.com>
Sun, 4 Dec 2011 07:00:22 +0000 (02:00 -0500)
Evidently, a lot of changes / pull requests were made to the koans
directory and not to the src directory.  Perhaps we should remove the
koans directory entirely from the repo.

16 files changed:
koans/about_control_statements.rb
koans/about_proxy_object_project.rb
koans/about_strings.rb
koans/about_symbols.rb
koans/about_triangle_project_2.rb
koans/edgecase.rb
src/about_classes.rb
src/about_constants.rb
src/about_control_statements.rb
src/about_hashes.rb
src/about_methods.rb
src/about_proxy_object_project.rb
src/about_regular_expressions.rb
src/about_triangle_project_2.rb
src/about_true_and_false.rb
src/edgecase.rb

index 71a7af0..df503d7 100644 (file)
@@ -117,7 +117,7 @@ class AboutControlStatements < EdgeCase::Koan
     while i < 10
       i += 1
       next if (i % 2) == 0
-      result << i 
+      result << i
     end
     assert_equal __, result
   end
index 1666e45..064eb68 100644 (file)
@@ -27,51 +27,52 @@ class AboutProxyObjectProject < EdgeCase::Koan
   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
@@ -100,7 +101,7 @@ end
 # Example class using in the proxy testing above.
 class Television
   attr_accessor :channel
-  
+
   def power
     if @power == :on
       @power = :off
@@ -108,7 +109,7 @@ class Television
       @power = :on
     end
   end
-  
+
   def on?
     @power == :on
   end
@@ -118,31 +119,31 @@ 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
 
index aa427d4..34d4a50 100644 (file)
@@ -141,6 +141,13 @@ EOS
     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
@@ -157,26 +164,6 @@ EOS
     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
index f4a4319..a88d6fe 100644 (file)
@@ -84,7 +84,7 @@ class AboutSymbols < EdgeCase::Koan
   # 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
index fc90ba1..0a57e25 100644 (file)
@@ -11,7 +11,7 @@ class AboutTriangleProject2 < EdgeCase::Koan
     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
 
index d727cd7..0707687 100644 (file)
@@ -2,10 +2,11 @@
 # -*- ruby -*-
 
 require 'test/unit/assertions'
-begin 
+begin
   require 'win32console'
 rescue LoadError
 end
+
 # --------------------------------------------------------------------
 # Support code for the Ruby Koans.
 # --------------------------------------------------------------------
@@ -121,6 +122,7 @@ module EdgeCase
     def using_windows?
       File::ALT_SEPARATOR
     end
+
     def using_win32console
       defined? Win32::Console
     end
index b8b67ea..a8336bf 100644 (file)
@@ -126,7 +126,7 @@ class AboutClasses < EdgeCase::Koan
     # 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")
 
index ad780c6..dd0bc39 100644 (file)
@@ -67,7 +67,7 @@ class AboutConstants < EdgeCase::Koan
   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?
 
   # ------------------------------------------------------------------
 
@@ -81,7 +81,7 @@ class AboutConstants < EdgeCase::Koan
     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
index 319bde0..1e799e6 100644 (file)
@@ -59,12 +59,20 @@ class AboutControlStatements < EdgeCase::Koan
 
   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
@@ -109,7 +117,7 @@ class AboutControlStatements < EdgeCase::Koan
     while i < 10
       i += 1
       next if (i % 2) == 0
-      result << i 
+      result << i
     end
     assert_equal __([1, 3, 5, 7, 9]), result
   end
index 49b3939..0915fa6 100644 (file)
@@ -20,6 +20,18 @@ class AboutHashes < EdgeCase::Koan
     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"
@@ -63,4 +75,42 @@ class AboutHashes < EdgeCase::Koan
     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
index 9e7af37..b720010 100644 (file)
@@ -72,6 +72,7 @@ class AboutMethods < EdgeCase::Koan
   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)
index 483a7d7..7e8be03 100644 (file)
@@ -29,7 +29,7 @@ class Proxy
     @messages << sym
     @object.send(sym, *args, &block)
   end
-  
+
   def called?(method)
     @messages.include?(method)
   end
@@ -46,50 +46,52 @@ class AboutProxyObjectProject < EdgeCase::Koan
   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
@@ -118,7 +120,7 @@ end
 # Example class using in the proxy testing above.
 class Television
   attr_accessor :channel
-  
+
   def power
     if @power == :on
       @power = :off
@@ -126,7 +128,7 @@ class Television
       @power = :on
     end
   end
-  
+
   def on?
     @power == :on
   end
@@ -136,31 +138,31 @@ 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
 
index 55b85f6..03e8f9a 100644 (file)
@@ -84,6 +84,8 @@ class AboutRegularExpressions < EdgeCase::Koan
   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
 
index c48c3cb..0a57e25 100644 (file)
@@ -11,6 +11,7 @@ class AboutTriangleProject2 < EdgeCase::Koan
     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
 
index 4c99d99..e9910f6 100644 (file)
@@ -10,7 +10,7 @@ class AboutTrueAndFalse < EdgeCase::Koan
   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
index ba49956..0707687 100644 (file)
@@ -2,6 +2,10 @@
 # -*- ruby -*-
 
 require 'test/unit/assertions'
+begin
+  require 'win32console'
+rescue LoadError
+end
 
 # --------------------------------------------------------------------
 # Support code for the Ruby Koans.
@@ -107,7 +111,9 @@ module EdgeCase
     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
@@ -116,6 +122,10 @@ module EdgeCase
     def using_windows?
       File::ALT_SEPARATOR
     end
+
+    def using_win32console
+      defined? Win32::Console
+    end
   end
 
   class Sensei