Add about_symbols koan
authorMatt Yoho <mby@mattyoho.com>
Tue, 21 Sep 2010 15:52:47 +0000 (11:52 -0400)
committerMatt Yoho <mby@mattyoho.com>
Tue, 21 Sep 2010 16:03:31 +0000 (12:03 -0400)
koans/about_symbols.rb [new file with mode: 0644]
koans/path_to_enlightenment.rb
src/about_symbols.rb [new file with mode: 0644]
src/path_to_enlightenment.rb

diff --git a/koans/about_symbols.rb b/koans/about_symbols.rb
new file mode 100644 (file)
index 0000000..978a0e0
--- /dev/null
@@ -0,0 +1,68 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutSymbols < EdgeCase::Koan
+  def test_symbols_are_symbols
+    symbol = :ruby
+    assert_equal __, symbol.is_a?(Symbol)
+  end
+
+  def test_symbols_are_not_strings
+    symbol = :ruby
+    assert_equal __, symbol.is_a?(String)
+    assert_equal __, symbol.eql?("ruby")
+  end
+
+  def test_symbols_have_unique_identity
+    symbol1 = :identity
+    symbol2 = :identity
+    symbol3 = :something_else
+
+    assert symbol1 == __
+    assert symbol1 != __
+  end
+
+  def test_identical_symbols_are_represented_by_a_single_internal_object
+    symbol1 = :identity
+    symbol2 = :identity
+
+    assert symbol1.equal?(__)
+    assert_equal __, symbol2.object_id
+  end
+
+  def test_method_names_become_symbols
+    all_symbols = Symbol.all_symbols
+
+    assert_equal __, all_symbols.include?(:test_method_names_are_symbols)
+  end
+
+  RubyConstant = "This string is assigned to a constant."
+  def test_constants_become_symbols
+    all_symbols = Symbol.all_symbols
+
+    assert_equal true, all_symbols.include?(__)
+  end
+
+  def test_symbols_can_be_made_from_strings
+    string = "catsAndDogs"
+    assert_equal __, string.to_sym
+  end
+
+  def test_symbols_with_spaces_can_by_built
+    symbol = :"cats and dogs"
+
+    assert_equal symbol, __.to_sym
+  end
+
+  def test_interpolated_symbols_become_strings
+    symbol = :cats
+    string = "It is raining #{symbol} and dogs."
+
+    assert_equal __, string
+  end
+
+  def test_symbols_cannot_be_concatenated
+    assert_raise(__) do
+      :cats + :dogs
+    end
+  end
+end
index 2e12c46..f7e0773 100644 (file)
@@ -8,6 +8,7 @@ require 'about_arrays'
 require 'about_array_assignment'
 require 'about_hashes'
 require 'about_strings'
+require 'about_symbols'
 require 'about_regular_expressions'
 require 'about_methods'
 require 'about_constants'
diff --git a/src/about_symbols.rb b/src/about_symbols.rb
new file mode 100644 (file)
index 0000000..65cd449
--- /dev/null
@@ -0,0 +1,68 @@
+require File.expand_path(File.dirname(__FILE__) + '/edgecase')
+
+class AboutSymbols < EdgeCase::Koan
+  def test_symbols_are_symbols
+    symbol = :ruby
+    assert_equal __(true), symbol.is_a?(Symbol)
+  end
+
+  def test_symbols_are_not_strings
+    symbol = :ruby
+    assert_equal __(false), symbol.is_a?(String)
+    assert_equal __(false), symbol.eql?("ruby")
+  end
+
+  def test_symbols_have_unique_identity
+    symbol1 = :identity
+    symbol2 = :identity
+    symbol3 = :something_else
+
+    assert symbol1 == __(symbol2)
+    assert symbol1 != __(symbol2)
+  end
+
+  def test_identical_symbols_are_represented_by_a_single_internal_object
+    symbol1 = :identity
+    symbol2 = :identity
+
+    assert symbol1.equal?(__(symbol2))
+    assert_equal __(symbol1.object_id), symbol2.object_id
+  end
+
+  def test_method_names_become_symbols
+    all_symbols = Symbol.all_symbols
+
+    assert_equal __(true), all_symbols.include?(:test_method_names_are_symbols)
+  end
+
+  RubyConstant = "This string is assigned to a constant."
+  def test_constants_become_symbols
+    all_symbols = Symbol.all_symbols
+
+    assert_equal true, all_symbols.include?(__(:RubyConstant))
+  end
+
+  def test_symbols_can_be_made_from_strings
+    string = "catsAndDogs"
+    assert_equal __(:catsAndDogs), string.to_sym
+  end
+
+  def test_symbols_with_spaces_can_by_built
+    symbol = :"cats and dogs"
+
+    assert_equal symbol, __("cats and dogs").to_sym
+  end
+
+  def test_interpolated_symbols_become_strings
+    symbol = :cats
+    string = "It is raining #{symbol} and dogs."
+
+    assert_equal __('It is raining cats and dogs.'), string
+  end
+
+  def test_symbols_cannot_be_concatenated
+    assert_raise(__(NoMethodError)) do
+      :cats + :dogs
+    end
+  end
+end
index 2e12c46..f7e0773 100644 (file)
@@ -8,6 +8,7 @@ require 'about_arrays'
 require 'about_array_assignment'
 require 'about_hashes'
 require 'about_strings'
+require 'about_symbols'
 require 'about_regular_expressions'
 require 'about_methods'
 require 'about_constants'