Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Created July 18, 2012 13:16
Show Gist options
  • Select an option

  • Save wojtekmach/3136154 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekmach/3136154 to your computer and use it in GitHub Desktop.
Set + minitest/spec
require 'minitest/autorun'
class SetTest < MiniTest::Spec
before do
@set = Set.new
end
specify '#size' do
@set.size.must_equal 0
@set.add 42
@set.size.must_equal 1
end
specify '#include?' do
@set.include?(42).must_equal false
@set.add 42
@set.include?(42).must_equal true
end
specify '#add' do
@set.add 13
@set.add 13
@set.size.must_equal 1
end
specify '#to_a' do
@set.add 1
@set.add 4
@set.add 2
ary = @set.to_a
ary.size.must_equal 3
ary.must_include 1
ary.must_include 2
ary.must_include 4
end
end
class Set
include Enumerable
def initialize
@hash = {}
end
def size
@hash.size
end
def add(obj)
@hash[obj] = true
end
def include?(obj)
@hash.include? obj
end
def each(&block)
@hash.keys.each(&block)
end
end
class SortedSetTest < SetTest
before do
@set = SortedSet.new
end
specify '#to_a' do
@set.add 1
@set.add 4
@set.add 2
@set.to_a.must_equal [1, 2, 4]
end
end
class SortedSet < Set
def each(&block)
@hash.keys.sort.each(&block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment