json-spec 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -8
- data/examples/example-4-to-be-as-defined-in.rb +5 -1
- data/examples/example-5-custom-expectations.rb +5 -1
- data/examples/example-7-full-example.rb +12 -6
- data/json-spec.gemspec +1 -1
- data/lib/cabeza-de-termo/json-spec/expectations-library/expectations-library.rb +1 -19
- data/lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb +4 -0
- data/lib/cabeza-de-termo/json-spec/expressions/json-expression.rb +6 -6
- data/lib/cabeza-de-termo/json-spec/expressions/json-spec.rb +16 -4
- data/lib/cabeza-de-termo/json-spec/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2136aa485a249788e83e6b962ae039d5cf3ceac9
|
4
|
+
data.tar.gz: 4786b07cf34a2c1045fcd64a8901e20709cc136a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74cc87284c78691d63eeca202be7b6216e1591bf047efa885fb99366956686db57c0ce31014fa1418ee11bd90bb22a25a1eb2864c606868047ec8a19f7179243
|
7
|
+
data.tar.gz: f77c2df721f9c977958774551650a6fe16098fbefd0bab75f826a2d92a6aacaf166bb3915e4d1c4ffd2e1dd8c884776b01100edc4980398cb523fac572dc2ff3
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ A framework to declare expectations and verify that a json object satisfies thos
|
|
13
13
|
Add this line to your application's Gemfile:
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
gem 'json-spec', '~> 0.
|
16
|
+
gem 'json-spec', '~> 0.2'
|
17
17
|
```
|
18
18
|
|
19
19
|
And then execute:
|
@@ -394,6 +394,10 @@ So now we have the class
|
|
394
394
|
|
395
395
|
```ruby
|
396
396
|
class ComposerJson
|
397
|
+
def self.validate_string(json_string)
|
398
|
+
self.new.spec.validate_string(json_string)
|
399
|
+
end
|
400
|
+
|
397
401
|
def spec()
|
398
402
|
CabezaDeTermo::JsonSpec::JsonSpec.new do |json_spec|
|
399
403
|
json_spec.expect_an(:object) do |object|
|
@@ -486,7 +490,7 @@ end
|
|
486
490
|
and to run the validation all we have to do is
|
487
491
|
|
488
492
|
```ruby
|
489
|
-
validator = ComposerJson.
|
493
|
+
validator = ComposerJson.validate_string(json_string)
|
490
494
|
```
|
491
495
|
|
492
496
|
If you want to validate a not very complex json, you can get away with it without factorizing the expectations. But as soon as the validation gets more complex, you can refactor the expectations using `:to_be_as_defined_in(some_object, :some_method)`.
|
@@ -803,8 +807,12 @@ Something like this:
|
|
803
807
|
|
804
808
|
```ruby
|
805
809
|
class ComposerLibraryInitializer
|
810
|
+
def self.new_library()
|
811
|
+
self.new.new_library
|
812
|
+
end
|
813
|
+
|
806
814
|
def new_library()
|
807
|
-
initialize_library CabezaDeTermo::JsonSpec::DefaultLibraryInitializer.
|
815
|
+
initialize_library CabezaDeTermo::JsonSpec::DefaultLibraryInitializer.new_library
|
808
816
|
end
|
809
817
|
|
810
818
|
def initialize_library(library)
|
@@ -815,14 +823,23 @@ class ComposerLibraryInitializer
|
|
815
823
|
end
|
816
824
|
```
|
817
825
|
|
818
|
-
and now plug
|
826
|
+
and now plug your library in the spec that will use it:
|
819
827
|
|
820
828
|
```ruby
|
821
|
-
CabezaDeTermo::JsonSpec::
|
822
|
-
|
829
|
+
CabezaDeTermo::JsonSpec::JsonSpec.new do
|
830
|
+
use_expectations_library ComposerLibraryInitializer.new_library
|
831
|
+
|
832
|
+
expect_an(:object) do
|
833
|
+
...
|
834
|
+
end
|
835
|
+
end
|
823
836
|
```
|
824
837
|
|
825
|
-
|
838
|
+
or, if you are going to use that library all around in your application, you can set it globally:
|
839
|
+
|
840
|
+
```ruby
|
841
|
+
CabezaDeTermo::JsonSpec::ExpectationsLibrary.set_current ComposerLibraryInitializer.new_library
|
842
|
+
```
|
826
843
|
|
827
844
|
To see how all the pieces fitted together, check and run the [seventh example](examples/example-7-full-example.rb) in the `examples/` folder.
|
828
845
|
|
@@ -950,7 +967,7 @@ and that's it. You have a fully prepared, ready to use development environment.
|
|
950
967
|
# Running the tests
|
951
968
|
|
952
969
|
* `bundle install`
|
953
|
-
* `rake
|
970
|
+
* `bundle exec rake`
|
954
971
|
|
955
972
|
## Contributing
|
956
973
|
|
@@ -19,13 +19,17 @@ class Example_4_ToBeAsDefinedIn
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
validator = ComposerJson.
|
22
|
+
validator = ComposerJson.validate_string(Fixtures.named :simple)
|
23
23
|
|
24
24
|
ValidationPrinter.new.print_report_of validator
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
class ComposerJson
|
29
|
+
def self.validate_string(json_string)
|
30
|
+
self.new.spec.validate_string(json_string)
|
31
|
+
end
|
32
|
+
|
29
33
|
def spec()
|
30
34
|
CabezaDeTermo::JsonSpec::JsonSpec.new do |json_spec|
|
31
35
|
json_spec.expect_an(:object) do |object|
|
@@ -48,7 +48,7 @@ class Example_5_CustomExpectations
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
validator = ComposerJson.
|
51
|
+
validator = ComposerJson.validate_string(Fixtures.named :simple)
|
52
52
|
|
53
53
|
ValidationPrinter.new.print_report_of validator
|
54
54
|
end
|
@@ -62,6 +62,10 @@ class IsVersionExpectation < CabezaDeTermo::JsonSpec::Expectation
|
|
62
62
|
end
|
63
63
|
|
64
64
|
class ComposerJson
|
65
|
+
def self.validate_string(json_string)
|
66
|
+
self.new.spec.validate_string(json_string)
|
67
|
+
end
|
68
|
+
|
65
69
|
def spec()
|
66
70
|
CabezaDeTermo::JsonSpec::JsonSpec.new do |json_spec|
|
67
71
|
json_spec.expect_an(:object) do |object|
|
@@ -12,10 +12,6 @@ class Example_7_FullExample
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def run()
|
15
|
-
# Set our ExpectationsLibrary initializer before using it
|
16
|
-
CabezaDeTermo::JsonSpec::ExpectationsLibrary
|
17
|
-
.set_default_library_initializer( ComposerLibraryInitializer.new )
|
18
|
-
|
19
15
|
fixtures = [:simple, :empty, :'with-errors-and-non-expected-fields']
|
20
16
|
fixtures.each do |eachFixture|
|
21
17
|
runFixture(eachFixture)
|
@@ -25,7 +21,7 @@ class Example_7_FullExample
|
|
25
21
|
def runFixture(fixture_name)
|
26
22
|
puts "--- Running the fixture '#{fixture_name}' ---\n".colorize(:blue)
|
27
23
|
|
28
|
-
validator = ComposerJson.
|
24
|
+
validator = ComposerJson.validate_string( Fixtures.named(fixture_name) )
|
29
25
|
|
30
26
|
ValidationPrinter.new.print_report_of validator
|
31
27
|
end
|
@@ -47,8 +43,12 @@ end
|
|
47
43
|
# need to validate a composer.json.
|
48
44
|
#
|
49
45
|
class ComposerLibraryInitializer
|
46
|
+
def self.new_library()
|
47
|
+
self.new.new_library
|
48
|
+
end
|
49
|
+
|
50
50
|
def new_library()
|
51
|
-
library = CabezaDeTermo::JsonSpec::DefaultLibraryInitializer.
|
51
|
+
library = CabezaDeTermo::JsonSpec::DefaultLibraryInitializer.new_library
|
52
52
|
define_library(library)
|
53
53
|
end
|
54
54
|
|
@@ -119,8 +119,14 @@ end
|
|
119
119
|
# to validate.
|
120
120
|
#
|
121
121
|
class ComposerJson
|
122
|
+
def self.validate_string(json_string)
|
123
|
+
self.new.spec.validate_string(json_string)
|
124
|
+
end
|
125
|
+
|
122
126
|
def spec()
|
123
127
|
CabezaDeTermo::JsonSpec::JsonSpec.new do |spec|
|
128
|
+
spec.use_expectations_library ComposerLibraryInitializer.new_library
|
129
|
+
|
124
130
|
spec.expect_an(:object) do |object|
|
125
131
|
object.expect('name') .not_blank
|
126
132
|
object.expect('type') .not_blank
|
data/json-spec.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["martin.rubi@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{A framework to declare expectations on a json format and validate that a json object satisfies those expectations.}
|
13
|
-
spec.description = %q{You can use this json expectations to validate jsons you send or receive in your application, or to test your API with unit tests.}
|
13
|
+
spec.description = %q{A framework to declare expectations on a json format and validate that a json object satisfies those expectations. You can use this json expectations to validate jsons you send or receive in your application, or to test your API with unit tests.}
|
14
14
|
spec.homepage = "https://github.com/cabeza-de-termo/ruby-json-spec"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -11,7 +11,6 @@ module CabezaDeTermo
|
|
11
11
|
module JsonSpec
|
12
12
|
class ExpectationsLibrary
|
13
13
|
@current = nil
|
14
|
-
@default_library_initializer = nil
|
15
14
|
|
16
15
|
# Instance accessing
|
17
16
|
|
@@ -37,25 +36,8 @@ module CabezaDeTermo
|
|
37
36
|
|
38
37
|
# Initializing
|
39
38
|
|
40
|
-
def self.default_library_initializer()
|
41
|
-
set_default_library_initializer(new_default_library_initializer) if @default_library_initializer.nil?
|
42
|
-
|
43
|
-
@default_library_initializer
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.set_default_library_initializer(library_initializer)
|
47
|
-
to_be_replaced = @default_library_initializer
|
48
|
-
@default_library_initializer = library_initializer
|
49
|
-
|
50
|
-
to_be_replaced
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.new_default_library_initializer()
|
54
|
-
DefaultLibraryInitializer.new
|
55
|
-
end
|
56
|
-
|
57
39
|
def self.new_default()
|
58
|
-
|
40
|
+
DefaultLibraryInitializer.new_library
|
59
41
|
end
|
60
42
|
|
61
43
|
# Instance methods
|
data/lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb
CHANGED
@@ -10,6 +10,10 @@ require "cabeza-de-termo/json-spec/modifiers/can-be-null-modifier"
|
|
10
10
|
module CabezaDeTermo
|
11
11
|
module JsonSpec
|
12
12
|
class DefaultLibraryInitializer < LibraryInitializer
|
13
|
+
def self.new_library()
|
14
|
+
self.new.new_library
|
15
|
+
end
|
16
|
+
|
13
17
|
def new_library()
|
14
18
|
library = ExpectationsLibrary.new
|
15
19
|
define_library(library)
|
@@ -71,8 +71,8 @@ module CabezaDeTermo
|
|
71
71
|
#
|
72
72
|
# @return JsonSpec
|
73
73
|
#
|
74
|
-
def
|
75
|
-
json_spec.
|
74
|
+
def expectations_library()
|
75
|
+
json_spec.expectations_library
|
76
76
|
end
|
77
77
|
|
78
78
|
# Error handling
|
@@ -108,7 +108,7 @@ module CabezaDeTermo
|
|
108
108
|
# @return boolean
|
109
109
|
#
|
110
110
|
def is_an_expectations_library_method?(message)
|
111
|
-
|
111
|
+
expectations_library.has_expectation_for?(message.method_name)
|
112
112
|
end
|
113
113
|
|
114
114
|
#
|
@@ -119,7 +119,7 @@ module CabezaDeTermo
|
|
119
119
|
# @return boolean
|
120
120
|
#
|
121
121
|
def is_a_modifier_library_method?(message)
|
122
|
-
|
122
|
+
expectations_library.has_modifier_for?(message.method_name)
|
123
123
|
end
|
124
124
|
|
125
125
|
# Defining Expectations
|
@@ -151,7 +151,7 @@ module CabezaDeTermo
|
|
151
151
|
# @return JsonExpression
|
152
152
|
#
|
153
153
|
def add_expectation_from_library(message)
|
154
|
-
expectation =
|
154
|
+
expectation = expectations_library.new_expectation_from_message(message)
|
155
155
|
|
156
156
|
expectation.set_expectation_method(message.method_name)
|
157
157
|
expectation.set_json_spec(json_spec)
|
@@ -173,7 +173,7 @@ module CabezaDeTermo
|
|
173
173
|
# @return JsonExpression
|
174
174
|
#
|
175
175
|
def perform_modifier_from_library(message)
|
176
|
-
modifier =
|
176
|
+
modifier = expectations_library
|
177
177
|
.new_modifier_from_message(message)
|
178
178
|
|
179
179
|
run_modifier(modifier)
|
@@ -26,6 +26,8 @@ module CabezaDeTermo
|
|
26
26
|
@expectation_messages_mapping = new_expectations_messages_mapping
|
27
27
|
@default_expectations = new_default_expectations_mapping
|
28
28
|
|
29
|
+
use_expectations_library(global_expectations_library)
|
30
|
+
|
29
31
|
CdT.bind_evaluation_of block, to: self unless block.nil?
|
30
32
|
end
|
31
33
|
|
@@ -39,14 +41,18 @@ module CabezaDeTermo
|
|
39
41
|
@default_expectations
|
40
42
|
end
|
41
43
|
|
42
|
-
def
|
43
|
-
|
44
|
+
def expectations_library()
|
45
|
+
@expectations_library
|
44
46
|
end
|
45
47
|
|
46
48
|
def root_expression()
|
47
49
|
@root_expression
|
48
50
|
end
|
49
51
|
|
52
|
+
def use_expectations_library(expectations_library)
|
53
|
+
@expectations_library = expectations_library
|
54
|
+
end
|
55
|
+
|
50
56
|
# Default expectations
|
51
57
|
|
52
58
|
def define(&block)
|
@@ -122,7 +128,7 @@ module CabezaDeTermo
|
|
122
128
|
return @expectation_messages_mapping.message_formatter_for(expectation_method)
|
123
129
|
end
|
124
130
|
|
125
|
-
|
131
|
+
expectations_library.message_formatter_for(expectation_method)
|
126
132
|
end
|
127
133
|
|
128
134
|
def define_message_formatter_for(expectation_method, message_block)
|
@@ -145,7 +151,7 @@ module CabezaDeTermo
|
|
145
151
|
end
|
146
152
|
|
147
153
|
def global_default_expectations_for(json_expression_type)
|
148
|
-
|
154
|
+
expectations_library.default_expectations.expectations_for(json_expression_type)
|
149
155
|
end
|
150
156
|
|
151
157
|
# Validating
|
@@ -170,6 +176,12 @@ module CabezaDeTermo
|
|
170
176
|
def explain()
|
171
177
|
new_expression_explainer.explain(self)
|
172
178
|
end
|
179
|
+
|
180
|
+
protected
|
181
|
+
|
182
|
+
def global_expectations_library()
|
183
|
+
ExpectationsLibrary.current
|
184
|
+
end
|
173
185
|
end
|
174
186
|
end
|
175
187
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Rubi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cdt-utilities
|
@@ -94,8 +94,10 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
description:
|
98
|
-
|
97
|
+
description: A framework to declare expectations on a json format and validate that
|
98
|
+
a json object satisfies those expectations. You can use this json expectations to
|
99
|
+
validate jsons you send or receive in your application, or to test your API with
|
100
|
+
unit tests.
|
99
101
|
email:
|
100
102
|
- martin.rubi@gmail.com
|
101
103
|
executables: []
|
@@ -240,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
240
242
|
version: '0'
|
241
243
|
requirements: []
|
242
244
|
rubyforge_project:
|
243
|
-
rubygems_version: 2.4.
|
245
|
+
rubygems_version: 2.4.5.1
|
244
246
|
signing_key:
|
245
247
|
specification_version: 4
|
246
248
|
summary: A framework to declare expectations on a json format and validate that a
|