Permalink
Browse files

Merge branch 'master' of https://github.com/edgecase/ruby_koans

Conflicts:
	.gitignore
  • Loading branch information...
Danish Khan
Danish Khan committed Feb 2, 2011
2 parents 71ef84e + a05d7b8 commit daea57ad9f86a82d0a618ec2565c1b1e8a7eff4c
View
@@ -2,3 +2,4 @@ dist
.project_env.rc
.path_progress
.DS_Store
+*.rbc
View
@@ -15,6 +15,7 @@ today = Time.now.strftime("%Y-%m-%d")
TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"
+CLEAN.include("**/*.rbc")
CLOBBER.include(DIST_DIR)
module Koans
@@ -57,6 +58,27 @@ module Koans
end
end
+module RubyImpls
+ # Calculate the list of relevant Ruby implementations.
+ def self.find_ruby_impls
+ rubys = `rvm list`.gsub(/=>/,'').split(/\n/).sort
+ expected.map { |impl|
+ last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
+ last ? last.split.first : nil
+ }.compact
+ end
+
+ # Return a (cached) list of relevant Ruby implementations.
+ def self.list
+ @list ||= find_ruby_impls
+ end
+
+ # List of expected ruby implementations.
+ def self.expected
+ %w(ruby-1.8.6 ruby-1.8.7 ruby-1.9.2 jruby ree)
+ end
+end
+
task :default => :walk_the_path
task :walk_the_path do
@@ -107,3 +129,38 @@ SRC_FILES.each do |koan_src|
Koans.make_koan_file koan_src, t.name
end
end
+
+task :run do
+ puts 'koans'
+ Dir.chdir("src") do
+ puts "in #{Dir.pwd}"
+ sh "ruby path_to_enlightenment.rb"
+ end
+end
+
+
+desc "Pre-checkin tests (=> run_all)"
+task :cruise => :run_all
+
+desc "Run the completed koans againts a list of relevant Ruby Implementations"
+task :run_all do
+ results = []
+ RubyImpls.list.each do |impl|
+ puts "=" * 40
+ puts "On Ruby #{impl}"
+ sh "rvm #{impl} rake run"
+ results << [impl, "RAN"]
+ puts
+ end
+ puts "=" * 40
+ puts "Summary:"
+ puts
+ results.each do |impl, res|
+ puts "#{impl} => RAN"
+ end
+ puts
+ RubyImpls.expected.each do |requested_impl|
+ impl_pattern = Regexp.new(Regexp.quote(requested_impl))
+ puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
+ end
+end
View
Binary file not shown.
@@ -93,6 +93,16 @@ def test_break_statement
assert_equal __, result
end
+ def test_break_statement_returns_values
+ i = 1
+ result = while i <= 10
+ break i if i % 2 == 0
+ i += 1
+ end
+
+ assert_equal __, result
+ end
+
def test_next_statement
i = 0
result = []
@@ -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
@@ -22,8 +22,8 @@ def test_rescue_clause
assert_equal __, result
- assert ex.is_a?(___), "Failure message."
- assert ex.is_a?(___), "Failure message."
+ assert_equal __, ex.is_a?(StandardError), "Should be a Standard Error"
+ assert_equal __, ex.is_a?(RuntimeError), "Should be a Runtime Error"
assert RuntimeError.ancestors.include?(StandardError),
"RuntimeError is a subclass of StandardError"
@@ -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"
View
@@ -183,7 +183,7 @@ def test_strings_can_be_joined
assert_equal __, words.join(" ")
end
- def test_strings_are_not_unique_objects
+ def test_strings_are_unique_objects
a = "a string"
b = "a string"
View
@@ -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
@@ -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?"
View
@@ -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
@@ -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
@@ -93,6 +93,16 @@ def test_break_statement
assert_equal __(3628800), result
end
+ def test_break_statement_returns_values
+ i = 1
+ result = while i <= 10
+ break i if i % 2 == 0
+ i += 1
+ end
+
+ assert_equal __(2), result
+ end
+
def test_next_statement
i = 0
result = []
@@ -1,12 +1,20 @@
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+# Implement a DiceSet Class here:
+#
+# class DiceSet
+# code ...
+# end
+
+#--
class DiceSet
attr_reader :values
def roll(n)
@values = (1..n).map { rand(6) + 1 }
end
end
+#++
class AboutDiceProject < EdgeCase::Koan
def test_can_create_a_dice_set
dice = DiceSet.new
View
@@ -22,8 +22,8 @@ def test_rescue_clause
assert_equal __(:exception_handled), result
- assert ex.is_a?(___(StandardError)), "Failure message."
- assert ex.is_a?(___(RuntimeError)), "Failure message."
+ assert_equal __(true), ex.is_a?(StandardError), "Should be a Standard Error"
+ assert_equal __(true), ex.is_a?(RuntimeError), "Should be a Runtime Error"
assert RuntimeError.ancestors.include?(StandardError), # __
"RuntimeError is a subclass of StandardError"
View
@@ -95,6 +95,19 @@ def test_however_most_methods_returning_strings_return_ruby_strings
assert_equal __(false), java_array.toString.is_a?(java.lang.String)
end
+ def test_some_ruby_objects_can_be_coerced_to_java
+ assert_equal __(Java::JavaLang::String), "ruby string".to_java.class
+ assert_equal __(Java::JavaLang::Long), 1.to_java.class
+ assert_equal __(Java::JavaLang::Double), 9.32.to_java.class
+ assert_equal __(Java::JavaLang::Boolean), false.to_java.class
+ end
+
+ def test_some_ruby_objects_are_not_coerced_to_what_you_might_expect
+ assert_equal __(false), [].to_java.class == Java::JavaUtil::ArrayList
+ assert_equal __(false), {}.to_java.class == Java::JavaUtil::HashMap
+ assert_equal __(false), 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"
View
@@ -183,7 +183,7 @@ def test_strings_can_be_joined
assert_equal __("Now is the time"), words.join(" ")
end
- def test_strings_are_not_unique_objects
+ def test_strings_are_unique_objects
a = "a string"
b = "a string"
View
@@ -11,8 +11,8 @@ def test_symbols_can_be_compared
symbol2 = :a_symbol
symbol3 = :something_else
- assert symbol1 == __(symbol2)
- assert symbol1 != __(symbol3)
+ assert_equal __(true), symbol1 == symbol2
+ assert_equal __(false), symbol1 == symbol3
end
def test_identical_symbols_are_a_single_internal_object
@@ -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 __(true), all_symbols.include?(:test_method_names_become_symbols)
+ symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
+ assert_equal __(true), 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?"
Oops, something went wrong.

0 comments on commit daea57a

Please sign in to comment.