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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +66 -25
- data/Gemfile +3 -0
- data/README.md +22 -69
- data/Rakefile +0 -14
- data/lib/mini_sanity/assert.rb +163 -0
- data/lib/mini_sanity/change.rb +107 -0
- data/lib/mini_sanity/error.rb +18 -8
- data/lib/mini_sanity/match.rb +57 -0
- data/lib/mini_sanity/results.rb +70 -0
- data/lib/mini_sanity/version.rb +1 -1
- data/lib/mini_sanity.rb +4 -6
- data/mini_sanity.gemspec +10 -12
- metadata +13 -79
- data/.gitignore +0 -9
- data/.travis.yml +0 -5
- data/lib/mini_sanity/array.rb +0 -63
- data/lib/mini_sanity/enumerable.rb +0 -52
- data/lib/mini_sanity/object.rb +0 -216
- data/lib/mini_sanity/pathname.rb +0 -135
- data/lib/mini_sanity/string.rb +0 -143
- data/lib/mini_sanity/util/enumerable.rb +0 -31
- data/lib/mini_sanity/util/regexp.rb +0 -27
- data/lib/mini_sanity/util/string.rb +0 -77
- data/lib/mini_sanity/util.rb +0 -3
data/lib/mini_sanity/pathname.rb
DELETED
|
@@ -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
|
data/lib/mini_sanity/string.rb
DELETED
|
@@ -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
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
module Enumerable
|
|
2
|
-
|
|
3
|
-
# Like {https://ruby-doc.org/core/Enumerable.html#method-i-first
|
|
4
|
-
# +Enumerable#first+}, but raises an exception if the Enumerable does
|
|
5
|
-
# not contain the requested number of elements.
|
|
6
|
-
#
|
|
7
|
-
# @example
|
|
8
|
-
# [7, 8, 9].first # == 7
|
|
9
|
-
# [7, 8, 9].first(1) # == [7]
|
|
10
|
-
# [7, 8, 9].first(2) # == [7, 8]
|
|
11
|
-
# [7, 8, 9].first(4) # raises exception
|
|
12
|
-
# [].first # raises exception
|
|
13
|
-
#
|
|
14
|
-
# @param n [Integer]
|
|
15
|
-
# requested number of elements
|
|
16
|
-
# @return [Object, Array]
|
|
17
|
-
# @raise [MiniSanity::Error]
|
|
18
|
-
# if the Enumerable does not contain the requested number of elements
|
|
19
|
-
def first!(n = nil)
|
|
20
|
-
result = n.nil? ? self.first : self.first(n)
|
|
21
|
-
|
|
22
|
-
if (result.nil? && !self.any?{ true }) || (!n.nil? && result.length < n)
|
|
23
|
-
raise MiniSanity::Error.new(nil,
|
|
24
|
-
"Enumerable having at least #{n || 1} elements",
|
|
25
|
-
self.inspect)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
result
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
class Regexp
|
|
2
|
-
|
|
3
|
-
# Like {https://ruby-doc.org/core/Regexp.html#method-i-match
|
|
4
|
-
# +Regexp#match+}, but raises an exception if the match fails.
|
|
5
|
-
#
|
|
6
|
-
# @example
|
|
7
|
-
# /^([^@]+)@(.+)$/.match!("user@example.com") # === MatchData
|
|
8
|
-
# /^([^@]+)@(.+)$/.match!("@user") # raises exception
|
|
9
|
-
#
|
|
10
|
-
# @param str [String]
|
|
11
|
-
# string to search
|
|
12
|
-
# @param pos [Integer]
|
|
13
|
-
# position in +str+ to begin the search
|
|
14
|
-
# @return [MatchData]
|
|
15
|
-
# @raise [MiniSanity::Error]
|
|
16
|
-
# if the Regexp does not match +str+
|
|
17
|
-
def match!(str, pos = 0)
|
|
18
|
-
result = self.match(str, pos)
|
|
19
|
-
if result.nil?
|
|
20
|
-
raise MiniSanity::Error.new(nil,
|
|
21
|
-
"String matching #{self.inspect}#{" from position #{pos}" if pos != 0}",
|
|
22
|
-
str.inspect)
|
|
23
|
-
end
|
|
24
|
-
result
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
end
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
class String
|
|
2
|
-
|
|
3
|
-
# Like {https://ruby-doc.org/core/String.html#method-i-match
|
|
4
|
-
# +String#match+}, but raises an exception if the match fails.
|
|
5
|
-
#
|
|
6
|
-
# @example
|
|
7
|
-
# "user@example.com".match!(/^([^@]+)@(.+)$/) # === MatchData
|
|
8
|
-
# "@user".match!(/^([^@]+)@(.+)$/) # raises exception
|
|
9
|
-
#
|
|
10
|
-
# @param pattern [Regexp]
|
|
11
|
-
# pattern to search for
|
|
12
|
-
# @param pos [Integer]
|
|
13
|
-
# position in the String to begin the search
|
|
14
|
-
# @return [MatchData]
|
|
15
|
-
# @raise [MiniSanity::Error]
|
|
16
|
-
# if +pattern+ does not match the String
|
|
17
|
-
def match!(pattern, pos = 0)
|
|
18
|
-
result = self.match(pattern, pos)
|
|
19
|
-
if result.nil?
|
|
20
|
-
raise MiniSanity::Error.new(nil,
|
|
21
|
-
"String matching #{pattern.inspect}#{" from position #{pos}" if pos != 0}",
|
|
22
|
-
self.inspect)
|
|
23
|
-
end
|
|
24
|
-
result
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
# Like {https://ruby-doc.org/core/String.html#method-i-sub-21
|
|
28
|
-
# +String#sub!+}, but raises an exception if no substitution was
|
|
29
|
-
# performed.
|
|
30
|
-
#
|
|
31
|
-
# @example
|
|
32
|
-
# "author: YOUR_NAME".change!(/\bYOUR_NAME\b/, "Me") # == "author: Me"
|
|
33
|
-
# "author: TODO".change!(/\bYOUR_NAME\b/, "Me") # raises exception
|
|
34
|
-
#
|
|
35
|
-
# @param pattern [Regexp, String]
|
|
36
|
-
# pattern to search for
|
|
37
|
-
# @param replacement [String, Hash, &block]
|
|
38
|
-
# substitution value (see +String#sub!+ documentation for full details)
|
|
39
|
-
# @return [String]
|
|
40
|
-
# @raise [MiniSanity::Error]
|
|
41
|
-
# if no substitution was performed
|
|
42
|
-
def change!(pattern, replacement = nil, &replacement_block)
|
|
43
|
-
result = if replacement
|
|
44
|
-
self.sub!(pattern, replacement)
|
|
45
|
-
else
|
|
46
|
-
self.sub!(pattern, &replacement_block)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
if !result
|
|
50
|
-
raise MiniSanity::Error.new(nil,
|
|
51
|
-
"String matching #{pattern.inspect}",
|
|
52
|
-
self.inspect)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
result
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# Like {https://ruby-doc.org/core/String.html#method-i-sub
|
|
59
|
-
# +String#sub+}, but raises an exception if no substitution was
|
|
60
|
-
# performed.
|
|
61
|
-
#
|
|
62
|
-
# @example
|
|
63
|
-
# "author: YOUR_NAME".change(/\bYOUR_NAME\b/, "Me") # == "author: Me"
|
|
64
|
-
# "author: TODO".change(/\bYOUR_NAME\b/, "Me") # raises exception
|
|
65
|
-
#
|
|
66
|
-
# @param pattern [Regexp, String]
|
|
67
|
-
# pattern to search for
|
|
68
|
-
# @param replacement [String, Hash, &block]
|
|
69
|
-
# substitution value (see +String#sub+ documentation for full details)
|
|
70
|
-
# @return [String]
|
|
71
|
-
# @raise [MiniSanity::Error]
|
|
72
|
-
# if no substitution was performed
|
|
73
|
-
def change(pattern, replacement = nil, &replacement_block)
|
|
74
|
-
self.dup.change!(pattern, replacement, &replacement_block)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
end
|
data/lib/mini_sanity/util.rb
DELETED