Skip to content

Commit

Permalink
updated to latest koans files (sans edgecase.rb)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Peabody committed Jan 11, 2011
1 parent ef8dfb6 commit 868d376
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 12 deletions.
11 changes: 5 additions & 6 deletions koans/about_dice_project.rb
@@ -1,11 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/edgecase')

class DiceSet
attr_reader :values
def roll(n)
@values = (1..n).map { rand(6) + 1 }
end
end
# Implement a DiceSet Class here:
#
# class DiceSet
# code ...
# end

class AboutDiceProject < EdgeCase::Koan
def test_can_create_a_dice_set
Expand Down
13 changes: 13 additions & 0 deletions koans/about_java_interop.rb
Expand Up @@ -95,6 +95,19 @@ def test_however_most_methods_returning_strings_return_ruby_strings
assert_equal __, java_array.toString.is_a?(java.lang.String)
end

def test_some_ruby_objects_can_be_coerced_to_java
assert_equal __, "ruby string".to_java.class
assert_equal __, 1.to_java.class
assert_equal __, 9.32.to_java.class
assert_equal __, false.to_java.class
end

def test_some_ruby_objects_are_not_coerced_to_what_you_might_expect
assert_equal __, [].to_java.class == Java::JavaUtil::ArrayList
assert_equal __, {}.to_java.class == Java::JavaUtil::HashMap
assert_equal __, Object.new.to_java.class == Java::JavaLang::Object
end

def test_java_collections_are_enumerable
java_array = java.util.ArrayList.new
java_array << "one" << "two" << "three"
Expand Down
12 changes: 6 additions & 6 deletions koans/about_symbols.rb
Expand Up @@ -11,8 +11,8 @@ def test_symbols_can_be_compared
symbol2 = :a_symbol
symbol3 = :something_else

assert symbol1 == __
assert symbol1 != __
assert_equal __, symbol1 == symbol2
assert_equal __, symbol1 == symbol3
end

def test_identical_symbols_are_a_single_internal_object
Expand All @@ -24,14 +24,14 @@ def test_identical_symbols_are_a_single_internal_object
end

def test_method_names_become_symbols
all_symbols = Symbol.all_symbols
assert_equal __, all_symbols.include?(:test_method_names_become_symbols)
symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
assert_equal __, symbols_as_strings.include?("test_method_names_become_symbols")
end

# THINK ABOUT IT:
#
# Why do we capture the list of symbols before we check for the
# method name?
# Why do we convert the list of symbols to strings and then compare
# against the string value rather than against symbols?

in_ruby_version("mri") do
RubyConstant = "What is the sound of one hand clapping?"
Expand Down
54 changes: 54 additions & 0 deletions koans/about_to_str.rb
@@ -0,0 +1,54 @@
require File.expand_path(File.dirname(__FILE__) + '/edgecase')

class AboutToStr < EdgeCase::Koan

class CanNotBeTreatedAsString
def to_s
"non-string-like"
end
end

def test_to_s_returns_a_string_representation
not_like_a_string = CanNotBeTreatedAsString.new
assert_equal __, not_like_a_string.to_s
end

def test_normally_objects_cannot_be_used_where_strings_are_expected
assert_raise(___) do
File.exist?(CanNotBeTreatedAsString.new)
end
end

# ------------------------------------------------------------------

class CanBeTreatedAsString
def to_s
"string-like"
end

def to_str
to_s
end
end

def test_to_str_also_returns_a_string_representation
like_a_string = CanBeTreatedAsString.new
assert_equal __, like_a_string.to_str
end

def test_to_str_allows_objects_to_be_treated_as_strings
assert_equal __, File.exist?(CanBeTreatedAsString.new)
end

# ------------------------------------------------------------------

def acts_like_a_string?(string)
string = string.to_str if string.respond_to?(:to_str)
string.is_a?(String)
end

def test_user_defined_code_can_check_for_to_str
assert_equal __, acts_like_a_string?(CanNotBeTreatedAsString.new)
assert_equal __, acts_like_a_string?(CanBeTreatedAsString.new)
end
end
1 change: 1 addition & 0 deletions koans/path_to_enlightenment.rb
Expand Up @@ -31,6 +31,7 @@
require 'about_class_methods'
require 'about_message_passing'
require 'about_proxy_object_project'
require 'about_to_str'
in_ruby_version("jruby") do
require 'about_java_interop'
end
Expand Down

0 comments on commit 868d376

Please sign in to comment.