finitio 0.7.0.pre.rc4 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +42 -0
  3. data/Gemfile +1 -11
  4. data/Gemfile.lock +101 -54
  5. data/README.md +89 -13
  6. data/finitio.gemspec +9 -157
  7. data/lib/finitio.rb +7 -5
  8. data/lib/finitio/generation.rb +106 -0
  9. data/lib/finitio/generation/ad_type.rb +10 -0
  10. data/lib/finitio/generation/alias_type.rb +9 -0
  11. data/lib/finitio/generation/any_type.rb +11 -0
  12. data/lib/finitio/generation/builtin_type.rb +9 -0
  13. data/lib/finitio/generation/hash_based_type.rb +15 -0
  14. data/lib/finitio/generation/heuristic.rb +8 -0
  15. data/lib/finitio/generation/heuristic/constant.rb +30 -0
  16. data/lib/finitio/generation/heuristic/random.rb +52 -0
  17. data/lib/finitio/generation/rel_based_type.rb +13 -0
  18. data/lib/finitio/generation/seq_type.rb +13 -0
  19. data/lib/finitio/generation/set_type.rb +13 -0
  20. data/lib/finitio/generation/sub_type.rb +9 -0
  21. data/lib/finitio/generation/union_type.rb +10 -0
  22. data/lib/finitio/inference.rb +51 -0
  23. data/lib/finitio/json_schema.rb +16 -0
  24. data/lib/finitio/json_schema/ad_type.rb +11 -0
  25. data/lib/finitio/json_schema/alias_type.rb +9 -0
  26. data/lib/finitio/json_schema/any_type.rb +9 -0
  27. data/lib/finitio/json_schema/builtin_type.rb +27 -0
  28. data/lib/finitio/json_schema/hash_based_type.rb +25 -0
  29. data/lib/finitio/json_schema/rel_based_type.rb +13 -0
  30. data/lib/finitio/json_schema/seq_type.rb +12 -0
  31. data/lib/finitio/json_schema/set_type.rb +13 -0
  32. data/lib/finitio/json_schema/struct_type.rb +12 -0
  33. data/lib/finitio/json_schema/sub_type.rb +10 -0
  34. data/lib/finitio/json_schema/union_type.rb +11 -0
  35. data/lib/finitio/support.rb +18 -0
  36. data/lib/finitio/support/attribute.rb +8 -0
  37. data/lib/finitio/support/compilation.rb +18 -18
  38. data/lib/finitio/support/contract.rb +10 -2
  39. data/lib/finitio/support/fetch_scope.rb +19 -0
  40. data/lib/finitio/support/heading.rb +42 -1
  41. data/lib/finitio/syntax.rb +1 -1
  42. data/lib/finitio/syntax/lexer.citrus +1 -1
  43. data/lib/finitio/syntax/type.rb +2 -0
  44. data/lib/finitio/syntax/type/high_order_type_instantiation.rb +29 -0
  45. data/lib/finitio/syntax/type/high_order_vars.rb +16 -0
  46. data/lib/finitio/syntax/type/type_def.rb +11 -1
  47. data/lib/finitio/syntax/types.citrus +14 -1
  48. data/lib/finitio/system.rb +18 -4
  49. data/lib/finitio/type.rb +19 -0
  50. data/lib/finitio/type/ad_type.rb +9 -1
  51. data/lib/finitio/type/alias_type.rb +8 -0
  52. data/lib/finitio/type/any_type.rb +12 -0
  53. data/lib/finitio/type/builtin_type.rb +4 -0
  54. data/lib/finitio/type/collection_type.rb +15 -0
  55. data/lib/finitio/type/heading_based_type.rb +17 -0
  56. data/lib/finitio/type/high_order_type.rb +39 -0
  57. data/lib/finitio/type/multi_relation_type.rb +4 -0
  58. data/lib/finitio/type/multi_tuple_type.rb +4 -0
  59. data/lib/finitio/type/proxy_type.rb +10 -20
  60. data/lib/finitio/type/relation_type.rb +4 -0
  61. data/lib/finitio/type/seq_type.rb +1 -1
  62. data/lib/finitio/type/struct_type.rb +8 -0
  63. data/lib/finitio/type/sub_type.rb +8 -0
  64. data/lib/finitio/type/tuple_type.rb +4 -0
  65. data/lib/finitio/type/union_type.rb +19 -0
  66. data/lib/finitio/version.rb +2 -2
  67. data/spec/finitio/test_system.rb +0 -11
  68. data/spec/generation/test_generation.rb +169 -0
  69. data/spec/heading/test_looks_similar.rb +45 -0
  70. data/spec/heading/test_suppremum.rb +56 -0
  71. data/spec/inference/test_inference.rb +42 -0
  72. data/spec/json_schema/test_ad_type.rb +20 -0
  73. data/spec/json_schema/test_alias_type.rb +15 -0
  74. data/spec/json_schema/test_any_type.rb +11 -0
  75. data/spec/json_schema/test_builtin_type.rb +51 -0
  76. data/spec/json_schema/test_multi_relation_type.rb +58 -0
  77. data/spec/json_schema/test_multi_tuple_type.rb +50 -0
  78. data/spec/json_schema/test_relation_type.rb +30 -0
  79. data/spec/json_schema/test_seq_type.rb +18 -0
  80. data/spec/json_schema/test_set_type.rb +19 -0
  81. data/spec/json_schema/test_struct_type.rb +18 -0
  82. data/spec/json_schema/test_sub_type.rb +17 -0
  83. data/spec/json_schema/test_tuple_type.rb +26 -0
  84. data/spec/json_schema/test_union_type.rb +17 -0
  85. data/spec/regression/test_heading_extra_are_proxy_resolved.rb +41 -0
  86. data/spec/spec_helper.rb +32 -6
  87. data/spec/support/test_compare_attrs.rb +67 -0
  88. data/spec/syntax/test_compile.rb +57 -0
  89. data/spec/system/fixtures/system.fio +2 -0
  90. data/spec/{finitio → system/fixtures}/with-duplicates.fio +2 -1
  91. data/spec/system/test_check_and_warn.rb +55 -0
  92. data/spec/type/ad_type/test_initialize.rb +1 -8
  93. data/spec/type/relation_type/test_suppremum.rb +104 -0
  94. data/spec/type/seq_type/test_suppremum.rb +54 -0
  95. data/spec/type/set_type/test_suppremum.rb +54 -0
  96. data/spec/type/test_suppremum.rb +49 -0
  97. data/spec/type/test_unconstrained.rb +150 -0
  98. data/spec/type/tuple_type/test_suppremum.rb +119 -0
  99. data/spec/type/union_type/test_suppremum.rb +51 -0
  100. data/tasks/test.rake +1 -1
  101. metadata +203 -17
  102. data/spec/type/proxy_type/test_delegation.rb +0 -37
  103. data/spec/type/proxy_type/test_resolve.rb +0 -29
@@ -44,5 +44,9 @@ module Finitio
44
44
  "{#{heading.to_name}}"
45
45
  end
46
46
 
47
+ def suppremum(other)
48
+ super(other, TupleType, MultiTupleType)
49
+ end
50
+
47
51
  end # class TupleType
48
52
  end # module Finitio
@@ -70,6 +70,17 @@ module Finitio
70
70
  candidates.map(&:name).join('|')
71
71
  end
72
72
 
73
+ def suppremum(other)
74
+ return self if other == self
75
+ cs = if (other.is_a?(UnionType))
76
+ candidates + other.candidates
77
+ else
78
+ candidates + [other]
79
+ end
80
+ UnionType.new(cs.uniq)
81
+ end
82
+ alias :_suppremum :suppremum
83
+
73
84
  def ==(other)
74
85
  super || (
75
86
  other.is_a?(UnionType) && set_equal?(candidates, other.candidates)
@@ -81,5 +92,13 @@ module Finitio
81
92
  self.class.hash ^ set_hash(self.candidates)
82
93
  end
83
94
 
95
+ def resolve_proxies(system)
96
+ UnionType.new(candidates.map{|t| t.resolve_proxies(system)}, name, metadata)
97
+ end
98
+
99
+ def unconstrained
100
+ UnionType.new(candidates.map{|c| c.unconstrained }, name, metadata)
101
+ end
102
+
84
103
  end # class UnionType
85
104
  end # module Finitio
@@ -2,8 +2,8 @@ module Finitio
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 7
6
- TINY = "0-rc4"
5
+ MINOR = 10
6
+ TINY = 0
7
7
 
8
8
  def self.to_s
9
9
  [ MAJOR, MINOR, TINY ].join('.')
@@ -29,15 +29,4 @@ describe Finitio, "system" do
29
29
  end
30
30
  end
31
31
 
32
- context "feedback about duplicate types" do
33
- before {
34
- Finitio.stdlib_path(Path.dir.parent)
35
- }
36
- subject{
37
- Finitio.system(Path.dir/"with-duplicates.fio").check_and_warn
38
- }
39
-
40
- it{ should be_a(Finitio::System) }
41
- end
42
-
43
32
  end
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+ module Finitio
3
+ describe Generation do
4
+
5
+ subject {
6
+ Generation.new({
7
+ :heuristic => Generation::Heuristic::Constant.new,
8
+ :generators => generators
9
+ })
10
+ }
11
+
12
+ let(:generators) {
13
+ {}
14
+ }
15
+
16
+ class SubString < String
17
+ end
18
+
19
+ describe 'when called on scalar types' do
20
+
21
+ it 'works for nil' do
22
+ expect(subject.call(nilType)).to eql(nil)
23
+ end
24
+
25
+ it 'works for ints' do
26
+ expect(subject.call(intType)).to eql(99)
27
+ end
28
+
29
+ it 'works for floats' do
30
+ expect(subject.call(floatType)).to eql(99.99)
31
+ end
32
+
33
+ it 'works for strings' do
34
+ expect(subject.call(stringType)).to eql("Hello world")
35
+ end
36
+
37
+ it 'works for true' do
38
+ expect(subject.call(trueType)).to be(true)
39
+ end
40
+
41
+ it 'works for false' do
42
+ expect(subject.call(falseType)).to be(false)
43
+ end
44
+
45
+ it 'works on ruby sub types' do
46
+ expect(subject.call(BuiltinType.new(SubString))).to eql("Hello world")
47
+ end
48
+
49
+ end
50
+
51
+ describe 'when called on Any type' do
52
+
53
+ it 'works' do
54
+ expect {
55
+ subject.call(anyType)
56
+ }.not_to raise_error
57
+ end
58
+
59
+ end
60
+
61
+ describe 'when called on an alias type' do
62
+
63
+ it 'works' do
64
+ expect(subject.call(AliasType.new(intType, "x"))).to eql(99)
65
+ end
66
+
67
+ end
68
+
69
+ describe 'when called on an sub type' do
70
+
71
+ it 'works' do
72
+ expect(subject.call(byte)).to eql(99)
73
+ end
74
+
75
+ end
76
+
77
+ describe 'when called on a collection type' do
78
+
79
+ it 'works on a SeqType' do
80
+ got = subject.call(SeqType.new(intType))
81
+ expect(got).to be_a(Array)
82
+ expect(got.all?{|x| x==99 }).to be_truthy
83
+ end
84
+
85
+ it 'works on a SetType' do
86
+ got = subject.call(SetType.new(intType))
87
+ expect(got).to be_a(Array)
88
+ expect(got.all?{|x| x==99 }).to be_truthy
89
+ end
90
+
91
+ end
92
+
93
+ describe 'when called on tuple types' do
94
+
95
+ it 'works as expected' do
96
+ type = TupleType.new(Heading.new [Attribute.new(:i, intType)])
97
+ expect(subject.call(type)).to eql({i: 99})
98
+ end
99
+
100
+ it 'works as expected' do
101
+ type = MultiTupleType.new(Heading.new [Attribute.new(:i, intType)])
102
+ expect(subject.call(type)).to eql({i: 99})
103
+ end
104
+
105
+ end
106
+
107
+ describe 'when called on relation types' do
108
+
109
+ it 'works as expected' do
110
+ type = RelationType.new(Heading.new [Attribute.new(:i, intType)])
111
+ expect(subject.call(type)).to eql([{i: 99}])
112
+ end
113
+
114
+ it 'works as expected' do
115
+ type = MultiRelationType.new(Heading.new [Attribute.new(:i, intType)])
116
+ expect(subject.call(type)).to eql([{i: 99}])
117
+ end
118
+ end
119
+
120
+ describe 'when called on a union type' do
121
+
122
+ it 'works as expected' do
123
+ type = UnionType.new([trueType, falseType])
124
+ expect([true, false].include? subject.call(type)).to be_truthy
125
+ end
126
+
127
+ end
128
+
129
+ describe 'when called on an AD type' do
130
+
131
+ it 'works' do
132
+ type = AdType.new(Color, [rgb_contract])
133
+ expect(subject.call(byte)).to eql(99)
134
+ end
135
+
136
+ end
137
+
138
+ describe 'when examples are provided in metadata' do
139
+
140
+ it 'takes the priority' do
141
+ type = AliasType.new(intType, "X", { examples: [97] })
142
+ expect(subject.call(type)).to eql(97)
143
+ end
144
+
145
+ end
146
+
147
+ describe 'when a generator exists' do
148
+
149
+ let(:generators) {
150
+ {
151
+ "X" => ->(type, gen, _) { 96 },
152
+ "Y" => ->(type, gen, world) { world }
153
+ }
154
+ }
155
+
156
+ it 'takes the priority even over examples' do
157
+ type = AliasType.new(intType, "X", { examples: [97] })
158
+ expect(subject.call(type)).to eql(96)
159
+ end
160
+
161
+ it 'lets pass a world' do
162
+ type = AliasType.new(intType, "Y", { examples: [97] })
163
+ expect(subject.call(type, 17)).to eql(17)
164
+ end
165
+
166
+ end
167
+
168
+ end
169
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ module Finitio
3
+ describe Heading, "looks_similar?" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, intType),
7
+ Attribute.new(:b, stringType)])
8
+ }
9
+
10
+ subject{
11
+ heading.looks_similar?(h2)
12
+ }
13
+
14
+ context 'when equal' do
15
+ let(:h2){ heading }
16
+
17
+ it 'says yes' do
18
+ expect(subject).to be_truthy
19
+ end
20
+ end
21
+
22
+ context 'when shared attributes are in majority' do
23
+ let(:h2){
24
+ Heading.new([Attribute.new(:a, intType),
25
+ Attribute.new(:c, stringType)])
26
+ }
27
+
28
+ it 'says yes' do
29
+ expect(subject).to be_truthy
30
+ end
31
+ end
32
+
33
+ context 'when shared attributes are in minority' do
34
+ let(:h2){
35
+ Heading.new([Attribute.new(:d, intType),
36
+ Attribute.new(:c, stringType)])
37
+ }
38
+
39
+ it 'says no' do
40
+ expect(subject).to be_falsy
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ module Finitio
3
+ describe Heading, "suppremum" do
4
+
5
+ subject{
6
+ left.suppremum(right)
7
+ }
8
+
9
+ let(:left){
10
+ Heading.new([Attribute.new(:a, intType),
11
+ Attribute.new(:b, stringType)])
12
+ }
13
+
14
+ context 'when both are equal' do
15
+ let(:right){
16
+ Heading.new([Attribute.new(:a, intType),
17
+ Attribute.new(:b, stringType)])
18
+ }
19
+
20
+ it 'works' do
21
+ expect(subject).to be(left)
22
+ end
23
+ end
24
+
25
+ context 'when they are different' do
26
+ let(:right){
27
+ Heading.new([Attribute.new(:a, nilType),
28
+ Attribute.new(:b, stringType)])
29
+ }
30
+
31
+ it 'works' do
32
+ expect(subject).to be_a(Heading)
33
+ expect(subject[:a].type).to eql(UnionType.new [intType, nilType])
34
+ expect(subject[:b].type).to eql(stringType)
35
+ end
36
+ end
37
+
38
+ context 'when they are different II' do
39
+ let(:right){
40
+ Heading.new([Attribute.new(:c, nilType),
41
+ Attribute.new(:b, stringType)])
42
+ }
43
+
44
+ it 'works' do
45
+ expect(subject).to be_a(Heading)
46
+ expect(subject[:a].type).to eql(intType)
47
+ expect(subject[:a].required).to eql(false)
48
+ expect(subject[:b].type).to eql(stringType)
49
+ expect(subject[:b].required).to eql(true)
50
+ expect(subject[:c].type).to eql(nilType)
51
+ expect(subject[:c].required).to eql(false)
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'finitio/inference'
3
+ module Finitio
4
+ describe Inference do
5
+
6
+ let(:system) {
7
+ Finitio.system <<~FIO
8
+ @import finitio/data
9
+
10
+ Nil = .NilClass
11
+ Boolean = .TrueClass|.FalseClass
12
+ Integer = .Integer
13
+ Date = .Date <iso8601> .String \\( s | Date.iso8601(s) )
14
+ \\( d | d.iso8601 )
15
+ String = .String
16
+ FIO
17
+ }
18
+
19
+ let(:input) {
20
+ [
21
+ { "id" => 1, "createdAt" => "2018-01-12", "size" => "15", "priority" => "XL", "meta" => { foo: true, bar: "foo" }, "hobbies" => [] },
22
+ { "id" => 2, "createdAt" => "2019-02-23", "size" => "1", "priority" => "L", "meta" => { foo: true, bar: "baz" }, "hobbies" => ["testing"] },
23
+ { "id" => 3, "createdAt" => "2018-07-15", "size" => "7", "priority" => "S", "meta" => { foo: true, bar: "bar" } },
24
+ { "id" => 4, "createdAt" => "2019-12-12", "size" => "9", "priority" => "M", "meta" => { foo: true, baz: "baz" } },
25
+ { "id" => 4, "createdAt" => nil, "size" => "9", "priority" => "M" },
26
+ ]
27
+ }
28
+
29
+ it 'works as expected' do
30
+ result = Inference.new(system).call(input)
31
+ expect(result).to be_a(SeqType)
32
+ expect(result.elm_type).to be_a(MultiTupleType)
33
+ expect(result.elm_type[:id].type.name).to eql("Integer")
34
+ expect(result.elm_type[:createdAt].type.name).to eql("Date|Nil")
35
+ expect(result.elm_type[:size].type.name).to eql("String")
36
+ expect(result.elm_type[:priority].type.name).to eql("String")
37
+
38
+ puts result
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AdType" do
4
+
5
+ let(:type) {
6
+ type = AdType.new(Color, [rgb_contract, hex_contract])
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ anyOf: [
12
+ { type: "integer" },
13
+ { type: "string" }
14
+ ]
15
+ })
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AliasType" do
4
+
5
+ let(:alias_type) {
6
+ AliasType.new(anyType, "X")
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(alias_type.to_json_schema).to eql({})
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AnyType" do
4
+
5
+ it 'works as expected' do
6
+ expect(anyType.to_json_schema).to eql({})
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "BuiltinType" do
4
+
5
+ let(:builtin_type) {
6
+ BuiltinType.new(ruby_type)
7
+ }
8
+
9
+ subject {
10
+ builtin_type.to_json_schema
11
+ }
12
+
13
+ context 'with NilClass' do
14
+ let(:ruby_type){ NilClass }
15
+
16
+ it 'works' do
17
+ expect(subject).to eql({ type: "null" })
18
+ end
19
+ end
20
+
21
+ context 'with String' do
22
+ let(:ruby_type){ String }
23
+
24
+ it 'works' do
25
+ expect(subject).to eql({ type: "string" })
26
+ end
27
+ end
28
+
29
+ [Fixnum, Bignum, Integer].each do |rt|
30
+ context "with #{rt}" do
31
+ let(:ruby_type){ rt }
32
+
33
+ it 'works' do
34
+ expect(subject).to eql({ type: "integer" })
35
+ end
36
+ end
37
+ end
38
+
39
+ [Float, Numeric].each do |rt|
40
+ context "with #{rt}" do
41
+ let(:ruby_type){ rt }
42
+
43
+ it 'works' do
44
+ expect(subject).to eql({ type: "number" })
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end