Skip to content

Commit

Permalink
Updated koans from src.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Sep 28, 2010
1 parent c0bbe77 commit b41d616
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 28 deletions.
12 changes: 9 additions & 3 deletions koans/about_array_assignment.rb
Expand Up @@ -5,7 +5,7 @@ def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal __, names
end

def test_parallel_assignments
first_name, last_name = ["John", "Smith"]
assert_equal __, first_name
Expand All @@ -18,13 +18,19 @@ def test_parallel_assignments_with_extra_values
assert_equal __, last_name
end

def test_parallel_assignments_with_extra_variables
def test_parallel_assignments_with_splat_operator
first_name, *last_name = ["John", "Smith", "III"]
assert_equal "John", first_name
assert_equal ["Smith","III"], last_name
end

def test_parallel_assignments_with_too_few_variables
first_name, last_name = ["Cher"]
assert_equal __, first_name
assert_equal __, last_name
end

def test_parallel_assignements_with_subarrays
def test_parallel_assignments_with_subarrays
first_name, last_name = [["Willie", "Rae"], "Johnson"]
assert_equal __, first_name
assert_equal __, last_name
Expand Down
4 changes: 2 additions & 2 deletions koans/about_arrays.rb
Expand Up @@ -45,9 +45,9 @@ def test_slicing_arrays
end

def test_arrays_and_ranges
assert_equal Range, (1..5).class
assert_equal __, (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..5).to_a
assert_equal __, (1...5).to_a
end

Expand Down
6 changes: 3 additions & 3 deletions koans/about_classes.rb
Expand Up @@ -130,7 +130,7 @@ def test_different_objects_have_difference_instance_variables
fido = Dog6.new("Fido")
rover = Dog6.new("Rover")

assert_not_equal rover.name, fido.name
assert_equal __, rover.name != fido.name
end

# ------------------------------------------------------------------
Expand Down Expand Up @@ -164,12 +164,12 @@ def test_inside_a_method_self_refers_to_the_containing_object

def test_to_s_provides_a_string_version_of_the_object
fido = Dog7.new("Fido")
assert_equal "Fido", fido.to_s
assert_equal __, 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}"
end

def test_inspect_provides_a_more_complete_string_version
Expand Down
6 changes: 3 additions & 3 deletions koans/about_exceptions.rb
Expand Up @@ -22,12 +22,12 @@ def test_rescue_clause

assert_equal __, result

assert ex.is_a?(StandardError), "Failure message."
assert ex.is_a?(RuntimeError), "Failure message."
assert ex.is_a?(___), "Failure message."
assert ex.is_a?(___), "Failure message."

assert RuntimeError.ancestors.include?(StandardError),
"RuntimeError is a subclass of StandardError"

assert_equal __, ex.message
end

Expand Down
16 changes: 8 additions & 8 deletions koans/about_hashes.rb
Expand Up @@ -3,7 +3,7 @@
class AboutHashes < EdgeCase::Koan
def test_creating_hashes
empty_hash = Hash.new
assert_equal Hash, empty_hash.class
assert_equal __, empty_hash.class
assert_equal({}, empty_hash)
assert_equal __, empty_hash.size
end
Expand All @@ -25,7 +25,7 @@ def test_changing_hashes
hash[:one] = "eins"

expected = { :one => __, :two => "dos" }
assert_equal expected, hash
assert_equal __, expected == hash

# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
Expand All @@ -35,32 +35,32 @@ def test_hash_is_unordered
hash1 = { :one => "uno", :two => "dos" }
hash2 = { :two => "dos", :one => "uno" }

assert_equal hash1, hash2
assert_equal __, hash1 == hash2
end

def test_hash_keys
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash.keys.size
assert_equal __, hash.keys.include?(:one)
assert_equal __, hash.keys.include?(:two)
assert_equal Array, hash.keys.class
assert_equal __, hash.keys.class
end

def test_hash_values
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash.values.size
assert_equal __, hash.values.include?("uno")
assert_equal __, hash.values.include?("dos")
assert_equal Array, hash.values.class
assert_equal __, 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 __, hash != new_hash

expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
assert_equal expected, new_hash
assert_equal __, expected == new_hash
end
end
2 changes: 1 addition & 1 deletion koans/about_iteration.rb
Expand Up @@ -12,7 +12,7 @@ def test_iterating_with_each
array.each do |item|
sum += item
end
assert_equal 6, sum
assert_equal __, sum
end

def test_each_can_use_curly_brace_blocks_too
Expand Down
98 changes: 98 additions & 0 deletions koans/about_java_interop.rb
@@ -0,0 +1,98 @@
require File.expand_path(File.dirname(__FILE__) + '/edgecase')

include Java

# Concepts
# * Pull in a java class
# * calling a method, Camel vs snake
# * Resovling module/class name conflicts
# * Showing what gets returned
# * Ruby Strings VS Java Strings
# * Calling custom java class
# * Calling Ruby from java???

class AboutJavaInterop < EdgeCase::Koan
def test_using_a_java_library_class
java_array = java.util.ArrayList.new
assert_equal __, java_array.class
end

def test_java_class_can_be_referenced_using_both_ruby_and_java_like_syntax
assert_equal __, Java::JavaUtil::ArrayList == java.util.ArrayList
end

def test_include_class_includes_class_in_module_scope
assert_nil defined?(TreeSet)
include_class "java.util.TreeSet"
assert_equal __, defined?(TreeSet)
end

# THINK ABOUT IT:
#
# What if we use:
#
# include_class "java.lang.String"
#
# What would be the value of the String constant after this
# include_class is run? Would it be useful to provide a way of
# aliasing java classes to different names?

JString = java.lang.String
def test_also_java_class_can_be_given_ruby_aliases
java_string = JString.new("A Java String")
assert_equal __, java_string.class
assert_equal __, JString
end

def test_can_directly_call_java_methods_on_java_objects
java_string = JString.new("A Java String")
assert_equal __, java_string.toLowerCase
end

def test_jruby_provides_snake_case_versions_of_java_methods
java_string = JString.new("A Java String")
assert_equal __, java_string.to_lower_case
end

def test_jruby_provides_question_mark_versions_of_boolean_methods
java_string = JString.new("A Java String")
assert_equal __, java_string.endsWith("String")
assert_equal __, java_string.ends_with("String")
assert_equal __, java_string.ends_with?("String")
end

def test_java_string_are_not_ruby_strings
ruby_string = "A Java String"
java_string = java.lang.String.new(ruby_string)
assert_equal __, java_string.is_a?(java.lang.String)
assert_equal __, java_string.is_a?(String)
end

def test_java_strings_can_be_compared_to_ruby_strings_maybe
ruby_string = "A Java String"
java_string = java.lang.String.new(ruby_string)
assert_equal __, ruby_string == java_string
assert_equal __, java_string == ruby_string

# THINK ABOUT IT:
#
# Is there any possible way for this to be more wrong?
#
# SERIOUSLY, THINK ABOUT IT:
#
# Why do you suppose that Ruby and Java strings compare like that?
#
# ADVANCED THINK ABOUT IT:
#
# Is there a way to make Ruby/Java string comparisons commutative?
# How would you do it?
end

def test_however_most_methods_returning_strings_return_ruby_strings
java_array = java.util.ArrayList.new
assert_equal __, java_array.toString
assert_equal __, java_array.toString.is_a?(String)
assert_equal __, java_array.toString.is_a?(java.lang.String)
end

end
2 changes: 1 addition & 1 deletion koans/about_modules.rb
Expand Up @@ -45,7 +45,7 @@ def test_normal_methods_are_available_in_the_object
def test_module_methods_are_also_availble_in_the_object
fido = Dog.new
assert_nothing_raised(Exception) do
fido.set_name("Rover")
fido.set_name("Rover")
end
end

Expand Down
4 changes: 2 additions & 2 deletions koans/about_regular_expressions.rb
Expand Up @@ -3,11 +3,11 @@

class AboutRegularExpressions < EdgeCase::Koan
def test_a_pattern_is_a_regular_expression
assert_equal Regexp, /pattern/.class
assert_equal __, /pattern/.class
end

def test_a_regexp_can_search_a_string_for_matching_content
assert_equal "match", "some matching content"[/match/]
assert_equal __, "some matching content"[/match/]
end

def test_a_failed_match_returns_nil
Expand Down
4 changes: 2 additions & 2 deletions koans/about_scope.rb
Expand Up @@ -29,8 +29,8 @@ def test_you_can_reference_nested_classes_using_the_scope_operator
assert_equal __, fido.identify
assert_equal __, rover.identify

assert_not_equal fido.class, rover.class
assert_not_equal Jims::Dog, Joes::Dog
assert_equal __, fido.class != rover.class
assert_equal __, Jims::Dog != Joes::Dog
end

# ------------------------------------------------------------------
Expand Down
48 changes: 45 additions & 3 deletions koans/edgecase.rb
Expand Up @@ -3,6 +3,10 @@

require 'test/unit/assertions'

# --------------------------------------------------------------------
# Support code for the Ruby Koans.
# --------------------------------------------------------------------

class FillMeInError < StandardError
end

Expand All @@ -16,6 +20,8 @@ def in_ruby_version(*versions)
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
Expand All @@ -24,6 +30,7 @@ def __(value="FILL ME IN", value19=:mu)
end
end

# Numeric replacement value.
def _n_(value=999999, value19=:mu)
if RUBY_VERSION < "1.9"
value
Expand All @@ -32,10 +39,12 @@ def _n_(value=999999, value19=:mu)
end
end

# Error object replacement value.
def ___(value=FillMeInError)
value
end

# Method name replacement.
class Object
def ____(method=nil)
if method
Expand All @@ -48,7 +57,25 @@ def ____(method=nil)
end
end

class String
def side_padding(width)
extra = width - self.size
if width < 0
self
else
left_padding = extra / 2
right_padding = (extra+1)/2
(" " * left_padding) + self + (" " *right_padding)
end
end
end

module EdgeCase
class << self
def simple_output
ENV['SIMPLE_KOAN_OUTPUT'] == 'true'
end
end

module Color
#shamelessly stolen (and modified) from redgreen
Expand Down Expand Up @@ -176,6 +203,21 @@ def show_progress
end

def end_screen
if EdgeCase.simple_output
boring_end_screen
else
artistic_end_screen
end
end

def boring_end_screen
puts "Mountains are again merely mountains"
end

def artistic_end_screen
"JRuby 1.9.x Koans"
ruby_version = "(in #{'J' if defined?(JRUBY_VERSION)}Ruby #{defined?(JRUBY_VERSION) ? JRUBY_VERSION : RUBY_VERSION})"
ruby_version = ruby_version.side_padding(54)
completed = <<-ENDTEXT
,, , ,,
: ::::, :::,
Expand All @@ -192,8 +234,8 @@ def end_screen
,:::::::::::, ::::::::::::,
:::::::::::, ,::::::::::::
::::::::::::: ,::::::::::::
:::::::::::: Ruby Koans ::::::::::::,
:::::::::::: ,::::::::::::,
:::::::::::: Ruby Koans ::::::::::::,
::::::::::::#{ ruby_version },::::::::::::,
:::::::::::, , ::::::::::::
,:::::::::::::, brought to you by ,,::::::::::::,
:::::::::::::: ,::::::::::::
Expand Down Expand Up @@ -260,7 +302,7 @@ def indent(text)

def find_interesting_lines(backtrace)
backtrace.reject { |line|
line =~ /test\/unit\/|edgecase\.rb/
line =~ /test\/unit\/|edgecase\.rb|minitest/
}
end

Expand Down

0 comments on commit b41d616

Please sign in to comment.