mini_sanity 1.1.0 → 3.0.0

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.
@@ -0,0 +1,70 @@
1
+ require_relative "error"
2
+
3
+
4
+ class Enumerator
5
+
6
+ # Iterates the Enumerator with a given +block+, and checks that the
7
+ # result is an Enumerable that has one or more elements. Raises an
8
+ # exception if this check fails. Otherwise, returns the Enumerable
9
+ # result.
10
+ #
11
+ # @example
12
+ # [1, 2, 3].select.results!(&:odd?) # == [1, 3]
13
+ # [2, 4, 6].select.results!(&:odd?) # raises exception
14
+ #
15
+ # @return [Enumerable]
16
+ # @raise [ArgumentError]
17
+ # if no +block+ is provided
18
+ # @raise [MiniSanity::Error]
19
+ # if iterating does not result in an Enumerable, or if the resulting
20
+ # Enumerable has no elements
21
+ def results!(&block)
22
+ raise ArgumentError, "Enumerator#results! requires a block" unless block
23
+
24
+ results = self.each(&block)
25
+
26
+ if !results.is_a?(Enumerable)
27
+ raise MiniSanity::Error.new("Result from Enumerator with block is not an Enumerable", {
28
+ "Enumerator" => self.inspect,
29
+ "Block" => MiniSanity::Error.describe_block(&block),
30
+ "Result" => results.inspect,
31
+ })
32
+ elsif !results.any?{ true }
33
+ raise MiniSanity::Error.new("No results from Enumerator with block", {
34
+ "Enumerator" => self.inspect,
35
+ "Block" => MiniSanity::Error.describe_block(&block),
36
+ })
37
+ end
38
+
39
+ results
40
+ end
41
+
42
+ # Iterates the Enumerator with a given +block+, and checks that the
43
+ # result is not nil. Raises an exception if the result is nil.
44
+ # Otherwise, returns the non-nil result.
45
+ #
46
+ # @example
47
+ # [1, 2, 3].find.result!(&:even?) # == 2
48
+ # [1, 3, 5].find.result!(&:even?) # raises exception
49
+ #
50
+ # @return [Object]
51
+ # @raise [ArgumentError]
52
+ # if no +block+ is provided
53
+ # @raise [MiniSanity::Error]
54
+ # if iterating results in a nil value
55
+ def result!(&block)
56
+ raise ArgumentError, "Enumerator#result! requires a block" unless block
57
+
58
+ result = self.each(&block)
59
+
60
+ if result.nil?
61
+ raise MiniSanity::Error.new("Nil result from Enumerator with block", {
62
+ "Enumerator" => self.inspect,
63
+ "Block" => MiniSanity::Error.describe_block(&block),
64
+ })
65
+ end
66
+
67
+ result
68
+ end
69
+
70
+ end
@@ -1,3 +1,3 @@
1
1
  module MiniSanity
2
- VERSION = "1.1.0"
2
+ VERSION = "3.0.0"
3
3
  end
data/lib/mini_sanity.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require_relative "mini_sanity/version"
2
- require_relative "mini_sanity/error"
3
- require_relative "mini_sanity/array"
4
- require_relative "mini_sanity/enumerable"
5
- require_relative "mini_sanity/object"
6
- require_relative "mini_sanity/pathname"
7
- require_relative "mini_sanity/string"
2
+ require_relative "mini_sanity/assert"
3
+ require_relative "mini_sanity/change"
4
+ require_relative "mini_sanity/match"
5
+ require_relative "mini_sanity/results"
data/mini_sanity.gemspec CHANGED
@@ -1,27 +1,25 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "mini_sanity/version"
1
+ require_relative "lib/mini_sanity/version"
5
2
 
6
3
  Gem::Specification.new do |spec|
7
4
  spec.name = "mini_sanity"
8
5
  spec.version = MiniSanity::VERSION
9
6
  spec.authors = ["Jonathan Hefner"]
10
- spec.email = ["jonathan.hefner@gmail.com"]
7
+ spec.email = ["jonathan@hefner.pro"]
11
8
 
12
9
  spec.summary = %q{In-line sanity checks}
13
10
  spec.homepage = "https://github.com/jonathanhefner/mini_sanity"
14
11
  spec.license = "MIT"
12
+ spec.required_ruby_version = ">= 3.4"
15
13
 
16
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
- f.match(%r{^(test|spec|features)/})
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+ spec.metadata["changelog_uri"] = spec.metadata["source_code_uri"] + "/blob/master/CHANGELOG.md"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.start_with?("test/", ".git") }
18
21
  end
19
22
  spec.bindir = "exe"
20
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
24
  spec.require_paths = ["lib"]
22
-
23
- spec.add_development_dependency "bundler", "~> 1.15"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "minitest", "~> 5.0"
26
- spec.add_development_dependency "yard", "~> 0.9"
27
25
  end
metadata CHANGED
@@ -1,103 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_sanity
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Hefner
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2018-04-11 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.15'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.15'
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: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '5.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '5.0'
55
- - !ruby/object:Gem::Dependency
56
- name: yard
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.9'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.9'
69
- description:
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
70
12
  email:
71
- - jonathan.hefner@gmail.com
13
+ - jonathan@hefner.pro
72
14
  executables: []
73
15
  extensions: []
74
16
  extra_rdoc_files: []
75
17
  files:
76
- - ".gitignore"
77
- - ".travis.yml"
78
18
  - CHANGELOG.md
79
19
  - Gemfile
80
20
  - LICENSE.txt
81
21
  - README.md
82
22
  - Rakefile
83
23
  - lib/mini_sanity.rb
84
- - lib/mini_sanity/array.rb
85
- - lib/mini_sanity/enumerable.rb
24
+ - lib/mini_sanity/assert.rb
25
+ - lib/mini_sanity/change.rb
86
26
  - lib/mini_sanity/error.rb
87
- - lib/mini_sanity/object.rb
88
- - lib/mini_sanity/pathname.rb
89
- - lib/mini_sanity/string.rb
90
- - lib/mini_sanity/util.rb
91
- - lib/mini_sanity/util/enumerable.rb
92
- - lib/mini_sanity/util/regexp.rb
93
- - lib/mini_sanity/util/string.rb
27
+ - lib/mini_sanity/match.rb
28
+ - lib/mini_sanity/results.rb
94
29
  - lib/mini_sanity/version.rb
95
30
  - mini_sanity.gemspec
96
31
  homepage: https://github.com/jonathanhefner/mini_sanity
97
32
  licenses:
98
33
  - MIT
99
- metadata: {}
100
- post_install_message:
34
+ metadata:
35
+ source_code_uri: https://github.com/jonathanhefner/mini_sanity
36
+ changelog_uri: https://github.com/jonathanhefner/mini_sanity/blob/master/CHANGELOG.md
101
37
  rdoc_options: []
102
38
  require_paths:
103
39
  - lib
@@ -105,16 +41,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
41
  requirements:
106
42
  - - ">="
107
43
  - !ruby/object:Gem::Version
108
- version: '0'
44
+ version: '3.4'
109
45
  required_rubygems_version: !ruby/object:Gem::Requirement
110
46
  requirements:
111
47
  - - ">="
112
48
  - !ruby/object:Gem::Version
113
49
  version: '0'
114
50
  requirements: []
115
- rubyforge_project:
116
- rubygems_version: 2.6.13
117
- signing_key:
51
+ rubygems_version: 4.0.10
118
52
  specification_version: 4
119
53
  summary: In-line sanity checks
120
54
  test_files: []
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.2.5
5
- before_install: gem install bundler -v 1.15.1
@@ -1,63 +0,0 @@
1
- class Array
2
-
3
- # Checks that the Array matches a given length, and returns the Array
4
- # unmodified. If the Array fails this check, an exception is
5
- # raised.
6
- #
7
- # @example
8
- # coord = [0, 0, 0]
9
- # coord.assert_length(3)! # == [0, 0, 0]
10
- #
11
- # coord = [0, 0]
12
- # coord.assert_length(3)! # raises exception
13
- #
14
- # coord = [0, 0]
15
- # coord.assert_length(2..3)! # == [0, 0]
16
- #
17
- # @param length [Integer, Range<Integer>]
18
- # length to match
19
- # @param name [String, Symbol]
20
- # optional name to include in the error message
21
- # @return [self]
22
- # @raise [MiniSanity::Error]
23
- # if the Array length does not match +length+
24
- def assert_length!(length, name = nil)
25
- if !(length === self.length)
26
- raise MiniSanity::Error.new(name,
27
- "#{self.class} having #{length} elements",
28
- self.inspect)
29
- end
30
- self
31
- end
32
-
33
- # Checks that the Array does not match a given length, and returns the
34
- # Array unmodified. If the Array fails this check, an exception is
35
- # raised.
36
- #
37
- # @example
38
- # some = ["one"]
39
- # some.refute_length!(0) # == ["one"]
40
- #
41
- # some = []
42
- # some.refute_length(0)! # raises exception
43
- #
44
- # many = ["one", "many"]
45
- # many.refute_length!(0..1) # == ["one", "many"]
46
- #
47
- # @param length [Integer, Range<Integer>]
48
- # length to not match
49
- # @param name [String, Symbol]
50
- # optional name to include in the error message
51
- # @return [self]
52
- # @raise [MiniSanity::Error]
53
- # if the Array length matches +length+
54
- def refute_length!(length, name = nil)
55
- if length === self.length
56
- raise MiniSanity::Error.new(name,
57
- "#{self.class} not having #{length} elements",
58
- self.inspect)
59
- end
60
- self
61
- end
62
-
63
- end
@@ -1,52 +0,0 @@
1
- module Enumerable
2
-
3
- # Checks that the Enumerable is empty, and returns the Enumerable
4
- # unmodified. If the Enumerable fails this check, an exception is
5
- # raised.
6
- #
7
- # @example
8
- # errors = []
9
- # errors.assert_empty! # == []
10
- #
11
- # errors = ["something went wrong"]
12
- # errors.assert_empty! # raises exception
13
- #
14
- # @param name [String, Symbol]
15
- # optional name to include in the error message
16
- # @return [self]
17
- # @raise [MiniSanity::Error]
18
- # if the Enumerable is not empty
19
- def assert_empty!(name = nil)
20
- if self.any?{ true }
21
- raise MiniSanity::Error.new(name,
22
- "empty #{self.class}",
23
- self.inspect)
24
- end
25
- self
26
- end
27
-
28
- # Checks that the Enumerable is not empty, and returns the Enumerable
29
- # unmodified. If the Enumerable fails this check, an exception is
30
- # raised.
31
- #
32
- # @example
33
- # ["result 1"].refute_empty! # == ["result 1"]
34
- # [].refute_empty! # raises exception
35
- #
36
- # @param name [String, Symbol]
37
- # optional name to include in the error message
38
- # @return [self]
39
- # @raise [MiniSanity::Error]
40
- # if the Enumerable is empty
41
- def refute_empty!(name = nil)
42
- # NOTE use #any? instead of #none? because Array#none? seems to be
43
- # significantly slower than Array#any? (and likewise for Hash)
44
- if !self.any?{ true }
45
- raise MiniSanity::Error.new(name,
46
- "non-empty #{self.class}",
47
- self.inspect)
48
- end
49
- self
50
- end
51
-
52
- end
@@ -1,216 +0,0 @@
1
- class Object
2
-
3
- # Checks that the Object is nil, and returns nil. If the Object fails
4
- # this check, an exception is raised.
5
- #
6
- # @example
7
- # result = {}
8
- # result[:error].assert_nil! # == nil
9
- #
10
- # result = { error: "something went wrong" }
11
- # result[:error].assert_nil! # == raises exception
12
- #
13
- # @param name [String, Symbol]
14
- # optional name to include in the error message
15
- # @return [self]
16
- # @raise [MiniSanity::Error]
17
- # if the Object is not nil
18
- def assert_nil!(name = nil)
19
- if !self.nil?
20
- raise MiniSanity::Error.new(name,
21
- "nil",
22
- self.inspect)
23
- end
24
- self
25
- end
26
-
27
- # Checks that the Object is not nil, and returns the Object
28
- # unmodified. If the Object fails this check, an exception is raised.
29
- #
30
- # @example
31
- # ["result 1"].first.refute_nil! # == "result 1"
32
- # [].first.refute_nil! # raises exception
33
- #
34
- # @param name [String, Symbol]
35
- # optional name to include in the error message
36
- # @return [self]
37
- # @raise [MiniSanity::Error]
38
- # if the Object is nil
39
- def refute_nil!(name = nil)
40
- if self.nil?
41
- raise MiniSanity::Error.new(name,
42
- "non-nil value",
43
- "nil")
44
- end
45
- self
46
- end
47
-
48
- # Checks that the Object equals a given expected value, and returns
49
- # the Object unmodified. If the Object fails this check, an exception
50
- # is raised.
51
- #
52
- # @example
53
- # "good".assert_equal!("good") # == "good"
54
- # "bad".assert_equal!("good") # raises exception
55
- #
56
- # @param expect [Object]
57
- # value to expect
58
- # @param name [String, Symbol]
59
- # optional name to include in the error message
60
- # @return [self]
61
- # @raise [MiniSanity::Error]
62
- # if the Object does not equal +expect+
63
- def assert_equal!(expect, name = nil)
64
- if self != expect
65
- raise MiniSanity::Error.new(name,
66
- expect.inspect,
67
- self.inspect)
68
- end
69
- self
70
- end
71
-
72
- # Checks that the Object does not equal a given reject value, and
73
- # returns the Object unmodified. If the Object fails this check, an
74
- # exception is raised.
75
- #
76
- # @example
77
- # "good".refute_equal!("bad") # == "good"
78
- # "bad".refute_equal!("bad") # raises exception
79
- #
80
- # @param reject [Object]
81
- # value to reject
82
- # @param name [String, Symbol]
83
- # optional name to include in the error message
84
- # @return [self]
85
- # @raise [MiniSanity::Error]
86
- # if the Object equals +reject+
87
- def refute_equal!(reject, name = nil)
88
- if self == reject
89
- raise MiniSanity::Error.new(name,
90
- "not #{reject.inspect}",
91
- self.inspect)
92
- end
93
- self
94
- end
95
-
96
- # Checks that the Object is included in a given collection, and
97
- # returns the Object unmodified. If the Object fails this check, an
98
- # exception is raised.
99
- #
100
- # @example
101
- # "good".assert_in!(["ok", "good", "great"]) # == "good"
102
- # "bad".assert_in!(["ok", "good", "great"]) # raises exception
103
- #
104
- # @param permitted [Enumerable, #include?]
105
- # collection of permitted values
106
- # @param name [String, Symbol]
107
- # optional name to include in the error message
108
- # @return [self]
109
- # @raise [MiniSanity::Error]
110
- # if the Object is not included in +permitted+
111
- def assert_in!(permitted, name = nil)
112
- if !permitted.include?(self)
113
- raise MiniSanity::Error.new(name,
114
- "value included in #{permitted.inspect}",
115
- self.inspect)
116
- end
117
- self
118
- end
119
-
120
- # Checks that the Object is not included in a given collection, and
121
- # returns the Object unmodified. If the Object fails this check, an
122
- # exception is raised.
123
- #
124
- # @example
125
- # "good".refute_in!(["bad", "poor", "fail"]) # == "good"
126
- # "bad".refute_in!(["bad", "poor", "fail"]) # raises exception
127
- #
128
- # @param prohibited [Enumerable, #include?]
129
- # collection of prohibited values
130
- # @param name [String, Symbol]
131
- # optional name to include in the error message
132
- # @return [self]
133
- # @raise [MiniSanity::Error]
134
- # if the Object is included in +prohibited+
135
- def refute_in!(prohibited, name = nil)
136
- if prohibited.include?(self)
137
- raise MiniSanity::Error.new(name,
138
- "value not included in #{prohibited.inspect}",
139
- self.inspect)
140
- end
141
- self
142
- end
143
-
144
- # Checks that the Object is an instance of a given class, and returns
145
- # the Object unmodified. If the Object fails this check, an exception
146
- # is raised.
147
- #
148
- # @example
149
- # 123.assert_instance_of!(Numeric) # == 123
150
- # 123.assert_instance_of!(String) # raises exception
151
- #
152
- # @param klass [Class]
153
- # class to check
154
- # @param name [String, Symbol]
155
- # optional name to include in the error message
156
- # @return [self]
157
- # @raise [MiniSanity::Error]
158
- # if the Object is not an instance of +klass+
159
- def assert_instance_of!(klass, name = nil)
160
- if !self.instance_of?(klass)
161
- raise MiniSanity::Error.new(name,
162
- "instance of #{klass}",
163
- "instance of #{self.class}: #{self.inspect}")
164
- end
165
- self
166
- end
167
-
168
- # Checks that the Object is an instance of a given class or one of its
169
- # subclasses, and returns the Object unmodified. If the Object fails
170
- # this check, an exception is raised.
171
- #
172
- # @example
173
- # 123.assert_kind_of!(Numeric) # == 123
174
- # 123.assert_kind_of!(Float) # raises exception
175
- #
176
- # @param klass [Class]
177
- # class to check
178
- # @param name [String, Symbol]
179
- # optional name to include in the error message
180
- # @return [self]
181
- # @raise [MiniSanity::Error]
182
- # if the Object is not an instance of +klass+ or its subclasses
183
- def assert_kind_of!(klass, name = nil)
184
- if !self.kind_of?(klass)
185
- raise MiniSanity::Error.new(name,
186
- "instance of #{klass} or one of its subclasses",
187
- "instance of #{self.class}: #{self.inspect}")
188
- end
189
- self
190
- end
191
-
192
- # Checks that the Object responds to a given method, and returns the
193
- # Object unmodified. If the Object fails this check, an exception is
194
- # raised.
195
- #
196
- # @example
197
- # "abc".assert_respond_to!(:empty?) # == "abc"
198
- # "abc".assert_respond_to!(:pop) # raises exception
199
- #
200
- # @param method_name [String, Symbol]
201
- # name of method to check
202
- # @param name [String, Symbol]
203
- # optional name to include in the error message
204
- # @return [self]
205
- # @raise [MiniSanity::Error]
206
- # if the Object does not respond to +method_name+
207
- def assert_respond_to!(method_name, name = nil)
208
- if !self.respond_to?(method_name)
209
- raise MiniSanity::Error.new(name,
210
- "object responding to method #{method_name}",
211
- "instance of #{self.class}: #{self.inspect}")
212
- end
213
- self
214
- end
215
-
216
- end