xpect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad2edb079c70798c80c0398f9f5dfde24caf52e476e59ba304e88f300f3698f0
4
+ data.tar.gz: f3e8745d6ca091aea1d78ccc75a637161c4551d959547dbd3757195e856df8e8
5
+ SHA512:
6
+ metadata.gz: 1a93009cb346efe42a2ee5c47a44bf266b47a1a49026fe109407cf211e660092a0993e797a0380d297fa79a02ccf2725eca58164efae61a890acdc3660c2afba
7
+ data.tar.gz: e2995f11148208b697d8ff2fd5854f69a3c95f9315af4cb24b35aedc31d3415db63d30890092e83efc925d129203f797c49c316c75a4f9b7211f27071b86b6e9
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .idea/
14
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at travis.douce@stitchfix.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in xpect.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xpect (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (9.1.0)
10
+ diff-lcs (1.3)
11
+ rake (10.4.2)
12
+ rspec (3.7.0)
13
+ rspec-core (~> 3.7.0)
14
+ rspec-expectations (~> 3.7.0)
15
+ rspec-mocks (~> 3.7.0)
16
+ rspec-core (3.7.0)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-expectations (3.7.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-mocks (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-support (3.7.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.16)
31
+ byebug
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ xpect!
35
+
36
+ BUNDLED WITH
37
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Travis Douce
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,639 @@
1
+ # Xpect
2
+
3
+ A Ruby Hash specification to ensure unstructured data is structured the way you expect it.
4
+
5
+ ## Design Goal
6
+
7
+ The design goal is write specifications in terms of the object that is being validated. For example, I want to write a
8
+ specification as a `Hash`, as opposed to a DSL, when validating a `Hash`.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'xpect'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install xpect
25
+
26
+ ## Usage
27
+
28
+ Verify that `Hash`es are the structure you expect at the on-set, as opposed to finding out a `Hash` isn't structured
29
+ the way you intended deep in a function body. This can be useful for various reasons, such as:
30
+ * HTTP responses
31
+ * RabbitMQ payloads
32
+ * Argument Hashes
33
+ * Or, anywhere that needs to ensure `Hash`es (or JSON, just parse it first) conform to a particular structure
34
+
35
+ ```ruby
36
+ def some_function(hash_payload)
37
+ spec = {
38
+ person: {
39
+ name: {
40
+ first: Xpect::Matchers.truthy,
41
+ last: Xpect::Matchers.truthy,
42
+ middle: Xpect::Matchers.anything,
43
+ },
44
+ footwear: lambda {|v| ['flip flops', 'flip flops and socks', 'hiking boots'].include?(v) },
45
+ state: 'GA',
46
+ age: lambda {|v| v >= 0 }
47
+ }
48
+ }
49
+
50
+ Xpect::Spect.validate!(spec: spec, data: hash_payload)
51
+ # Continue processing data knowing that the Hash contains valid data.
52
+ end
53
+
54
+ ```
55
+
56
+ ## `.conform!` vs `.validate!`
57
+
58
+ `.validate!` ensures the data (i.e. the `Hash`) adheres to the structure according to the spec (i.e. validates the data).
59
+ If the data is valid then it returns the data it was given. `.conform!` validates the data according to the spec, just like
60
+ `.validate!`, but it returns only the data specified in the spec. Both functions raise a `Xpect::FailedSpec` if the data
61
+ does not adhere to the structure of the spec.
62
+
63
+ ### `.validate!`
64
+
65
+ ```ruby
66
+ spec = {
67
+ name: 'Andre 3000',
68
+ age: lambda {|v| v > 40 }
69
+ }
70
+
71
+ # Passes
72
+ validated_data = Xpect::Spect.validate!(
73
+ spec: spec,
74
+ data: {
75
+ name: 'Andre 3000',
76
+ age: 47,
77
+ footwear: 'flip flops and socks'
78
+ }
79
+ )
80
+
81
+ puts validated_data
82
+ # {
83
+ # name: 'Andre 3000',
84
+ # age: 47,
85
+ # foot_wear: 'flip flops and socks'
86
+ # }
87
+
88
+ # Fails and raises Xpect::FailedSpec
89
+ Xpect::Spect.validate!(
90
+ spec: spec,
91
+ data: {
92
+ name: 'Andre 3000',
93
+ age: 4, # Not greater than 40 as specified in spec
94
+ footwear: 'flip flops and socks'
95
+ }
96
+ )
97
+ ```
98
+
99
+ ### `.conform!`
100
+
101
+ ```ruby
102
+ spec = {
103
+ name: 'Andre 3000',
104
+ age: lambda {|v| v > 40 }
105
+ }
106
+
107
+ # Passes
108
+ conformed_data = Xpect::Spect.conform!(
109
+ spec: spec,
110
+ data: {
111
+ name: 'Andre 3000',
112
+ age: 47,
113
+ footwear: 'flip flops and socks'
114
+ }
115
+ )
116
+
117
+ puts conformed_data
118
+ # {
119
+ # name: 'Andre 3000',
120
+ # age: 47,
121
+ # }
122
+
123
+ # Fails and raises Xpect::FailedSpec
124
+ Xpect::Spect.conform!(
125
+ spec: spec,
126
+ data: {
127
+ name: 'Andre 3000',
128
+ age: 4, # Not greater than 40 as specified in spec
129
+ footwear: 'flip flops and socks'
130
+ }
131
+ )
132
+ ```
133
+
134
+ ## `Hash` values
135
+
136
+ ### `truthy`
137
+
138
+ ```ruby
139
+ spec = { name: Xpect::Matchers.truthy }
140
+
141
+ # Passes
142
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre 3000'})
143
+
144
+ # Fails
145
+ Xpect::Spect.validate!(spec: spec, data: { name: false})
146
+ ```
147
+
148
+ ### `falsy`
149
+
150
+ ```ruby
151
+ spec = { name: Xpect::Matchers.falsy }
152
+
153
+ # Passes
154
+ Xpect::Spect.validate!(spec: spec, data: { name: ''})
155
+
156
+ # Fails
157
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre 3000'})
158
+ ```
159
+
160
+ ### `anything`
161
+
162
+ ```ruby
163
+ spec = { name: Xpect::Matchers.anything }
164
+
165
+ # Passes
166
+ Xpect::Spect.validate!(spec: spec, data: { name: nil})
167
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre 3000'})
168
+
169
+ # Fails
170
+ Xpect::Spect.validate!(spec: spec, data: {})
171
+ ```
172
+
173
+ ### `nil`
174
+
175
+ ```ruby
176
+ spec = { name: Xpect::Matchers.nil }
177
+
178
+ # Passes
179
+ Xpect::Spect.validate!(spec: spec, data: { name: nil})
180
+
181
+ # Fails
182
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre 3000'})
183
+ ```
184
+
185
+ ### Custom
186
+
187
+ ```ruby
188
+ spec = { name: lambda {|v| ['Andre 3000', 'Big Boi'].include?(v) } }
189
+
190
+ # Passes
191
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Big Boi'})
192
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre'})
193
+
194
+ # Fails
195
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Back Street Boys'})
196
+ ```
197
+
198
+ ### `Pred`
199
+
200
+ ```ruby
201
+ spec = {
202
+ name: Xpect::Pred.new(
203
+ pred: lambda {|v| ['Andre 3000', 'Big Boi'].include?(v) }
204
+ )
205
+ }
206
+
207
+ # Passes
208
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Big Boi'})
209
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre'})
210
+
211
+ # Fails
212
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Back Street Boys'})
213
+ ```
214
+
215
+ Providing a default value
216
+
217
+ ```ruby
218
+ spec = {
219
+ name: Xpect::Pred.new(
220
+ pred: lambda {|v| ['Andre 3000', 'Big Boi'].include?(v) },
221
+ default: 'Dr. Seuss'
222
+ )
223
+ }
224
+
225
+ # Passes
226
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Big Boi'})
227
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Andre'})
228
+ validated_data = Xpect::Spect.validate!(spec: spec, data: {})
229
+
230
+ puts validated_data
231
+ # { name: 'Dr. Seuss' }
232
+
233
+ # Fails
234
+ Xpect::Spect.validate!(spec: spec, data: { name: 'Back Street Boys'})
235
+ ```
236
+
237
+ ### Arrays
238
+
239
+ Exact item comparison
240
+
241
+ ```ruby
242
+ spec = {
243
+ people: [
244
+ {
245
+ name: 'Andre 3000',
246
+ footwear: 'flip flops'
247
+ },
248
+ {
249
+ name: 'Big Boi',
250
+ footwear: 'flip flops and socks'
251
+ }
252
+ ]
253
+ }
254
+
255
+ # Passes
256
+ Xpect::Spect.validate!(
257
+ spec: spec,
258
+ data: {
259
+ people: [
260
+ {
261
+ name: 'Andre 3000',
262
+ footwear: 'flip flops'
263
+ },
264
+ {
265
+ name: 'Big Boi',
266
+ footwear: 'flip flops and socks'
267
+ }
268
+ ]
269
+ }
270
+ )
271
+
272
+ # Passes
273
+ Xpect::Spect.validate!(
274
+ spec: spec,
275
+ data: {
276
+ people: [
277
+ {
278
+ name: 'Andre 3000',
279
+ footwear: 'flip flops'
280
+ },
281
+ {
282
+ name: 'Big Boi',
283
+ footwear: 'flip flops and socks'
284
+ },
285
+ {
286
+ name: 'CeeLo Green',
287
+ footwear: 'boots'
288
+ }
289
+ ]
290
+ }
291
+ )
292
+
293
+ # Fails - missing second item in array in spec
294
+ Xpect::Spect.validate!(
295
+ spec: spec,
296
+ data: {
297
+ people: [
298
+ {
299
+ name: 'Andre 3000',
300
+ footwear: 'flip flops'
301
+ }
302
+ ]
303
+ }
304
+ )
305
+ ```
306
+
307
+ Ensuring every item in Array meets specification
308
+
309
+ ```ruby
310
+ spec = {
311
+ people: Xpect::Every.new(
312
+ {
313
+ name: Xpect::Matchers.truthy,
314
+ footwear: lambda {|v| ['flip flops', 'flip flops and socks'].include?(v) }
315
+ }
316
+ )
317
+ }
318
+
319
+ # Passes
320
+ Xpect::Spect.validate!(
321
+ spec: spec,
322
+ data: {
323
+ people: [
324
+ {
325
+ name: 'Andre 3000',
326
+ footwear: 'flip flops'
327
+ },
328
+ {
329
+ name: 'Big Boi',
330
+ footwear: 'flip flops and socks'
331
+ }
332
+ ]
333
+ }
334
+ )
335
+
336
+ # Passes
337
+ Xpect::Spect.validate!(
338
+ spec: spec,
339
+ data: {
340
+ people: [
341
+ {
342
+ name: 'Andre 3000',
343
+ footwear: 'flip flops'
344
+ },
345
+ {
346
+ name: 'Big Boi',
347
+ footwear: 'flip flops and socks'
348
+ },
349
+ {
350
+ name: 'CeeLo Green',
351
+ footwear: 'flip flops'
352
+ }
353
+ ]
354
+ }
355
+ )
356
+
357
+ # Fails
358
+ Xpect::Spect.validate!(
359
+ spec: spec,
360
+ data: {
361
+ people: [
362
+ {
363
+ name: 'Andre 3000',
364
+ footwear: 'flip flops'
365
+ },
366
+ {
367
+ name: 'Big Boi',
368
+ footwear: 'flip flops and socks'
369
+ },
370
+ {
371
+ name: 'Travis',
372
+ footwear: 'Hiking Boots' # footwear does not adhere to specification
373
+ }
374
+ ]
375
+ }
376
+ )
377
+ ````
378
+
379
+ ## `Hash` keys
380
+
381
+ Requiring keys
382
+
383
+ ```ruby
384
+ spec = {
385
+ person: Xpect::Keys.new(
386
+ required: {
387
+ name: 'Andre 3000',
388
+ footwear: lambda {|v| ['flip flops', 'socks'].include?(v) }
389
+ }
390
+ ),
391
+ }
392
+
393
+ # Passes
394
+ Xpect::Spect.validate!(
395
+ spec: spec,
396
+ data: {
397
+ person: {
398
+ name: 'Andre 3000',
399
+ footwear: 'socks',
400
+ age: 45
401
+ }
402
+ }
403
+ )
404
+
405
+ # Fails
406
+ Xpect::Spect.validate!(
407
+ spec: spec,
408
+ data: {
409
+ person: {
410
+ footwear: 'socks',
411
+ age: 45 # Missing the `name` key as specified in spec
412
+ }
413
+ }
414
+ )
415
+ ```
416
+
417
+ Optional keys
418
+
419
+ ```ruby
420
+ spec = {
421
+ person: Xpect::Keys.new(
422
+ required: {
423
+ name: 'Andre 3000',
424
+ footwear: lambda {|v| ['flip flops', 'socks'].include?(v) }
425
+ },
426
+ optional: {
427
+ style: 'ice cold'
428
+ }
429
+ ),
430
+ }
431
+
432
+ # Passes
433
+ Xpect::Spect.validate!(
434
+ spec: spec,
435
+ data: {
436
+ person: {
437
+ name: 'Andre 3000',
438
+ footwear: 'socks',
439
+ age: 45
440
+ }
441
+ }
442
+ )
443
+
444
+ Xpect::Spect.validate!(
445
+ spec: spec,
446
+ data: {
447
+ person: {
448
+ name: 'Andre 3000',
449
+ footwear: 'socks',
450
+ age: 45,
451
+ style: 'ice cold'
452
+ }
453
+ }
454
+ )
455
+
456
+ # Fails
457
+ Xpect::Spect.validate!(
458
+ spec: spec,
459
+ data: {
460
+ person: {
461
+ footwear: 'socks',
462
+ age: 45,
463
+ style: 'too hot for the hot tub' # Does not meet specification
464
+ }
465
+ }
466
+ )
467
+ ```
468
+
469
+ ## Highly Nested Hashes
470
+
471
+ ```ruby
472
+ spec = {
473
+ people: Xpect::Every.new(
474
+ {
475
+ name: Xpect::Matchers.truthy,
476
+ footwear: lambda {|v| ['flip flops', 'flip flops and socks'].include?(v) },
477
+ things: [
478
+ 1,
479
+ 2,
480
+ 3,
481
+ {a: 'a'},
482
+ [
483
+ 'one',
484
+ 'two',
485
+ Xpect::Pred.new(
486
+ pred: lambda {|v| v == 'three'},
487
+ default: 'my-default'
488
+ )
489
+ ]
490
+ ],
491
+ other: Xpect::Keys.new(
492
+ required: {
493
+ b: 'b',
494
+ c: 'c',
495
+ f: [
496
+ 4,
497
+ 5,
498
+ 6,
499
+ lambda {|v| v > 8},
500
+ ],
501
+ g: Xpect::Every.new(
502
+ {
503
+ id: lambda {|v| v > 100}
504
+ }
505
+ )
506
+ },
507
+ optional: {d: 'd', e: 'e'}
508
+ )
509
+ }
510
+ ),
511
+ }
512
+
513
+ # Passes
514
+ conformed_data = Xpect::Spect.conform!(
515
+ spec: spec,
516
+ data: {
517
+ people: [
518
+ {
519
+ name: 'Andre 3000',
520
+ footwear: 'flip flops',
521
+ not_required_by_spec: 'not_required_by_spec',
522
+ things: [
523
+ 1,
524
+ 2,
525
+ 3,
526
+ {a: 'a'},
527
+ [
528
+ 'one',
529
+ 'two'
530
+ ]
531
+ ],
532
+ other: {
533
+ b: 'b',
534
+ c: 'c',
535
+ e: 'e',
536
+ f: [
537
+ 4,
538
+ 5,
539
+ 6,
540
+ 9
541
+ ],
542
+ g: [
543
+ {
544
+ id: 101,
545
+ },
546
+ {
547
+ id: 102
548
+ }
549
+ ],
550
+ not_required_by_spec: [1, 2, 3, 4]
551
+ }
552
+ }
553
+ ]
554
+ }
555
+ )
556
+
557
+ puts conformed_data
558
+ # {
559
+ # :people => [
560
+ # {
561
+ # :name => "Andre 3000",
562
+ # :footwear => "flip flops",
563
+ # :things => [1, 2, 3, {:a => "a"}, ["one", "two", "my-default"]],
564
+ # :other => {:b => "b", :c => "c", :f => [4, 5, 6, 9], :g => [{:id=>101}, {:id => 102}], :e => "e"}
565
+ # }
566
+ # ]
567
+ # }
568
+
569
+ # Fails
570
+ Xpect::Spect.conform!(
571
+ spec: spec,
572
+ data: {
573
+ people: [
574
+ {
575
+ name: 'Andre 3000',
576
+ footwear: 'flip flops',
577
+ not_required_by_spec: 'not_required_by_spec',
578
+ things: [
579
+ 1,
580
+ 2,
581
+ 3,
582
+ {a: 'a'},
583
+ [
584
+ 'one',
585
+ 'two'
586
+ ]
587
+ ],
588
+ other: {
589
+ b: 'b',
590
+ c: 'c',
591
+ e: 'e',
592
+ f: [
593
+ 4,
594
+ 5,
595
+ 6,
596
+ 9
597
+ ],
598
+ g: [
599
+ {
600
+ id: 101,
601
+
602
+ },
603
+ {
604
+ id: 2 # Not greater than 100 as specified in spec
605
+ }
606
+ ],
607
+ not_required_by_spec: [1, 2, 3, 4]
608
+ }
609
+ }
610
+ ]
611
+ }
612
+ )
613
+ ```
614
+
615
+ ## Development
616
+
617
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
618
+
619
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
620
+
621
+ ## Contributing
622
+
623
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tdouce/xpect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
624
+
625
+ ## License
626
+
627
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
628
+
629
+ ## Code of Conduct
630
+
631
+ Everyone interacting in the Xpect project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tdouce/xpect/blob/master/CODE_OF_CONDUCT.md).
632
+
633
+ ## TODOs
634
+ * Fix paths in tests or provide a better way to communicate where failure happended
635
+ * Add unit tests for
636
+ * Keys and arrays
637
+ * Every
638
+ * Type
639
+ * Then remove A LOT of tests in spect_spec.rb
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xpect"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ module Xpect
2
+ class EqualityHelpers
3
+ def self.equal?(val_1, val_2, path)
4
+ unless val_1 == val_2
5
+ raise FailedSpec, "'#{ val_1 }' is not equal to '#{ val_2 }' at '#{ path }'"
6
+ end
7
+ end
8
+
9
+ def self.equal_with_proc?(fn, val, path)
10
+ unless fn.call(val)
11
+ raise FailedSpec, "'#{ val }' does not meet expectation at '#{ path }'"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module Xpect
2
+ # TODO: Add tests
3
+ class Every
4
+ def initialize(item_spec)
5
+ @item_spec = item_spec
6
+ end
7
+
8
+ def conform!(data:, path: [])
9
+ data.map do |val|
10
+ Xpect::Type.process(@item_spec, @item_spec, val, path)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Xpect
2
+ class FailedSpec < StandardError; end
3
+ end
data/lib/xpect/keys.rb ADDED
@@ -0,0 +1,53 @@
1
+ module Xpect
2
+ class Keys
3
+ def initialize(required: {}, optional: {})
4
+ raise "required must be a Hash" unless required.is_a?(Hash) && optional.is_a?(Hash)
5
+
6
+ @optional = optional
7
+ @required = required
8
+ end
9
+
10
+ def conform!(value:, path: [])
11
+ required = process_required(value, path)
12
+ process_optional(required, value, path)
13
+ end
14
+
15
+ private
16
+
17
+ def process_optional(init, value, path)
18
+ @optional.reduce(init) do |memo, (key, val)|
19
+ if val.is_a?(Keys)
20
+ memo[key] = val.conform!(value: value[key], path: path)
21
+ else
22
+ if value.has_key?(key)
23
+ data_value = value.fetch(key)
24
+ memo[key] = Xpect::Type.process(val, val, data_value, path)
25
+ end
26
+ end
27
+
28
+ memo
29
+ end
30
+ end
31
+
32
+ def process_required(value, path)
33
+ @required.reduce({}) do |memo, (key, val)|
34
+ if val.is_a?(Keys)
35
+ memo[key] = val.conform!(value: value[key], path: path)
36
+ else
37
+ if value.has_key?(key)
38
+ data_value = value.fetch(key)
39
+ memo[key] = Xpect::Type.process(val, val, data_value, path)
40
+ else
41
+ if val.is_a?(Pred) && val.default
42
+ memo[key] = val.default
43
+ else
44
+ raise FailedSpec, "does not include '#{ key }' at '#{ path }'"
45
+ end
46
+ end
47
+ end
48
+
49
+ memo
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,49 @@
1
+ module Xpect
2
+ class Matchers
3
+ def self.anything
4
+ lambda {|_| true }
5
+ end
6
+
7
+ def self.nil
8
+ lambda do |val|
9
+ unless val.nil?
10
+ raise_error("'#{ val }' is not nil.")
11
+ end
12
+
13
+ true
14
+ end
15
+ end
16
+
17
+ def self.falsy
18
+ lambda do |val|
19
+ if val.is_a?(Integer) || val.is_a?(Float)
20
+ raise_error("'#{ val }' is not falsy.")
21
+ end
22
+
23
+ if val.nil? || val.empty?
24
+ return true
25
+ end
26
+
27
+ raise_error("'#{ val }' is not falsy.")
28
+ end
29
+ end
30
+
31
+ def self.truthy
32
+ lambda do |val|
33
+ if val.is_a?(Integer) || val.is_a?(Float)
34
+ return true
35
+ end
36
+
37
+ if val.nil? || val.empty?
38
+ raise_error("'#{ val }' is not truthy.")
39
+ end
40
+
41
+ true
42
+ end
43
+ end
44
+
45
+ def self.raise_error(msg)
46
+ raise FailedSpec, msg
47
+ end
48
+ end
49
+ end
data/lib/xpect/pred.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'xpect/failed_spec'
2
+
3
+ module Xpect
4
+ class Pred
5
+ attr_reader :default
6
+
7
+ def initialize(pred:, error_msg: nil, default: nil)
8
+ unless pred.is_a?(Proc)
9
+ raise "pred must be a Proc"
10
+ end
11
+
12
+ @pred = pred
13
+ @error_msg = error_msg
14
+ @default = default
15
+ end
16
+
17
+ def conform!(value:, path: nil)
18
+ return @default if @default && value.nil?
19
+
20
+ if value.nil?
21
+ raise FailedSpec, "the value at path '#{ path }' is missing"
22
+ end
23
+
24
+ unless @pred.call(value)
25
+ error_msg = if @error_msg
26
+ "'#{ value }' does not meet spec for '#{ path }': '#{ @error_msg }'"
27
+ else
28
+ "'#{ value }' does not meet spec for '#{ path }'"
29
+ end
30
+
31
+ raise FailedSpec, error_msg
32
+ end
33
+
34
+ value
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,44 @@
1
+ module Xpect
2
+ class Spect
3
+ def self.conform!(spec:, data:, path: [])
4
+ new.conform!(spec: spec, data: data, path: path)
5
+ end
6
+
7
+ def self.validate!(spec:, data:)
8
+ new.validate!(spec: spec, data: data)
9
+ end
10
+
11
+ # 1) Raise exception if spec isn't satisfied
12
+ # 2) Return original data
13
+ def validate!(spec:, data:)
14
+ call(spec: spec, data: data, init: data)
15
+ end
16
+
17
+ # 1) Raise exception if spec isn't satisfied
18
+ # 2) Return data as it adheres to the spec
19
+ def conform!(spec:, data:, path: [])
20
+ call(spec: spec, data: data, path: path)
21
+ end
22
+
23
+ private
24
+
25
+ def call(spec:, data:, path: [], init: {})
26
+ spec.reduce(init) do |memo, (key, value)|
27
+
28
+ unless data.is_a?(Hash)
29
+ raise(FailedSpec, "'#{ data }' is not equal to '#{ value }'")
30
+ end
31
+
32
+ path = path << key
33
+ data_value = data[key]
34
+ memo[key] = if !value.is_a?(Hash)
35
+ Xpect::Type.process(value, value, data_value, path)
36
+ else
37
+ call(spec: spec[key], data: data_value, path: path)
38
+ end
39
+
40
+ memo
41
+ end
42
+ end
43
+ end
44
+ end
data/lib/xpect/type.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Xpect
2
+ class Type
3
+ def self.process_array(iterable, data, path)
4
+ iterable.map.with_index do |spc, idx|
5
+ Type.process(spc, spc, data[idx], path)
6
+ end
7
+ end
8
+
9
+ def self.process(case_item, spec, val, path)
10
+ case case_item
11
+ when Xpect::Every
12
+ case_item.conform!(data: val, path: path)
13
+ when Array
14
+ unless val.is_a?(Array)
15
+ raise(FailedSpec, "'#{ val }' must be an array")
16
+ end
17
+
18
+ Type.process_array(spec, val, path)
19
+ when Hash
20
+ Xpect::Spect.conform!(spec: spec, data: val, path: path)
21
+ when Xpect::Pred, Xpect::Keys
22
+ spec.conform!(value: val, path: path)
23
+ when Proc
24
+ Xpect::EqualityHelpers.equal_with_proc?(spec, val, path)
25
+ val
26
+ else
27
+ Xpect::EqualityHelpers.equal?(spec, val, path)
28
+ val
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Xpect
2
+ VERSION = "0.1.0"
3
+ end
data/lib/xpect.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'xpect/version'
2
+ require 'xpect/matchers'
3
+ require 'xpect/failed_spec'
4
+ require 'xpect/pred'
5
+ require 'xpect/keys'
6
+ require 'xpect/equality_helpers'
7
+ require 'xpect/type'
8
+ require 'xpect/every'
9
+ require 'xpect/spect'
data/xpect.gemspec ADDED
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "xpect/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xpect"
8
+ spec.version = Xpect::VERSION
9
+ spec.authors = ["Travis Douce"]
10
+ spec.email = ["travisdouce@gmail.com"]
11
+
12
+ spec.summary = %q{Specs for Hashes.}
13
+ spec.description = %q{Specs for Hashes in the terms of Hashes.}
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "byebug"
36
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xpect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Travis Douce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Specs for Hashes in the terms of Hashes.
70
+ email:
71
+ - travisdouce@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/xpect.rb
88
+ - lib/xpect/equality_helpers.rb
89
+ - lib/xpect/every.rb
90
+ - lib/xpect/failed_spec.rb
91
+ - lib/xpect/keys.rb
92
+ - lib/xpect/matchers.rb
93
+ - lib/xpect/pred.rb
94
+ - lib/xpect/spect.rb
95
+ - lib/xpect/type.rb
96
+ - lib/xpect/version.rb
97
+ - xpect.gemspec
98
+ homepage:
99
+ licenses:
100
+ - MIT
101
+ metadata:
102
+ allowed_push_host: https://rubygems.org/
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.7.4
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Specs for Hashes.
123
+ test_files: []