babl-json 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/lib/babl.rb +2 -2
  3. data/lib/babl/builder.rb +2 -0
  4. data/lib/babl/builder/chain_builder.rb +1 -2
  5. data/lib/babl/builder/template_base.rb +3 -4
  6. data/lib/babl/nodes.rb +15 -0
  7. data/lib/babl/nodes/create_pin.rb +1 -2
  8. data/lib/babl/nodes/dep.rb +1 -2
  9. data/lib/babl/nodes/each.rb +2 -2
  10. data/lib/babl/nodes/fixed_array.rb +2 -3
  11. data/lib/babl/nodes/goto_pin.rb +1 -2
  12. data/lib/babl/nodes/internal_value.rb +1 -1
  13. data/lib/babl/nodes/merge.rb +14 -35
  14. data/lib/babl/nodes/nav.rb +1 -1
  15. data/lib/babl/nodes/object.rb +5 -4
  16. data/lib/babl/nodes/parent.rb +1 -2
  17. data/lib/babl/nodes/static.rb +2 -3
  18. data/lib/babl/nodes/switch.rb +2 -3
  19. data/lib/babl/nodes/terminal_value.rb +17 -12
  20. data/lib/babl/nodes/typed.rb +2 -2
  21. data/lib/babl/nodes/with.rb +1 -2
  22. data/lib/babl/operators.rb +21 -0
  23. data/lib/babl/operators/array.rb +1 -2
  24. data/lib/babl/operators/dep.rb +1 -1
  25. data/lib/babl/operators/each.rb +1 -1
  26. data/lib/babl/operators/enter.rb +1 -0
  27. data/lib/babl/operators/merge.rb +1 -2
  28. data/lib/babl/operators/nav.rb +1 -1
  29. data/lib/babl/operators/object.rb +1 -2
  30. data/lib/babl/operators/parent.rb +1 -1
  31. data/lib/babl/operators/partial.rb +1 -1
  32. data/lib/babl/operators/pin.rb +2 -3
  33. data/lib/babl/operators/static.rb +3 -3
  34. data/lib/babl/operators/switch.rb +1 -3
  35. data/lib/babl/operators/typed.rb +1 -1
  36. data/lib/babl/operators/with.rb +1 -2
  37. data/lib/babl/rendering.rb +3 -0
  38. data/lib/babl/rendering/compiled_template.rb +2 -2
  39. data/lib/babl/schema.rb +7 -0
  40. data/lib/babl/schema/any_of.rb +43 -38
  41. data/lib/babl/schema/dyn_array.rb +1 -1
  42. data/lib/babl/schema/fixed_array.rb +1 -1
  43. data/lib/babl/schema/object.rb +1 -1
  44. data/lib/babl/schema/static.rb +1 -1
  45. data/lib/babl/schema/typed.rb +1 -1
  46. data/lib/babl/template.rb +2 -23
  47. data/lib/babl/utils.rb +3 -0
  48. data/lib/babl/version.rb +1 -1
  49. metadata +9 -63
  50. data/.gitignore +0 -3
  51. data/.rubocop.yml +0 -201
  52. data/.ruby-version +0 -1
  53. data/.travis.yml +0 -4
  54. data/CHANGELOG.md +0 -36
  55. data/Gemfile +0 -5
  56. data/LICENSE +0 -7
  57. data/README.md +0 -89
  58. data/babl.gemspec +0 -24
  59. data/logo-babl.png +0 -0
  60. data/spec/operators/array_spec.rb +0 -37
  61. data/spec/operators/call_spec.rb +0 -64
  62. data/spec/operators/continue_spec.rb +0 -25
  63. data/spec/operators/default_spec.rb +0 -15
  64. data/spec/operators/dep_spec.rb +0 -15
  65. data/spec/operators/each_spec.rb +0 -42
  66. data/spec/operators/enter_spec.rb +0 -28
  67. data/spec/operators/extends_spec.rb +0 -30
  68. data/spec/operators/merge_spec.rb +0 -124
  69. data/spec/operators/nav_spec.rb +0 -71
  70. data/spec/operators/null_spec.rb +0 -15
  71. data/spec/operators/nullable_spec.rb +0 -29
  72. data/spec/operators/object_spec.rb +0 -30
  73. data/spec/operators/parent_spec.rb +0 -50
  74. data/spec/operators/partial_spec.rb +0 -36
  75. data/spec/operators/pin_spec.rb +0 -172
  76. data/spec/operators/source_spec.rb +0 -33
  77. data/spec/operators/static_spec.rb +0 -27
  78. data/spec/operators/switch_spec.rb +0 -151
  79. data/spec/operators/typed_spec.rb +0 -84
  80. data/spec/operators/with_spec.rb +0 -31
  81. data/spec/spec_helper.rb +0 -2
  82. data/spec/spec_helper/operator_testing.rb +0 -46
  83. data/spec/spec_helper/schema_utils.rb +0 -53
  84. data/spec/utils/value_spec.rb +0 -54
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Source do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#source' do
7
- let(:object) { { abc: { def: 12 } } }
8
-
9
- context 'block returning primitive' do
10
- template { source { true } }
11
-
12
- it { expect(json).to eq(true) }
13
- end
14
-
15
- context 'block using operators' do
16
- template { source { static(3) } }
17
-
18
- it { expect(json).to eq(3) }
19
- end
20
-
21
- context 'two level sourcing' do
22
- template { source { nav(:abc).source { nav(:def) } } }
23
-
24
- it { expect(json).to eq(12) }
25
- end
26
-
27
- context 'string template' do
28
- template { source('object(a: static(true))') }
29
-
30
- it { expect(json).to eq('a' => true) }
31
- end
32
- end
33
- end
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Static do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#static' do
7
- context 'static object' do
8
- template { static('1': 'cava') }
9
-
10
- it { expect(json).to eq('1' => 'cava') }
11
- it { expect(dependencies).to eq({}) }
12
- it { expect(schema).to eq s_object(s_property(:'1', s_static('cava'))) }
13
- end
14
-
15
- context 'static primitive' do
16
- template { static('ok') }
17
-
18
- it { expect(schema).to eq s_static('ok') }
19
- end
20
-
21
- context 'invalid' do
22
- template { static(test: Object.new) }
23
-
24
- it { expect { compiled }.to raise_error Babl::Errors::InvalidTemplate }
25
- end
26
- end
27
- end
@@ -1,151 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Switch do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#switch' do
7
- context 'realistic use case' do
8
- template {
9
- even = nullable.nav(:even?)
10
- odd = nullable.nav(:odd?)
11
-
12
- each.switch(
13
- even => nav { |x| "#{x} is even" },
14
- odd => nav { |x| "#{x} is odd" },
15
- default => continue
16
- ).static('WTF')
17
- }
18
-
19
- let(:object) { [1, 2, nil, 5] }
20
-
21
- it { expect(json).to eq ['1 is odd', '2 is even', 'WTF', '5 is odd'] }
22
- it { expect(dependencies).to eq(__each__: { even?: {}, odd?: {} }) }
23
- it { expect(schema).to eq s_dyn_array(s_any_of(s_static('WTF'), s_anything)) }
24
- end
25
-
26
- context 'static condition' do
27
- template { switch(true => 42) }
28
-
29
- let(:object) { {} }
30
-
31
- it { expect(json).to eq 42 }
32
- end
33
-
34
- context 'only one output possible' do
35
- template { switch(false => 2, true => 2) }
36
-
37
- it { expect(schema).to eq s_static(2) }
38
- end
39
-
40
- context 'no branch taken' do
41
- template { switch(false => 1) }
42
-
43
- let(:object) { { abc: { lol: { ok: 42 } } } }
44
-
45
- it { expect(dependencies).to eq({}) }
46
- it { expect { json }.to raise_error Babl::Errors::RenderingError }
47
- end
48
-
49
- context 'non serializable objects are allowed internally' do
50
- template { switch(test: 42) }
51
-
52
- let(:object) { { test: Object.new } }
53
-
54
- it { expect(json).to eq 42 }
55
- end
56
-
57
- context 'switch between empty array and a dyn array' do
58
- template { switch(1 => each.object(a: 1), 2 => []) }
59
-
60
- let(:object) { [nil] }
61
-
62
- it { expect(json).to eq(['a' => 1]) }
63
- it { expect(schema).to eq s_dyn_array(s_object(s_property(:a, s_static(1)))) }
64
- end
65
-
66
- context 'switch between fixed array and a dyn array producing different output' do
67
- template { switch(1 => each.static(1), 2 => [2]) }
68
-
69
- it {
70
- expect(schema).to eq s_any_of(
71
- s_dyn_array(s_static(1)), s_fixed_array(s_static(2))
72
- )
73
- }
74
- end
75
-
76
- context 'switch between fixed array and a dyn array producing identical output' do
77
- template { switch(1 => nullable.each.static(1), 2 => [1]) }
78
-
79
- it { expect(schema).to eq s_any_of(s_null, s_dyn_array(s_static(1))) }
80
- end
81
-
82
- context 'switch between similar objects having only one different property' do
83
- template { switch(1 => { a: 34, b: 3 }, 2 => { a: 34, b: 1 }, 3 => { b: 2, a: 34 }) }
84
-
85
- it {
86
- expect(schema).to eq s_object(
87
- s_property(:a, s_static(34)),
88
- s_property(:b, s_any_of(s_static(1), s_static(2), s_static(3)))
89
- )
90
- }
91
- end
92
-
93
- context 'switch between similar objects having more than one different property' do
94
- template { switch(1 => { a: 35, b: 1 }, 2 => { a: 34, b: 2 }) }
95
-
96
- it {
97
- expect(schema).to eq s_any_of(
98
- s_object(
99
- s_property(:a, s_static(35)),
100
- s_property(:b, s_static(1))
101
- ),
102
- s_object(
103
- s_property(:a, s_static(34)),
104
- s_property(:b, s_static(2))
105
- )
106
- )
107
- }
108
- end
109
-
110
- context 'switch between two possible dyn arrays' do
111
- template { switch(1 => each.static('a'), 2 => each.static('b'), 3 => nullable.each.static('c')) }
112
-
113
- it { expect(schema).to eq s_any_of(s_null, s_dyn_array(s_any_of(s_static('a'), s_static('c'), s_static('b')))) }
114
- end
115
-
116
- context 'switch between true and false' do
117
- template { switch(1 => true, 2 => false) }
118
- it { expect(schema).to eq s_boolean }
119
- end
120
-
121
- context 'switch between true and a string' do
122
- template { switch(1 => true, 3 => string) }
123
- it { expect(schema).to eq s_any_of(s_static(true), s_string) }
124
- end
125
-
126
- context 'switch between any string and a specific string' do
127
- template { switch(1 => string, 3 => 'lol') }
128
- it { expect(schema).to eq s_string }
129
- end
130
-
131
- context 'switch between any boolean and a specific boolean' do
132
- template { switch(1 => boolean, 3 => true) }
133
- it { expect(schema).to eq s_boolean }
134
- end
135
-
136
- context 'switch between a specific float, specific integer and any number' do
137
- template { switch(1 => 1.2, 2 => 2, 3 => number) }
138
- it { expect(schema).to eq s_number }
139
- end
140
-
141
- context 'switch between a specific float, specific integer and any integer' do
142
- template { switch(1 => 1.2, 2 => 2, 3 => integer) }
143
- it { expect(schema).to eq s_any_of(s_integer, s_static(1.2)) }
144
- end
145
-
146
- context 'with dependencies' do
147
- template { nav(:test).switch(nav(:keke) => parent.nav(:lol)) }
148
- it { expect(dependencies).to eq(test: { keke: {} }, lol: {}) }
149
- end
150
- end
151
- end
@@ -1,84 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::Typed do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#integer' do
7
- template { [integer] }
8
-
9
- it { expect(schema).to eq s_fixed_array(s_integer) }
10
-
11
- context do
12
- let(:object) { 12 }
13
- it { expect(json).to eq [12] }
14
- end
15
-
16
- context do
17
- let(:object) { 12.5 }
18
- it { expect { json }.to raise_error Babl::Errors::RenderingError }
19
- end
20
- end
21
-
22
- describe '#number' do
23
- template { [number] }
24
-
25
- it { expect(schema).to eq s_fixed_array(s_number) }
26
-
27
- context do
28
- let(:object) { 12 }
29
- it { expect(json).to eq [12] }
30
- end
31
-
32
- context do
33
- let(:object) { 12.5 }
34
- it { expect(json).to eq [12.5] }
35
- end
36
-
37
- context do
38
- let(:object) { '12' }
39
- it { expect { json }.to raise_error Babl::Errors::RenderingError }
40
- end
41
- end
42
-
43
- describe '#string' do
44
- template { [string] }
45
-
46
- it { expect(schema).to eq s_fixed_array(s_string) }
47
-
48
- context do
49
- let(:object) { [12] }
50
- it { expect { json }.to raise_error Babl::Errors::RenderingError }
51
- end
52
-
53
- context do
54
- let(:object) { '12' }
55
- it { expect(json).to eq ['12'] }
56
- end
57
- end
58
-
59
- describe '#boolean' do
60
- template { [boolean] }
61
-
62
- it { expect(schema).to eq s_fixed_array(s_boolean) }
63
-
64
- context do
65
- let(:object) { true }
66
- it { expect(json).to eq [true] }
67
- end
68
-
69
- context do
70
- let(:object) { false }
71
- it { expect(json).to eq [false] }
72
- end
73
-
74
- context do
75
- let(:object) { 'true' }
76
- it { expect { json }.to raise_error Babl::Errors::RenderingError }
77
- end
78
- end
79
-
80
- context 'obviously invalid template' do
81
- template { integer.string }
82
- it { expect { schema }.to raise_error Babl::Errors::InvalidTemplate }
83
- end
84
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Babl::Operators::With do
4
- extend SpecHelper::OperatorTesting
5
-
6
- describe '#with' do
7
- context 'everything is fine' do
8
- template {
9
- object(
10
- result: with(
11
- unscoped,
12
- :msg,
13
- _.parent.nav(:msg).dep(:lol)
14
- ) { |obj, a, b| "#{a} #{b} #{obj[:msg]}" }
15
- )
16
- }
17
-
18
- let(:object) { { result: 42, msg: 'Hello C' } }
19
-
20
- it { expect(json).to eq('result' => 'Hello C Hello C Hello C') }
21
- it { expect(dependencies).to eq(result: {}, msg: { lol: {} }) }
22
- it { expect(schema).to eq s_object(s_property(:result, s_anything)) }
23
- end
24
-
25
- context 'when the block raise an exception' do
26
- template { with { raise 'lol' } }
27
-
28
- it { expect { json }.to raise_error(/\__root__\.__block__/) }
29
- end
30
- end
31
- end
@@ -1,2 +0,0 @@
1
- require 'spec_helper/operator_testing'
2
- require 'spec_helper/schema_utils'
@@ -1,46 +0,0 @@
1
- require 'babl'
2
- require 'oj'
3
- require 'json-schema'
4
- require 'spec_helper/schema_utils'
5
-
6
- module SpecHelper
7
- module OperatorTesting
8
- def template(*args, &block)
9
- let(:template) { dsl.source(*args, &block) }
10
- end
11
-
12
- def self.extended(base)
13
- base.include SchemaUtils
14
-
15
- base.let(:dsl) { Babl::Template.new }
16
- base.let(:compiled) { template.compile }
17
- base.let(:unchecked_json) { ::Oj.load(compiled.json(object)) }
18
- base.let(:dependencies) { compiled.send(:dependencies) }
19
- base.let(:schema) { compiled.send(:node).schema }
20
- base.let(:json_schema) { compiled.json_schema }
21
- base.let(:object) { nil }
22
-
23
- base.let(:json) {
24
- JSON::Validator.validate!(json_schema, unchecked_json, validate_schema: true)
25
- unchecked_json
26
- }
27
-
28
- base.before {
29
- stub_const('TestLookupContext', Class.new {
30
- attr_reader :code, :childs
31
-
32
- def initialize(code = nil, **childs)
33
- @code = code
34
- @childs = childs
35
- end
36
-
37
- def find(name)
38
- name = name.to_sym
39
- return unless childs[name]
40
- [name.to_s, childs[name].code, childs[name]]
41
- end
42
- })
43
- }
44
- end
45
- end
46
- end
@@ -1,53 +0,0 @@
1
- require 'babl'
2
-
3
- module SpecHelper
4
- module SchemaUtils
5
- def s_any_of(*args)
6
- Babl::Schema::AnyOf.canonicalized(args)
7
- end
8
-
9
- def s_anything
10
- Babl::Schema::Anything.instance
11
- end
12
-
13
- def s_dyn_array(schema)
14
- Babl::Schema::DynArray.new(schema)
15
- end
16
-
17
- def s_null
18
- Babl::Schema::Static::NULL
19
- end
20
-
21
- def s_integer
22
- Babl::Schema::Typed::INTEGER
23
- end
24
-
25
- def s_string
26
- Babl::Schema::Typed::STRING
27
- end
28
-
29
- def s_boolean
30
- Babl::Schema::Typed::BOOLEAN
31
- end
32
-
33
- def s_number
34
- Babl::Schema::Typed::NUMBER
35
- end
36
-
37
- def s_fixed_array(*schemas)
38
- Babl::Schema::FixedArray.new(schemas)
39
- end
40
-
41
- def s_object(*properties, additional: false)
42
- Babl::Schema::Object.new(properties, additional)
43
- end
44
-
45
- def s_static(value)
46
- Babl::Schema::Static.new(value)
47
- end
48
-
49
- def s_property(name, value, required: true)
50
- Babl::Schema::Object::Property.new(name, value, required)
51
- end
52
- end
53
- end
@@ -1,54 +0,0 @@
1
- require 'babl/utils/value'
2
-
3
- describe Babl::Utils::Value do
4
- let(:clazz) { described_class.new(:val1) }
5
- let(:inst1) { clazz.new([1]) }
6
- let(:inst2) { clazz.new([1]) }
7
-
8
- it { expect(inst1).to eq inst2 }
9
- it { expect(inst1).to eql inst2 }
10
- it { expect(inst1.hash).to eq inst2.hash }
11
-
12
- describe 'immutability' do
13
- it { expect { inst1.val1 = 2 }.to raise_error ::RuntimeError }
14
- end
15
-
16
- describe 'test when not equal' do
17
- before { inst2.val1 << 3 }
18
-
19
- it { expect(inst1).not_to eq inst2 }
20
- it { expect(inst1.hash).to eq inst2.hash }
21
- end
22
-
23
- describe 'use hash for equality test' do
24
- let(:inst1dup) { inst1.dup }
25
-
26
- it { expect(inst1dup).to eq inst1 }
27
-
28
- context do
29
- before { inst1dup._cached_hash = 42 }
30
- it { expect(inst1dup).not_to eq inst1 }
31
- it { expect(inst1dup).not_to eq inst2 }
32
- end
33
- end
34
-
35
- describe 'ensure eql? behavior' do
36
- let(:inst1) { clazz.new(1.0) }
37
- let(:inst2) { clazz.new(1) }
38
-
39
- it { expect(inst1).not_to eql inst2 }
40
- it { expect(inst1).not_to eq inst2 }
41
- end
42
-
43
- describe 'no property' do
44
- let(:clazz1) { described_class.new }
45
- let(:clazz2) { described_class.new }
46
-
47
- let(:clazz1_inst1) { clazz1.new }
48
- let(:clazz1_inst2) { clazz1.new }
49
- let(:clazz2_inst1) { clazz2.new }
50
-
51
- it { expect(clazz1_inst1).to eq clazz1_inst2 }
52
- it { expect(clazz1_inst1).not_to eq clazz2_inst1 }
53
- end
54
- end