1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutToStr < EdgeCase::Koan
5 class CanNotBeTreatedAsString
11 def test_to_s_returns_a_string_representation
12 not_like_a_string = CanNotBeTreatedAsString.new
13 assert_equal __, not_like_a_string.to_s
16 def test_normally_objects_cannot_be_used_where_strings_are_expected
18 File.exist?(CanNotBeTreatedAsString.new)
22 # ------------------------------------------------------------------
24 class CanBeTreatedAsString
34 def test_to_str_also_returns_a_string_representation
35 like_a_string = CanBeTreatedAsString.new
36 assert_equal __, like_a_string.to_str
39 def test_to_str_allows_objects_to_be_treated_as_strings
40 assert_equal __, File.exist?(CanBeTreatedAsString.new)
43 # ------------------------------------------------------------------
45 def acts_like_a_string?(string)
46 string = string.to_str if string.respond_to?(:to_str)
50 def test_user_defined_code_can_check_for_to_str
51 assert_equal __, acts_like_a_string?(CanNotBeTreatedAsString.new)
52 assert_equal __, acts_like_a_string?(CanBeTreatedAsString.new)