Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Add testing example
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Feb 25, 2011
1 parent f9cf594 commit 8d57026
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
8 changes: 7 additions & 1 deletion testing/01_testing.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
!SLIDE
!SLIDE subsection
# Testing


!SLIDE
# Unit::Test

### See `tag_list.rb`
87 changes: 87 additions & 0 deletions testing/tag_list.rb
Original file line number Diff line number Diff line change
@@ -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.