This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |