mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
Compare commits
1 Commits
rubykoans-
...
become_the
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d27004c70d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,3 @@
|
||||
dist
|
||||
.project_env.rc
|
||||
.path_progress
|
||||
*.rbc
|
||||
|
||||
57
Rakefile
57
Rakefile
@@ -15,7 +15,6 @@ 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
|
||||
@@ -58,27 +57,6 @@ 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
|
||||
@@ -129,38 +107,3 @@ 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
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
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
|
||||
|
||||
@@ -22,8 +22,8 @@ class AboutExceptions < EdgeCase::Koan
|
||||
|
||||
assert_equal __, result
|
||||
|
||||
assert_equal __, ex.is_a?(StandardError), "Should be a Standard Error"
|
||||
assert_equal __, ex.is_a?(RuntimeError), "Should be a Runtime Error"
|
||||
assert ex.is_a?(___), "Failure message."
|
||||
assert ex.is_a?(___), "Failure message."
|
||||
|
||||
assert RuntimeError.ancestors.include?(StandardError),
|
||||
"RuntimeError is a subclass of StandardError"
|
||||
|
||||
@@ -95,19 +95,6 @@ class AboutJavaInterop < EdgeCase::Koan
|
||||
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"
|
||||
|
||||
118
koans/about_sensei.rb
Normal file
118
koans/about_sensei.rb
Normal file
@@ -0,0 +1,118 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class Sensei
|
||||
def observe(step)
|
||||
# Step Protocol: step.passed? | step.failure | step.koan_file | step.name
|
||||
# WRITE THIS CODE
|
||||
end
|
||||
|
||||
def pass_count
|
||||
# WRITE THIS CODE
|
||||
end
|
||||
|
||||
def failed?
|
||||
# WRITE THIS CODE
|
||||
end
|
||||
|
||||
def instruct
|
||||
# WRITE THIS CODE
|
||||
end
|
||||
end
|
||||
|
||||
def student_passes_test
|
||||
assert true # DO NOT CHANGE YOUR STUDENT'S ANSWER
|
||||
end
|
||||
|
||||
def student_fails_test
|
||||
assert false # DO NOT CHANGE YOUR STUDENT'S ANSWER
|
||||
end
|
||||
|
||||
class AboutSensei < EdgeCase::Koan
|
||||
def test_sensei_comments_about_expanded_awareness
|
||||
you = Sensei.new
|
||||
student_step = EdgeCase::Koan.new(:student_passes_test, self)
|
||||
student_meditation = student_step.meditate
|
||||
observation = you.observe(student_meditation)
|
||||
assert_equal "AboutSensei#student_passes_test has expanded your awareness.", observation
|
||||
end
|
||||
|
||||
def test_sensei_comments_about_damaged_karma
|
||||
you = Sensei.new
|
||||
student_step = EdgeCase::Koan.new(:student_fails_test, self)
|
||||
student_meditation = student_step.meditate
|
||||
observation = you.observe(student_meditation)
|
||||
assert_equal "AboutSensei#student_fails_test has damaged your karma.", observation
|
||||
end
|
||||
|
||||
def test_sensei_counts_passed_steps
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
assert_equal 4, you.pass_count
|
||||
end
|
||||
|
||||
def test_sensei_reports_failed_true
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step
|
||||
end
|
||||
assert you.failed?
|
||||
end
|
||||
|
||||
def test_sensei_reports_failed_true
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step
|
||||
end
|
||||
assert ! you.failed?
|
||||
end
|
||||
|
||||
def test_sensei_shows_beginning_progress
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
instructions = you.instruct
|
||||
assert_match "You have not yet reached enlightenment", instructions
|
||||
assert_match "You have passed 0 steps", instructions
|
||||
end
|
||||
|
||||
def test_sensei_shows_partial_progress
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
instructions = you.instruct
|
||||
assert_match "You have not yet reached enlightenment", instructions
|
||||
assert_match "You have passed 3 steps", instructions
|
||||
end
|
||||
|
||||
def test_sensei_congratulates_student
|
||||
you = Sensei.new
|
||||
# run all of Ruby Koans with you as the sensei
|
||||
EdgeCase::ThePath.new(you).walk
|
||||
|
||||
instructions = you.instruct
|
||||
assert_match "The student has become the master", instructions
|
||||
end
|
||||
end
|
||||
@@ -11,8 +11,8 @@ class AboutSymbols < EdgeCase::Koan
|
||||
symbol2 = :a_symbol
|
||||
symbol3 = :something_else
|
||||
|
||||
assert_equal __, symbol1 == symbol2
|
||||
assert_equal __, symbol1 == symbol3
|
||||
assert symbol1 == __
|
||||
assert symbol1 != __
|
||||
end
|
||||
|
||||
def test_identical_symbols_are_a_single_internal_object
|
||||
@@ -24,14 +24,14 @@ class AboutSymbols < EdgeCase::Koan
|
||||
end
|
||||
|
||||
def 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")
|
||||
all_symbols = Symbol.all_symbols
|
||||
assert_equal __, all_symbols.include?(:test_method_names_become_symbols)
|
||||
end
|
||||
|
||||
# THINK ABOUT IT:
|
||||
#
|
||||
# Why do we convert the list of symbols to strings and then compare
|
||||
# against the string value rather than against symbols?
|
||||
# Why do we capture the list of symbols before we check for the
|
||||
# method name?
|
||||
|
||||
in_ruby_version("mri") do
|
||||
RubyConstant = "What is the sound of one hand clapping?"
|
||||
@@ -53,7 +53,7 @@ class AboutSymbols < EdgeCase::Koan
|
||||
assert_equal symbol, __.to_sym
|
||||
end
|
||||
|
||||
def test_symbols_with_interpolation_can_be_built
|
||||
def test_symbols_with_spaces_can_be_built
|
||||
value = "and"
|
||||
symbol = :"cats #{value} dogs"
|
||||
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
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
|
||||
@@ -93,29 +93,16 @@ module EdgeCase
|
||||
end
|
||||
|
||||
def colorize(string, color_value)
|
||||
if use_colors?
|
||||
color(color_value) + string + color(COLORS[:clear])
|
||||
else
|
||||
if ENV['NO_COLOR']
|
||||
string
|
||||
else
|
||||
color(color_value) + string + color(COLORS[:clear])
|
||||
end
|
||||
end
|
||||
|
||||
def color(color_value)
|
||||
"\e[#{color_value}m"
|
||||
end
|
||||
|
||||
def use_colors?
|
||||
return false if ENV['NO_COLOR']
|
||||
if ENV['ANSI_COLOR'].nil?
|
||||
! using_windows?
|
||||
else
|
||||
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
||||
end
|
||||
end
|
||||
|
||||
def using_windows?
|
||||
File::ALT_SEPARATOR
|
||||
end
|
||||
end
|
||||
|
||||
class Sensei
|
||||
@@ -137,7 +124,10 @@ module EdgeCase
|
||||
@pass_count = 0
|
||||
@failure = nil
|
||||
@failed_test = nil
|
||||
@observations = []
|
||||
end
|
||||
|
||||
def observations
|
||||
@observations ||= []
|
||||
end
|
||||
|
||||
PROGRESS_FILE_NAME = '.path_progress'
|
||||
@@ -167,13 +157,13 @@ module EdgeCase
|
||||
if step.passed?
|
||||
@pass_count += 1
|
||||
if @pass_count > progress.last.to_i
|
||||
@observations << Color.green("#{step.koan_file}##{step.name} has expanded your awareness.")
|
||||
observations << Color.green("#{step.koan_file}##{step.name} has expanded your awareness.")
|
||||
end
|
||||
else
|
||||
@failed_test = step
|
||||
@failure = step.failure
|
||||
add_progress(@pass_count)
|
||||
@observations << Color.red("#{step.koan_file}##{step.name} has damaged your karma.")
|
||||
observations << Color.red("#{step.koan_file}##{step.name} has damaged your karma.")
|
||||
throw :edgecase_exit
|
||||
end
|
||||
end
|
||||
@@ -188,7 +178,7 @@ module EdgeCase
|
||||
|
||||
def instruct
|
||||
if failed?
|
||||
@observations.each{|c| puts c }
|
||||
observations.each{|c| puts c }
|
||||
encourage
|
||||
guide_through_error
|
||||
a_zenlike_statement
|
||||
@@ -216,22 +206,23 @@ module EdgeCase
|
||||
end
|
||||
|
||||
def end_screen
|
||||
if EdgeCase.simple_output
|
||||
boring_end_screen
|
||||
screen = if EdgeCase.simple_output
|
||||
end_message
|
||||
else
|
||||
artistic_end_screen
|
||||
end
|
||||
puts screen
|
||||
end
|
||||
|
||||
def boring_end_screen
|
||||
puts "Mountains are again merely mountains"
|
||||
def end_message
|
||||
"The student has become the master"
|
||||
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
|
||||
return <<-ENDTEXT
|
||||
,, , ,,
|
||||
: ::::, :::,
|
||||
, ,,: :::::::::::::,, :::: : ,
|
||||
@@ -242,7 +233,7 @@ module EdgeCase
|
||||
,: , ,:,,: :::::::::::::
|
||||
::,: ,,:::, ,::::::::::::,
|
||||
,:::, :,,::: ::::::::::::,
|
||||
,::: :::::::, Mountains are again merely mountains ,::::::::::::
|
||||
,::: :::::::,#{ end_message.side_padding 48 },::::::::::::
|
||||
:::,,,:::::: ::::::::::::
|
||||
,:::::::::::, ::::::::::::,
|
||||
:::::::::::, ,::::::::::::
|
||||
@@ -266,7 +257,6 @@ module EdgeCase
|
||||
,:::: , ,,
|
||||
,,,
|
||||
ENDTEXT
|
||||
puts completed
|
||||
end
|
||||
|
||||
def encourage
|
||||
@@ -443,21 +433,26 @@ ENDTEXT
|
||||
end
|
||||
|
||||
class ThePath
|
||||
def initialize(sensei=nil)
|
||||
@sensei = sensei || Sensei.new
|
||||
end
|
||||
|
||||
def walk
|
||||
sensei = EdgeCase::Sensei.new
|
||||
each_step do |step|
|
||||
sensei.observe(step.meditate)
|
||||
@sensei.observe(step.meditate)
|
||||
end
|
||||
sensei.instruct
|
||||
@sensei.instruct
|
||||
end
|
||||
|
||||
def each_step
|
||||
catch(:edgecase_exit) {
|
||||
step_count = 0
|
||||
EdgeCase::Koan.subclasses.each_with_index do |koan,koan_index|
|
||||
koan.testmethods.each do |method_name|
|
||||
step = koan.new(method_name, koan.to_s, koan_index+1, step_count+=1)
|
||||
yield step
|
||||
if @sensei.instance_of?(EdgeCase::Sensei) || (koan.to_s != "AboutSensei")
|
||||
koan.testmethods.each do |method_name|
|
||||
step = koan.new(method_name, koan.to_s, koan_index+1, step_count+=1)
|
||||
yield step
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ require 'about_scope'
|
||||
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
|
||||
require 'about_extra_credit'
|
||||
require 'about_sensei'
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
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)
|
||||
@@ -14,7 +7,6 @@ class DiceSet
|
||||
end
|
||||
end
|
||||
|
||||
#++
|
||||
class AboutDiceProject < EdgeCase::Koan
|
||||
def test_can_create_a_dice_set
|
||||
dice = DiceSet.new
|
||||
|
||||
@@ -22,8 +22,8 @@ class AboutExceptions < EdgeCase::Koan
|
||||
|
||||
assert_equal __(:exception_handled), result
|
||||
|
||||
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 ex.is_a?(___(StandardError)), "Failure message."
|
||||
assert ex.is_a?(___(RuntimeError)), "Failure message."
|
||||
|
||||
assert RuntimeError.ancestors.include?(StandardError), # __
|
||||
"RuntimeError is a subclass of StandardError"
|
||||
|
||||
@@ -95,19 +95,6 @@ class AboutJavaInterop < EdgeCase::Koan
|
||||
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"
|
||||
|
||||
141
src/about_sensei.rb
Normal file
141
src/about_sensei.rb
Normal file
@@ -0,0 +1,141 @@
|
||||
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
|
||||
|
||||
class Sensei
|
||||
def observe(step)
|
||||
# Step Protocol: step.passed? | step.failure | step.koan_file | step.name
|
||||
# WRITE THIS CODE
|
||||
#--
|
||||
@pass_count ||= 0
|
||||
if step.passed?
|
||||
@pass_count += 1
|
||||
"#{step.koan_file.class.to_s}##{step.name} has expanded your awareness."
|
||||
else
|
||||
@failure = step.failure
|
||||
"#{step.koan_file.class.to_s}##{step.name} has damaged your karma."
|
||||
end
|
||||
#++
|
||||
end
|
||||
|
||||
def pass_count
|
||||
# WRITE THIS CODE
|
||||
#--
|
||||
@pass_count
|
||||
#++
|
||||
end
|
||||
|
||||
def failed?
|
||||
# WRITE THIS CODE
|
||||
#--
|
||||
! @failure.nil?
|
||||
#++
|
||||
end
|
||||
|
||||
def instruct
|
||||
# WRITE THIS CODE
|
||||
#--
|
||||
if failed?
|
||||
"You have not yet reached enlightenment. You have passed #{pass_count} steps."
|
||||
else
|
||||
"The student has become the master"
|
||||
end
|
||||
#++
|
||||
end
|
||||
end
|
||||
|
||||
def student_passes_test
|
||||
assert true # DO NOT CHANGE YOUR STUDENT'S ANSWER
|
||||
end
|
||||
|
||||
def student_fails_test
|
||||
assert false # DO NOT CHANGE YOUR STUDENT'S ANSWER
|
||||
end
|
||||
|
||||
class AboutSensei < EdgeCase::Koan
|
||||
def test_sensei_comments_about_expanded_awareness
|
||||
you = Sensei.new
|
||||
student_step = EdgeCase::Koan.new(:student_passes_test, self)
|
||||
student_meditation = student_step.meditate
|
||||
observation = you.observe(student_meditation)
|
||||
assert_equal "AboutSensei#student_passes_test has expanded your awareness.", observation
|
||||
end
|
||||
|
||||
def test_sensei_comments_about_damaged_karma
|
||||
you = Sensei.new
|
||||
student_step = EdgeCase::Koan.new(:student_fails_test, self)
|
||||
student_meditation = student_step.meditate
|
||||
observation = you.observe(student_meditation)
|
||||
assert_equal "AboutSensei#student_fails_test has damaged your karma.", observation
|
||||
end
|
||||
|
||||
def test_sensei_counts_passed_steps
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
assert_equal 4, you.pass_count
|
||||
end
|
||||
|
||||
def test_sensei_reports_failed_true
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step
|
||||
end
|
||||
assert you.failed?
|
||||
end
|
||||
|
||||
def test_sensei_reports_failed_true
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step
|
||||
end
|
||||
assert ! you.failed?
|
||||
end
|
||||
|
||||
def test_sensei_shows_beginning_progress
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
instructions = you.instruct
|
||||
assert_match "You have not yet reached enlightenment", instructions
|
||||
assert_match "You have passed 0 steps", instructions
|
||||
end
|
||||
|
||||
def test_sensei_shows_partial_progress
|
||||
you = Sensei.new
|
||||
student_steps = [EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_passes_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self),
|
||||
EdgeCase::Koan.new(:student_fails_test, self)]
|
||||
student_steps.each do |step|
|
||||
you.observe step.meditate
|
||||
end
|
||||
instructions = you.instruct
|
||||
assert_match "You have not yet reached enlightenment", instructions
|
||||
assert_match "You have passed 3 steps", instructions
|
||||
end
|
||||
|
||||
def test_sensei_congratulates_student
|
||||
you = Sensei.new
|
||||
# run all of Ruby Koans with you as the sensei
|
||||
EdgeCase::ThePath.new(you).walk
|
||||
|
||||
instructions = you.instruct
|
||||
assert_match "The student has become the master", instructions
|
||||
end
|
||||
end
|
||||
@@ -11,8 +11,8 @@ class AboutSymbols < EdgeCase::Koan
|
||||
symbol2 = :a_symbol
|
||||
symbol3 = :something_else
|
||||
|
||||
assert_equal __(true), symbol1 == symbol2
|
||||
assert_equal __(false), symbol1 == symbol3
|
||||
assert symbol1 == __(symbol2)
|
||||
assert symbol1 != __(symbol3)
|
||||
end
|
||||
|
||||
def test_identical_symbols_are_a_single_internal_object
|
||||
@@ -24,14 +24,14 @@ class AboutSymbols < EdgeCase::Koan
|
||||
end
|
||||
|
||||
def 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")
|
||||
all_symbols = Symbol.all_symbols
|
||||
assert_equal __(true), all_symbols.include?(:test_method_names_become_symbols)
|
||||
end
|
||||
|
||||
# THINK ABOUT IT:
|
||||
#
|
||||
# Why do we convert the list of symbols to strings and then compare
|
||||
# against the string value rather than against symbols?
|
||||
# Why do we capture the list of symbols before we check for the
|
||||
# method name?
|
||||
|
||||
in_ruby_version("mri") do
|
||||
RubyConstant = "What is the sound of one hand clapping?"
|
||||
@@ -53,7 +53,7 @@ class AboutSymbols < EdgeCase::Koan
|
||||
assert_equal symbol, __("cats and dogs").to_sym
|
||||
end
|
||||
|
||||
def test_symbols_with_interpolation_can_be_built
|
||||
def test_symbols_with_spaces_can_be_built
|
||||
value = "and"
|
||||
symbol = :"cats #{value} dogs"
|
||||
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
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 __("non-string-like"), not_like_a_string.to_s
|
||||
end
|
||||
|
||||
def test_normally_objects_cannot_be_used_where_strings_are_expected
|
||||
assert_raise(___(TypeError)) 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 __("string-like"), like_a_string.to_str
|
||||
end
|
||||
|
||||
def test_to_str_allows_objects_to_be_treated_as_strings
|
||||
assert_equal __(false), 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 __(false), acts_like_a_string?(CanNotBeTreatedAsString.new)
|
||||
assert_equal __(true), acts_like_a_string?(CanBeTreatedAsString.new)
|
||||
end
|
||||
end
|
||||
@@ -93,29 +93,16 @@ module EdgeCase
|
||||
end
|
||||
|
||||
def colorize(string, color_value)
|
||||
if use_colors?
|
||||
color(color_value) + string + color(COLORS[:clear])
|
||||
else
|
||||
if ENV['NO_COLOR']
|
||||
string
|
||||
else
|
||||
color(color_value) + string + color(COLORS[:clear])
|
||||
end
|
||||
end
|
||||
|
||||
def color(color_value)
|
||||
"\e[#{color_value}m"
|
||||
end
|
||||
|
||||
def use_colors?
|
||||
return false if ENV['NO_COLOR']
|
||||
if ENV['ANSI_COLOR'].nil?
|
||||
! using_windows?
|
||||
else
|
||||
ENV['ANSI_COLOR'] =~ /^(t|y)/i
|
||||
end
|
||||
end
|
||||
|
||||
def using_windows?
|
||||
File::ALT_SEPARATOR
|
||||
end
|
||||
end
|
||||
|
||||
class Sensei
|
||||
@@ -137,7 +124,10 @@ module EdgeCase
|
||||
@pass_count = 0
|
||||
@failure = nil
|
||||
@failed_test = nil
|
||||
@observations = []
|
||||
end
|
||||
|
||||
def observations
|
||||
@observations ||= []
|
||||
end
|
||||
|
||||
PROGRESS_FILE_NAME = '.path_progress'
|
||||
@@ -167,13 +157,13 @@ module EdgeCase
|
||||
if step.passed?
|
||||
@pass_count += 1
|
||||
if @pass_count > progress.last.to_i
|
||||
@observations << Color.green("#{step.koan_file}##{step.name} has expanded your awareness.")
|
||||
observations << Color.green("#{step.koan_file}##{step.name} has expanded your awareness.")
|
||||
end
|
||||
else
|
||||
@failed_test = step
|
||||
@failure = step.failure
|
||||
add_progress(@pass_count)
|
||||
@observations << Color.red("#{step.koan_file}##{step.name} has damaged your karma.")
|
||||
observations << Color.red("#{step.koan_file}##{step.name} has damaged your karma.")
|
||||
throw :edgecase_exit
|
||||
end
|
||||
end
|
||||
@@ -188,7 +178,7 @@ module EdgeCase
|
||||
|
||||
def instruct
|
||||
if failed?
|
||||
@observations.each{|c| puts c }
|
||||
observations.each{|c| puts c }
|
||||
encourage
|
||||
guide_through_error
|
||||
a_zenlike_statement
|
||||
@@ -216,22 +206,23 @@ module EdgeCase
|
||||
end
|
||||
|
||||
def end_screen
|
||||
if EdgeCase.simple_output
|
||||
boring_end_screen
|
||||
screen = if EdgeCase.simple_output
|
||||
end_message
|
||||
else
|
||||
artistic_end_screen
|
||||
end
|
||||
puts screen
|
||||
end
|
||||
|
||||
def boring_end_screen
|
||||
puts "Mountains are again merely mountains"
|
||||
def end_message
|
||||
"The student has become the master"
|
||||
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
|
||||
return <<-ENDTEXT
|
||||
,, , ,,
|
||||
: ::::, :::,
|
||||
, ,,: :::::::::::::,, :::: : ,
|
||||
@@ -242,7 +233,7 @@ module EdgeCase
|
||||
,: , ,:,,: :::::::::::::
|
||||
::,: ,,:::, ,::::::::::::,
|
||||
,:::, :,,::: ::::::::::::,
|
||||
,::: :::::::, Mountains are again merely mountains ,::::::::::::
|
||||
,::: :::::::,#{ end_message.side_padding 48 },::::::::::::
|
||||
:::,,,:::::: ::::::::::::
|
||||
,:::::::::::, ::::::::::::,
|
||||
:::::::::::, ,::::::::::::
|
||||
@@ -266,7 +257,6 @@ module EdgeCase
|
||||
,:::: , ,,
|
||||
,,,
|
||||
ENDTEXT
|
||||
puts completed
|
||||
end
|
||||
|
||||
def encourage
|
||||
@@ -443,21 +433,26 @@ ENDTEXT
|
||||
end
|
||||
|
||||
class ThePath
|
||||
def initialize(sensei=nil)
|
||||
@sensei = sensei || Sensei.new
|
||||
end
|
||||
|
||||
def walk
|
||||
sensei = EdgeCase::Sensei.new
|
||||
each_step do |step|
|
||||
sensei.observe(step.meditate)
|
||||
@sensei.observe(step.meditate)
|
||||
end
|
||||
sensei.instruct
|
||||
@sensei.instruct
|
||||
end
|
||||
|
||||
def each_step
|
||||
catch(:edgecase_exit) {
|
||||
step_count = 0
|
||||
EdgeCase::Koan.subclasses.each_with_index do |koan,koan_index|
|
||||
koan.testmethods.each do |method_name|
|
||||
step = koan.new(method_name, koan.to_s, koan_index+1, step_count+=1)
|
||||
yield step
|
||||
if @sensei.instance_of?(EdgeCase::Sensei) || (koan.to_s != "AboutSensei")
|
||||
koan.testmethods.each do |method_name|
|
||||
step = koan.new(method_name, koan.to_s, koan_index+1, step_count+=1)
|
||||
yield step
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ require 'about_scope'
|
||||
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
|
||||
require 'about_extra_credit'
|
||||
require 'about_sensei'
|
||||
|
||||
Reference in New Issue
Block a user