Browse files

Add testing example

  • Loading branch information...
1 parent f9cf594 commit 8d57026914b9b01f306c003ca5d5e278aad6e592 @weppos committed Feb 25, 2011
Showing with 94 additions and 1 deletion.
  1. +7 −1 testing/01_testing.md
  2. +87 −0 testing/tag_list.rb
View
8 testing/01_testing.md
@@ -1,2 +1,8 @@
-!SLIDE
+!SLIDE subsection
# Testing
+
+
+!SLIDE
+# Unit::Test
+
+### See `tag_list.rb`
View
87 testing/tag_list.rb
@@ -0,0 +1,87 @@
+class TagList
+ include Enumerable
+
+ attr_reader :tags
+
+ def initialize
+ @tags = []
+ end
+
+ def add(tag)
+ @tags << tag
+ @tags.uniq!
+ self
+ end
+
+ def remove(tag)
+ @tags.delete_if { |t| t == tag }
+ self
+ end
+
+ def each(&block)
+ @tags.each(&block)
+ end
+end
+
+
+if __FILE__ == $0
+
+ require 'test/unit'
+
+ class TagListTest < Test::Unit::TestCase
+ def setup
+ @i = TagList.new
+ end
+
+ def test_implements_enumerable
+ assert TagList.ancestors.include?(Enumerable)
+ assert TagList.public_instance_methods.include?(:each)
+ end
+
+ def test_initialize_requires_no_parameters
+ assert_raise(ArgumentError) { TagList.new([]) }
+ assert_nothing_raised { TagList.new }
+ end
+
+ def test_initialize_initializes_tags
+ i = TagList.new
+ assert_equal [], i.tags
+ end
+
+ def test_add_adds_a_tag
+ assert_equal [], @i.tags
+ @i.add("one")
+ assert_equal ["one"], @i.tags
+ @i.add("two")
+ assert_equal ["one", "two"], @i.tags
+ end
+
+ def test_add_filters_duplicate_tags
+ @i.add("one")
+ assert_equal ["one"], @i.tags
+ @i.add("one")
+ assert_equal ["one"], @i.tags
+ end
+
+ def test_add_returns_self
+ assert_equal @i, @i.add("tag")
+ end
+
+ def test_remove_removes_a_tag_if_tag_exists
+ %w( one two ).each { |tag| @i.add(tag) }
+ @i.remove("one")
+ assert_equal ["two"], @i.tags
+ end
+
+ def test_remove_does_not_do_anything_if_tag_does_not_exist
+ %w( one two ).each { |tag| @i.add(tag) }
+ @i.remove("three")
+ assert_equal ["one", "two"], @i.tags
+ end
+
+ def test_remove_returns_self
+ assert_equal @i, @i.remove("tag")
+ end
+ end
+
+end

0 comments on commit 8d57026

Please sign in to comment.