diff --git a/lib/pasteboard.rb b/lib/pasteboard.rb index 76272a4..1d8f9d0 100644 --- a/lib/pasteboard.rb +++ b/lib/pasteboard.rb @@ -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. @@ -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+. # @@ -164,6 +166,8 @@ def put item, id = 0 item.each do |flavor, data| put_item_flavor id, flavor, data end + + self end end diff --git a/test/test_pasteboard.rb b/test/test_pasteboard.rb index 31a1769..966f85b 100644 --- a/test/test_pasteboard.rb +++ b/test/test_pasteboard.rb @@ -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 @@ -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 @@ -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