Skip to content

Commit

Permalink
Tests for pasteboard.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Mar 28, 2011
1 parent 09b16d2 commit 1881433
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/pasteboard.rb
Expand Up @@ -111,21 +111,19 @@ def each flavor = nil # :yields: item

def get id, flavor = nil
item = copy_item_flavors(id).map do |item_flavor|
return copy_item_flavor_data(id, item_flavor) if
flavor and item_flavor == flavor
if flavor then
return copy_item_flavor_data(id, item_flavor) if item_flavor == flavor
next
end

[item_flavor, copy_item_flavor_data(id, item_flavor)]
end

return nil if item.empty?
return nil if item.compact.empty?

item
end

def inspect # :nodoc:
'#<%s:0x%x %s>' % [self.class, object_id, name]
end

##
# An array of item ids in the pasteboard. You must sync the clipboard to
# get the latest ids.
Expand All @@ -136,6 +134,10 @@ def ids
end
end

def inspect # :nodoc:
'#<%s:0x%x %s>' % [self.class, object_id, name]
end

##
# Clears the pasteboard and adds +item+ to the pasteboard with +id+.
#
Expand Down Expand Up @@ -164,6 +166,8 @@ def put item, id = 0
item.each do |flavor, data|
put_item_flavor id, flavor, data
end

self
end

end
Expand Down
44 changes: 43 additions & 1 deletion test/test_pasteboard.rb
Expand Up @@ -62,6 +62,26 @@ def test_copy_item_flavor_data
assert_equal Encoding::UTF_8, data.encoding if @encoding
end

def test_get
util_put

item = @pb.get(0)

assert_equal @item, item[0, 2]
end

def test_get_flavor
util_put

assert_equal 'pi', @pb.get(0, Pasteboard::Type::PLAIN_TEXT)
end

def test_get_flavor_missing
util_put

assert_nil @pb.get(0, Pasteboard::Type::JPEG)
end

def test_get_item_count
assert_equal 0, @pb.get_item_count

Expand All @@ -78,10 +98,30 @@ def test_get_item_identifier
assert_equal 0, id
end

def test_ids
util_put

assert_equal [0], @pb.ids
end

def test_inspect
expected = '#<%s:0x%x %s>' % [Pasteboard, @pb.object_id, @pb.name]

assert_equal expected, @pb.inspect
end

def test_name
assert_match %r%CFPasteboardUnique%, @pb.name
end

def test_put
util_put

assert_equal 1, @pb.get_item_count
assert_equal [Pasteboard::Type::PLAIN_TEXT, Pasteboard::Type::UTF_8],
@pb.copy_item_flavors(0)[0, 2]
end

def test_put_item_flavor
@pb.clear

Expand All @@ -101,10 +141,12 @@ def test_sync
end

def util_put
@pb.put [
@item = [
[Pasteboard::Type::PLAIN_TEXT, 'pi'],
[Pasteboard::Type::UTF_8, 'π'],
]

@pb.put @item
end

end
Expand Down

0 comments on commit 1881433

Please sign in to comment.