machine_tag_set_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. expect(tags).to eq Set['a', 'b', 'c']
  6. expect(tags.plain_tags).to eq Set['a', 'b', 'c']
  7. expect(tags.machine_tags).to 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. expect(tags).to eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
  12. expect(tags.plain_tags).to be_empty
  13. expect(tags.machine_tags).to eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd:e=f']
  14. expect(tags['a']).to eq Set['a:b=x', 'a:b=y', 'a:c=z']
  15. expect(tags['a:b']).to eq Set['a:b=x', 'a:b=y']
  16. expect(tags['a', 'c']).to 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. expect(tags).to eq Set['a:b=x', 'a:b=y', 'a:c=z', 'd', 'e', 'f']
  21. expect(tags.plain_tags).to eq Set['d', 'e', 'f']
  22. expect(tags.machine_tags).to eq Set['a:b=x', 'a:b=y', 'a:c=z']
  23. expect(tags['a']).to eq Set['a:b=x', 'a:b=y', 'a:c=z']
  24. expect(tags['a:b']).to eq Set['a:b=x', 'a:b=y']
  25. expect(tags['a', 'c']).to eq Set['a:c=z']
  26. end
  27. describe '#add' do
  28. let(:tags) { MachineTag::Set[] }
  29. it 'should add strings' do
  30. tags << 'a'
  31. expect(tags.first).to be_an_instance_of(MachineTag::Tag)
  32. end
  33. it 'should add tags' do
  34. tag = MachineTag::Tag.new('a')
  35. tags << tag
  36. expect(tags.first).to 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. expect(tags[/^a/]).to 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. expect(tags[/^.b:c.$/]).to 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. expect(tags[/^[ab]/, 'cc']).to 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. expect(tags['cc', /^e/]).to eq Set['cc:ee=5', 'cc:ef=6']
  62. end
  63. it 'should retrieve with a Regexp for namespace and predicate' do
  64. expect(tags[/^[abc]/, /^(cc|ee)$/]).to eq Set['aa:cc=1', 'bb:cc=3', 'cc:ee=5']
  65. end
  66. it 'should return an empty set with a String for namespace and no predicate' do
  67. expect(tags['xx']).to eq Set[]
  68. end
  69. it 'should return an empty set with a String for namespace and predicate' do
  70. expect(tags['aa', 'xx']).to eq Set[]
  71. end
  72. it 'should return an empty set with a String for namespace and predicate as one argument' do
  73. expect(tags['aa:xx']).to eq Set[]
  74. end
  75. it 'should return an empty set with a Regexp for namespace and no predicate' do
  76. expect(tags[/^x/]).to eq Set[]
  77. end
  78. it 'should return an empty set with a Regexp for namespace and predicate as one argument' do
  79. expect(tags[/^.x:y.$/]).to eq Set[]
  80. end
  81. it 'should return an empty set with a Regexp for namespace and a String for predicate' do
  82. expect(tags[/^a/, 'xx']).to eq Set[]
  83. end
  84. it 'should return an empty set with a String for namespace and Regexp for predicate' do
  85. expect(tags['cc', /^x/]).to eq Set[]
  86. end
  87. it 'should return an empty set with a Regexp for namespace and predicate' do
  88. expect(tags[/^a/, /^x/]).to eq Set[]
  89. end
  90. it 'should not retrieve with an invalid predicate' do
  91. expect do
  92. tags['a', '!']
  93. end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
  94. expect do
  95. tags[/a/, '!']
  96. end.to raise_error(ArgumentError, 'Invalid machine tag predicate: "!"')
  97. end
  98. it 'should not retrieve with a combined namespace and predicate and a predicate' do
  99. expect do
  100. tags['a:b', 'c']
  101. end.to raise_error(ArgumentError, 'Separate predicate passed with namespace and predicate: "a:b", "c"')
  102. end
  103. it 'should not retreive with an invalid namespace and/or predicate' do
  104. expect do
  105. tags['!']
  106. end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "!", nil')
  107. expect do
  108. tags['a:!']
  109. end.to raise_error(ArgumentError, 'Invalid machine tag namespace and/or predicate: "a:!", nil')
  110. end
  111. end
  112. end