finitio 0.7.0.pre.rc3 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +34 -0
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +41 -41
  5. data/README.md +88 -12
  6. data/lib/finitio.rb +37 -5
  7. data/lib/finitio/generation.rb +106 -0
  8. data/lib/finitio/generation/ad_type.rb +10 -0
  9. data/lib/finitio/generation/alias_type.rb +9 -0
  10. data/lib/finitio/generation/any_type.rb +11 -0
  11. data/lib/finitio/generation/builtin_type.rb +9 -0
  12. data/lib/finitio/generation/hash_based_type.rb +15 -0
  13. data/lib/finitio/generation/heuristic.rb +8 -0
  14. data/lib/finitio/generation/heuristic/constant.rb +30 -0
  15. data/lib/finitio/generation/heuristic/random.rb +52 -0
  16. data/lib/finitio/generation/rel_based_type.rb +13 -0
  17. data/lib/finitio/generation/seq_type.rb +13 -0
  18. data/lib/finitio/generation/set_type.rb +13 -0
  19. data/lib/finitio/generation/sub_type.rb +9 -0
  20. data/lib/finitio/generation/union_type.rb +10 -0
  21. data/lib/finitio/inference.rb +51 -0
  22. data/lib/finitio/json_schema.rb +16 -0
  23. data/lib/finitio/json_schema/ad_type.rb +11 -0
  24. data/lib/finitio/json_schema/alias_type.rb +9 -0
  25. data/lib/finitio/json_schema/any_type.rb +9 -0
  26. data/lib/finitio/json_schema/builtin_type.rb +27 -0
  27. data/lib/finitio/json_schema/hash_based_type.rb +25 -0
  28. data/lib/finitio/json_schema/rel_based_type.rb +13 -0
  29. data/lib/finitio/json_schema/seq_type.rb +12 -0
  30. data/lib/finitio/json_schema/set_type.rb +13 -0
  31. data/lib/finitio/json_schema/struct_type.rb +12 -0
  32. data/lib/finitio/json_schema/sub_type.rb +10 -0
  33. data/lib/finitio/json_schema/union_type.rb +11 -0
  34. data/lib/finitio/support.rb +18 -0
  35. data/lib/finitio/support/attribute.rb +8 -0
  36. data/lib/finitio/support/compilation.rb +18 -18
  37. data/lib/finitio/support/contract.rb +10 -2
  38. data/lib/finitio/support/fetch_scope.rb +19 -0
  39. data/lib/finitio/support/heading.rb +42 -1
  40. data/lib/finitio/syntax.rb +1 -1
  41. data/lib/finitio/syntax/import.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 +20 -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_stdlib_memoization.rb +22 -0
  68. data/spec/finitio/test_system.rb +0 -11
  69. data/spec/generation/test_generation.rb +169 -0
  70. data/spec/heading/test_looks_similar.rb +45 -0
  71. data/spec/heading/test_suppremum.rb +56 -0
  72. data/spec/inference/test_inference.rb +42 -0
  73. data/spec/json_schema/test_ad_type.rb +20 -0
  74. data/spec/json_schema/test_alias_type.rb +15 -0
  75. data/spec/json_schema/test_any_type.rb +11 -0
  76. data/spec/json_schema/test_builtin_type.rb +51 -0
  77. data/spec/json_schema/test_multi_relation_type.rb +58 -0
  78. data/spec/json_schema/test_multi_tuple_type.rb +50 -0
  79. data/spec/json_schema/test_relation_type.rb +30 -0
  80. data/spec/json_schema/test_seq_type.rb +18 -0
  81. data/spec/json_schema/test_set_type.rb +19 -0
  82. data/spec/json_schema/test_struct_type.rb +18 -0
  83. data/spec/json_schema/test_sub_type.rb +17 -0
  84. data/spec/json_schema/test_tuple_type.rb +26 -0
  85. data/spec/json_schema/test_union_type.rb +17 -0
  86. data/spec/regression/test_heading_extra_are_proxy_resolved.rb +41 -0
  87. data/spec/spec_helper.rb +32 -6
  88. data/spec/support/test_compare_attrs.rb +67 -0
  89. data/spec/syntax/test_compile.rb +57 -0
  90. data/spec/system/fixtures/system.fio +2 -0
  91. data/spec/{finitio → system/fixtures}/with-duplicates.fio +2 -1
  92. data/spec/system/test_check_and_warn.rb +55 -0
  93. data/spec/type/ad_type/test_initialize.rb +1 -8
  94. data/spec/type/relation_type/test_suppremum.rb +104 -0
  95. data/spec/type/seq_type/test_suppremum.rb +54 -0
  96. data/spec/type/set_type/test_suppremum.rb +54 -0
  97. data/spec/type/test_suppremum.rb +49 -0
  98. data/spec/type/test_unconstrained.rb +150 -0
  99. data/spec/type/tuple_type/test_suppremum.rb +119 -0
  100. data/spec/type/union_type/test_suppremum.rb +51 -0
  101. data/tasks/test.rake +1 -1
  102. metadata +99 -15
  103. data/spec/type/proxy_type/test_delegation.rb +0 -37
  104. data/spec/type/proxy_type/test_resolve.rb +0 -29
@@ -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
@@ -0,0 +1,58 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "MultiRelationType" do
4
+
5
+ context 'with a true allow extra and some optional attribute' do
6
+ let(:heading){
7
+ Heading.new([
8
+ Attribute.new(:a, anyType),
9
+ Attribute.new(:b, anyType, false),
10
+ ], allow_extra: true)
11
+ }
12
+
13
+ let(:multi_relation_type) {
14
+ MultiRelationType.new(heading)
15
+ }
16
+
17
+ it 'works as expected' do
18
+ expect(multi_relation_type.to_json_schema).to eql({
19
+ type: "array",
20
+ items: {
21
+ type: "object",
22
+ properties: {
23
+ a: {},
24
+ b: {}
25
+ },
26
+ required: [:a],
27
+ additionalProperties: {}
28
+ },
29
+ uniqueItems: true
30
+ })
31
+ end
32
+ end
33
+
34
+ context 'with a allow extra requiring Strings' do
35
+ let(:heading){
36
+ Heading.new([
37
+ ], allow_extra: BuiltinType.new(String))
38
+ }
39
+
40
+ let(:multi_relation_type) {
41
+ MultiRelationType.new(heading)
42
+ }
43
+
44
+ it 'works as expected' do
45
+ expect(multi_relation_type.to_json_schema).to eql({
46
+ type: "array",
47
+ items: {
48
+ type: "object",
49
+ additionalProperties: { type: "string" }
50
+ },
51
+ uniqueItems: true
52
+ })
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "MultiTupleType" do
4
+
5
+ context 'with a true allow extra and some optional attribute' do
6
+ let(:heading){
7
+ Heading.new([
8
+ Attribute.new(:a, anyType),
9
+ Attribute.new(:b, anyType, false),
10
+ ], allow_extra: true)
11
+ }
12
+
13
+ let(:multi_tuple_type) {
14
+ MultiTupleType.new(heading)
15
+ }
16
+
17
+ it 'works as expected' do
18
+ expect(multi_tuple_type.to_json_schema).to eql({
19
+ type: "object",
20
+ properties: {
21
+ a: {},
22
+ b: {}
23
+ },
24
+ required: [:a],
25
+ additionalProperties: {}
26
+ })
27
+ end
28
+ end
29
+
30
+ context 'with a allow extra requiring Strings' do
31
+ let(:heading){
32
+ Heading.new([
33
+ ], allow_extra: BuiltinType.new(String))
34
+ }
35
+
36
+ let(:multi_tuple_type) {
37
+ MultiTupleType.new(heading)
38
+ }
39
+
40
+ it 'works as expected' do
41
+ expect(multi_tuple_type.to_json_schema).to eql({
42
+ type: "object",
43
+ additionalProperties: { type: "string" }
44
+ })
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "RelationType" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, anyType)])
7
+ }
8
+
9
+ let(:tuple_type) {
10
+ RelationType.new(heading)
11
+ }
12
+
13
+ it 'works as expected' do
14
+ expect(tuple_type.to_json_schema).to eql({
15
+ type: "array",
16
+ items: {
17
+ type: "object",
18
+ properties: {
19
+ a: {}
20
+ },
21
+ required: [:a],
22
+ additionalProperties: false
23
+ },
24
+ uniqueItems: true
25
+ })
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "SeqType" do
4
+
5
+ let(:type) {
6
+ SeqType.new(anyType)
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: {}
13
+ })
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "SetType" do
4
+
5
+ let(:type) {
6
+ SetType.new(anyType)
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: {},
13
+ uniqueItems: true
14
+ })
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "StructType" do
4
+
5
+ let(:type) {
6
+ StructType.new([anyType,anyType])
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: [{},{}]
13
+ })
14
+ end
15
+
16
+ end
17
+ end
18
+ end