finitio 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +66 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +34 -30
- data/finitio.gemspec +1 -1
- data/lib/finitio.rb +7 -39
- data/lib/finitio/errors.rb +14 -0
- data/lib/finitio/support.rb +4 -1
- data/lib/finitio/support/attribute.rb +13 -7
- data/lib/finitio/support/compilation.rb +55 -0
- data/lib/finitio/support/constraint.rb +46 -0
- data/lib/finitio/support/contract.rb +33 -0
- data/lib/finitio/support/dress_helper.rb +3 -2
- data/lib/finitio/support/heading.rb +57 -17
- data/lib/finitio/support/metadata.rb +20 -0
- data/lib/finitio/support/type_factory.rb +154 -41
- data/lib/finitio/syntax.rb +16 -31
- data/lib/finitio/syntax/expr.rb +56 -0
- data/lib/finitio/syntax/expr/arith_op.rb +22 -0
- data/lib/finitio/syntax/expr/comparison.rb +22 -0
- data/lib/finitio/syntax/expr/fn_call.rb +24 -0
- data/lib/finitio/syntax/expr/identifier.rb +18 -0
- data/lib/finitio/syntax/expr/literal.rb +19 -0
- data/lib/finitio/syntax/expr/logic_dyadic.rb +22 -0
- data/lib/finitio/syntax/expr/logic_not.rb +21 -0
- data/lib/finitio/syntax/expr/oo_call.rb +24 -0
- data/lib/finitio/syntax/expr/parenthesized.rb +20 -0
- data/lib/finitio/syntax/expr/unary_minus_op.rb +21 -0
- data/lib/finitio/syntax/expressions.citrus +129 -0
- data/lib/finitio/syntax/finitio.citrus +10 -208
- data/lib/finitio/syntax/finitio.sexp +5 -1
- data/lib/finitio/syntax/lexer.citrus +91 -0
- data/lib/finitio/syntax/literal.rb +16 -0
- data/lib/finitio/syntax/literal/boolean.rb +22 -0
- data/lib/finitio/syntax/literal/integer.rb +14 -0
- data/lib/finitio/syntax/literal/real.rb +14 -0
- data/lib/finitio/syntax/literal/string.rb +14 -0
- data/lib/finitio/syntax/literals.citrus +44 -0
- data/lib/finitio/syntax/node.rb +53 -0
- data/lib/finitio/syntax/type.rb +28 -0
- data/lib/finitio/syntax/type/ad_type.rb +29 -0
- data/lib/finitio/syntax/{any_type.rb → type/any_type.rb} +1 -1
- data/lib/finitio/syntax/type/attribute.rb +33 -0
- data/lib/finitio/syntax/{builtin_type.rb → type/builtin_type.rb} +3 -1
- data/lib/finitio/syntax/{constraint_def.rb → type/constraint_def.rb} +4 -3
- data/lib/finitio/syntax/type/constraints.rb +17 -0
- data/lib/finitio/syntax/{contract.rb → type/contract.rb} +9 -3
- data/lib/finitio/syntax/{definitions.rb → type/definitions.rb} +1 -0
- data/lib/finitio/syntax/{expression.rb → type/expression.rb} +2 -1
- data/lib/finitio/syntax/{external_pair.rb → type/external_pair.rb} +3 -1
- data/lib/finitio/syntax/type/heading.rb +30 -0
- data/lib/finitio/syntax/{inline_pair.rb → type/inline_pair.rb} +3 -1
- data/lib/finitio/syntax/{lambda_expr.rb → type/lambda_expr.rb} +3 -0
- data/lib/finitio/syntax/type/main_type.rb +18 -0
- data/lib/finitio/syntax/type/metadata.rb +18 -0
- data/lib/finitio/syntax/type/metadata_attr.rb +15 -0
- data/lib/finitio/syntax/{named_constraint.rb → type/named_constraint.rb} +10 -3
- data/lib/finitio/syntax/type/relation_type.rb +24 -0
- data/lib/finitio/syntax/{seq_type.rb → type/seq_type.rb} +3 -0
- data/lib/finitio/syntax/{set_type.rb → type/set_type.rb} +3 -0
- data/lib/finitio/syntax/type/struct_type.rb +17 -0
- data/lib/finitio/syntax/{sub_type.rb → type/sub_type.rb} +3 -0
- data/lib/finitio/syntax/{system.rb → type/system.rb} +5 -4
- data/lib/finitio/syntax/type/tuple_type.rb +24 -0
- data/lib/finitio/syntax/{type_def.rb → type/type_def.rb} +8 -3
- data/lib/finitio/syntax/{type_ref.rb → type/type_ref.rb} +4 -1
- data/lib/finitio/syntax/{union_type.rb → type/union_type.rb} +1 -0
- data/lib/finitio/syntax/{unnamed_constraint.rb → type/unnamed_constraint.rb} +7 -1
- data/lib/finitio/syntax/types.citrus +196 -0
- data/lib/finitio/system.rb +15 -15
- data/lib/finitio/type.rb +38 -6
- data/lib/finitio/type/ad_type.rb +41 -18
- data/lib/finitio/type/alias_type.rb +37 -0
- data/lib/finitio/type/any_type.rb +7 -3
- data/lib/finitio/type/builtin_type.rb +5 -4
- data/lib/finitio/{support → type}/collection_type.rb +3 -4
- data/lib/finitio/type/hash_based_type.rb +91 -0
- data/lib/finitio/type/heading_based_type.rb +28 -0
- data/lib/finitio/type/multi_relation_type.rb +34 -0
- data/lib/finitio/type/multi_tuple_type.rb +29 -0
- data/lib/finitio/type/proxy_type.rb +40 -0
- data/lib/finitio/type/rel_based_type.rb +42 -0
- data/lib/finitio/type/relation_type.rb +6 -47
- data/lib/finitio/type/seq_type.rb +4 -0
- data/lib/finitio/type/set_type.rb +4 -0
- data/lib/finitio/type/struct_type.rb +84 -0
- data/lib/finitio/type/sub_type.rb +25 -22
- data/lib/finitio/type/tuple_type.rb +6 -57
- data/lib/finitio/type/union_type.rb +12 -5
- data/lib/finitio/version.rb +2 -2
- data/spec/{unit/attribute → attribute}/test_equality.rb +8 -5
- data/spec/{unit/attribute → attribute}/test_fetch_on.rb +5 -5
- data/spec/attribute/test_initialize.rb +26 -0
- data/spec/attribute/test_optional.rb +18 -0
- data/spec/attribute/test_required.rb +18 -0
- data/spec/attribute/test_to_name.rb +20 -0
- data/spec/constraint/test_anonymous.rb +20 -0
- data/spec/constraint/test_equality.rb +39 -0
- data/spec/constraint/test_name.rb +20 -0
- data/spec/constraint/test_named.rb +20 -0
- data/spec/constraint/test_triple_equal.rb +13 -0
- data/spec/{unit/qrb/system.q → finitio/system.fio} +0 -0
- data/spec/{unit/qrb → finitio}/test_ast.rb +0 -0
- data/spec/finitio/test_parse.rb +23 -0
- data/spec/{unit/qrb/test_parse.rb → finitio/test_system.rb} +6 -6
- data/spec/heading/test_allow_extra.rb +24 -0
- data/spec/{unit/heading → heading}/test_each.rb +2 -2
- data/spec/heading/test_equality.rb +38 -0
- data/spec/heading/test_hash.rb +38 -0
- data/spec/heading/test_hash_get.rb +17 -0
- data/spec/{unit/heading → heading}/test_initialize.rb +2 -2
- data/spec/heading/test_multi.rb +57 -0
- data/spec/{unit/heading → heading}/test_size.rb +0 -0
- data/spec/heading/test_to_name.rb +58 -0
- data/spec/spec_helper.rb +17 -1
- data/spec/syntax/expr/test_free_variables.rb +46 -0
- data/spec/syntax/expr/test_to_proc_source.rb +43 -0
- data/spec/{unit/syntax → syntax}/nodes/test_ad_type.rb +26 -26
- data/spec/{unit/syntax → syntax}/nodes/test_any_type.rb +2 -2
- data/spec/syntax/nodes/test_attribute.rb +65 -0
- data/spec/{unit/syntax → syntax}/nodes/test_builtin_type.rb +5 -5
- data/spec/{unit/syntax → syntax}/nodes/test_comment.rb +1 -1
- data/spec/{unit/syntax → syntax}/nodes/test_constraint_def.rb +3 -8
- data/spec/{unit/syntax → syntax}/nodes/test_constraints.rb +21 -16
- data/spec/{unit/syntax → syntax}/nodes/test_contract.rb +28 -35
- data/spec/{unit/syntax → syntax}/nodes/test_expression.rb +13 -5
- data/spec/{unit/syntax → syntax}/nodes/test_heading.rb +25 -7
- data/spec/syntax/nodes/test_metadata.rb +28 -0
- data/spec/{unit/syntax → syntax}/nodes/test_named_constraint.rb +8 -8
- data/spec/syntax/nodes/test_relation_type.rb +84 -0
- data/spec/{unit/syntax → syntax}/nodes/test_seq_type.rb +4 -4
- data/spec/{unit/syntax → syntax}/nodes/test_set_type.rb +4 -4
- data/spec/{unit/syntax → syntax}/nodes/test_spacing.rb +1 -1
- data/spec/syntax/nodes/test_struct_type.rb +38 -0
- data/spec/{unit/syntax → syntax}/nodes/test_sub_type.rb +14 -14
- data/spec/{unit/syntax → syntax}/nodes/test_system.rb +3 -3
- data/spec/syntax/nodes/test_tuple_type.rb +94 -0
- data/spec/syntax/nodes/test_type_def.rb +57 -0
- data/spec/{unit/syntax → syntax}/nodes/test_type_ref.rb +3 -3
- data/spec/{unit/syntax → syntax}/nodes/test_union_type.rb +3 -3
- data/spec/{unit/syntax → syntax}/nodes/test_unnamed_constraint.rb +7 -7
- data/spec/syntax/test_compile.rb +41 -0
- data/spec/{unit/syntax → syntax}/test_compile_type.rb +1 -1
- data/spec/{unit/system → system}/test_add_type.rb +6 -6
- data/spec/{unit/system → system}/test_dsl.rb +2 -2
- data/spec/{unit/system → system}/test_dup.rb +4 -4
- data/spec/{unit/system → system}/test_fetch.rb +4 -4
- data/spec/{unit/system → system}/test_get_type.rb +2 -2
- data/spec/{unit/system → system}/test_initialize.rb +0 -0
- data/spec/test_finitio.rb +8 -0
- data/spec/{unit/type → type}/ad_type/test_default_name.rb +1 -2
- data/spec/{unit/type → type}/ad_type/test_dress.rb +19 -9
- data/spec/{unit/type → type}/ad_type/test_include.rb +3 -3
- data/spec/{unit/type → type}/ad_type/test_initialize.rb +15 -8
- data/spec/{unit/type → type}/ad_type/test_name.rb +2 -2
- data/spec/type/alias_type/test_default_name.rb +10 -0
- data/spec/type/alias_type/test_delegation.rb +29 -0
- data/spec/type/alias_type/test_name.rb +10 -0
- data/spec/{unit/type → type}/any_type/test_default_name.rb +1 -1
- data/spec/{unit/type → type}/any_type/test_dress.rb +0 -0
- data/spec/{unit/type → type}/any_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/any_type/test_include.rb +2 -2
- data/spec/{unit/type → type}/any_type/test_initialize.rb +0 -0
- data/spec/{unit/type → type}/any_type/test_name.rb +2 -2
- data/spec/{unit/type → type}/builtin_type/test_default_name.rb +1 -1
- data/spec/{unit/type → type}/builtin_type/test_dress.rb +3 -3
- data/spec/{unit/type → type}/builtin_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/builtin_type/test_include.rb +2 -2
- data/spec/{unit/type → type}/builtin_type/test_initialize.rb +1 -1
- data/spec/{unit/type → type}/builtin_type/test_name.rb +2 -2
- data/spec/type/multi_relation_type/test_default_name.rb +19 -0
- data/spec/type/multi_relation_type/test_dress.rb +206 -0
- data/spec/type/multi_relation_type/test_equality.rb +36 -0
- data/spec/type/multi_relation_type/test_include.rb +89 -0
- data/spec/type/multi_relation_type/test_initialize.rb +29 -0
- data/spec/type/multi_relation_type/test_name.rb +27 -0
- data/spec/type/multi_tuple_type/test_default_name.rb +17 -0
- data/spec/type/multi_tuple_type/test_dress.rb +146 -0
- data/spec/type/multi_tuple_type/test_equality.rb +32 -0
- data/spec/type/multi_tuple_type/test_include.rb +73 -0
- data/spec/type/multi_tuple_type/test_initialize.rb +30 -0
- data/spec/type/multi_tuple_type/test_name.rb +24 -0
- data/spec/type/proxy_type/test_delegation.rb +37 -0
- data/spec/type/proxy_type/test_resolve.rb +29 -0
- data/spec/{unit/type → type}/relation_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/relation_type/test_dress.rb +24 -24
- data/spec/{unit/type → type}/relation_type/test_equality.rb +6 -6
- data/spec/{unit/type → type}/relation_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/relation_type/test_initialize.rb +2 -2
- data/spec/{unit/type → type}/relation_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/seq_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/seq_type/test_dress.rb +5 -5
- data/spec/{unit/type → type}/seq_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/seq_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/seq_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/seq_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/set_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/set_type/test_dress.rb +8 -8
- data/spec/{unit/type → type}/set_type/test_equality.rb +4 -4
- data/spec/{unit/type → type}/set_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/set_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/set_type/test_name.rb +0 -0
- data/spec/type/struct_type/test_default_name.rb +10 -0
- data/spec/type/struct_type/test_dress.rb +105 -0
- data/spec/type/struct_type/test_equality.rb +28 -0
- data/spec/type/struct_type/test_include.rb +40 -0
- data/spec/type/struct_type/test_initialize.rb +22 -0
- data/spec/type/struct_type/test_name.rb +20 -0
- data/spec/{unit/type → type}/sub_type/test_default_name.rb +2 -2
- data/spec/{unit/type → type}/sub_type/test_dress.rb +14 -14
- data/spec/type/sub_type/test_equality.rb +46 -0
- data/spec/{unit/type → type}/sub_type/test_include.rb +6 -6
- data/spec/type/sub_type/test_initialize.rb +13 -0
- data/spec/{unit/type → type}/sub_type/test_name.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/tuple_type/test_dress.rb +18 -18
- data/spec/{unit/type → type}/tuple_type/test_equality.rb +6 -6
- data/spec/{unit/type → type}/tuple_type/test_include.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_initialize.rb +4 -4
- data/spec/{unit/type → type}/tuple_type/test_name.rb +0 -0
- data/spec/{unit/type → type}/union_type/test_default_name.rb +0 -0
- data/spec/{unit/type → type}/union_type/test_dress.rb +7 -6
- data/spec/{unit/type → type}/union_type/test_equality.rb +7 -7
- data/spec/{unit/type → type}/union_type/test_include.rb +3 -3
- data/spec/{unit/type → type}/union_type/test_initialize.rb +3 -3
- data/spec/{unit/type → type}/union_type/test_name.rb +0 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_adt.rb +4 -4
- data/spec/{unit/type_factory → type_factory}/dsl/test_any.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/dsl/test_attribute.rb +16 -2
- data/spec/{unit/type_factory → type_factory}/dsl/test_attributes.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/dsl/test_builtin.rb +3 -3
- data/spec/type_factory/dsl/test_multi_relation.rb +39 -0
- data/spec/type_factory/dsl/test_multi_tuple.rb +37 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_relation.rb +6 -6
- data/spec/{unit/type_factory → type_factory}/dsl/test_seq.rb +4 -4
- data/spec/{unit/type_factory → type_factory}/dsl/test_set.rb +4 -4
- data/spec/type_factory/dsl/test_struct.rb +45 -0
- data/spec/{unit/type_factory → type_factory}/dsl/test_subtype.rb +10 -8
- data/spec/{unit/type_factory → type_factory}/dsl/test_tuple.rb +5 -5
- data/spec/{unit/type_factory → type_factory}/dsl/test_union.rb +6 -6
- data/spec/{unit/type_factory → type_factory}/factory/test_builtin.rb +1 -1
- data/spec/{unit/type_factory → type_factory}/factory/test_seq_type.rb +2 -2
- data/spec/{unit/type_factory → type_factory}/factory/test_set_type.rb +2 -2
- data/spec/type_factory/factory/test_struct_type.rb +18 -0
- data/spec/{unit/type_factory → type_factory}/factory/test_sub_type.rb +7 -7
- data/spec/{unit/type_factory → type_factory}/factory/test_tuple_type.rb +4 -4
- metadata +398 -286
- data/lib/finitio/data_type.rb +0 -29
- data/lib/finitio/syntax/ad_type.rb +0 -32
- data/lib/finitio/syntax/attribute.rb +0 -15
- data/lib/finitio/syntax/constraints.rb +0 -22
- data/lib/finitio/syntax/heading.rb +0 -19
- data/lib/finitio/syntax/relation_type.rb +0 -15
- data/lib/finitio/syntax/support.rb +0 -13
- data/lib/finitio/syntax/tuple_type.rb +0 -15
- data/spec/acceptance/Finitio/test_default.rb +0 -96
- data/spec/acceptance/Finitio/test_parsing.rb +0 -15
- data/spec/acceptance/ad_type/test_in_finitio.rb +0 -82
- data/spec/acceptance/ad_type/test_in_ruby.rb +0 -60
- data/spec/unit/attribute/test_initialize.rb +0 -13
- data/spec/unit/attribute/test_to_name.rb +0 -10
- data/spec/unit/heading/test_equality.rb +0 -28
- data/spec/unit/heading/test_to_name.rb +0 -32
- data/spec/unit/syntax/nodes/test_attribute.rb +0 -38
- data/spec/unit/syntax/nodes/test_relation_type.rb +0 -59
- data/spec/unit/syntax/nodes/test_tuple_type.rb +0 -59
- data/spec/unit/syntax/nodes/test_type_def.rb +0 -33
- data/spec/unit/test_finitio.rb +0 -15
- data/spec/unit/type/sub_type/test_equality.rb +0 -34
- data/spec/unit/type/sub_type/test_initialize.rb +0 -16
File without changes
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Finitio
|
3
|
+
describe Heading, "to_name" do
|
4
|
+
|
5
|
+
let(:red) { Attribute.new(:red, intType) }
|
6
|
+
let(:blue) { Attribute.new(:blue, intType) }
|
7
|
+
let(:maybe_blue){ Attribute.new(:blue, intType, false) }
|
8
|
+
|
9
|
+
let(:heading){ Heading.new(attributes) }
|
10
|
+
|
11
|
+
subject{ heading.to_name }
|
12
|
+
|
13
|
+
context 'with no attribute' do
|
14
|
+
let(:attributes){
|
15
|
+
[ ]
|
16
|
+
}
|
17
|
+
|
18
|
+
it{ should eq('') }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with one attribute' do
|
22
|
+
let(:attributes){
|
23
|
+
[ red ]
|
24
|
+
}
|
25
|
+
|
26
|
+
it{ should eq('red: intType') }
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with multiple attributes' do
|
30
|
+
let(:attributes){
|
31
|
+
[ red, blue ]
|
32
|
+
}
|
33
|
+
|
34
|
+
it{ should eq('red: intType, blue: intType') }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with some optional attributes' do
|
38
|
+
let(:attributes){
|
39
|
+
[ red, maybe_blue ]
|
40
|
+
}
|
41
|
+
|
42
|
+
it{ should eq('red: intType, blue :? intType') }
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when allowing extra' do
|
46
|
+
let(:heading){ Heading.new([red], allow_extra: true) }
|
47
|
+
|
48
|
+
it{ should eq('red: intType, ...') }
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when allowing extra only' do
|
52
|
+
let(:heading){ Heading.new([], allow_extra: true) }
|
53
|
+
|
54
|
+
it{ should eq('...') }
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -56,8 +56,24 @@ module SpecHelpers
|
|
56
56
|
Finitio::BuiltinType.new(NilClass, "nilType")
|
57
57
|
end
|
58
58
|
|
59
|
+
def byte_full
|
60
|
+
@byte_full ||= Finitio::Constraint.new(->(i){ i>=0 && i<=255 }, :byte)
|
61
|
+
end
|
62
|
+
|
63
|
+
def byte_min
|
64
|
+
@byte_min ||= Finitio::Constraint.new(->(i){ i>=0 }, :byte_min)
|
65
|
+
end
|
66
|
+
|
67
|
+
def byte_max
|
68
|
+
@byte_max ||= Finitio::Constraint.new(->(i){ i<=255 }, :byte_max)
|
69
|
+
end
|
70
|
+
|
71
|
+
def positive
|
72
|
+
@positive ||= Finitio::Constraint.new(->(i){ i>=0 }, :positive)
|
73
|
+
end
|
74
|
+
|
59
75
|
def byte
|
60
|
-
Finitio::SubType.new(intType,
|
76
|
+
Finitio::SubType.new(intType, [byte_full])
|
61
77
|
end
|
62
78
|
|
63
79
|
def type_factory
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Finitio
|
3
|
+
module Syntax
|
4
|
+
describe Expr, "free_variables" do
|
5
|
+
|
6
|
+
def fv(src)
|
7
|
+
Syntax.parse(src, { root: :expr, memoize: true }).free_variables
|
8
|
+
end
|
9
|
+
|
10
|
+
context "on literals" do
|
11
|
+
|
12
|
+
it 'returns an empty array' do
|
13
|
+
expect(fv("true")).to eq([])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "on a single identifier" do
|
18
|
+
|
19
|
+
it 'works' do
|
20
|
+
expect(fv("foo")).to eq(["foo"])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "on a comparison" do
|
25
|
+
|
26
|
+
it 'works' do
|
27
|
+
expect(fv("foo == bar")).to eq(["foo", "bar"])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "on a function call" do
|
32
|
+
|
33
|
+
it 'works' do
|
34
|
+
expect(fv("foo(bar)")).to eq(["bar"])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "on a OO call" do
|
39
|
+
|
40
|
+
it 'works' do
|
41
|
+
expect(fv("foo.bar")).to eq(["foo"])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Finitio
|
3
|
+
module Syntax
|
4
|
+
describe Expr, "to_proc_source" do
|
5
|
+
|
6
|
+
def source(src)
|
7
|
+
Syntax.parse(src, { root: :expr, memoize: true }).to_proc_source
|
8
|
+
end
|
9
|
+
|
10
|
+
context "on literals" do
|
11
|
+
|
12
|
+
it 'returns an empty array' do
|
13
|
+
expect(source("true")).to eq("true")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "on a single identifier" do
|
18
|
+
|
19
|
+
it 'works' do
|
20
|
+
expect(source("foo")).to eq("foo")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "on a comparison" do
|
25
|
+
|
26
|
+
it 'works' do
|
27
|
+
expect(source("foo == bar")).to eq(%Q{foo.==(bar)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "on a OO call" do
|
32
|
+
|
33
|
+
it 'works' do
|
34
|
+
expect(source("foo.bar")).to eq(%Q{foo.fetch(:bar)})
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'works' do
|
38
|
+
expect(source("foo.bar.baz")).to eq(%Q{foo.fetch(:bar).fetch(:baz)})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -18,17 +18,17 @@ module Finitio
|
|
18
18
|
let(:input){ '.Color <rgb> {r: .Integer, g: .Integer, b: .Integer}' }
|
19
19
|
|
20
20
|
it 'compiles to an AdType' do
|
21
|
-
compiled.
|
22
|
-
compiled.ruby_type.
|
23
|
-
compiled.contract_names.
|
21
|
+
expect(compiled).to be_a(AdType)
|
22
|
+
expect(compiled.ruby_type).to be(Color)
|
23
|
+
expect(compiled.contract_names).to eq([:rgb])
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should behave as expected' do
|
27
|
-
compiled.dress(r: 138, g: 43, b: 226).
|
27
|
+
expect(compiled.dress(r: 138, g: 43, b: 226)).to eq(blueviolet)
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'has expected AST' do
|
31
|
-
ast.
|
31
|
+
expect(ast).to eq([
|
32
32
|
:ad_type,
|
33
33
|
"Color",
|
34
34
|
[:contract,
|
@@ -54,17 +54,17 @@ module Finitio
|
|
54
54
|
}
|
55
55
|
|
56
56
|
it 'compiles to an AdType' do
|
57
|
-
compiled.
|
58
|
-
compiled.ruby_type.
|
59
|
-
compiled.contract_names.
|
57
|
+
expect(compiled).to be_a(AdType)
|
58
|
+
expect(compiled.ruby_type).to be(Color)
|
59
|
+
expect(compiled.contract_names).to eq([:rgb, :hex])
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'should behave as expected' do
|
63
|
-
compiled.dress("#8A2BE2").
|
63
|
+
expect(compiled.dress("#8A2BE2")).to eq(blueviolet)
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'has expected AST' do
|
67
|
-
ast.
|
67
|
+
expect(ast).to eq([
|
68
68
|
:ad_type,
|
69
69
|
"Color",
|
70
70
|
[:contract,
|
@@ -89,20 +89,20 @@ module Finitio
|
|
89
89
|
let(:input){ '<as> {r: .Integer}' }
|
90
90
|
|
91
91
|
it 'compiles to an AdType' do
|
92
|
-
compiled.
|
93
|
-
compiled.ruby_type.
|
94
|
-
compiled.contract_names.
|
92
|
+
expect(compiled).to be_a(AdType)
|
93
|
+
expect(compiled.ruby_type).to be_nil
|
94
|
+
expect(compiled.contract_names).to eq([:as])
|
95
95
|
end
|
96
96
|
|
97
97
|
it 'should behave as expected' do
|
98
|
-
compiled.dress(r: 12).
|
99
|
-
|
98
|
+
expect(compiled.dress(r: 12)).to eq(r: 12)
|
99
|
+
expect{
|
100
100
|
compiled.dress("foo")
|
101
|
-
}.
|
101
|
+
}.to raise_error(TypeError)
|
102
102
|
end
|
103
103
|
|
104
104
|
it 'has expected AST' do
|
105
|
-
ast.
|
105
|
+
expect(ast).to eq([
|
106
106
|
:ad_type,
|
107
107
|
nil,
|
108
108
|
[:contract,
|
@@ -121,9 +121,9 @@ module Finitio
|
|
121
121
|
let(:input){ '.Color <rgb> {r: .Integer}, <rgb> .String' }
|
122
122
|
|
123
123
|
it 'raises an error' do
|
124
|
-
|
124
|
+
expect{
|
125
125
|
compiled
|
126
|
-
}.
|
126
|
+
}.to raise_error(Error, "Duplicate contract name `rgb`")
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
@@ -131,23 +131,23 @@ module Finitio
|
|
131
131
|
let(:input){ '.DateTime <iso> .String \( s | DateTime.parse(s) ) \( d | d.to_s )' }
|
132
132
|
|
133
133
|
it 'compiles to an AdType' do
|
134
|
-
compiled.
|
135
|
-
compiled.ruby_type.
|
136
|
-
compiled.contract_names.
|
134
|
+
expect(compiled).to be_a(AdType)
|
135
|
+
expect(compiled.ruby_type).to be(DateTime)
|
136
|
+
expect(compiled.contract_names).to eq([:iso])
|
137
137
|
end
|
138
138
|
|
139
139
|
it 'should behave as expected' do
|
140
|
-
compiled.dress("2014-01-19T12:00").
|
140
|
+
expect(compiled.dress("2014-01-19T12:00")).to be_a(DateTime)
|
141
141
|
end
|
142
142
|
|
143
143
|
it 'should hide errors' do
|
144
144
|
err = compiled.dress("foo") rescue $!
|
145
|
-
err.
|
146
|
-
err.message.
|
145
|
+
expect(err).to be_a(TypeError)
|
146
|
+
expect(err.message).to eq("Invalid DateTime `foo`")
|
147
147
|
end
|
148
148
|
|
149
149
|
it 'has expected AST' do
|
150
|
-
ast.
|
150
|
+
expect(ast).to eq([
|
151
151
|
:ad_type,
|
152
152
|
"DateTime",
|
153
153
|
[:contract,
|
@@ -14,7 +14,7 @@ module Finitio
|
|
14
14
|
}
|
15
15
|
|
16
16
|
it 'compiles to an AnyType' do
|
17
|
-
compiled.
|
17
|
+
expect(compiled).to be_a(AnyType)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -23,7 +23,7 @@ module Finitio
|
|
23
23
|
subject.to_ast
|
24
24
|
}
|
25
25
|
|
26
|
-
it{ ast.
|
26
|
+
it{ expect(ast).to eq([:any_type]) }
|
27
27
|
end
|
28
28
|
|
29
29
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Finitio
|
3
|
+
describe Syntax, "attribute" do
|
4
|
+
|
5
|
+
subject{
|
6
|
+
Syntax.parse(input, root: "attribute")
|
7
|
+
}
|
8
|
+
|
9
|
+
describe 'compilation result' do
|
10
|
+
let(:compiled){
|
11
|
+
subject.compile(type_factory)
|
12
|
+
}
|
13
|
+
|
14
|
+
context 'a: .Integer' do
|
15
|
+
let(:input){ 'a: .Integer' }
|
16
|
+
|
17
|
+
it 'compiles to an mandatory Attribute' do
|
18
|
+
expect(compiled).to be_a(Attribute)
|
19
|
+
expect(compiled.name).to eq(:a)
|
20
|
+
expect(compiled.type).to be_a(BuiltinType)
|
21
|
+
expect(compiled.type.ruby_type).to be(Integer)
|
22
|
+
expect(compiled).to be_required
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'a :? .Integer' do
|
27
|
+
let(:input){ 'a :? .Integer' }
|
28
|
+
|
29
|
+
it 'compiles to an optional Attribute' do
|
30
|
+
expect(compiled).to be_a(Attribute)
|
31
|
+
expect(compiled.name).to eq(:a)
|
32
|
+
expect(compiled.type).to be_a(BuiltinType)
|
33
|
+
expect(compiled.type.ruby_type).to be(Integer)
|
34
|
+
expect(compiled).not_to be_required
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'AST' do
|
40
|
+
let(:ast){
|
41
|
+
subject.to_ast
|
42
|
+
}
|
43
|
+
|
44
|
+
context 'a: .Integer' do
|
45
|
+
let(:input){ 'a: .Integer' }
|
46
|
+
|
47
|
+
it{ expect(ast).to eq([:attribute, "a", [:builtin_type, "Integer"]]) }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'a :? .Integer' do
|
51
|
+
let(:input){ 'a :? .Integer' }
|
52
|
+
|
53
|
+
it{ expect(ast).to eq([:attribute, "a", [:builtin_type, "Integer"], false]) }
|
54
|
+
end
|
55
|
+
|
56
|
+
context '_ : .Integer' do
|
57
|
+
let(:input){ '_ : .Integer' }
|
58
|
+
|
59
|
+
it{ expect(ast).to eq([:attribute, "_", [:builtin_type, "Integer"]]) }
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -15,8 +15,8 @@ module Finitio
|
|
15
15
|
let(:source){ ".Integer" }
|
16
16
|
|
17
17
|
it 'compiles to a BuiltinType' do
|
18
|
-
compiled.
|
19
|
-
compiled.ruby_type.
|
18
|
+
expect(compiled).to be_a(BuiltinType)
|
19
|
+
expect(compiled.ruby_type).to be(Integer)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -24,8 +24,8 @@ module Finitio
|
|
24
24
|
let(:source){ ".Finitio::Type" }
|
25
25
|
|
26
26
|
it 'compiles to a BuiltinType' do
|
27
|
-
compiled.
|
28
|
-
compiled.ruby_type.
|
27
|
+
expect(compiled).to be_a(BuiltinType)
|
28
|
+
expect(compiled.ruby_type).to be(::Finitio::Type)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -37,7 +37,7 @@ module Finitio
|
|
37
37
|
subject.to_ast
|
38
38
|
}
|
39
39
|
|
40
|
-
it{ ast.
|
40
|
+
it{ expect(ast).to eq([:builtin_type, "Finitio::Type"]) }
|
41
41
|
end
|
42
42
|
|
43
43
|
end
|
@@ -17,17 +17,12 @@ module Finitio
|
|
17
17
|
context '(i | i >= 0)' do
|
18
18
|
let(:input){ '(i | i >= 0)' }
|
19
19
|
|
20
|
-
it 'compiles to an
|
21
|
-
compiled.
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'compiled to the correct proc' do
|
25
|
-
compiled[:predicate].call(12).should be_true
|
26
|
-
compiled[:predicate].call(-12).should be_false
|
20
|
+
it 'compiles to an Array' do
|
21
|
+
expect(compiled).to be_a(Array)
|
27
22
|
end
|
28
23
|
|
29
24
|
it 'has the expected AST' do
|
30
|
-
ast.
|
25
|
+
expect(ast).to eq([
|
31
26
|
[
|
32
27
|
:constraint,
|
33
28
|
"default",
|
@@ -17,30 +17,33 @@ module Finitio
|
|
17
17
|
context 'a >= 10' do
|
18
18
|
let(:input){ 'a >= 10' }
|
19
19
|
|
20
|
-
it 'compiles to an
|
21
|
-
compiled.
|
22
|
-
compiled.
|
20
|
+
it 'compiles to an Array' do
|
21
|
+
expect(compiled).to be_a(Array)
|
22
|
+
expect(compiled.size).to eq(1)
|
23
|
+
expect(compiled.first).to be_a(Constraint)
|
23
24
|
end
|
24
25
|
|
25
26
|
it 'has the expected AST' do
|
26
|
-
ast.
|
27
|
+
expect(ast).to eq([[
|
27
28
|
:constraint,
|
28
29
|
"default",
|
29
30
|
[:fn, [:parameters, "a"], [:source, "a >= 10"]]
|
30
|
-
])
|
31
|
+
]])
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
35
|
context 'foo: a >= 10' do
|
35
36
|
let(:input){ 'foo: a >= 10' }
|
36
37
|
|
37
|
-
it 'compiles to an
|
38
|
-
compiled.
|
39
|
-
compiled.
|
38
|
+
it 'compiles to an Array' do
|
39
|
+
expect(compiled).to be_a(Array)
|
40
|
+
expect(compiled.size).to eq(1)
|
41
|
+
expect(compiled.first).to be_a(Constraint)
|
42
|
+
expect(compiled.map(&:name)).to eq([:foo])
|
40
43
|
end
|
41
44
|
|
42
45
|
it 'has the expected AST' do
|
43
|
-
ast.
|
46
|
+
expect(ast).to eq([[
|
44
47
|
:constraint,
|
45
48
|
"foo",
|
46
49
|
[:fn, [:parameters, "a"], [:source, "a >= 10"]]
|
@@ -51,13 +54,15 @@ module Finitio
|
|
51
54
|
context 'foo: a >= 10, bar: a <= 255' do
|
52
55
|
let(:input){ 'foo: a >= 10, bar: a <= 255' }
|
53
56
|
|
54
|
-
it 'compiles to an
|
55
|
-
compiled.
|
56
|
-
compiled.
|
57
|
+
it 'compiles to an Array' do
|
58
|
+
expect(compiled).to be_a(Array)
|
59
|
+
expect(compiled.size).to eq(2)
|
60
|
+
expect(compiled.first).to be_a(Constraint)
|
61
|
+
expect(compiled.map(&:name)).to eq([:foo, :bar])
|
57
62
|
end
|
58
63
|
|
59
64
|
it 'has the expected AST' do
|
60
|
-
ast.
|
65
|
+
expect(ast).to eq([
|
61
66
|
[
|
62
67
|
:constraint,
|
63
68
|
"foo",
|
@@ -75,10 +80,10 @@ module Finitio
|
|
75
80
|
context 'foo: a >= 10, foo: a <= 255' do
|
76
81
|
let(:input){ 'foo: a >= 10, foo: a <= 255' }
|
77
82
|
|
78
|
-
it '
|
79
|
-
|
83
|
+
it 'raises an error' do
|
84
|
+
expect{
|
80
85
|
compiled
|
81
|
-
}.
|
86
|
+
}.to raise_error("Duplicate constraint name `foo`")
|
82
87
|
end
|
83
88
|
end
|
84
89
|
|