machine_tag_set_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require 'spec_helper'
  2. describe MachineTag::Set do
  3. it 'should create sets with plain tags' do
  4. tags = MachineTag::Set['a', 'b', 'c']
  5. tags.should eq Set['a', 'b', 'c']
  6. tags.plain_tags.should eq Set['a', 'b', 'c']
  7. tags.machine_tags.should be_empty
  8. end
  9. it 'should create sets with machine tags' do
  10. tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
  11. tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
  12. tags.plain_tags.should be_empty
  13. tags.machine_tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
  14. tags['a'].should eq Set['a:b=x', 'a:b=y', 'a:c=z']
  15. tags['a:b'].should eq Set['a:b=x', 'a:b=y']
  16. tags['a', 'c'].should eq Set['a:c=z']
  17. end
  18. it 'should create sets with mixed plain and machine tags' do
  19. tags = MachineTag::Set['a:b=x', 'a:b=y', 'a:c=z', 'd', 'e', 'f']
  20. tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd', 'e', 'f']
  21. tags.plain_tags.should eq Set['d', 'e', 'f']
  22. tags.machine_tags.should eq Set['a:b=x', 'a:b=y', 'a:c=z']
  23. tags['a'].should eq Set['a:b=x', 'a:b=y', 'a:c=z']
  24. tags['a:b'].should eq Set['a:b=x', 'a:b=y']
  25. tags['a', 'c'].should eq Set['a:c=z']
  26. end
  27. describe '#add' do
  28. let(:tags) { MachineTag::Set.new }
  29. it 'should add strings' do
  30. tags << 'a'
  31. tags.first.should be_an_instance_of(MachineTag::Tag)
  32. end
  33. it 'should add tags' do
  34. tag = MachineTag::Tag.new('a')
  35. tags << tag
  36. tags.first.should be(tag)
  37. end
  38. end
  39. describe '#[]' do
  40. let(:tags) do
  41. MachineTag::Set[
  42. 'aa:cc=1',
  43. 'ab:cd=2',
  44. 'bb:cc=3',
  45. 'ba:dd=4',
  46. 'cc:ee=5',
  47. 'cc:ef=6',
  48. 'cc:ff=7',
  49. ]
  50. end
  51. it 'should retrieve with a Regexp for namespace and no predicate' do
  52. tags[/^a/].should eq Set['aa:cc=1', 'ab:cd=2']
  53. end
  54. it 'should retrieve with a Regexp for namespace and predicate as one argument' do
  55. tags[/^.b:c.$/].should eq Set['ab:cd=2', 'bb:cc=3']
  56. end
  57. it 'should retrieve with a Regexp for namespace and a String for predicate' do
  58. tags[/^[ab]/, 'cc'].should eq Set['aa:cc=1', 'bb:cc=3']
  59. end
  60. it 'should retrieve with a String for namespace and Regexp for predicate' do
  61. tags['cc', /^e/].should eq Set['cc:ee=5', 'cc:ef=6']
  62. end
  63. it 'should retrieve with a Regexp for namespace and predicate' do
  64. tags[/^[abc]/, /^(cc|ee)$/].should eq Set['aa:cc=1', 'bb:cc=3', 'cc:ee=5']
  65. end
  66. it 'should not retrieve with an invalid predicate' do
  67. expect do
  68. tags['a', '!']
  69. end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
  70. expect do
  71. tags[/a/, '!']
  72. end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
  73. end
  74. it 'should not retrieve with a combined namespace and predicate and a predicate' do
  75. expect do
  76. tags['a:b', 'c']
  77. end.to raise_error(ArgumentError, 'Separate predicate passed with namespace and predicate: "a:b", "c"')
  78. end
  79. it 'should not retreive with an invalid namespace and/or predicate' do
  80. expect do
  81. tags['!']
  82. end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "!", nil')
  83. expect do
  84. tags['a:!']
  85. end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "a:!", nil')
  86. end
  87. end
  88. end