hana 1.2.0 → 1.2.1

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: b259b5422ec5303610246f9c0359dddab090f41d
4
- data.tar.gz: 0e05b0271035942c551969550f2d6fc549d6d9c1
3
+ metadata.gz: 1cbfce7020c1e6b550e0c4b08659f36ec2dcee8e
4
+ data.tar.gz: c3283388b7d72f50c74d66c693a154bfd3a223a4
5
5
  SHA512:
6
- metadata.gz: 4236a94452c5aa0672009813ab7082248fcd7a0be36bb23c372321da44134ff193f5beb6864a48383739e28eb430c8dd46d6533cbdbb89a0b10d8d5e65f78690
7
- data.tar.gz: 330794b059b22dc3d2a088ff95eba30445f36fa8c08f13f2f959b92c4b74fab7c037385160b0b67b9b2c7f4413f83e3b487596525f03e09e8739be0ba211e77c
6
+ metadata.gz: 944e3a00cbd11622b76d000cd28ba368338b4483e5827da783ea2a6d8cda46d7e378fb9c2eb654b328774b2f97d4d3504232cb879a97f297f633a58c688ce526
7
+ data.tar.gz: c890fff53ef18b8ff1b4df345749e2097106e3a636e6cc307a9bed7bd757e591447115561dbeb861030f254159a1d088777d58ab35f9668d990314f2e623b10a
@@ -8,5 +8,6 @@ test/helper.rb
8
8
  test/json-patch-tests/README.md
9
9
  test/json-patch-tests/spec_tests.json
10
10
  test/json-patch-tests/tests.json
11
+ test/mine.json
11
12
  test/test_hana.rb
12
13
  test/test_ietf.rb
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ Hoe.spec 'hana' do
13
13
  self.readme_file = 'README.md'
14
14
  self.history_file = 'CHANGELOG.rdoc'
15
15
  self.extra_rdoc_files = FileList['*.rdoc']
16
+ extra_dev_deps << ["minitest", "~> 5.0"]
16
17
  end
17
18
 
18
19
  # vim: syntax=ruby
@@ -1,5 +1,5 @@
1
1
  module Hana
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
 
4
4
  class Pointer
5
5
  include Enumerable
@@ -20,7 +20,7 @@ module Hana
20
20
  def self.eval list, object
21
21
  list.inject(object) { |o, part|
22
22
  if Array === o
23
- raise Patch::IndexException unless part =~ /\A\d+\Z/
23
+ raise Patch::IndexError unless part =~ /\A\d+\Z/
24
24
  part = part.to_i
25
25
  end
26
26
  o[part]
@@ -56,7 +56,10 @@ module Hana
56
56
  class ObjectOperationOnArrayException < Exception
57
57
  end
58
58
 
59
- class IndexException < Exception
59
+ class IndexError < Exception
60
+ end
61
+
62
+ class MissingTargetException < Exception
60
63
  end
61
64
 
62
65
  def initialize is
@@ -81,6 +84,8 @@ module Hana
81
84
  dest = Pointer.eval list, doc
82
85
  obj = ins['value']
83
86
 
87
+ raise(MissingTargetException, ins['path']) unless dest
88
+
84
89
  if key
85
90
  add_op dest, key, obj
86
91
  else
@@ -109,6 +114,7 @@ module Hana
109
114
  dest = Pointer.eval to, doc
110
115
 
111
116
  if Array === src
117
+ raise Patch::IndexError unless from_key =~ /\A\d+\Z/
112
118
  obj = src.fetch from_key.to_i
113
119
  else
114
120
  obj = src.fetch from_key
@@ -118,11 +124,7 @@ module Hana
118
124
  end
119
125
 
120
126
  def test ins, doc
121
- begin
122
- expected = Pointer.new(ins['path']).eval doc
123
- rescue Patch::IndexException
124
- raise FailedTestException.new(ins['value'], ins['path'])
125
- end
127
+ expected = Pointer.new(ins['path']).eval doc
126
128
 
127
129
  unless expected == ins['value']
128
130
  raise FailedTestException.new(ins['value'], ins['path'])
@@ -135,6 +137,7 @@ module Hana
135
137
  obj = Pointer.eval list, doc
136
138
 
137
139
  if Array === obj
140
+ raise Patch::IndexError unless key =~ /\A\d+\Z/
138
141
  obj[key.to_i] = ins['value']
139
142
  else
140
143
  obj[key] = ins['value']
@@ -167,6 +170,7 @@ module Hana
167
170
 
168
171
  def rm_op obj, key
169
172
  if Array === obj
173
+ raise Patch::IndexError unless key =~ /\A\d+\Z/
170
174
  obj.delete_at key.to_i
171
175
  else
172
176
  obj.delete key
@@ -1,7 +1,59 @@
1
1
  require 'minitest/autorun'
2
2
  require 'hana'
3
+ require 'json'
3
4
 
4
5
  module Hana
5
- class TestCase < MiniTest::Unit::TestCase
6
+ class TestCase < Minitest::Test
7
+ TESTDIR = File.dirname File.expand_path __FILE__
8
+
9
+ def self.read_test_json_file file
10
+ Module.new {
11
+ tests = JSON.load File.read file
12
+ tests.each_with_index do |test, i|
13
+ next unless test['doc']
14
+
15
+ define_method("test_#{test['comment'] || i }") do
16
+ skip "disabled" if test['disabled']
17
+
18
+ doc = test['doc']
19
+ patch = test['patch']
20
+
21
+ patch = Hana::Patch.new patch
22
+
23
+ if test['error']
24
+ assert_raises(*ex(test['error'])) do
25
+ patch.apply doc
26
+ end
27
+ else
28
+ assert_equal(test['expected'] || doc, patch.apply(doc))
29
+ end
30
+ end
31
+ end
32
+ }
33
+ end
34
+
35
+ def self.skip regexp, message
36
+ instance_methods.grep(regexp).each do |method|
37
+ define_method(method) { skip message }
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def ex msg
44
+ case msg
45
+ when /Out of bounds/i then [Hana::Patch::OutOfBoundsException]
46
+ when /Object operation on array/ then
47
+ [Hana::Patch::ObjectOperationOnArrayException]
48
+ when /test op shouldn't get array element/ then
49
+ [Hana::Patch::IndexError, Hana::Patch::ObjectOperationOnArrayException]
50
+ when /bad number$/ then
51
+ [Hana::Patch::IndexError, Hana::Patch::ObjectOperationOnArrayException]
52
+ when /missing|non-existant/ then
53
+ [Hana::Patch::MissingTargetException]
54
+ else
55
+ [Hana::Patch::FailedTestException]
56
+ end
57
+ end
6
58
  end
7
59
  end
@@ -0,0 +1,36 @@
1
+ [
2
+ { "comment": "test remove with bad number should fail",
3
+ "doc": {"foo": 1, "baz": [{"qux": "hello"}]},
4
+ "patch": [{"op": "remove", "path": "/baz/1e0/qux"}],
5
+ "error": "remove op shouldn't remove from array with bad number" },
6
+
7
+ { "comment": "test remove on array",
8
+ "doc": [1, 2, 3, 4],
9
+ "patch": [{"op": "remove", "path": "/0"}],
10
+ "expected": [2, 3, 4] },
11
+
12
+ { "comment": "test remove with bad index should fail",
13
+ "doc": [1, 2, 3, 4],
14
+ "patch": [{"op": "remove", "path": "/1e0"}],
15
+ "error": "remove op shouldn't remove from array with bad number" },
16
+
17
+ { "comment": "test replace with bad number should fail",
18
+ "doc": [""],
19
+ "patch": [{"op": "replace", "path": "/1e0", "value": false}],
20
+ "error": "replace op shouldn't replace in array with bad number" },
21
+
22
+ { "comment": "test copy with bad number should fail",
23
+ "doc": {"baz": [1,2,3], "bar": 1},
24
+ "patch": [{"op": "copy", "from": "/baz/1e0", "path": "/boo"}],
25
+ "error": "copy op shouldn't work with bad number" },
26
+
27
+ { "comment": "test move with bad number should fail",
28
+ "doc": {"foo": 1, "baz": [1,2,3,4]},
29
+ "patch": [{"op": "move", "from": "/baz/1e0", "path": "/foo"}],
30
+ "error": "move op shouldn't work with bad number" },
31
+
32
+ { "comment": "test add with bad number should fail",
33
+ "doc": ["foo", "sil"],
34
+ "patch": [{"op": "add", "path": "/1e0", "value": "bar"}],
35
+ "error": "add op shouldn't add to array with bad number" }
36
+ ]
@@ -1,42 +1,20 @@
1
1
  require 'helper'
2
- require 'json'
3
2
 
4
3
  module Hana
5
4
  class TestIETF < TestCase
6
- TESTDIR = File.dirname File.expand_path __FILE__
7
- json = File.read File.join TESTDIR, 'json-patch-tests', 'tests.json'
8
- tests = JSON.load json
9
- tests.each_with_index do |test, i|
10
- next unless test['doc']
11
-
12
- define_method("test_#{test['comment'] || i }") do
13
- skip "disabled" if test['disabled']
14
-
15
- doc = test['doc']
16
- patch = test['patch']
17
-
18
- patch = Hana::Patch.new patch
5
+ filename = File.join TESTDIR, 'json-patch-tests', 'tests.json'
6
+ include read_test_json_file filename
7
+ end
19
8
 
20
- if test['error']
21
- assert_raises(ex(test['error'])) do
22
- patch.apply doc
23
- end
24
- else
25
- assert_equal(test['expected'] || doc, patch.apply(doc))
26
- end
27
- end
28
- end
9
+ class TestSpec < TestCase
10
+ filename = File.join TESTDIR, 'json-patch-tests', 'spec_tests.json'
11
+ include read_test_json_file filename
29
12
 
30
- private
13
+ skip(/A\.13/, 'This test depends on the JSON parser')
14
+ end
31
15
 
32
- def ex msg
33
- case msg
34
- when /Out of bounds/i then Hana::Patch::OutOfBoundsException
35
- when /Object operation on array/ then
36
- Hana::Patch::ObjectOperationOnArrayException
37
- else
38
- Hana::Patch::FailedTestException
39
- end
40
- end
16
+ class MyTests < TestCase
17
+ filename = File.join TESTDIR, 'mine.json'
18
+ include read_test_json_file filename
41
19
  end
42
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -72,6 +72,7 @@ files:
72
72
  - test/json-patch-tests/README.md
73
73
  - test/json-patch-tests/spec_tests.json
74
74
  - test/json-patch-tests/tests.json
75
+ - test/mine.json
75
76
  - test/test_hana.rb
76
77
  - test/test_ietf.rb
77
78
  - .gemtest