CLOBBER.include(DIST_DIR)
module Koans
+ # Remove solution info from source
+ # __(a,b) => __
+ # _n_(number) => __
+ # # __ =>
def Koans.remove_solution(line)
line = line.gsub(/\b____\([^\)]+\)/, "____")
line = line.gsub(/\b___\([^\)]+\)/, "___")
line = line.gsub(/\b__\([^\)]+\)/, "__")
line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
line = line.gsub(%r(/\#\{__\}/), "/__/")
+ line = line.gsub(/\s*#\s*__\s*$/, '')
line
end
sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
end
-desc "Check that the require files match the about_* files"
-task :check do
- about_files = Dir['src/about_*.rb'].size
- about_requires = `grep require src/path_to_enlightenment.rb | wc -l`.to_i
- puts "# of about files: #{about_files}"
- puts "# of about requires: #{about_requires}"
-end
-
desc "Generate the Koans from the source files from scratch."
task :regen => [:clobber_koans, :gen]
class AboutArrays < EdgeCase::Koan
def test_creating_arrays
empty_array = Array.new
- assert_equal Array, empty_array.class
+ assert_equal __, empty_array.class
assert_equal __, empty_array.size
end
class AboutArrays < EdgeCase::Koan
def test_creating_arrays
empty_array = Array.new
- assert_equal Array, empty_array.class
+ assert_equal __(Array), empty_array.class
assert_equal __(0), empty_array.size
end
def test_array_literals
array = Array.new
- assert_equal [], array
+ assert_equal [], array # __
array[0] = 1
- assert_equal [1], array
+ assert_equal [1], array # __
array[1] = 2
assert_equal [1, __(2)], array
end
def test_arrays_and_ranges
- assert_equal Range, (1..5).class
- assert_not_equal [1,2,3,4,5], (1..5)
- assert_equal [1,2,3,4,5], (1..5).to_a
+ assert_equal __(Range), (1..5).class
+ assert_not_equal [1,2,3,4,5], (1..5) # __
+ assert_equal __([1,2,3,4,5]), (1..5).to_a
assert_equal __([1,2,3,4]), (1...5).to_a
end
fido = Dog6.new("Fido")
rover = Dog6.new("Rover")
- assert_not_equal rover.name, fido.name
+ assert_equal __(true), rover.name != fido.name
end
# ------------------------------------------------------------------
def test_to_s_provides_a_string_version_of_the_object
fido = Dog7.new("Fido")
- assert_equal "Fido", fido.to_s
+ assert_equal __("Fido"), fido.to_s
end
def test_to_s_is_used_in_string_interpolation
fido = Dog7.new("Fido")
- assert_equal "My dog is Fido", "My dog is #{fido}"
+ assert_equal __("My dog is Fido"), "My dog is #{fido}"
end
def test_inspect_provides_a_more_complete_string_version
assert_equal __(:exception_handled), result
- assert ex.is_a?(StandardError), "Failure message."
- assert ex.is_a?(RuntimeError), "Failure message."
+ assert ex.is_a?(___(StandardError)), "Failure message."
+ assert ex.is_a?(___(RuntimeError)), "Failure message."
- assert RuntimeError.ancestors.include?(StandardError),
+ assert RuntimeError.ancestors.include?(StandardError), # __
"RuntimeError is a subclass of StandardError"
-
+
assert_equal __("Oops"), ex.message
end
end
# Sometimes, we must know about the unknown
- def test_asserting_an_error_is_raised
+ def test_asserting_an_error_is_raised # __
# A do-end is a block, a topic to explore more later
assert_raise(___(MySpecialError)) do
raise MySpecialError.new("New instances can be raised directly.")
class AboutHashes < EdgeCase::Koan
def test_creating_hashes
empty_hash = Hash.new
- assert_equal Hash, empty_hash.class
- assert_equal({}, empty_hash)
+ assert_equal __(Hash), empty_hash.class
+ assert_equal({}, empty_hash) # __
assert_equal __(0), empty_hash.size
end
hash[:one] = "eins"
expected = { :one => __("eins"), :two => "dos" }
- assert_equal expected, hash
+ assert_equal __(true), expected == hash
# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
hash1 = { :one => "uno", :two => "dos" }
hash2 = { :two => "dos", :one => "uno" }
- assert_equal hash1, hash2
+ assert_equal __(true), hash1 == hash2
end
def test_hash_keys
assert_equal __(2), hash.keys.size
assert_equal __(true), hash.keys.include?(:one)
assert_equal __(true), hash.keys.include?(:two)
- assert_equal Array, hash.keys.class
+ assert_equal __(Array), hash.keys.class
end
def test_hash_values
assert_equal __(2), hash.values.size
assert_equal __(true), hash.values.include?("uno")
assert_equal __(true), hash.values.include?("dos")
- assert_equal Array, hash.values.class
+ assert_equal __(Array), hash.values.class
end
def test_combining_hashes
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
- assert_not_equal hash, new_hash
-
+ assert_equal __(true), hash != new_hash
+
expected = { "jim" => __(54), "amy" => 20, "dan" => 23, "jenny" => __(26) }
- assert_equal expected, new_hash
+ assert_equal __(true), expected == new_hash
end
end
array.each do |item|
sum += item
end
- assert_equal 6, sum
+ assert_equal __(6), sum
end
def test_each_can_use_curly_brace_blocks_too
def test_methods_can_be_called_directly
mc = MessageCatcher.new
- assert mc.caught?
+ assert mc.caught? # __
end
def test_methods_can_be_invoked_by_sending_the_message
mc = MessageCatcher.new
- assert mc.send(:caught?)
+ assert mc.send(:caught?) # __
end
def test_methods_can_be_invoked_more_dynamically
mc = MessageCatcher.new
- assert mc.send("caught?")
+ assert mc.send("caught?") # __
assert mc.send("caught" + __("?") ) # What do you need to add to the first string?
assert mc.send("CAUGHT?".____(:downcase) ) # What would you need to do to the string?
end
exception = assert_raise(___(NoMethodError)) do
typical.foobar
end
- assert_match(/foobar/, exception.message)
+ assert_match(/foobar/, exception.message) # __
end
def test_calling_method_missing_causes_the_no_method_error
exception = assert_raise(___(NoMethodError)) do
typical.method_missing(:foobar)
end
- assert_match(/foobar/, exception.message)
+ assert_match(/foobar/, exception.message) # __
# THINK ABOUT IT:
#
def test_catching_messages_makes_respond_to_lie
catcher = AllMessageCatcher.new
- assert_nothing_raised(NoMethodError) do
+ assert_nothing_raised(NoMethodError) do # __
catcher.any_method
end
assert_equal __(false), catcher.respond_to?(:any_method)
# considered to be syntactically invalid).
def test_sometimes_missing_parentheses_are_ambiguous
#--
- eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK
+ eval "assert_equal 5, my_global_method(2, 3)" # REMOVE CHECK # __
if false
#++
- eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
+ eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK # __
#--
end
#++
def test_module_methods_are_also_availble_in_the_object
fido = Dog.new
- assert_nothing_raised(Exception) do
- fido.set_name("Rover")
+ assert_nothing_raised(Exception) do # __
+ fido.set_name("Rover")
end
end
class AboutRegularExpressions < EdgeCase::Koan
def test_a_pattern_is_a_regular_expression
- assert_equal Regexp, /pattern/.class
+ assert_equal __(Regexp), /pattern/.class
end
def test_a_regexp_can_search_a_string_for_matching_content
- assert_equal "match", "some matching content"[/match/]
+ assert_equal __("match"), "some matching content"[/match/]
end
def test_a_failed_match_returns_nil
assert_equal __(:jims_dog), fido.identify
assert_equal __(:joes_dog), rover.identify
- assert_not_equal fido.class, rover.class
- assert_not_equal Jims::Dog, Joes::Dog
+ assert_equal __(true), fido.class != rover.class
+ assert_equal __(true), Jims::Dog != Joes::Dog
end
# ------------------------------------------------------------------
require 'test/unit/assertions'
+# --------------------------------------------------------------------
+# Support code for the Ruby Koans.
+# --------------------------------------------------------------------
+
class FillMeInError < StandardError
end
yield if versions.any? { |v| ruby_version?(v) }
end
+# Standard, generic replacement value.
+# If value19 is given, it is used inplace of value for Ruby 1.9.
def __(value="FILL ME IN", value19=:mu)
if RUBY_VERSION < "1.9"
value
end
end
+# Numeric replacement value.
def _n_(value=999999, value19=:mu)
if RUBY_VERSION < "1.9"
value
end
end
+# Error object replacement value.
def ___(value=FillMeInError)
value
end
+# Method name replacement.
class Object
def ____(method=nil)
if method