Browse Source

Always return a ::Set from MachineTag::Set#[].

Douglas Thrift 10 years ago
parent
commit
d54bf8ceda
3 changed files with 41 additions and 9 deletions
  1. 7 7
      lib/machine_tag/set.rb
  2. 1 1
      lib/machine_tag/version.rb
  3. 33 1
      spec/machine_tag_set_spec.rb

+ 7 - 7
lib/machine_tag/set.rb

@@ -46,8 +46,8 @@ module MachineTag
     # @param block [Proc] the optional block to preprocess elements before inserting them
     #
     def initialize(enum = nil, &block)
-      @plain_tags = ::Set.new
-      @machine_tags = ::Set.new
+      @plain_tags = ::Set[]
+      @machine_tags = ::Set[]
       @tags_by_namespace = {}
       @tags_by_namespace_and_predicate = {}
       super
@@ -65,9 +65,9 @@ module MachineTag
 
       if tag.machine_tag?
         @machine_tags << tag
-        @tags_by_namespace[tag.namespace] ||= ::Set.new
+        @tags_by_namespace[tag.namespace] ||= ::Set[]
         @tags_by_namespace[tag.namespace] << tag
-        @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set.new
+        @tags_by_namespace_and_predicate[tag.namespace_and_predicate] ||= ::Set[]
         @tags_by_namespace_and_predicate[tag.namespace_and_predicate] << tag
       else
         @plain_tags << tag
@@ -116,20 +116,20 @@ module MachineTag
           namespace = namespace_or_namespace_and_predicate
 
           unless predicate
-            @tags_by_namespace[namespace]
+            @tags_by_namespace[namespace] || Set[]
           else
             case predicate
             when Regexp
               ::Set.new @tags_by_namespace[namespace].select { |machine_tag| machine_tag.predicate =~ predicate }
             else
               raise ArgumentError, "Invalid machine tag predicate: #{predicate.inspect}" unless predicate =~ /^#{PREFIX}$/
-              @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"]
+              @tags_by_namespace_and_predicate["#{namespace}:#{predicate}"] || Set[]
             end
           end
         elsif namespace_or_namespace_and_predicate =~ /^#{NAMESPACE_AND_PREDICATE}$/
           namespace_and_predicate = namespace_or_namespace_and_predicate
           raise ArgumentError, "Separate predicate passed with namespace and predicate: #{namespace_and_predicate.inspect}, #{predicate.inspect}" if predicate
-          @tags_by_namespace_and_predicate[namespace_and_predicate]
+          @tags_by_namespace_and_predicate[namespace_and_predicate] || Set[]
         else
           raise ArgumentError, "Invalid machine tag namespace and/or predicate: #{namespace_or_namespace_and_predicate.inspect}, #{predicate.inspect}"
         end

+ 1 - 1
lib/machine_tag/version.rb

@@ -26,5 +26,5 @@
 module MachineTag
   # The version of this library.
   #
-  VERSION = "1.1.2"
+  VERSION = "1.1.3"
 end

+ 33 - 1
spec/machine_tag_set_spec.rb

@@ -29,7 +29,7 @@ describe MachineTag::Set do
   end
 
   describe '#add' do
-    let(:tags) { MachineTag::Set.new }
+    let(:tags) { MachineTag::Set[] }
 
     it 'should add strings' do
       tags << 'a'
@@ -76,6 +76,38 @@ describe MachineTag::Set do
       tags[/^[abc]/, /^(cc|ee)$/].should eq Set['aa:cc=1', 'bb:cc=3', 'cc:ee=5']
     end
 
+    it 'should return an empty set with a String for namespace and no predicate' do
+      tags['xx'].should eq Set[]
+    end
+
+    it 'should return an empty set with a String for namespace and predicate' do
+      tags['aa', 'xx'].should eq Set[]
+    end
+
+    it 'should return an empty set with a String for namespace and predicate as one argument' do
+      tags['aa:xx'].should eq Set[]
+    end
+
+    it 'should return an empty set with a Regexp for namespace and no predicate' do
+      tags[/^x/].should eq Set[]
+    end
+
+    it 'should return an empty set with a Regexp for namespace and predicate as one argument' do
+      tags[/^.x:y.$/].should eq Set[]
+    end
+
+    it 'should return an empty set with a Regexp for namespace and a String for predicate' do
+      tags[/^a/, 'xx'].should eq Set[]
+    end
+
+    it 'should return an empty set with a String for namespace and Regexp for predicate' do
+      tags['cc', /^x/].should eq Set[]
+    end
+
+    it 'should return an empty set with a Regexp for namespace and predicate' do
+      tags[/^a/, /^x/].should eq Set[]
+    end
+
     it 'should not retrieve with an invalid predicate' do
       expect do
         tags['a', '!']