json_test_data 0.3.0.beta → 0.4.0.beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/Gemfile.lock +1 -1
- data/README.md +5 -1
- data/features/generate_empty_objects.feature +25 -0
- data/features/step_definitions/json_test_data_steps.rb +12 -0
- data/lib/json_test_data/data_structures/helpers/number_helper.rb +2 -6
- data/lib/json_test_data/data_structures/string.rb +0 -2
- data/lib/json_test_data.rb +4 -2
- data/spec/json_test_data/data_structures/helpers/number_helper_spec.rb +146 -0
- data/spec/matchers/number_matchers.rb +6 -0
- data/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71ba5da3a2348f2065d4163a0530a5d8c6a6eff4
|
4
|
+
data.tar.gz: d36bbc1dc9474d5c76bb798b11aa3a9679a2d0ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 664eb22114d96a3e9065a28d68df01cae560d4746f53207c56453b52910043f64d1a2144d87c342dfa435dfb34fcd3aa3b20d4d7116b9389570484937b052384
|
7
|
+
data.tar.gz: a731a6ea79cedba2e929c6d2785071886e749a32e88108da5d7cbdae38c2f74be5f492950c20c8e35ca6b69966cbb8734c4c4f7afc683fa3d690926755bedad9
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,11 @@ gem "json_test_data", "~> 0.0.1"
|
|
12
12
|
```
|
13
13
|
To use it, first require it, then generate the test data using:
|
14
14
|
```ruby
|
15
|
-
data = JsonTestData.generate!
|
15
|
+
data = JsonTestData.generate!(schema)
|
16
|
+
```
|
17
|
+
If you would prefer to return a Ruby object instead of a JSON object, simply include the `:ruby` option:
|
18
|
+
```ruby
|
19
|
+
data = JsonTestData.generate!(schema, ruby: true)
|
16
20
|
```
|
17
21
|
|
18
22
|
The input that you put into it must be valid [JSON schema](http://json-schema.org). The top-level object must have a `:type` key indicating the type of object to be generated ("object", "array", "string", etc.).
|
@@ -28,3 +28,28 @@ Feature: Generating empty objects
|
|
28
28
|
"""json
|
29
29
|
[null]
|
30
30
|
"""
|
31
|
+
|
32
|
+
Scenario: Ruby hash
|
33
|
+
Given the following JSON schema:
|
34
|
+
"""json
|
35
|
+
{
|
36
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
37
|
+
"type": "object",
|
38
|
+
"properties": {}
|
39
|
+
}
|
40
|
+
"""
|
41
|
+
When I run the JSON data generator with the "ruby" option
|
42
|
+
Then the output should be a Ruby hash
|
43
|
+
|
44
|
+
|
45
|
+
Scenario: Ruby array
|
46
|
+
Given the following JSON schema:
|
47
|
+
"""json
|
48
|
+
{
|
49
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
50
|
+
"type": "array",
|
51
|
+
"items": {}
|
52
|
+
}
|
53
|
+
"""
|
54
|
+
When I run the JSON data generator with the "ruby" option
|
55
|
+
Then the output should be a Ruby array
|
@@ -6,10 +6,22 @@ When(/^I run the JSON data generator$/) do
|
|
6
6
|
@output = JsonTestData.generate!(@schema)
|
7
7
|
end
|
8
8
|
|
9
|
+
When(/^I run the JSON data generator with the "ruby" option$/) do
|
10
|
+
@output = JsonTestData.generate!(@schema, ruby: true)
|
11
|
+
end
|
12
|
+
|
9
13
|
Then(/^the JSON output should be:$/) do |json|
|
10
14
|
expect(@output).to eq json
|
11
15
|
end
|
12
16
|
|
17
|
+
Then(/^the output should be a Ruby hash$/) do
|
18
|
+
expect(@output).to be_a(Hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
Then(/^the output should be a Ruby array$/) do
|
22
|
+
expect(@output).to be_a(Array)
|
23
|
+
end
|
24
|
+
|
13
25
|
Then(/^the "([^" ]*)" property of the JSON output should be a ([^ ]*)$/) do |prop, type|
|
14
26
|
klass = Class.const_get(type)
|
15
27
|
expect(JSON.parse(@output).fetch(prop).class).to eql klass
|
@@ -7,7 +7,7 @@ module JsonTestData
|
|
7
7
|
adjust_for_maximum(number: num, maximum: maximum, step_size: step_size)
|
8
8
|
end
|
9
9
|
|
10
|
-
def adjust_for_minimum(number:, minimum
|
10
|
+
def adjust_for_minimum(number:, minimum: nil, step_size: nil)
|
11
11
|
return number unless minimum && minimum >= number
|
12
12
|
|
13
13
|
num = !!step_size ? number + step_size : minimum + 1
|
@@ -15,15 +15,11 @@ module JsonTestData
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def between(min:, max:, integer: false)
|
18
|
-
return integer ? mean(min, max).
|
18
|
+
return integer ? mean(min, max).round(0) : mean(min, max)
|
19
19
|
end
|
20
20
|
|
21
21
|
def mean(*numbers)
|
22
22
|
numbers.inject(:+).to_f.quo(numbers.length)
|
23
23
|
end
|
24
|
-
|
25
|
-
def infinity
|
26
|
-
Math.atanh(1)
|
27
|
-
end
|
28
24
|
end
|
29
25
|
end
|
data/lib/json_test_data.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'regxing'
|
2
3
|
|
3
4
|
require_relative './json_test_data/json_schema'
|
4
5
|
|
5
6
|
module JsonTestData
|
6
|
-
def self.generate!(schema)
|
7
|
-
JsonSchema.new(schema).generate_example
|
7
|
+
def self.generate!(schema, opts={})
|
8
|
+
schema = JsonSchema.new(schema).generate_example
|
9
|
+
opts[:ruby] ? JSON.parse(schema) : schema
|
8
10
|
end
|
9
11
|
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
describe JsonTestData::NumberHelper do
|
2
|
+
include JsonTestData::NumberHelper
|
3
|
+
|
4
|
+
describe "#adjust_for_maximum" do
|
5
|
+
context "no maximum" do
|
6
|
+
let(:number) { 3 }
|
7
|
+
|
8
|
+
it "returns the given number" do
|
9
|
+
expect(adjust_for_maximum(number: number)).to eql number
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "number less than maximum" do
|
14
|
+
let(:number) { 3 }
|
15
|
+
let(:maximum) { 4 }
|
16
|
+
|
17
|
+
it "returns the given number" do
|
18
|
+
expect(adjust_for_maximum(number: number, maximum: maximum)).to eql number
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "number greater than or equal to maximum" do
|
23
|
+
let(:number) { 3 }
|
24
|
+
let(:maximum) { 3 }
|
25
|
+
|
26
|
+
it "returns one less than the maximum" do
|
27
|
+
expect(adjust_for_maximum(number: number, maximum: maximum)).to eql maximum - 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "step size greater than 1" do
|
32
|
+
let(:number) { 3 }
|
33
|
+
let(:maximum) { 3 }
|
34
|
+
let(:step_size) { 2 }
|
35
|
+
|
36
|
+
it "returns the number reduced by step size" do
|
37
|
+
expect(
|
38
|
+
adjust_for_maximum(number: number, maximum: maximum, step_size: step_size)
|
39
|
+
).to eql maximum - step_size
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "step size less than 1" do
|
44
|
+
let(:number) { 3 }
|
45
|
+
let(:maximum) { 3 }
|
46
|
+
let(:step_size) { 0.25 }
|
47
|
+
|
48
|
+
it "returns the number reduced by step size" do
|
49
|
+
expect(
|
50
|
+
adjust_for_maximum(number: number, maximum: maximum, step_size: step_size)
|
51
|
+
).to eql maximum - step_size
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#adjust_for_minimum" do
|
57
|
+
context "no minimum" do
|
58
|
+
let(:number) { 1 }
|
59
|
+
|
60
|
+
it "returns the given number" do
|
61
|
+
expect(adjust_for_minimum(number: number)).to eql number
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "number is greater than minimum" do
|
66
|
+
let(:number) { 1 }
|
67
|
+
let(:minimum) { 0 }
|
68
|
+
|
69
|
+
it "returns the number" do
|
70
|
+
expect(adjust_for_minimum(number: number, minimum: minimum)).to eql number
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "number is less than or equal to minimum" do
|
75
|
+
let(:number) { 2 }
|
76
|
+
let(:minimum) { 2 }
|
77
|
+
|
78
|
+
it "returns one more than the minimum" do
|
79
|
+
expect(adjust_for_minimum(number: number, minimum: minimum)).to eql minimum + 1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "step size greater than 1" do
|
84
|
+
let(:number) { 2 }
|
85
|
+
let(:minimum) { 2 }
|
86
|
+
let(:step_size) { 2 }
|
87
|
+
|
88
|
+
it "returns the number increased by step size" do
|
89
|
+
expect(
|
90
|
+
adjust_for_minimum(number: number, minimum: minimum, step_size: step_size)
|
91
|
+
).to eql minimum + step_size
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "step size less than 1" do
|
96
|
+
let(:number) { 2 }
|
97
|
+
let(:minimum) { 2 }
|
98
|
+
let(:step_size) { 0.5 }
|
99
|
+
|
100
|
+
it "returns the number reduced by step size" do
|
101
|
+
expect(
|
102
|
+
adjust_for_minimum(number: number, minimum: minimum, step_size: step_size)
|
103
|
+
).to eql minimum + step_size
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "#between" do
|
109
|
+
context "when it doesn't have to be an integer" do
|
110
|
+
let(:min) { 1 }
|
111
|
+
let(:max) { 2 }
|
112
|
+
let(:result) { 1.5 }
|
113
|
+
|
114
|
+
it "returns the mean" do
|
115
|
+
expect(between(min: min, max: max)).to be_within(0.0002).of(result)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "is less than the max" do
|
119
|
+
expect(between(min: min, max: max)).to be_less_than(max)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "is greater than the min" do
|
123
|
+
expect(between(min: min, max: max)).to be_greater_than(min)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "when it has to be an integer" do
|
128
|
+
let(:min) { 2 }
|
129
|
+
let(:max) { 5 }
|
130
|
+
let(:result) { 4 }
|
131
|
+
|
132
|
+
it "returns the nearest integer" do
|
133
|
+
expect(between(min: min, max: max, integer: true)).to eql result
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "#mean" do
|
139
|
+
let(:numbers) { [ 3, 4, 5, 6 ] }
|
140
|
+
let(:avg) { 4.5 }
|
141
|
+
|
142
|
+
it "returns the mean" do
|
143
|
+
expect(mean(*numbers)).to be_within(0.0002).of(avg)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -12,6 +12,12 @@ RSpec::Matchers.define :be_greater_than do |expected|
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
RSpec::Matchers.define :be_less_than do |expected|
|
16
|
+
match do |actual|
|
17
|
+
actual < expected
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
15
21
|
RSpec::Matchers.define :be_less_than_or_equal_to do |expected|
|
16
22
|
match do |actual|
|
17
23
|
actual <= expected
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_test_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0.beta
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dana Scheider
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: regxing
|
@@ -151,6 +151,7 @@ files:
|
|
151
151
|
- lib/json_test_data/data_structures/number.rb
|
152
152
|
- lib/json_test_data/data_structures/string.rb
|
153
153
|
- lib/json_test_data/json_schema.rb
|
154
|
+
- spec/json_test_data/data_structures/helpers/number_helper_spec.rb
|
154
155
|
- spec/json_test_data/data_structures/number_spec.rb
|
155
156
|
- spec/json_test_data/data_structures/string_spec.rb
|
156
157
|
- spec/json_test_data/json_schema_spec.rb
|
@@ -181,7 +182,7 @@ rubyforge_project:
|
|
181
182
|
rubygems_version: 2.5.1
|
182
183
|
signing_key:
|
183
184
|
specification_version: 4
|
184
|
-
summary: rambo-0.
|
185
|
+
summary: rambo-0.4.0.beta
|
185
186
|
test_files:
|
186
187
|
- features/array_constraints.feature
|
187
188
|
- features/data_types.feature
|
@@ -193,6 +194,7 @@ test_files:
|
|
193
194
|
- features/step_definitions/json_test_data_steps.rb
|
194
195
|
- features/string_constraints.feature
|
195
196
|
- features/support/env.rb
|
197
|
+
- spec/json_test_data/data_structures/helpers/number_helper_spec.rb
|
196
198
|
- spec/json_test_data/data_structures/number_spec.rb
|
197
199
|
- spec/json_test_data/data_structures/string_spec.rb
|
198
200
|
- spec/json_test_data/json_schema_spec.rb
|