qrb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +58 -0
- data/LICENCE.md +22 -0
- data/Manifest.txt +11 -0
- data/README.md +118 -0
- data/Rakefile +11 -0
- data/lib/qrb/Q/default.q +29 -0
- data/lib/qrb/data_type.rb +23 -0
- data/lib/qrb/errors.rb +23 -0
- data/lib/qrb/support/attribute.rb +53 -0
- data/lib/qrb/support/collection_type.rb +25 -0
- data/lib/qrb/support/dress_helper.rb +68 -0
- data/lib/qrb/support/heading.rb +57 -0
- data/lib/qrb/support/type_factory.rb +178 -0
- data/lib/qrb/support.rb +5 -0
- data/lib/qrb/syntax/ad_type.rb +25 -0
- data/lib/qrb/syntax/attribute.rb +11 -0
- data/lib/qrb/syntax/builtin_type.rb +13 -0
- data/lib/qrb/syntax/constraint_def.rb +11 -0
- data/lib/qrb/syntax/constraints.rb +18 -0
- data/lib/qrb/syntax/contract.rb +25 -0
- data/lib/qrb/syntax/definitions.rb +14 -0
- data/lib/qrb/syntax/expression.rb +12 -0
- data/lib/qrb/syntax/heading.rb +15 -0
- data/lib/qrb/syntax/lambda_expr.rb +11 -0
- data/lib/qrb/syntax/named_constraint.rb +11 -0
- data/lib/qrb/syntax/q.citrus +195 -0
- data/lib/qrb/syntax/relation_type.rb +11 -0
- data/lib/qrb/syntax/seq_type.rb +12 -0
- data/lib/qrb/syntax/set_type.rb +12 -0
- data/lib/qrb/syntax/sub_type.rb +13 -0
- data/lib/qrb/syntax/support.rb +13 -0
- data/lib/qrb/syntax/system.rb +15 -0
- data/lib/qrb/syntax/tuple_type.rb +11 -0
- data/lib/qrb/syntax/type_def.rb +14 -0
- data/lib/qrb/syntax/type_ref.rb +13 -0
- data/lib/qrb/syntax/union_type.rb +12 -0
- data/lib/qrb/syntax/unnamed_constraint.rb +11 -0
- data/lib/qrb/syntax.rb +42 -0
- data/lib/qrb/system.rb +63 -0
- data/lib/qrb/type/ad_type.rb +111 -0
- data/lib/qrb/type/builtin_type.rb +56 -0
- data/lib/qrb/type/relation_type.rb +81 -0
- data/lib/qrb/type/seq_type.rb +51 -0
- data/lib/qrb/type/set_type.rb +52 -0
- data/lib/qrb/type/sub_type.rb +94 -0
- data/lib/qrb/type/tuple_type.rb +99 -0
- data/lib/qrb/type/union_type.rb +78 -0
- data/lib/qrb/type.rb +63 -0
- data/lib/qrb/version.rb +14 -0
- data/lib/qrb.rb +63 -0
- data/qrb.gemspec +186 -0
- data/spec/acceptance/Q/test_default.rb +96 -0
- data/spec/acceptance/Q/test_parsing.rb +15 -0
- data/spec/acceptance/ad_type/test_in_q.rb +82 -0
- data/spec/acceptance/ad_type/test_in_ruby.rb +60 -0
- data/spec/spec_helper.rb +68 -0
- data/spec/unit/attribute/test_equality.rb +26 -0
- data/spec/unit/attribute/test_fetch_on.rb +50 -0
- data/spec/unit/attribute/test_initialize.rb +13 -0
- data/spec/unit/attribute/test_to_name.rb +10 -0
- data/spec/unit/heading/test_each.rb +28 -0
- data/spec/unit/heading/test_equality.rb +28 -0
- data/spec/unit/heading/test_initialize.rb +36 -0
- data/spec/unit/heading/test_size.rb +30 -0
- data/spec/unit/heading/test_to_name.rb +32 -0
- data/spec/unit/qrb/test_parse.rb +18 -0
- data/spec/unit/syntax/nodes/test_ad_type.rb +94 -0
- data/spec/unit/syntax/nodes/test_attribute.rb +25 -0
- data/spec/unit/syntax/nodes/test_builtin_type.rb +32 -0
- data/spec/unit/syntax/nodes/test_comment.rb +26 -0
- data/spec/unit/syntax/nodes/test_constraint_def.rb +27 -0
- data/spec/unit/syntax/nodes/test_constraints.rb +51 -0
- data/spec/unit/syntax/nodes/test_contract.rb +62 -0
- data/spec/unit/syntax/nodes/test_expression.rb +43 -0
- data/spec/unit/syntax/nodes/test_heading.rb +41 -0
- data/spec/unit/syntax/nodes/test_named_constraint.rb +31 -0
- data/spec/unit/syntax/nodes/test_relation_type.rb +41 -0
- data/spec/unit/syntax/nodes/test_seq_type.rb +24 -0
- data/spec/unit/syntax/nodes/test_set_type.rb +24 -0
- data/spec/unit/syntax/nodes/test_spacing.rb +25 -0
- data/spec/unit/syntax/nodes/test_sub_type.rb +52 -0
- data/spec/unit/syntax/nodes/test_tuple_type.rb +41 -0
- data/spec/unit/syntax/nodes/test_union_type.rb +23 -0
- data/spec/unit/syntax/nodes/test_unnamed_constraint.rb +31 -0
- data/spec/unit/syntax/test_compile_type.rb +22 -0
- data/spec/unit/system/test_add_type.rb +47 -0
- data/spec/unit/system/test_dsl.rb +30 -0
- data/spec/unit/system/test_dup.rb +30 -0
- data/spec/unit/system/test_fetch.rb +42 -0
- data/spec/unit/system/test_get_type.rb +30 -0
- data/spec/unit/system/test_initialize.rb +10 -0
- data/spec/unit/test_qrb.rb +15 -0
- data/spec/unit/type/ad_type/test_default_name.rb +15 -0
- data/spec/unit/type/ad_type/test_dress.rb +55 -0
- data/spec/unit/type/ad_type/test_include.rb +22 -0
- data/spec/unit/type/ad_type/test_initialize.rb +40 -0
- data/spec/unit/type/ad_type/test_name.rb +20 -0
- data/spec/unit/type/builtin_type/test_default_name.rb +12 -0
- data/spec/unit/type/builtin_type/test_dress.rb +33 -0
- data/spec/unit/type/builtin_type/test_equality.rb +26 -0
- data/spec/unit/type/builtin_type/test_include.rb +22 -0
- data/spec/unit/type/builtin_type/test_initialize.rb +12 -0
- data/spec/unit/type/builtin_type/test_name.rb +24 -0
- data/spec/unit/type/relation_type/test_default_name.rb +16 -0
- data/spec/unit/type/relation_type/test_dress.rb +164 -0
- data/spec/unit/type/relation_type/test_equality.rb +32 -0
- data/spec/unit/type/relation_type/test_include.rb +46 -0
- data/spec/unit/type/relation_type/test_initialize.rb +26 -0
- data/spec/unit/type/relation_type/test_name.rb +24 -0
- data/spec/unit/type/seq_type/test_default_name.rb +14 -0
- data/spec/unit/type/seq_type/test_dress.rb +49 -0
- data/spec/unit/type/seq_type/test_equality.rb +26 -0
- data/spec/unit/type/seq_type/test_include.rb +43 -0
- data/spec/unit/type/seq_type/test_initialize.rb +28 -0
- data/spec/unit/type/seq_type/test_name.rb +24 -0
- data/spec/unit/type/set_type/test_default_name.rb +14 -0
- data/spec/unit/type/set_type/test_dress.rb +66 -0
- data/spec/unit/type/set_type/test_equality.rb +26 -0
- data/spec/unit/type/set_type/test_include.rb +43 -0
- data/spec/unit/type/set_type/test_initialize.rb +28 -0
- data/spec/unit/type/set_type/test_name.rb +24 -0
- data/spec/unit/type/sub_type/test_default_name.rb +14 -0
- data/spec/unit/type/sub_type/test_dress.rb +75 -0
- data/spec/unit/type/sub_type/test_equality.rb +34 -0
- data/spec/unit/type/sub_type/test_include.rb +34 -0
- data/spec/unit/type/sub_type/test_initialize.rb +16 -0
- data/spec/unit/type/sub_type/test_name.rb +24 -0
- data/spec/unit/type/tuple_type/test_default_name.rb +14 -0
- data/spec/unit/type/tuple_type/test_dress.rb +112 -0
- data/spec/unit/type/tuple_type/test_equality.rb +32 -0
- data/spec/unit/type/tuple_type/test_include.rb +38 -0
- data/spec/unit/type/tuple_type/test_initialize.rb +30 -0
- data/spec/unit/type/tuple_type/test_name.rb +24 -0
- data/spec/unit/type/union_type/test_default_name.rb +12 -0
- data/spec/unit/type/union_type/test_dress.rb +43 -0
- data/spec/unit/type/union_type/test_equality.rb +30 -0
- data/spec/unit/type/union_type/test_include.rb +28 -0
- data/spec/unit/type/union_type/test_initialize.rb +24 -0
- data/spec/unit/type/union_type/test_name.rb +20 -0
- data/spec/unit/type_factory/dsl/test_adt.rb +54 -0
- data/spec/unit/type_factory/dsl/test_attribute.rb +37 -0
- data/spec/unit/type_factory/dsl/test_attributes.rb +41 -0
- data/spec/unit/type_factory/dsl/test_builtin.rb +45 -0
- data/spec/unit/type_factory/dsl/test_relation.rb +85 -0
- data/spec/unit/type_factory/dsl/test_seq.rb +57 -0
- data/spec/unit/type_factory/dsl/test_set.rb +57 -0
- data/spec/unit/type_factory/dsl/test_subtype.rb +91 -0
- data/spec/unit/type_factory/dsl/test_tuple.rb +73 -0
- data/spec/unit/type_factory/dsl/test_union.rb +81 -0
- data/spec/unit/type_factory/factory/test_builtin.rb +24 -0
- data/spec/unit/type_factory/factory/test_seq_type.rb +44 -0
- data/spec/unit/type_factory/factory/test_set_type.rb +44 -0
- data/spec/unit/type_factory/factory/test_sub_type.rb +53 -0
- data/spec/unit/type_factory/factory/test_tuple_type.rb +43 -0
- data/tasks/gem.rake +73 -0
- data/tasks/test.rake +31 -0
- 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,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,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
|