From fe2ee8617268b6081fbde68b82b67dfbc4a5fc8e Mon Sep 17 00:00:00 2001 From: Jim Weirich Date: Mon, 27 Sep 2010 11:36:00 -0400 Subject: [PATCH] Added Koan on basic objects --- koans/about_objects.rb | 56 ++++++++++++++++++++++++++++++++++++++++++ koans/path_to_enlightenment.rb | 1 + src/about_objects.rb | 56 ++++++++++++++++++++++++++++++++++++++++++ src/path_to_enlightenment.rb | 1 + 4 files changed, 114 insertions(+) create mode 100644 koans/about_objects.rb create mode 100644 src/about_objects.rb diff --git a/koans/about_objects.rb b/koans/about_objects.rb new file mode 100644 index 0000000..05c44de --- /dev/null +++ b/koans/about_objects.rb @@ -0,0 +1,56 @@ +require File.expand_path(File.dirname(__FILE__) + '/edgecase') + +class AboutObjects < EdgeCase::Koan + def test_everything_is_an_object + assert_equal __, 1.is_a?(Object) + assert_equal __, 1.5.is_a?(Object) + assert_equal __, "string".is_a?(Object) + assert_equal __, nil.is_a?(Object) + assert_equal __, Object.is_a?(Object) + end + + def test_objects_can_be_converted_to_strings + assert_equal __, 123.to_s + assert_equal __, nil.to_s + end + + def test_objects_can_be_inspected + assert_equal __, 123.inspect + assert_equal __, nil.inspect + end + + def test_every_object_has_an_id + obj = Object.new + assert_equal __, obj.object_id.class + end + + def test_every_object_has_different_id + obj = Object.new + another_obj = Object.new + assert_equal __, obj.object_id != another_obj.object_id + end + + def test_some_system_objects_always_have_the_same_id + assert_equal __, false.object_id + assert_equal __, true.object_id + assert_equal __, nil.object_id + end + + def test_small_integers_have_fixed_ids + assert_equal __, 0.object_id + assert_equal __, 1.object_id + assert_equal __, 2.object_id + assert_equal __, 100.object_id + + # THINK ABOUT IT: + # What pattern do the object IDs for small integers follow? + end + + def test_clone_creates_a_different_object + obj = Object.new + copy = obj.clone + + assert_equal __, obj != copy + assert_equal __, obj.object_id != copy.object_id + end +end diff --git a/koans/path_to_enlightenment.rb b/koans/path_to_enlightenment.rb index f7e0773..f8b09c7 100644 --- a/koans/path_to_enlightenment.rb +++ b/koans/path_to_enlightenment.rb @@ -4,6 +4,7 @@ $LOAD_PATH << File.dirname(__FILE__) require 'about_asserts' require 'about_nil' +require 'about_objects' require 'about_arrays' require 'about_array_assignment' require 'about_hashes' diff --git a/src/about_objects.rb b/src/about_objects.rb new file mode 100644 index 0000000..1faf0a0 --- /dev/null +++ b/src/about_objects.rb @@ -0,0 +1,56 @@ +require File.expand_path(File.dirname(__FILE__) + '/edgecase') + +class AboutObjects < EdgeCase::Koan + def test_everything_is_an_object + assert_equal __(true), 1.is_a?(Object) + assert_equal __(true), 1.5.is_a?(Object) + assert_equal __(true), "string".is_a?(Object) + assert_equal __(true), nil.is_a?(Object) + assert_equal __(true), Object.is_a?(Object) + end + + def test_objects_can_be_converted_to_strings + assert_equal __("123"), 123.to_s + assert_equal __(""), nil.to_s + end + + def test_objects_can_be_inspected + assert_equal __("123"), 123.inspect + assert_equal __("nil"), nil.inspect + end + + def test_every_object_has_an_id + obj = Object.new + assert_equal __(Fixnum), obj.object_id.class + end + + def test_every_object_has_different_id + obj = Object.new + another_obj = Object.new + assert_equal __(true), obj.object_id != another_obj.object_id + end + + def test_some_system_objects_always_have_the_same_id + assert_equal __(0), false.object_id + assert_equal __(2), true.object_id + assert_equal __(4), nil.object_id + end + + def test_small_integers_have_fixed_ids + assert_equal __(1), 0.object_id + assert_equal __(3), 1.object_id + assert_equal __(5), 2.object_id + assert_equal __(201), 100.object_id + + # THINK ABOUT IT: + # What pattern do the object IDs for small integers follow? + end + + def test_clone_creates_a_different_object + obj = Object.new + copy = obj.clone + + assert_equal __(true), obj != copy + assert_equal __(true), obj.object_id != copy.object_id + end +end diff --git a/src/path_to_enlightenment.rb b/src/path_to_enlightenment.rb index f7e0773..f8b09c7 100644 --- a/src/path_to_enlightenment.rb +++ b/src/path_to_enlightenment.rb @@ -4,6 +4,7 @@ $LOAD_PATH << File.dirname(__FILE__) require 'about_asserts' require 'about_nil' +require 'about_objects' require 'about_arrays' require 'about_array_assignment' require 'about_hashes' -- 1.8.0.2