qrb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +58 -0
  4. data/LICENCE.md +22 -0
  5. data/Manifest.txt +11 -0
  6. data/README.md +118 -0
  7. data/Rakefile +11 -0
  8. data/lib/qrb/Q/default.q +29 -0
  9. data/lib/qrb/data_type.rb +23 -0
  10. data/lib/qrb/errors.rb +23 -0
  11. data/lib/qrb/support/attribute.rb +53 -0
  12. data/lib/qrb/support/collection_type.rb +25 -0
  13. data/lib/qrb/support/dress_helper.rb +68 -0
  14. data/lib/qrb/support/heading.rb +57 -0
  15. data/lib/qrb/support/type_factory.rb +178 -0
  16. data/lib/qrb/support.rb +5 -0
  17. data/lib/qrb/syntax/ad_type.rb +25 -0
  18. data/lib/qrb/syntax/attribute.rb +11 -0
  19. data/lib/qrb/syntax/builtin_type.rb +13 -0
  20. data/lib/qrb/syntax/constraint_def.rb +11 -0
  21. data/lib/qrb/syntax/constraints.rb +18 -0
  22. data/lib/qrb/syntax/contract.rb +25 -0
  23. data/lib/qrb/syntax/definitions.rb +14 -0
  24. data/lib/qrb/syntax/expression.rb +12 -0
  25. data/lib/qrb/syntax/heading.rb +15 -0
  26. data/lib/qrb/syntax/lambda_expr.rb +11 -0
  27. data/lib/qrb/syntax/named_constraint.rb +11 -0
  28. data/lib/qrb/syntax/q.citrus +195 -0
  29. data/lib/qrb/syntax/relation_type.rb +11 -0
  30. data/lib/qrb/syntax/seq_type.rb +12 -0
  31. data/lib/qrb/syntax/set_type.rb +12 -0
  32. data/lib/qrb/syntax/sub_type.rb +13 -0
  33. data/lib/qrb/syntax/support.rb +13 -0
  34. data/lib/qrb/syntax/system.rb +15 -0
  35. data/lib/qrb/syntax/tuple_type.rb +11 -0
  36. data/lib/qrb/syntax/type_def.rb +14 -0
  37. data/lib/qrb/syntax/type_ref.rb +13 -0
  38. data/lib/qrb/syntax/union_type.rb +12 -0
  39. data/lib/qrb/syntax/unnamed_constraint.rb +11 -0
  40. data/lib/qrb/syntax.rb +42 -0
  41. data/lib/qrb/system.rb +63 -0
  42. data/lib/qrb/type/ad_type.rb +111 -0
  43. data/lib/qrb/type/builtin_type.rb +56 -0
  44. data/lib/qrb/type/relation_type.rb +81 -0
  45. data/lib/qrb/type/seq_type.rb +51 -0
  46. data/lib/qrb/type/set_type.rb +52 -0
  47. data/lib/qrb/type/sub_type.rb +94 -0
  48. data/lib/qrb/type/tuple_type.rb +99 -0
  49. data/lib/qrb/type/union_type.rb +78 -0
  50. data/lib/qrb/type.rb +63 -0
  51. data/lib/qrb/version.rb +14 -0
  52. data/lib/qrb.rb +63 -0
  53. data/qrb.gemspec +186 -0
  54. data/spec/acceptance/Q/test_default.rb +96 -0
  55. data/spec/acceptance/Q/test_parsing.rb +15 -0
  56. data/spec/acceptance/ad_type/test_in_q.rb +82 -0
  57. data/spec/acceptance/ad_type/test_in_ruby.rb +60 -0
  58. data/spec/spec_helper.rb +68 -0
  59. data/spec/unit/attribute/test_equality.rb +26 -0
  60. data/spec/unit/attribute/test_fetch_on.rb +50 -0
  61. data/spec/unit/attribute/test_initialize.rb +13 -0
  62. data/spec/unit/attribute/test_to_name.rb +10 -0
  63. data/spec/unit/heading/test_each.rb +28 -0
  64. data/spec/unit/heading/test_equality.rb +28 -0
  65. data/spec/unit/heading/test_initialize.rb +36 -0
  66. data/spec/unit/heading/test_size.rb +30 -0
  67. data/spec/unit/heading/test_to_name.rb +32 -0
  68. data/spec/unit/qrb/test_parse.rb +18 -0
  69. data/spec/unit/syntax/nodes/test_ad_type.rb +94 -0
  70. data/spec/unit/syntax/nodes/test_attribute.rb +25 -0
  71. data/spec/unit/syntax/nodes/test_builtin_type.rb +32 -0
  72. data/spec/unit/syntax/nodes/test_comment.rb +26 -0
  73. data/spec/unit/syntax/nodes/test_constraint_def.rb +27 -0
  74. data/spec/unit/syntax/nodes/test_constraints.rb +51 -0
  75. data/spec/unit/syntax/nodes/test_contract.rb +62 -0
  76. data/spec/unit/syntax/nodes/test_expression.rb +43 -0
  77. data/spec/unit/syntax/nodes/test_heading.rb +41 -0
  78. data/spec/unit/syntax/nodes/test_named_constraint.rb +31 -0
  79. data/spec/unit/syntax/nodes/test_relation_type.rb +41 -0
  80. data/spec/unit/syntax/nodes/test_seq_type.rb +24 -0
  81. data/spec/unit/syntax/nodes/test_set_type.rb +24 -0
  82. data/spec/unit/syntax/nodes/test_spacing.rb +25 -0
  83. data/spec/unit/syntax/nodes/test_sub_type.rb +52 -0
  84. data/spec/unit/syntax/nodes/test_tuple_type.rb +41 -0
  85. data/spec/unit/syntax/nodes/test_union_type.rb +23 -0
  86. data/spec/unit/syntax/nodes/test_unnamed_constraint.rb +31 -0
  87. data/spec/unit/syntax/test_compile_type.rb +22 -0
  88. data/spec/unit/system/test_add_type.rb +47 -0
  89. data/spec/unit/system/test_dsl.rb +30 -0
  90. data/spec/unit/system/test_dup.rb +30 -0
  91. data/spec/unit/system/test_fetch.rb +42 -0
  92. data/spec/unit/system/test_get_type.rb +30 -0
  93. data/spec/unit/system/test_initialize.rb +10 -0
  94. data/spec/unit/test_qrb.rb +15 -0
  95. data/spec/unit/type/ad_type/test_default_name.rb +15 -0
  96. data/spec/unit/type/ad_type/test_dress.rb +55 -0
  97. data/spec/unit/type/ad_type/test_include.rb +22 -0
  98. data/spec/unit/type/ad_type/test_initialize.rb +40 -0
  99. data/spec/unit/type/ad_type/test_name.rb +20 -0
  100. data/spec/unit/type/builtin_type/test_default_name.rb +12 -0
  101. data/spec/unit/type/builtin_type/test_dress.rb +33 -0
  102. data/spec/unit/type/builtin_type/test_equality.rb +26 -0
  103. data/spec/unit/type/builtin_type/test_include.rb +22 -0
  104. data/spec/unit/type/builtin_type/test_initialize.rb +12 -0
  105. data/spec/unit/type/builtin_type/test_name.rb +24 -0
  106. data/spec/unit/type/relation_type/test_default_name.rb +16 -0
  107. data/spec/unit/type/relation_type/test_dress.rb +164 -0
  108. data/spec/unit/type/relation_type/test_equality.rb +32 -0
  109. data/spec/unit/type/relation_type/test_include.rb +46 -0
  110. data/spec/unit/type/relation_type/test_initialize.rb +26 -0
  111. data/spec/unit/type/relation_type/test_name.rb +24 -0
  112. data/spec/unit/type/seq_type/test_default_name.rb +14 -0
  113. data/spec/unit/type/seq_type/test_dress.rb +49 -0
  114. data/spec/unit/type/seq_type/test_equality.rb +26 -0
  115. data/spec/unit/type/seq_type/test_include.rb +43 -0
  116. data/spec/unit/type/seq_type/test_initialize.rb +28 -0
  117. data/spec/unit/type/seq_type/test_name.rb +24 -0
  118. data/spec/unit/type/set_type/test_default_name.rb +14 -0
  119. data/spec/unit/type/set_type/test_dress.rb +66 -0
  120. data/spec/unit/type/set_type/test_equality.rb +26 -0
  121. data/spec/unit/type/set_type/test_include.rb +43 -0
  122. data/spec/unit/type/set_type/test_initialize.rb +28 -0
  123. data/spec/unit/type/set_type/test_name.rb +24 -0
  124. data/spec/unit/type/sub_type/test_default_name.rb +14 -0
  125. data/spec/unit/type/sub_type/test_dress.rb +75 -0
  126. data/spec/unit/type/sub_type/test_equality.rb +34 -0
  127. data/spec/unit/type/sub_type/test_include.rb +34 -0
  128. data/spec/unit/type/sub_type/test_initialize.rb +16 -0
  129. data/spec/unit/type/sub_type/test_name.rb +24 -0
  130. data/spec/unit/type/tuple_type/test_default_name.rb +14 -0
  131. data/spec/unit/type/tuple_type/test_dress.rb +112 -0
  132. data/spec/unit/type/tuple_type/test_equality.rb +32 -0
  133. data/spec/unit/type/tuple_type/test_include.rb +38 -0
  134. data/spec/unit/type/tuple_type/test_initialize.rb +30 -0
  135. data/spec/unit/type/tuple_type/test_name.rb +24 -0
  136. data/spec/unit/type/union_type/test_default_name.rb +12 -0
  137. data/spec/unit/type/union_type/test_dress.rb +43 -0
  138. data/spec/unit/type/union_type/test_equality.rb +30 -0
  139. data/spec/unit/type/union_type/test_include.rb +28 -0
  140. data/spec/unit/type/union_type/test_initialize.rb +24 -0
  141. data/spec/unit/type/union_type/test_name.rb +20 -0
  142. data/spec/unit/type_factory/dsl/test_adt.rb +54 -0
  143. data/spec/unit/type_factory/dsl/test_attribute.rb +37 -0
  144. data/spec/unit/type_factory/dsl/test_attributes.rb +41 -0
  145. data/spec/unit/type_factory/dsl/test_builtin.rb +45 -0
  146. data/spec/unit/type_factory/dsl/test_relation.rb +85 -0
  147. data/spec/unit/type_factory/dsl/test_seq.rb +57 -0
  148. data/spec/unit/type_factory/dsl/test_set.rb +57 -0
  149. data/spec/unit/type_factory/dsl/test_subtype.rb +91 -0
  150. data/spec/unit/type_factory/dsl/test_tuple.rb +73 -0
  151. data/spec/unit/type_factory/dsl/test_union.rb +81 -0
  152. data/spec/unit/type_factory/factory/test_builtin.rb +24 -0
  153. data/spec/unit/type_factory/factory/test_seq_type.rb +44 -0
  154. data/spec/unit/type_factory/factory/test_set_type.rb +44 -0
  155. data/spec/unit/type_factory/factory/test_sub_type.rb +53 -0
  156. data/spec/unit/type_factory/factory/test_tuple_type.rb +43 -0
  157. data/tasks/gem.rake +73 -0
  158. data/tasks/test.rake +31 -0
  159. metadata +344 -0
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe BuiltinType, "name" do
4
+
5
+ subject{ type.name }
6
+
7
+ context 'when not provided' do
8
+ let(:type){ BuiltinType.new(Integer) }
9
+
10
+ it 'uses the default name' do
11
+ subject.should eq("Integer")
12
+ end
13
+ end
14
+
15
+ context 'when provided' do
16
+ let(:type){ BuiltinType.new(Integer, "int") }
17
+
18
+ it 'uses the specified name' do
19
+ subject.should eq("int")
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "default_name" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, byte)])
7
+ }
8
+
9
+ subject{ type.default_name }
10
+
11
+ let(:type){ RelationType.new(heading) }
12
+
13
+ it{ should eq("{{a: Byte}}") }
14
+
15
+ end
16
+ end
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "dress" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:r, byte),
7
+ Attribute.new(:g, byte),
8
+ Attribute.new(:b, byte)])
9
+ }
10
+
11
+ let(:type){
12
+ RelationType.new(heading, "colors")
13
+ }
14
+
15
+ subject{ type.dress(arg) }
16
+
17
+ context 'with a valid array of Hashes' do
18
+ let(:arg){
19
+ [
20
+ { "r" => 12, "g" => 13, "b" => 255 },
21
+ { "r" => 12, "g" => 15, "b" => 198 }
22
+ ]
23
+ }
24
+ let(:expected){
25
+ [
26
+ { r: 12, g: 13, b: 255 },
27
+ { r: 12, g: 15, b: 198 }
28
+ ]
29
+ }
30
+
31
+ it 'should coerce to an Enumerable of tuples' do
32
+ subject.should be_a(Enumerable)
33
+ subject.to_a.should eq(expected)
34
+ end
35
+ end
36
+
37
+ context 'with an empty array' do
38
+ let(:arg){
39
+ []
40
+ }
41
+ let(:expected){
42
+ []
43
+ }
44
+
45
+ it 'should coerce to an Enumerable of tuples' do
46
+ subject.should be_a(Enumerable)
47
+ subject.to_a.should eq(expected)
48
+ end
49
+ end
50
+
51
+ context 'when raising an error' do
52
+
53
+ subject do
54
+ type.dress(arg) rescue $!
55
+ end
56
+
57
+ context 'with something else than an Array' do
58
+ let(:arg){
59
+ "foo"
60
+ }
61
+
62
+ it 'should raise a TypeError' do
63
+ subject.should be_a(TypeError)
64
+ subject.message.should eq("Invalid value `foo` for colors")
65
+ end
66
+
67
+ it 'should have no cause' do
68
+ subject.cause.should be_nil
69
+ end
70
+
71
+ it 'should have an empty location' do
72
+ subject.location.should eq('')
73
+ end
74
+ end
75
+
76
+ context 'with Array of non-tuples' do
77
+ let(:arg){
78
+ ["foo"]
79
+ }
80
+
81
+ it 'should raise a TypeError' do
82
+ subject.should be_a(TypeError)
83
+ subject.message.should eq("Invalid value `foo` for {r: Byte, g: Byte, b: Byte}")
84
+ end
85
+
86
+ it 'should have no cause' do
87
+ subject.cause.should be_nil
88
+ end
89
+
90
+ it 'should have the correct location' do
91
+ subject.location.should eq('0')
92
+ end
93
+ end
94
+
95
+ context 'with a wrong tuple' do
96
+ let(:arg){
97
+ [
98
+ { "r" => 12, "g" => 13, "b" => 255 },
99
+ { "r" => 12, "g" => 13 }
100
+ ]
101
+ }
102
+
103
+ it 'should raise a TypeError' do
104
+ subject.should be_a(TypeError)
105
+ subject.message.should eq("Missing attribute `b`")
106
+ end
107
+
108
+ it 'should have no cause' do
109
+ subject.cause.should be_nil
110
+ end
111
+
112
+ it 'should have the correct location' do
113
+ subject.location.should eq('1')
114
+ end
115
+ end
116
+
117
+ context 'with a wrong tuple attribute' do
118
+ let(:arg){
119
+ [
120
+ { "r" => 12, "g" => 13, "b" => 255 },
121
+ { "r" => 12, "g" => 13, "b" => 12.0 }
122
+ ]
123
+ }
124
+
125
+ it 'should raise a TypeError' do
126
+ subject.should be_a(TypeError)
127
+ subject.message.should eq("Invalid value `12.0` for Byte")
128
+ end
129
+
130
+ it 'should have a cause' do
131
+ subject.cause.should_not be_nil
132
+ end
133
+
134
+ it 'should have the correct location' do
135
+ subject.location.should eq('1/b')
136
+ end
137
+ end
138
+
139
+ context 'with a duplicate tuple' do
140
+ let(:arg){
141
+ [
142
+ { "r" => 12, "g" => 13, "b" => 255 },
143
+ { "r" => 12, "g" => 192, "b" => 13 },
144
+ { "r" => 12, "g" => 13, "b" => 255 }
145
+ ]
146
+ }
147
+
148
+ it 'should raise a TypeError' do
149
+ subject.should be_a(TypeError)
150
+ subject.message.should eq("Duplicate tuple")
151
+ end
152
+
153
+ it 'should have no cause' do
154
+ subject.cause.should be_nil
155
+ end
156
+
157
+ it 'should have the correct location' do
158
+ subject.location.should eq('2')
159
+ end
160
+ end
161
+ end
162
+
163
+ end
164
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "equality" do
4
+
5
+ let(:h1){ Heading.new([Attribute.new(:r, intType), Attribute.new(:b, intType)]) }
6
+ let(:h2){ Heading.new([Attribute.new(:b, intType), Attribute.new(:r, intType)]) }
7
+ let(:h3){ Heading.new([Attribute.new(:b, intType)]) }
8
+
9
+ let(:type1) { RelationType.new(h1) }
10
+ let(:type2) { RelationType.new(h2) }
11
+ let(:type3) { RelationType.new(h3) }
12
+
13
+ it 'should apply structural equality' do
14
+ (type1 == type2).should be_true
15
+ (type2 == type1).should be_true
16
+ end
17
+
18
+ it 'should apply distinguish different types' do
19
+ (type1 == type3).should be_false
20
+ (type2 == type3).should be_false
21
+ end
22
+
23
+ it 'should be a total function, with nil for non types' do
24
+ (type1 == 12).should be_false
25
+ end
26
+
27
+ it 'should implement hash accordingly' do
28
+ [type1, type2].map(&:hash).uniq.size.should eq(1)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "include?" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, intType)])
7
+ }
8
+
9
+ let(:type){ RelationType.new(heading) }
10
+
11
+ subject{ type.include?(arg) }
12
+
13
+ context 'when a empty set' do
14
+ let(:arg){ Set.new }
15
+
16
+ it{ should be_true }
17
+ end
18
+
19
+ context 'when a valid, non empty set' do
20
+ let(:arg){ Set.new }
21
+
22
+ before do
23
+ arg << {a: 12} << {a: 15}
24
+ end
25
+
26
+ it{ should be_true }
27
+ end
28
+
29
+ context 'when not a set' do
30
+ let(:arg){ "foo" }
31
+
32
+ it{ should be_false }
33
+ end
34
+
35
+ context 'when a set containing invalid tuples' do
36
+ let(:arg){ Set.new }
37
+
38
+ before do
39
+ arg << {a: 12.0}
40
+ end
41
+
42
+ it{ should be_false }
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "initialize" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, intType)])
7
+ }
8
+
9
+ context 'with a valid heading' do
10
+ subject{ RelationType.new(heading) }
11
+
12
+ it{ should be_a(RelationType) }
13
+ end
14
+
15
+ context 'with an invalid heading' do
16
+ subject{ RelationType.new("foo", "bar") }
17
+
18
+ it 'should raise an error' do
19
+ ->{
20
+ subject
21
+ }.should raise_error(ArgumentError, "Heading expected, got `foo`")
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe RelationType, "name" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, byte)])
7
+ }
8
+
9
+ subject{ type.name }
10
+
11
+ context 'when not provided' do
12
+ let(:type){ RelationType.new(heading) }
13
+
14
+ it{ should eq("{{a: Byte}}") }
15
+ end
16
+
17
+ context 'when provided' do
18
+ let(:type){ RelationType.new(heading, "colors") }
19
+
20
+ it{ should eq("colors") }
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, 'default_name' do
4
+
5
+ let(:type){
6
+ SeqType.new(intType, "foo")
7
+ }
8
+
9
+ subject{ type.default_name }
10
+
11
+ it{ should eq('[intType]') }
12
+
13
+ end
14
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, 'dress' do
4
+
5
+ let(:type){ SeqType.new(byte) }
6
+
7
+ subject{ type.dress(arg) }
8
+
9
+ context 'with an empty array' do
10
+ let(:arg){ [] }
11
+
12
+ it{ should eq([]) }
13
+ end
14
+
15
+ context 'with a valid array' do
16
+ let(:arg){ [12, 16] }
17
+
18
+ it{ should eq([12, 16]) }
19
+ end
20
+
21
+ context 'with not a enumerable' do
22
+ let(:arg){ "foo" }
23
+
24
+ it 'should raise an error' do
25
+ ->{
26
+ subject
27
+ }.should raise_error("Invalid value `foo` for [Byte]")
28
+ end
29
+ end
30
+
31
+ context 'with an array with non bytes' do
32
+ let(:arg){ [2, 4, -12] }
33
+
34
+ subject{
35
+ type.dress(arg) rescue $!
36
+ }
37
+
38
+ it 'should raise an error' do
39
+ subject.should be_a(TypeError)
40
+ subject.message.should eq("Invalid value `-12` for Byte")
41
+ end
42
+
43
+ it 'should have correct location' do
44
+ subject.location.should eq("2")
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, "equality" do
4
+
5
+ let(:type) { SeqType.new(intType) }
6
+ let(:type2){ SeqType.new(intType) }
7
+ let(:type3){ SeqType.new(floatType) }
8
+
9
+ it 'should apply structural equality' do
10
+ (type == type2).should be_true
11
+ end
12
+
13
+ it 'should apply distinguish different types' do
14
+ (type == type3).should be_false
15
+ end
16
+
17
+ it 'should be a total function, with false for non types' do
18
+ (type == 12).should be_false
19
+ end
20
+
21
+ it 'should implement hash accordingly' do
22
+ (type.hash == type2.hash).should be_true
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, "include?" do
4
+
5
+ let(:type){ SeqType.new(intType) }
6
+
7
+ subject{ type.include?(arg) }
8
+
9
+ context 'when included on empty array' do
10
+ let(:arg){ [] }
11
+
12
+ it{ should be_true }
13
+ end
14
+
15
+ context 'when included on non empty array' do
16
+ let(:arg){ [] }
17
+
18
+ before do
19
+ arg << 12
20
+ end
21
+
22
+ it{ should be_true }
23
+ end
24
+
25
+ context 'when not an array' do
26
+ let(:arg){ Set.new }
27
+
28
+ it{ should be_false }
29
+ end
30
+
31
+ context 'when an array with non ints' do
32
+ let(:arg){ [] }
33
+
34
+ before do
35
+ arg << 12
36
+ arg << "foo"
37
+ end
38
+
39
+ it{ should be_false }
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, 'initialize' do
4
+
5
+ subject{
6
+ SeqType.new(intType)
7
+ }
8
+
9
+ context 'with valid arguments' do
10
+ it{ should be_a(SeqType) }
11
+
12
+ it 'should set the instance variables' do
13
+ subject.elm_type.should eq(intType)
14
+ end
15
+ end
16
+
17
+ context 'with invalid arguments' do
18
+ subject{ SeqType.new("foo") }
19
+
20
+ it 'should raise an error' do
21
+ ->{
22
+ subject
23
+ }.should raise_error(ArgumentError, 'Qrb::Type expected, got `foo`')
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SeqType, 'name' do
4
+
5
+ subject{ type.name }
6
+
7
+ context 'when not specified' do
8
+ let(:type){
9
+ SeqType.new(intType)
10
+ }
11
+
12
+ it{ should eq('[intType]') }
13
+ end
14
+
15
+ context 'when specified' do
16
+ let(:type){
17
+ SeqType.new(intType, "foo")
18
+ }
19
+
20
+ it{ should eq('foo') }
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SetType, 'default_name' do
4
+
5
+ let(:type){
6
+ SetType.new(intType, "foo")
7
+ }
8
+
9
+ subject{ type.default_name }
10
+
11
+ it{ should eq('{intType}') }
12
+
13
+ end
14
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SetType, 'dress' do
4
+
5
+ let(:type){ SetType.new(byte) }
6
+
7
+ subject{ type.dress(arg) }
8
+
9
+ context 'with an empty array' do
10
+ let(:arg){ [] }
11
+
12
+ it{ should eq([].to_set) }
13
+ end
14
+
15
+ context 'with a valid array' do
16
+ let(:arg){ [12, 16] }
17
+
18
+ it{ should eq([12, 16].to_set) }
19
+ end
20
+
21
+ context 'with not a enumerable' do
22
+ let(:arg){ "foo" }
23
+
24
+ it 'should raise an error' do
25
+ ->{
26
+ subject
27
+ }.should raise_error("Invalid value `foo` for {Byte}")
28
+ end
29
+ end
30
+
31
+ context 'with an array with non bytes' do
32
+ let(:arg){ [2, 4, -12] }
33
+
34
+ subject{
35
+ type.dress(arg) rescue $!
36
+ }
37
+
38
+ it 'should raise an error' do
39
+ subject.should be_a(TypeError)
40
+ subject.message.should eq("Invalid value `-12` for Byte")
41
+ end
42
+
43
+ it 'should have correct location' do
44
+ subject.location.should eq("2")
45
+ end
46
+ end
47
+
48
+ context 'with an array with duplicates' do
49
+ let(:arg){ [2, 4, 2] }
50
+
51
+ subject{
52
+ type.dress(arg) rescue $!
53
+ }
54
+
55
+ it 'should raise an error' do
56
+ subject.should be_a(TypeError)
57
+ subject.message.should eq("Duplicate value `2`")
58
+ end
59
+
60
+ it 'should have correct location' do
61
+ subject.location.should eq("2")
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SetType, "equality" do
4
+
5
+ let(:type) { SetType.new(intType) }
6
+ let(:type2){ SetType.new(intType) }
7
+ let(:type3){ SetType.new(floatType) }
8
+
9
+ it 'should apply structural equality' do
10
+ (type == type2).should be_true
11
+ end
12
+
13
+ it 'should apply distinguish different types' do
14
+ (type == type3).should be_false
15
+ end
16
+
17
+ it 'should be a total function, with false for non types' do
18
+ (type == 12).should be_false
19
+ end
20
+
21
+ it 'should implement hash accordingly' do
22
+ (type.hash == type2.hash).should be_true
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ module Qrb
3
+ describe SetType, "include?" do
4
+
5
+ let(:type){ SetType.new(intType) }
6
+
7
+ subject{ type.include?(arg) }
8
+
9
+ context 'when included on empty set' do
10
+ let(:arg){ Set.new }
11
+
12
+ it{ should be_true }
13
+ end
14
+
15
+ context 'when included on non empty set' do
16
+ let(:arg){ Set.new }
17
+
18
+ before do
19
+ arg << 12
20
+ end
21
+
22
+ it{ should be_true }
23
+ end
24
+
25
+ context 'when not a set' do
26
+ let(:arg){ [] }
27
+
28
+ it{ should be_false }
29
+ end
30
+
31
+ context 'when a set with non ints' do
32
+ let(:arg){ Set.new }
33
+
34
+ before do
35
+ arg << 12
36
+ arg << "foo"
37
+ end
38
+
39
+ it{ should be_false }
40
+ end
41
+
42
+ end
43
+ end