1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutSymbols < EdgeCase::Koan
4 def test_symbols_are_symbols
6 assert_equal __, symbol.is_a?(Symbol)
9 def test_symbols_can_be_compared
12 symbol3 = :something_else
14 assert_equal __, symbol1 == symbol2
15 assert_equal __, symbol1 == symbol3
18 def test_identical_symbols_are_a_single_internal_object
22 assert_equal __, symbol1 == symbol2
23 assert_equal __, symbol1.object_id == symbol2.object_id
26 def test_method_names_become_symbols
27 symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
28 assert_equal __, symbols_as_strings.include?("test_method_names_become_symbols")
33 # Why do we convert the list of symbols to strings and then compare
34 # against the string value rather than against symbols?
36 in_ruby_version("mri") do
37 RubyConstant = "What is the sound of one hand clapping?"
38 def test_constants_become_symbols
39 all_symbols = Symbol.all_symbols
41 assert_equal __, all_symbols.include?(__)
45 def test_symbols_can_be_made_from_strings
46 string = "catsAndDogs"
47 assert_equal __, string.to_sym
50 def test_symbols_with_spaces_can_be_built
51 symbol = :"cats and dogs"
53 assert_equal symbol, __.to_sym
56 def test_symbols_with_interpolation_can_be_built
58 symbol = :"cats #{value} dogs"
60 assert_equal symbol, __.to_sym
63 def test_to_s_is_called_on_interpolated_symbols
65 string = "It is raining #{symbol} and dogs."
67 assert_equal __, string
70 def test_symbols_are_not_strings
72 assert_equal __, symbol.is_a?(String)
73 assert_equal __, symbol.eql?("ruby")
76 def test_symbols_do_not_have_string_methods
77 symbol = :not_a_string
78 assert_equal __, symbol.respond_to?(:each_char)
79 assert_equal __, symbol.respond_to?(:reverse)
82 # It's important to realize that symbols are not "immutable
83 # strings", though they are immutable. None of the
84 # interesting string operations are available on symbols.
86 def test_symbols_cannot_be_concatenated
87 # Exceptions will be pondered further farther down the path
93 def test_symbols_can_be_dynamically_created
94 assert_equal __, ("cats" + "dogs").to_sym
99 # Why is it not a good idea to dynamically create a lot of symbols?