mini_sanity 1.1.0 → 2.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.
@@ -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
@@ -1,135 +0,0 @@
1
- class Pathname
2
-
3
- # Checks that the Pathname represents an existing file or directory,
4
- # and returns the Pathname unmodified. If the Pathname fails this
5
- # check, an exception is raised.
6
- #
7
- # @example
8
- # Pathname.new(__FILE__).assert_exist! # == Pathname.new(__FILE__)
9
- # Pathname.new("/dev/null/nope").assert_exist! # == raises exception
10
- #
11
- # @param name [String, Symbol]
12
- # optional name to include in the error message
13
- # @return [self]
14
- # @raise [MiniSanity::Error]
15
- # if the Pathname does not represent an existing file or directory
16
- def assert_exist!(name = nil)
17
- if !self.exist?
18
- raise MiniSanity::Error.new(name,
19
- "existent file or directory",
20
- "#{self} does not exist")
21
- end
22
- self
23
- end
24
-
25
- # Checks that the Pathname does not represent an existing file or
26
- # directory, and returns the Pathname unmodified. If the Pathname
27
- # fails this check, an exception is raised.
28
- #
29
- # @example
30
- # Pathname.new("/dev/null/nope").refute_exist! # == Pathname.new("/dev/null/nope")
31
- # Pathname.new(__FILE__).refute_exist! # raises exception
32
- #
33
- # @param name [String, Symbol]
34
- # optional name to include in the error message
35
- # @return [self]
36
- # @raise [MiniSanity::Error]
37
- # if the Pathname represents an existing file or directory
38
- def refute_exist!(name = nil)
39
- if self.exist?
40
- raise MiniSanity::Error.new(name,
41
- "non-existent file or directory",
42
- "#{self} already exists")
43
- end
44
- self
45
- end
46
-
47
- # Checks that the Pathname represents an existing directory, and
48
- # returns the Pathname unmodified. If the Pathname fails this check,
49
- # an exception is raised.
50
- #
51
- # @example
52
- # Pathname.new(__dir__).assert_dir! # == Pathname.new(__dir__)
53
- # Pathname.new(__FILE__).assert_dir! # == raises exception
54
- #
55
- # @param name [String, Symbol]
56
- # optional name to include in the error message
57
- # @return [self]
58
- # @raise [MiniSanity::Error]
59
- # if the Pathname does not represent an existing directory
60
- def assert_dir!(name = nil)
61
- if !self.directory?
62
- raise MiniSanity::Error.new(name,
63
- "existent directory",
64
- "#{self} is not a directory")
65
- end
66
- self
67
- end
68
-
69
- # Checks that the Pathname does not represent an existing directory,
70
- # and returns the Pathname unmodified. If the Pathname fails this
71
- # check, an exception is raised.
72
- #
73
- # @example
74
- # Pathname.new(__FILE__).refute_dir! # == Pathname.new(__FILE__)
75
- # Pathname.new(__dir__).refute_dir! # raises exception
76
- #
77
- # @param name [String, Symbol]
78
- # optional name to include in the error message
79
- # @return [self]
80
- # @raise [MiniSanity::Error]
81
- # if the Pathname represents an existing directory
82
- def refute_dir!(name = nil)
83
- if self.directory?
84
- raise MiniSanity::Error.new(name,
85
- "not an existent directory",
86
- "#{self} is a directory")
87
- end
88
- self
89
- end
90
-
91
- # Checks that the Pathname represents an existing file, and returns
92
- # the Pathname unmodified. If the Pathname fails this check, an
93
- # exception is raised.
94
- #
95
- # @example
96
- # Pathname.new(__FILE__).assert_file! # == Pathname.new(__FILE__)
97
- # Pathname.new(__dir__).assert_file! # == raises exception
98
- #
99
- # @param name [String, Symbol]
100
- # optional name to include in the error message
101
- # @return [self]
102
- # @raise [MiniSanity::Error]
103
- # if the Pathname does not represent an existing file
104
- def assert_file!(name = nil)
105
- if !self.file?
106
- raise MiniSanity::Error.new(name,
107
- "existent file",
108
- "#{self} is not a file")
109
- end
110
- self
111
- end
112
-
113
- # Checks that the Pathname does not represent an existing file, and
114
- # returns the Pathname unmodified. If the Pathname fails this check,
115
- # an exception is raised.
116
- #
117
- # @example
118
- # Pathname.new(__dir__).refute_file! # == Pathname.new(__dir__)
119
- # Pathname.new(__FILE__).refute_file! # raises exception
120
- #
121
- # @param name [String, Symbol]
122
- # optional name to include in the error message
123
- # @return [self]
124
- # @raise [MiniSanity::Error]
125
- # if the Pathname represents an existing file
126
- def refute_file!(name = nil)
127
- if self.file?
128
- raise MiniSanity::Error.new(name,
129
- "not an existent file",
130
- "#{self} is a file")
131
- end
132
- self
133
- end
134
-
135
- end
@@ -1,143 +0,0 @@
1
- class String
2
-
3
- # Checks that the String is empty, and returns the String unmodified.
4
- # If the String fails this check, an exception is raised.
5
- #
6
- # @example
7
- # "".assert_empty! # == ""
8
- # "bad".assert_empty! # raises exception
9
- #
10
- # @param name [String, Symbol]
11
- # optional name to include in the error message
12
- # @return [self]
13
- # @raise [MiniSanity::Error]
14
- # if the String is not empty
15
- def assert_empty!(name = nil)
16
- if !self.empty?
17
- raise MiniSanity::Error.new(name,
18
- "empty #{self.class}",
19
- self.inspect)
20
- end
21
- self
22
- end
23
-
24
- # Checks that the String is not empty, and returns the String
25
- # unmodified. If the String fails this check, an exception is raised.
26
- #
27
- # @example
28
- # "result".refute_empty! # == "result"
29
- # "".refute_empty! # raises exception
30
- #
31
- # @param name [String, Symbol]
32
- # optional name to include in the error message
33
- # @return [self]
34
- # @raise [MiniSanity::Error]
35
- # if the String is empty
36
- def refute_empty!(name = nil)
37
- if self.empty?
38
- raise MiniSanity::Error.new(name,
39
- "non-empty #{self.class}",
40
- self.inspect)
41
- end
42
- self
43
- end
44
-
45
- # Checks that the String matches a given length, and returns the
46
- # String unmodified. If the String fails this check, an exception is
47
- # raised.
48
- #
49
- # @example
50
- # "password".assert_length!(8) # == "password"
51
- # "long password".assert_length!(8..64) # == "long password"
52
- # "pass".assert_length!(8..64) # == raises exception
53
- #
54
- # @param length [Integer, Range<Integer>]
55
- # length to match
56
- # @param name [String, Symbol]
57
- # optional name to include in the error message
58
- # @return [self]
59
- # @raise [MiniSanity::Error]
60
- # if the String length does not match +length+
61
- def assert_length!(length, name = nil)
62
- if !(length === self.length)
63
- raise MiniSanity::Error.new(name,
64
- "#{self.class} having #{length} characters",
65
- self.inspect)
66
- end
67
- self
68
- end
69
-
70
- # Checks that the String does not match a given length, and returns
71
- # the String unmodified. If the String fails this check, an exception
72
- # is raised.
73
- #
74
- # @example
75
- # "password".refute_length!(0) # == "password"
76
- # "long password".refute_length!(0...8) # == "long password"
77
- # "pass".refute_length!(0...8) # == raises exception
78
- #
79
- # @param length [Integer, Range<Integer>]
80
- # length to not match
81
- # @param name [String, Symbol]
82
- # optional name to include in the error message
83
- # @return [self]
84
- # @raise [MiniSanity::Error]
85
- # if the String length matches +length+
86
- def refute_length!(length, name = nil)
87
- if length === self.length
88
- raise MiniSanity::Error.new(name,
89
- "#{self.class} not having #{length} characters",
90
- self.inspect)
91
- end
92
- self
93
- end
94
-
95
- # Checks that the String matches a given regular expression, and
96
- # returns the String unmodified. If the String fails this check, an
97
- # exception is raised.
98
- #
99
- # @example
100
- # "good result".assert_match!(/^good/) # == "good result"
101
- # "bad result".assert_match!(/^good/) # raises exception
102
- #
103
- # @param regexp [Regexp]
104
- # regular expression to check
105
- # @param name [String, Symbol]
106
- # optional name to include in the error message
107
- # @return [self]
108
- # @raise [MiniSanity::Error]
109
- # if the String does not match +regexp+
110
- def assert_match!(regexp, name = nil)
111
- if regexp !~ self
112
- raise MiniSanity::Error.new(name,
113
- "#{self.class} matching #{regexp.inspect}",
114
- self.inspect)
115
- end
116
- self
117
- end
118
-
119
- # Checks that the String does not match a given regular expression,
120
- # and returns the String unmodified. If the String fails this check,
121
- # an exception is raised.
122
- #
123
- # @example
124
- # "good result".refute_match!(/^bad/) # == "good result"
125
- # "bad result".refute_match!(/^bad/) # raises exception
126
- #
127
- # @param regexp [Regexp]
128
- # regular expression to check
129
- # @param name [String, Symbol]
130
- # optional name to include in the error message
131
- # @return [self]
132
- # @raise [MiniSanity::Error]
133
- # if the String matches +regexp+
134
- def refute_match!(regexp, name = nil)
135
- if regexp =~ self
136
- raise MiniSanity::Error.new(name,
137
- "#{self.class} not matching #{regexp.inspect}",
138
- self.inspect)
139
- end
140
- self
141
- end
142
-
143
- end