Browse Source

Add rspec tests.

Douglas Thrift 10 years ago
parent
commit
d24be4d128
3 changed files with 124 additions and 4 deletions
  1. 69 0
      spec/machine_tag_set_spec.rb
  2. 0 4
      spec/machine_tag_spec.rb
  3. 55 0
      spec/machine_tag_tag_spec.rb

+ 69 - 0
spec/machine_tag_set_spec.rb

@@ -1,2 +1,71 @@
 require 'spec_helper'
 
+describe MachineTag::Set do
+  it 'should create sets with plain tags' do
+    tags = MachineTag::Set['a', 'b', 'c']
+    tags.should eq Set['a', 'b', 'c']
+    tags.plain_tags.should eq Set['a', 'b', 'c']
+    tags.machine_tags.should be_empty
+  end
+
+  it 'should create sets with machine tags' do
+    tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
+    tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
+    tags.plain_tags.should be_empty
+    tags.machine_tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
+    tags['a'].should eq Set['a:b=x', 'a:b=y', 'a:c=z']
+    tags['a:b'].should eq Set['a:b=x', 'a:b=y']
+    tags['a', 'c'].should eq Set['a:c=z']
+  end
+
+  it 'should create sets with mixed plain and machine tags' do
+    tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z', 'd', 'e', 'f']
+    tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd', 'e', 'f']
+    tags.plain_tags.should eq Set['d', 'e', 'f']
+    tags.machine_tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z']
+    tags['a'].should eq Set['a:b=x', 'a:b=y', 'a:c=z']
+    tags['a:b'].should eq Set['a:b=x', 'a:b=y']
+    tags['a', 'c'].should eq Set['a:c=z']
+  end
+
+  describe '#add' do
+    let(:tags) { MachineTag::Set.new }
+
+    it 'should add strings' do
+      tags << 'a'
+      tags.first.should be_an_instance_of(MachineTag::Tag)
+    end
+
+    it 'should add tags' do
+      tag = MachineTag::Tag.new('a')
+      tags << tag
+      tags.first.should be(tag)
+    end
+  end
+
+  describe '#[]' do
+    let(:tags) { MachineTag::Set.new }
+
+    it 'should not retrieve with an invalid predicate' do
+      expect do
+        tags['a', '!']
+      end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
+    end
+
+    it 'should not retrieve with a combined namespace and predicate and a predicate' do
+      expect do
+        tags['a:b', 'c']
+      end.to raise_error(ArgumentError, 'Separate predicate passed with namespace and predicate: "a:b", "c"')
+    end
+
+    it 'should not retreive with an invalid namespace and/or predicate' do
+      expect do
+        tags['!']
+      end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "!", nil')
+
+      expect do
+        tags['a:!']
+      end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "a:!", nil')
+    end
+  end
+end

+ 0 - 4
spec/machine_tag_spec.rb

@@ -4,8 +4,4 @@ describe MachineTag do
   it 'should have a version number' do
     MachineTag::VERSION.should_not be_nil
   end
-
-  it 'should do something useful' do
-    false.should be_true
-  end
 end

+ 55 - 0
spec/machine_tag_tag_spec.rb

@@ -1,2 +1,57 @@
 require 'spec_helper'
 
+describe MachineTag::Tag do
+  it 'should create plain tags' do
+    tag = MachineTag::Tag.new('a')
+    tag.should eq 'a'
+    tag.namespace.should be_nil
+    tag.predicate.should be_nil
+    tag.namespace_and_predicate.should be_nil
+    tag.value.should be_nil
+    tag.machine_tag?.should be_false
+  end
+
+  it 'should create machine tags' do
+    tag = MachineTag::Tag.new('a:b=c')
+    tag.should eq 'a:b=c'
+    tag.namespace.should eq 'a'
+    tag.predicate.should eq 'b'
+    tag.namespace_and_predicate.should eq 'a:b'
+    tag.value.should eq 'c'
+    tag.machine_tag?.should be_true
+  end
+
+  it 'should handle a machine tag with a quoted value' do
+    tag = MachineTag::Tag.new('a:b="c d"')
+    tag.should eq 'a:b="c d"'
+    tag.namespace.should eq 'a'
+    tag.predicate.should eq 'b'
+    tag.namespace_and_predicate.should eq 'a:b'
+    tag.value.should eq 'c d'
+    tag.machine_tag?.should be_true
+  end
+
+  describe '::machine_tag' do
+    it 'should create machine tags' do
+      tag = MachineTag::Tag.machine_tag('a', 'b', 'c')
+      tag.should eq 'a:b=c'
+      tag.namespace.should eq 'a'
+      tag.predicate.should eq 'b'
+      tag.namespace_and_predicate.should eq 'a:b'
+      tag.value.should eq 'c'
+      tag.machine_tag?.should be_true
+    end
+
+    it 'should not create machine tags with invalid namespaces' do
+      expect do
+        MachineTag::Tag.machine_tag('!', 'b', 'c')
+      end.to raise_error(ArgumentError, 'Invalid machine tag namespace: "!"')
+    end
+
+    it 'should not create machine tags with invalid predicates' do
+      expect do
+        MachineTag::Tag.machine_tag('a', '!', 'c')
+      end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
+    end
+  end
+end