Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test #[]

  • Loading branch information...
commit 12939200691f6896541250a46c93124cd7985e0a 1 parent 76eb343
@drbrain authored
Showing with 50 additions and 3 deletions.
  1. +8 −3 lib/pasteboard.rb
  2. +42 −0 test/test_pasteboard.rb
View
11 lib/pasteboard.rb
@@ -91,6 +91,9 @@ def [] index, flavor = nil
id = get_item_identifier index + 1
get id, flavor
+ rescue => e
+ return nil if e.message.end_with? 'does not exist' # HACK
+ raise
end
##
@@ -185,15 +188,17 @@ def inspect # :nodoc:
# The pasteboard considers items added earlier in the list to have a higher
# preference.
- def put item, id = 0
+ def put *items
clear
flags = sync
raise Error, 'pasteboard sync error' if (flags & MODIFIED) != 0
raise Error, 'pasteboard not owned' if (flags & CLIENT_IS_OWNER) == 0
- item.each do |flavor, data|
- put_item_flavor id, flavor, data
+ items.each_with_index do |item, id|
+ item.each do |flavor, data|
+ put_item_flavor id, flavor, data
+ end
end
self
View
42 test/test_pasteboard.rb
@@ -120,6 +120,34 @@ def test_ids
assert_equal [0], @pb.ids
end
+ def test_index
+ util_put
+
+ item = @pb[0]
+
+ assert_equal @item, item[0, 2]
+
+ assert_nil @pb[1]
+ end
+
+ def test_index_flavor
+ util_put
+
+ item = @pb[0, Pasteboard::Type::UTF_8]
+
+ assert_equal 'π', item
+
+ assert_nil @pb[1, Pasteboard::Type::UTF_8]
+ end
+
+ def test_index_multi
+ util_put_many
+
+ assert_equal @item1, @pb[0][0, 2]
+
+ assert_equal @item2, @pb[1][0, 2]
+ end
+
def test_inspect
expected = '#<%s:0x%x %s>' % [Pasteboard, @pb.object_id, @pb.name]
@@ -165,5 +193,19 @@ def util_put
@pb.put @item
end
+ def util_put_many
+ @item1 = [
+ [Pasteboard::Type::PLAIN_TEXT, 'pi'],
+ [Pasteboard::Type::UTF_8, 'π'],
+ ]
+
+ @item2 = [
+ [Pasteboard::Type::PLAIN_TEXT, ''],
+ [Pasteboard::Type::UTF_8, '0 degrees'],
+ ]
+
+ @pb.put @item1, @item2
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.