babl-json 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4dec3c593ea00b70b6d191739c4c84b1d78492fa
4
- data.tar.gz: f9bcac08981d7ed996443692bd7f8752b402bab2
3
+ metadata.gz: de6e2596717bacec917c4ccb9b293676ba32f7b5
4
+ data.tar.gz: 2756b7fd36d5fdc9c661d0a1341f1353d6200260
5
5
  SHA512:
6
- metadata.gz: 15ed5dc2e9baf8cb6b94e178e9a8d743c719d73b229d4e33312525f3e743976702bcba8bc7b90069f17e2ddb8b49d4efaccb9a5518e111686847d8adc6c24f3e
7
- data.tar.gz: d18b19d2550f3cac80d054474c3d85b60eb1be7f4ddf2e43172477f5b05b3f4a01f535adfe2f322e60dee0d69265b1162757d7886159dbd52474e1395fc7d6e8
6
+ metadata.gz: 78ebc4868c41208e176f32637aa781c292ec6025612b988edb88a20ed9a640e250d942864789be496687651c19208df26335e5d2c9959307900c96a304886ea8
7
+ data.tar.gz: 6f1fb869d2345fabc61d52eb4065b5d2202e641afdd1d344b7218c6cdd70f1de4b3c3b919019b1fb8328e176c116c5614ae8aeff25b99b2d3305148f4b10a0b7
@@ -1,5 +1,9 @@
1
1
  # BABL Changelog
2
2
 
3
+ ## 0.2.3 (July 21, 2017)
4
+ - Try to simplify JSON-Schema when types are specified.
5
+ - Cut out dependency on [Values](https://github.com/tcrayford/Values).
6
+
3
7
  ## 0.2.2 (July 13, 2017)
4
8
 
5
9
  - Added four typing operators: `integer`, `number`, `string` and `boolean`.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # BABL #
1
+ ![BABL Logo](https://github.com/getbannerman/babl/raw/master/logo-babl.png)
2
2
 
3
3
  [![Build Status](https://travis-ci.org/getbannerman/babl.svg?branch=master)](https://travis-ci.org/getbannerman/babl)
4
+ [![Gem](https://img.shields.io/gem/v/babl-json.svg)](https://rubygems.org/gems/babl-json)
5
+ [![Downloads](https://img.shields.io/gem/dt/babl-json.svg)](https://rubygems.org/gems/babl-json)
4
6
 
5
7
  BABL (Bannerman API Builder Language) is a templating langage for generating JSON in APIs.
6
8
 
@@ -24,7 +26,7 @@ BABL template:
24
26
  ```ruby
25
27
  object(
26
28
  document: object(
27
- :id, :title
29
+ :id, :title,
28
30
 
29
31
  owner: _.nullable.object(:id, :name),
30
32
  authors: _.each.object(:id, :name),
@@ -21,5 +21,4 @@ Gem::Specification.new do |gem|
21
21
  gem.add_development_dependency 'json-schema', '~> 2.8'
22
22
 
23
23
  gem.add_dependency 'oj', '~> 3.0'
24
- gem.add_dependency 'values', '~> 1.8'
25
24
  end
@@ -1,9 +1,9 @@
1
1
  require 'babl/utils/hash'
2
- require 'values'
2
+ require 'babl/utils/value'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class CreatePin < ::Value.new(:node, :ref)
6
+ class CreatePin < Utils::Value.new(:node, :ref)
7
7
  def render(ctx)
8
8
  node.render(ctx.create_pin(ref))
9
9
  end
@@ -1,9 +1,9 @@
1
1
  require 'babl/utils/hash'
2
- require 'values'
2
+ require 'babl/utils/value'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class Dep < ::Value.new(:node, :path)
6
+ class Dep < Utils::Value.new(:node, :path)
7
7
  def initialize(node, path)
8
8
  super(node, canonicalize(path))
9
9
  end
@@ -1,10 +1,10 @@
1
1
  require 'babl/schema/dyn_array'
2
2
  require 'babl/errors'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class Each < ::Value.new(:node)
7
+ class Each < Utils::Value.new(:node)
8
8
  def dependencies
9
9
  { __each__: node.dependencies }
10
10
  end
@@ -1,10 +1,10 @@
1
1
  require 'babl/utils/hash'
2
2
  require 'babl/schema/fixed_array'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class FixedArray < ::Value.new(:nodes)
7
+ class FixedArray < Utils::Value.new(:nodes)
8
8
  def schema
9
9
  Schema::FixedArray.new(nodes.map(&:schema))
10
10
  end
@@ -1,9 +1,9 @@
1
1
  require 'babl/utils/hash'
2
- require 'values'
2
+ require 'babl/utils/value'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class GotoPin < ::Value.new(:node, :ref)
6
+ class GotoPin < Utils::Value.new(:node, :ref)
7
7
  def dependencies
8
8
  {}
9
9
  end
@@ -1,10 +1,10 @@
1
1
  require 'babl/utils/hash'
2
2
  require 'babl/errors'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class Merge < ::Value.new(:nodes)
7
+ class Merge < Utils::Value.new(:nodes)
8
8
  def dependencies
9
9
  nodes.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
10
10
  end
@@ -64,7 +64,7 @@ module Babl
64
64
  .map do |name, properties|
65
65
  Schema::Object::Property.new(
66
66
  name,
67
- Schema::AnyOf.canonical(properties.map(&:value)),
67
+ Schema::AnyOf.canonicalized(properties.map(&:value)),
68
68
  properties.size == all_docs.size && properties.all?(&:required)
69
69
  )
70
70
  end
@@ -89,7 +89,7 @@ module Babl
89
89
  def allow_anything(property)
90
90
  Schema::Object::Property.new(
91
91
  property.name,
92
- Schema::AnyOf.canonical([property.value, Schema::Anything.instance]),
92
+ Schema::AnyOf.canonicalized([property.value, Schema::Anything.instance]),
93
93
  property.required
94
94
  )
95
95
  end
@@ -1,8 +1,8 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
 
3
3
  module Babl
4
4
  module Nodes
5
- class Nav < ::Value.new(:through, :node)
5
+ class Nav < Utils::Value.new(:through, :node)
6
6
  def dependencies
7
7
  { through => node.dependencies }
8
8
  end
@@ -1,10 +1,10 @@
1
1
  require 'babl/utils/hash'
2
2
  require 'babl/schema/object'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class Object < ::Value.new(:nodes)
7
+ class Object < Utils::Value.new(:nodes)
8
8
  def dependencies
9
9
  nodes.values.map(&:dependencies).reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
10
10
  end
@@ -1,13 +1,13 @@
1
1
  require 'babl/utils/ref'
2
2
  require 'babl/errors'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class Parent < ::Value.new(:node)
7
+ class Parent < Utils::Value.new(:node)
8
8
  PARENT_MARKER = Utils::Ref.new
9
9
 
10
- class Resolver < ::Value.new(:node)
10
+ class Resolver < Utils::Value.new(:node)
11
11
  def dependencies
12
12
  backpropagate_dependencies(node.dependencies)
13
13
  end
@@ -1,10 +1,10 @@
1
1
  require 'babl/nodes/terminal_value'
2
2
  require 'babl/schema/static'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Nodes
7
- class Static < ::Value.new(:value)
7
+ class Static < Utils::Value.new(:value)
8
8
  def schema
9
9
  Schema::Static.new(value)
10
10
  end
@@ -1,11 +1,11 @@
1
1
  require 'babl/utils/hash'
2
2
  require 'babl/schema/any_of'
3
3
  require 'babl/errors'
4
- require 'values'
4
+ require 'babl/utils/value'
5
5
 
6
6
  module Babl
7
7
  module Nodes
8
- class Switch < ::Value.new(:nodes)
8
+ class Switch < Utils::Value.new(:nodes)
9
9
  def dependencies
10
10
  (nodes.values + nodes.keys).map(&:dependencies)
11
11
  .reduce({}) { |a, b| Babl::Utils::Hash.deep_merge(a, b) }
@@ -17,7 +17,7 @@ module Babl
17
17
  end
18
18
 
19
19
  def schema
20
- Schema::AnyOf.canonical(nodes.values.map(&:schema))
20
+ Schema::AnyOf.canonicalized(nodes.values.map(&:schema))
21
21
  end
22
22
 
23
23
  def render(ctx)
@@ -1,13 +1,15 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
  require 'babl/schema/typed'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class Typed < ::Value.new(:types, :schema, :node)
7
- BOOLEAN = method(:new).curry(3).call([TrueClass, FalseClass], Schema::Typed::BOOLEAN)
8
- INTEGER = method(:new).curry(3).call([Integer], Schema::Typed::INTEGER)
9
- NUMBER = method(:new).curry(3).call([Numeric], Schema::Typed::NUMBER)
10
- STRING = method(:new).curry(3).call([String], Schema::Typed::STRING)
6
+ class Typed < Utils::Value.new(:schema, :node)
7
+ CURRIED = method(:new).curry(2)
8
+
9
+ BOOLEAN = CURRIED.call(Schema::Typed::BOOLEAN)
10
+ INTEGER = CURRIED.call(Schema::Typed::INTEGER)
11
+ NUMBER = CURRIED.call(Schema::Typed::NUMBER)
12
+ STRING = CURRIED.call(Schema::Typed::STRING)
11
13
 
12
14
  def dependencies
13
15
  node.dependencies
@@ -19,10 +21,8 @@ module Babl
19
21
 
20
22
  def render(ctx)
21
23
  value = node.render(ctx)
22
- unless types.any? { |type| type === value }
23
- raise Errors::RenderingError, "Expected type '#{schema.type}': #{value}\n#{ctx.formatted_stack}"
24
- end
25
- value
24
+ return value if schema.classes.any? { |clazz| clazz === value }
25
+ raise Errors::RenderingError, "Expected type '#{schema.type}': #{value}\n#{ctx.formatted_stack}"
26
26
  end
27
27
 
28
28
  private
@@ -1,9 +1,9 @@
1
1
  require 'babl/utils/hash'
2
- require 'values'
2
+ require 'babl/utils/value'
3
3
 
4
4
  module Babl
5
5
  module Nodes
6
- class With < ::Value.new(:node, :nodes, :block)
6
+ class With < Utils::Value.new(:node, :nodes, :block)
7
7
  def schema
8
8
  node.schema
9
9
  end
@@ -1,10 +1,10 @@
1
1
  require 'oj'
2
2
  require 'babl/rendering/context'
3
- require 'values'
3
+ require 'babl/utils/value'
4
4
 
5
5
  module Babl
6
6
  module Rendering
7
- class CompiledTemplate < ::Value.new(:node, :dependencies, :preloader, :pretty, :json_schema)
7
+ class CompiledTemplate < Utils::Value.new(:node, :dependencies, :preloader, :pretty, :json_schema)
8
8
  def json(root)
9
9
  data = render(root)
10
10
  ::Oj.dump(data, indent: pretty ? 4 : 0, mode: :strict)
@@ -1,11 +1,11 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
  require 'set'
3
3
  require 'babl/schema/static'
4
4
  require 'babl/schema/object'
5
5
 
6
6
  module Babl
7
7
  module Schema
8
- class AnyOf < ::Value.new(:choice_set)
8
+ class AnyOf < Utils::Value.new(:choice_set)
9
9
  attr_reader :choices
10
10
 
11
11
  def initialize(choices)
@@ -22,6 +22,8 @@ module Babl
22
22
  # schema.
23
23
  def simplify
24
24
  simplify_single ||
25
+ simplify_boolean ||
26
+ simplify_typed_and_static ||
25
27
  simplify_nullability ||
26
28
  simplify_empty_array ||
27
29
  simplify_push_down_dyn_array ||
@@ -30,7 +32,7 @@ module Babl
30
32
  self
31
33
  end
32
34
 
33
- def self.canonical(choices)
35
+ def self.canonicalized(choices)
34
36
  new(choices).simplify
35
37
  end
36
38
 
@@ -41,20 +43,44 @@ module Babl
41
43
  choices.size == 1 ? choices.first : nil
42
44
  end
43
45
 
44
- # Try to merge null with another Anything
46
+ # Anything is nullable, so AnyOf[null, Anything] can be replaced by Anything
47
+ # (in general, everything can be merged into Anything but I decided not to do it in order
48
+ # to retain as much information as possible)
45
49
  def simplify_nullability
46
- AnyOf.canonical(choices - [Static::NULL]) if ([Static::NULL, Anything.instance] - choices).empty?
50
+ return unless choice_set.include?(Static::NULL) && choice_set.include?(Anything.instance)
51
+ AnyOf.canonicalized(choice_set - [Static::NULL])
52
+ end
53
+
54
+ # AnyOf[true, false] is just boolean
55
+ def simplify_boolean
56
+ return unless choice_set.include?(Static::TRUE) && choice_set.include?(Static::FALSE)
57
+ AnyOf.canonicalized(choice_set - [Static::TRUE, Static::FALSE] + [Typed::BOOLEAN])
58
+ end
59
+
60
+ # AnyOf[string, 'a string instance', 'another string'] is just string
61
+ # AnyOf[boolean, true, false] is just boolean
62
+ # AnyOf[number, 2, 3.1] is just number
63
+ # AnyOf[integer, 2, 1] is just integer
64
+ def simplify_typed_and_static
65
+ choices.each do |typed|
66
+ next unless Typed === typed
67
+ instances = choices.select { |instance|
68
+ Static === instance && typed.classes.any? { |clazz| clazz === instance.value }
69
+ }
70
+ next if instances.empty?
71
+ return AnyOf.canonicalized(choice_set - instances)
72
+ end
73
+ nil
47
74
  end
48
75
 
49
76
  # An always empty FixedArray is just a special case of a DynArray
50
77
  # We can get rid of the former and only keep the DynArray
51
78
  def simplify_empty_array
52
- return unless choices.include?(FixedArray::EMPTY)
53
- others = choices - [FixedArray::EMPTY]
54
- others.each do |other|
79
+ return unless choice_set.include?(FixedArray::EMPTY)
80
+ choice_set.each do |other|
55
81
  next unless DynArray === other
56
82
  new_other = DynArray.new(other.item)
57
- return AnyOf.canonical(others - [other] + [new_other])
83
+ return AnyOf.canonicalized(choice_set - [other, FixedArray::EMPTY] + [new_other])
58
84
  end
59
85
  nil
60
86
  end
@@ -63,12 +89,13 @@ module Babl
63
89
  # removed.
64
90
  def simplify_dyn_and_fixed_array
65
91
  fixed_arrays = choices.select { |s| FixedArray === s && s.items.uniq.size == 1 }
92
+ return if fixed_arrays.empty?
66
93
 
67
94
  choices.each do |dyn|
68
95
  next unless DynArray === dyn
69
96
  fixed_arrays.each do |fixed|
70
97
  new_dyn = DynArray.new(dyn.item)
71
- return AnyOf.canonical(choices - [fixed, dyn] + [new_dyn]) if dyn.item == fixed.items.first
98
+ return AnyOf.canonicalized(choice_set - [fixed, dyn] + [new_dyn]) if dyn.item == fixed.items.first
72
99
  end
73
100
  end
74
101
 
@@ -79,11 +106,15 @@ module Babl
79
106
  # having a different type, then AnyOf can be pushed down to this property.
80
107
  def simplify_many_objects_only_one_difference
81
108
  choices.each_with_index { |obj1, index1|
109
+ next unless Object === obj1
110
+
82
111
  choices.each_with_index { |obj2, index2|
83
- next if index2 <= index1
84
- next unless Object === obj1 && Object === obj2 && obj1.additional == obj2.additional
85
- next unless obj1.properties.map { |p| [p.name, p.required] }.to_set ==
86
- obj2.properties.map { |p| [p.name, p.required] }.to_set
112
+ break if index2 >= index1
113
+
114
+ next unless Object === obj2 &&
115
+ obj1.additional == obj2.additional &&
116
+ obj1.properties.map { |p| [p.name, p.required] }.to_set ==
117
+ obj2.properties.map { |p| [p.name, p.required] }.to_set
87
118
 
88
119
  diff1 = obj1.properties - obj2.properties
89
120
  diff2 = obj2.properties - obj1.properties
@@ -95,14 +126,14 @@ module Babl
95
126
  next property unless property == diff1.first
96
127
  Object::Property.new(
97
128
  property.name,
98
- AnyOf.canonical([diff1.first.value, diff2.first.value]),
129
+ AnyOf.canonicalized([diff1.first.value, diff2.first.value]),
99
130
  property.required
100
131
  )
101
132
  },
102
133
  obj1.additional
103
134
  )
104
135
 
105
- return AnyOf.canonical(choices - [obj1, obj2] + [merged])
136
+ return AnyOf.canonicalized(choice_set - [obj1, obj2] + [merged])
106
137
  }
107
138
  }
108
139
 
@@ -112,11 +143,12 @@ module Babl
112
143
  # Push down the AnyOf to the item if all outputs are of type DynArray
113
144
  def simplify_push_down_dyn_array
114
145
  choices.each_with_index { |arr1, index1|
146
+ next unless DynArray === arr1
115
147
  choices.each_with_index { |arr2, index2|
116
- next if index2 <= index1
117
- next unless DynArray === arr1 && DynArray === arr2
118
- new_arr = DynArray.new(AnyOf.canonical([arr1.item, arr2.item]))
119
- return AnyOf.canonical(choices - [arr1, arr2] + [new_arr])
148
+ break if index2 >= index1
149
+ next unless DynArray === arr2
150
+ new_arr = DynArray.new(AnyOf.canonicalized([arr1.item, arr2.item]))
151
+ return AnyOf.canonicalized(choice_set - [arr1, arr2] + [new_arr])
120
152
  }
121
153
  }
122
154
  nil
@@ -1,8 +1,8 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
 
3
3
  module Babl
4
4
  module Schema
5
- class DynArray < ::Value.new(:item)
5
+ class DynArray < Utils::Value.new(:item)
6
6
  def json
7
7
  { type: 'array', items: item.json }
8
8
  end
@@ -1,8 +1,8 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
 
3
3
  module Babl
4
4
  module Schema
5
- class FixedArray < ::Value.new(:items)
5
+ class FixedArray < Utils::Value.new(:items)
6
6
  EMPTY = new([])
7
7
 
8
8
  def json
@@ -1,9 +1,9 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
  require 'set'
3
3
 
4
4
  module Babl
5
5
  module Schema
6
- class Object < ::Value.new(:property_set, :additional)
6
+ class Object < Utils::Value.new(:property_set, :additional)
7
7
  attr_reader :properties
8
8
 
9
9
  def initialize(properties, additional)
@@ -14,7 +14,7 @@ module Babl
14
14
  EMPTY = new([], false)
15
15
  EMPTY_WITH_ADDITIONAL = new([], true)
16
16
 
17
- class Property < ::Value.new(:name, :value, :required)
17
+ class Property < Utils::Value.new(:name, :value, :required)
18
18
  def initialize(name, value, required)
19
19
  super(name, value, required)
20
20
  end
@@ -1,9 +1,11 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
 
3
3
  module Babl
4
4
  module Schema
5
- class Static < ::Value.new(:value)
5
+ class Static < Utils::Value.new(:value)
6
6
  NULL = new(nil)
7
+ TRUE = new(true)
8
+ FALSE = new(false)
7
9
 
8
10
  def json
9
11
  return { type: 'null' } if value.nil?
@@ -1,12 +1,12 @@
1
- require 'values'
1
+ require 'babl/utils/value'
2
2
 
3
3
  module Babl
4
4
  module Schema
5
- class Typed < ::Value.new(:type)
6
- INTEGER = new('integer')
7
- BOOLEAN = new('boolean')
8
- NUMBER = new('number')
9
- STRING = new('string')
5
+ class Typed < Utils::Value.new(:type, :classes)
6
+ INTEGER = new('integer', [::Integer])
7
+ BOOLEAN = new('boolean', [::TrueClass, ::FalseClass])
8
+ NUMBER = new('number', [::Numeric])
9
+ STRING = new('string', [::String])
10
10
 
11
11
  def json
12
12
  { type: type }
@@ -0,0 +1,37 @@
1
+ module Babl
2
+ module Utils
3
+ # Construct deeply immutable value objects
4
+ # Similar to Struct, but:
5
+ # - Properties are assumed deeply immutable (#hash is assumed constant)
6
+ # - Constructor requires all arguments
7
+ # - #== has the same meaning as #eql?
8
+ class Value
9
+ def self.new(*fields)
10
+ ::Class.new(::Struct.new(:_cached_hash, *fields)) do
11
+ field_aliases = ::Array.new(fields.size) { |i| "v#{i}" }
12
+ const_set(:FIELDS, fields.map(&:to_sym))
13
+ class_eval <<-RUBY
14
+ def initialize(#{field_aliases.join(',')})
15
+ super(#{['nil', field_aliases].join(',')})
16
+ hash
17
+ freeze
18
+ end
19
+
20
+ def hash
21
+ self._cached_hash ||= super
22
+ end
23
+
24
+ def ==(other)
25
+ eql?(other)
26
+ end
27
+
28
+ def self.with(hash = {})
29
+ raise ::ArgumentError unless ::Hash === hash && (hash.keys - FIELDS).empty?
30
+ new(*FIELDS.map { |f| hash.fetch(f) })
31
+ end
32
+ RUBY
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Babl
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
Binary file
@@ -113,9 +113,38 @@ describe Babl::Operators::Switch do
113
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
114
  end
115
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
+
116
146
  context 'with dependencies' do
117
147
  template { nav(:test).switch(nav(:keke) => parent.nav(:lol)) }
118
-
119
148
  it { expect(dependencies).to eq(test: { keke: {} }, lol: {}) }
120
149
  end
121
150
  end
@@ -3,7 +3,7 @@ require 'babl'
3
3
  module SpecHelper
4
4
  module SchemaUtils
5
5
  def s_any_of(*args)
6
- Babl::Schema::AnyOf.canonical(args)
6
+ Babl::Schema::AnyOf.canonicalized(args)
7
7
  end
8
8
 
9
9
  def s_anything
@@ -0,0 +1,54 @@
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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babl-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Terrazzoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-13 00:00:00.000000000 Z
11
+ date: 2017-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- - !ruby/object:Gem::Dependency
84
- name: values
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '1.8'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '1.8'
97
83
  description: JSON templating on steroids
98
84
  email:
99
85
  - frederic.terrazzoni@gmail.com
@@ -164,7 +150,9 @@ files:
164
150
  - lib/babl/template.rb
165
151
  - lib/babl/utils/hash.rb
166
152
  - lib/babl/utils/ref.rb
153
+ - lib/babl/utils/value.rb
167
154
  - lib/babl/version.rb
155
+ - logo-babl.png
168
156
  - spec/operators/array_spec.rb
169
157
  - spec/operators/call_spec.rb
170
158
  - spec/operators/continue_spec.rb
@@ -189,6 +177,7 @@ files:
189
177
  - spec/spec_helper.rb
190
178
  - spec/spec_helper/operator_testing.rb
191
179
  - spec/spec_helper/schema_utils.rb
180
+ - spec/utils/value_spec.rb
192
181
  homepage: https://github.com/getbannerman/babl
193
182
  licenses:
194
183
  - MIT
@@ -238,3 +227,4 @@ test_files:
238
227
  - spec/spec_helper.rb
239
228
  - spec/spec_helper/operator_testing.rb
240
229
  - spec/spec_helper/schema_utils.rb
230
+ - spec/utils/value_spec.rb