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.
- checksums.yaml +5 -5
- data/.travis.yml +3 -3
- data/CHANGELOG.md +60 -24
- data/Gemfile +3 -0
- data/README.md +21 -58
- data/Rakefile +0 -14
- data/lib/mini_sanity.rb +4 -6
- 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/mini_sanity.gemspec +11 -12
- metadata +14 -73
- 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.rb +0 -3
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c8c35bb9700789272562d521aac498397bbc97c889147386fd9381787ea59f0
|
4
|
+
data.tar.gz: 8be2caee26ff3dd317f5cd7efe81c2c2628fcb7870f14d0b7e8fb4295a2b32fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d61148344efa1863fdf331cc9a2e42deec1e92e5d40d596b566592b3accc826f6042eeaa440340acd7e456e0ccffcef5c0fc5cc13cbf2e573cf9695d1befeb8e
|
7
|
+
data.tar.gz: bbf8dbfbb7c41a1cdd0084ab81a938e85a99d3271716d6fb2d67593a503ed026bc2439c3d489097c8fd0190c6741eed8c2ba56d197d2883e20be06904e04f728
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,30 +1,66 @@
|
|
1
|
-
##
|
1
|
+
## 2.0.0
|
2
|
+
|
3
|
+
* [BREAKING] Drop support for Ruby < 2.6
|
4
|
+
* Add `Object#assert!`
|
5
|
+
* Add `Object#refute!`
|
6
|
+
* [BREAKING] Remove methods obsoleted by `Object#assert!` and `#refute!`
|
7
|
+
* `Object#assert_equal!` => `assert!(expected *OR* Set[expected])`
|
8
|
+
* `Object#assert_in!` => `assert!(permitted.to_set)`
|
9
|
+
* `Object#assert_instance_of!` => `assert!(Set[klass], &:class)`
|
10
|
+
* `Object#assert_kind_of!` => `assert!(klass)`
|
11
|
+
* `Object#assert_nil!` => `assert!(nil)`
|
12
|
+
* `Object#refute_equal!` => `refute!(forbidden *OR* Set[forbidden])`
|
13
|
+
* `Object#refute_in!` => `refute!(prohibited.to_set)`
|
14
|
+
* `Object#refute_nil!` => `refute!(nil)`
|
15
|
+
* `Array#assert_length!` => `assert!(permitted_length, &:length)`
|
16
|
+
* `Array#refute_length!` => `refute!(prohibited_length, &:length)`
|
17
|
+
* `Enumerable#assert_empty!` => `assert!(&:empty?)`
|
18
|
+
* `Enumerable#refute_empty!` => `refute!(&:empty?)`
|
19
|
+
* `String#assert_empty!` => `assert!(&:empty?)`
|
20
|
+
* `String#assert_length!` => `assert!(permitted_length, &:length)`
|
21
|
+
* `String#assert_match!` => `assert!(pattern)`
|
22
|
+
* `String#refute_empty!` => `refute!(&:empty?)`
|
23
|
+
* `String#refute_length!` => `refute!(prohibited_length, &:length)`
|
24
|
+
* `String#refute_match!` => `refute!(pattern)`
|
25
|
+
* `Pathname#assert_dir!` => `assert!(&:directory?)`
|
26
|
+
* `Pathname#assert_exist!` => `assert!(&:exist?)`
|
27
|
+
* `Pathname#assert_file!` => `assert!(&:file?)`
|
28
|
+
* `Pathname#refute_dir!` => `refute!(&:directory?)`
|
29
|
+
* `Pathname#refute_exist!` => `refute!(&:exist?)`
|
30
|
+
* `Pathname#refute_file!` => `refute!(&:file?)`
|
31
|
+
* [BREAKING] Remove `Object#assert_respond_to!`
|
32
|
+
* [BREAKING] Remove `Enumerable#first!`
|
33
|
+
* Replacement for most cases: `assert!(n.., &:size).first(n)`
|
34
|
+
* [BREAKING] `require "mini_sanity"` now loads all pieces of the API.
|
35
|
+
However, individual pieces of the API can be loaded a la carte via
|
36
|
+
e.g. `require "mini_sanity/assert"`.
|
37
|
+
* Add `Enumerator#result!`
|
38
|
+
* Add `Enumerator#results!`
|
2
39
|
|
3
|
-
* Standardized exception error messages
|
4
40
|
|
5
|
-
|
6
|
-
* Array#assert_length!
|
7
|
-
* Array#refute_length!
|
8
|
-
* Enumerable#assert_empty!
|
9
|
-
* Object#assert_equal!
|
10
|
-
* Object#assert_in!
|
11
|
-
* Object#assert_nil!
|
12
|
-
* Object#refute_equal!
|
13
|
-
* Object#refute_in!
|
14
|
-
* Pathname#assert_dir!
|
15
|
-
* Pathname#assert_file!
|
16
|
-
* Pathname#refute_dir!
|
17
|
-
* Pathname#refute_file!
|
18
|
-
* String#assert_empty!
|
19
|
-
* String#assert_length!
|
20
|
-
* String#refute_length!
|
41
|
+
## 1.1.0
|
21
42
|
|
22
|
-
*
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
43
|
+
* Standardize error messages
|
44
|
+
* Add `Array#assert_length!`
|
45
|
+
* Add `Array#refute_length!`
|
46
|
+
* Add `Enumerable#assert_empty!`
|
47
|
+
* Add `Object#assert_equal!`
|
48
|
+
* Add `Object#assert_in!`
|
49
|
+
* Add `Object#assert_nil!`
|
50
|
+
* Add `Object#refute_equal!`
|
51
|
+
* Add `Object#refute_in!`
|
52
|
+
* Add `Pathname#assert_dir!`
|
53
|
+
* Add `Pathname#assert_file!`
|
54
|
+
* Add `Pathname#refute_dir!`
|
55
|
+
* Add `Pathname#refute_file!`
|
56
|
+
* Add `String#assert_empty!`
|
57
|
+
* Add `String#assert_length!`
|
58
|
+
* Add `String#refute_length!`
|
59
|
+
* Add `Enumerable#first!` (loaded via `require "mini_sanity/util"`)
|
60
|
+
* Add `Regexp#match!` (loaded via `require "mini_sanity/util"`)
|
61
|
+
* Add `String#change` (loaded via `require "mini_sanity/util"`)
|
62
|
+
* Add `String#change!` (loaded via `require "mini_sanity/util"`)
|
63
|
+
* Add `String#match!` (loaded via `require "mini_sanity/util"`)
|
28
64
|
|
29
65
|
|
30
66
|
## 1.0.0
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# mini_sanity
|
1
|
+
# mini_sanity [](https://travis-ci.org/jonathanhefner/mini_sanity)
|
2
2
|
|
3
3
|
In-line [sanity checks], written as extensions to core Ruby objects.
|
4
4
|
See API listing below, or browse the [full documentation].
|
5
5
|
|
6
6
|
[sanity checks]: https://en.wikipedia.org/wiki/Sanity_check
|
7
|
-
[full documentation]:
|
7
|
+
[full documentation]: https://www.rubydoc.info/gems/mini_sanity/
|
8
8
|
|
9
9
|
|
10
10
|
## Example
|
@@ -13,91 +13,54 @@ See API listing below, or browse the [full documentation].
|
|
13
13
|
require "json"
|
14
14
|
require "pathname"
|
15
15
|
|
16
|
-
path = Pathname.new("hosted_files.json").
|
16
|
+
path = Pathname.new("hosted_files.json").assert!(&:exist?)
|
17
17
|
|
18
|
-
hosted_files = JSON.parse(path.read).
|
18
|
+
hosted_files = JSON.parse(path.read).assert!(Array)
|
19
19
|
|
20
20
|
urls = hosted_files.flat_map do |file_info|
|
21
|
-
file_info.fetch("mirror_urls").
|
21
|
+
file_info.fetch("mirror_urls").refute!(&:empty?)
|
22
22
|
end
|
23
23
|
|
24
24
|
domains = urls.map do |url|
|
25
|
-
url.
|
25
|
+
url.assert!(%r"^https?://").split("/")[2]
|
26
26
|
end.uniq
|
27
27
|
```
|
28
28
|
|
29
29
|
|
30
30
|
## API
|
31
31
|
|
32
|
-
-
|
33
|
-
- [#
|
34
|
-
- [#
|
35
|
-
|
36
|
-
- [#
|
37
|
-
- [#
|
38
|
-
|
39
|
-
- [#
|
40
|
-
- [#
|
41
|
-
|
42
|
-
- [
|
43
|
-
- [#
|
44
|
-
- [#refute_length!](http://www.rubydoc.info/gems/mini_sanity/Array:refute_length%21)
|
45
|
-
- [Enumerable](http://www.rubydoc.info/gems/mini_sanity/Enumerable)
|
46
|
-
- [#assert_empty!](http://www.rubydoc.info/gems/mini_sanity/Enumerable:assert_empty%21)
|
47
|
-
- [#refute_empty!](http://www.rubydoc.info/gems/mini_sanity/Enumerable:refute_empty%21)
|
48
|
-
- [String](http://www.rubydoc.info/gems/mini_sanity/String)
|
49
|
-
- [#assert_empty!](http://www.rubydoc.info/gems/mini_sanity/String:assert_empty%21)
|
50
|
-
- [#assert_length!](http://www.rubydoc.info/gems/mini_sanity/String:assert_length%21)
|
51
|
-
- [#assert_match!](http://www.rubydoc.info/gems/mini_sanity/String:assert_match%21)
|
52
|
-
- [#refute_empty!](http://www.rubydoc.info/gems/mini_sanity/String:refute_empty%21)
|
53
|
-
- [#refute_length!](http://www.rubydoc.info/gems/mini_sanity/String:refute_length%21)
|
54
|
-
- [#refute_match!](http://www.rubydoc.info/gems/mini_sanity/String:refute_match%21)
|
55
|
-
- [Pathname](http://www.rubydoc.info/gems/mini_sanity/Pathname)
|
56
|
-
- [#assert_dir!](http://www.rubydoc.info/gems/mini_sanity/Pathname:assert_dir%21)
|
57
|
-
- [#assert_exist!](http://www.rubydoc.info/gems/mini_sanity/Pathname:assert_exist%21)
|
58
|
-
- [#assert_file!](http://www.rubydoc.info/gems/mini_sanity/Pathname:assert_file%21)
|
59
|
-
- [#refute_dir!](http://www.rubydoc.info/gems/mini_sanity/Pathname:refute_dir%21)
|
60
|
-
- [#refute_exist!](http://www.rubydoc.info/gems/mini_sanity/Pathname:refute_exist%21)
|
61
|
-
- [#refute_file!](http://www.rubydoc.info/gems/mini_sanity/Pathname:refute_file%21)
|
62
|
-
|
63
|
-
|
64
|
-
## Util API
|
65
|
-
|
66
|
-
*mini_sanity* also includes a few optional utility methods which perform
|
67
|
-
some function, check an assertion on the result, and raise an error with
|
68
|
-
a helpful message if the assertion fails. You must add
|
69
|
-
`require "mini_sanity/util"` to your script to access these methods.
|
70
|
-
|
71
|
-
- [Enumerable](http://www.rubydoc.info/gems/mini_sanity/Enumerable)
|
72
|
-
- [#first!](http://www.rubydoc.info/gems/mini_sanity/Enumerable:first%21)
|
73
|
-
- [Regexp](http://www.rubydoc.info/gems/mini_sanity/Regexp)
|
74
|
-
- [#match!](http://www.rubydoc.info/gems/mini_sanity/Regexp:match%21)
|
75
|
-
- [String](http://www.rubydoc.info/gems/mini_sanity/String)
|
76
|
-
- [#change](http://www.rubydoc.info/gems/mini_sanity/String:change)
|
77
|
-
- [#change!](http://www.rubydoc.info/gems/mini_sanity/String:change%21)
|
78
|
-
- [#match!](http://www.rubydoc.info/gems/mini_sanity/String:match%21)
|
32
|
+
- `"mini_sanity/assert"`
|
33
|
+
- [Object#assert!](https://www.rubydoc.info/gems/mini_sanity/Object:assert%21)
|
34
|
+
- [Object#refute!](https://www.rubydoc.info/gems/mini_sanity/Object:refute%21)
|
35
|
+
- `"mini_sanity/change"`
|
36
|
+
- [String#change](https://www.rubydoc.info/gems/mini_sanity/String:change)
|
37
|
+
- [String#change!](https://www.rubydoc.info/gems/mini_sanity/String:change%21)
|
38
|
+
- `"mini_sanity/match"`
|
39
|
+
- [Regexp#match!](https://www.rubydoc.info/gems/mini_sanity/Regexp:match%21)
|
40
|
+
- [String#match!](https://www.rubydoc.info/gems/mini_sanity/String:match%21)
|
41
|
+
- `"mini_sanity/results"`
|
42
|
+
- [Enumerator#result!](https://www.rubydoc.info/gems/mini_sanity/Enumerator:result%21)
|
43
|
+
- [Enumerator#results!](https://www.rubydoc.info/gems/mini_sanity/Enumerator:results%21)
|
79
44
|
|
80
45
|
|
81
46
|
## Installation
|
82
47
|
|
83
|
-
Install
|
48
|
+
Install the [gem](https://rubygems.org/gems/mini_sanity):
|
84
49
|
|
85
50
|
```bash
|
86
51
|
$ gem install mini_sanity
|
87
52
|
```
|
88
53
|
|
89
|
-
Then require in your Ruby
|
54
|
+
Then require in your Ruby code:
|
90
55
|
|
91
56
|
```ruby
|
92
57
|
require "mini_sanity"
|
93
|
-
require "mini_sanity/util" # OPTIONAL
|
94
58
|
```
|
95
59
|
|
96
60
|
|
97
61
|
## Contributing
|
98
62
|
|
99
|
-
Run `rake test` to run the tests.
|
100
|
-
interactive prompt that pre-loads the project code.
|
63
|
+
Run `rake test` to run the tests.
|
101
64
|
|
102
65
|
|
103
66
|
## License
|
data/Rakefile
CHANGED
@@ -1,19 +1,5 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rake/testtask"
|
3
|
-
require "yard"
|
4
|
-
|
5
|
-
|
6
|
-
YARD::Rake::YardocTask.new(:doc) do |t|
|
7
|
-
end
|
8
|
-
|
9
|
-
desc "Launch IRB with this gem pre-loaded"
|
10
|
-
task :irb do
|
11
|
-
require "mini_sanity"
|
12
|
-
require "mini_sanity/util"
|
13
|
-
require "irb"
|
14
|
-
ARGV.clear
|
15
|
-
IRB.start
|
16
|
-
end
|
17
3
|
|
18
4
|
Rake::TestTask.new(:test) do |t|
|
19
5
|
t.libs << "test"
|
data/lib/mini_sanity.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require_relative "mini_sanity/version"
|
2
|
-
require_relative "mini_sanity/
|
3
|
-
require_relative "mini_sanity/
|
4
|
-
require_relative "mini_sanity/
|
5
|
-
require_relative "mini_sanity/
|
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"
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require_relative "error"
|
2
|
+
|
3
|
+
|
4
|
+
module MiniSanity
|
5
|
+
# Matches truthy values (i.e. not +false+ or +nil+).
|
6
|
+
TRUTHY = ->(x){ !!x }
|
7
|
+
|
8
|
+
def TRUTHY.inspect
|
9
|
+
"MiniSanity::TRUTHY"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class Object
|
15
|
+
|
16
|
+
# Checks that a given +pattern+ matches the Object (or a derivative
|
17
|
+
# value), and returns the Object. Raises an exception if the pattern
|
18
|
+
# does not match.
|
19
|
+
#
|
20
|
+
# If a block is given, the Object is yielded to the block, and the
|
21
|
+
# derivative value returned by the block is checked instead.
|
22
|
+
#
|
23
|
+
# @example Match truthy
|
24
|
+
# "foo".assert! # == "foo"
|
25
|
+
# nil.assert! # raises exception
|
26
|
+
#
|
27
|
+
# @example Match expected value
|
28
|
+
# "foo".assert!("foo") # == "foo"
|
29
|
+
# "bar".assert!("foo") # raises exception
|
30
|
+
#
|
31
|
+
# @example Match Set of permitted values
|
32
|
+
# "foo".assert!(Set["foo", "bar"]) # == "foo"
|
33
|
+
# "bar".assert!(Set["foo", "bar"]) # == "bar"
|
34
|
+
# "baz".assert!(Set["foo", "bar"]) # raises exception
|
35
|
+
#
|
36
|
+
# @example Match Class
|
37
|
+
# 25.assert!(Integer) # == 25
|
38
|
+
# 2.5.assert!(Integer) # raises exception
|
39
|
+
#
|
40
|
+
# @example Match Regexp
|
41
|
+
# "foo".assert!(/^f/) # == "foo"
|
42
|
+
# "bar".assert!(/^f/) # raises exception
|
43
|
+
#
|
44
|
+
# @example Match Range
|
45
|
+
# 2.assert!(1..4) # == 2
|
46
|
+
# 5.assert!(1..4) # raises exception
|
47
|
+
#
|
48
|
+
# @example Match truthy derivative value
|
49
|
+
# [2, 5].assert!(&:any?) # == [2, 5]
|
50
|
+
# [nil].assert!(&:any?) # raises exception
|
51
|
+
#
|
52
|
+
# @example Match derivative value
|
53
|
+
# [2, 5].assert!(1.., &:length) # == [2, 5]
|
54
|
+
# [].assert!(1.., &:length) # raises exception
|
55
|
+
#
|
56
|
+
# @overload assert!(pattern = MiniSanity::TRUTHY, hint: nil)
|
57
|
+
# @param pattern [#===]
|
58
|
+
# @param hint [String]
|
59
|
+
# Hint to include in the error message
|
60
|
+
# @return [self]
|
61
|
+
# @raise [MiniSanity::Error]
|
62
|
+
# if +pattern+ does not match the Object
|
63
|
+
#
|
64
|
+
# @overload assert!(pattern = MiniSanity::TRUTHY, hint: nil, &block)
|
65
|
+
# @param pattern [#===]
|
66
|
+
# @param hint [String]
|
67
|
+
# Hint to include in the error message
|
68
|
+
# @yieldparam itself [self]
|
69
|
+
# @yieldreturn [Object]
|
70
|
+
# Derivative value
|
71
|
+
# @return [self]
|
72
|
+
# @raise [MiniSanity::Error]
|
73
|
+
# if +pattern+ does not match the value returned by +block+
|
74
|
+
def assert!(pattern = MiniSanity::TRUTHY, hint: nil, &block)
|
75
|
+
result = block ? block.call(self) : self
|
76
|
+
|
77
|
+
unless pattern === result
|
78
|
+
raise MiniSanity::Error.new("Assert failed", {
|
79
|
+
"Value" => self.inspect,
|
80
|
+
"Derived value (from #{MiniSanity::Error.describe_block(&block) || "block"})" =>
|
81
|
+
(result.inspect if block),
|
82
|
+
"Assert matches" => pattern.inspect,
|
83
|
+
"Hint" => hint,
|
84
|
+
})
|
85
|
+
end
|
86
|
+
|
87
|
+
self
|
88
|
+
end
|
89
|
+
|
90
|
+
# Checks that a given +pattern+ does not match the Object (or a
|
91
|
+
# derivative value), and returns the Object. Raises an exception if
|
92
|
+
# the pattern matches.
|
93
|
+
#
|
94
|
+
# If a block is given, the Object is yielded to the block, and the
|
95
|
+
# derivative value returned by the block is checked instead.
|
96
|
+
#
|
97
|
+
# @example Refute truthy
|
98
|
+
# false.refute! # == false
|
99
|
+
# "bad".refute! # raises exception
|
100
|
+
#
|
101
|
+
# @example Refute forbidden value
|
102
|
+
# "foo".refute!("bad") # == "foo"
|
103
|
+
# "bad".refute!("bad") # raises exception
|
104
|
+
#
|
105
|
+
# @example Refute Set of prohibited values
|
106
|
+
# "foo".refute!(Set["bad", "worse"]) # == "foo"
|
107
|
+
# "bad".refute!(Set["bad", "worse"]) # raises exception
|
108
|
+
#
|
109
|
+
# @example Refute Class
|
110
|
+
# 25.refute!(Float) # == 25
|
111
|
+
# 2.5.refute!(Float) # raises exception
|
112
|
+
#
|
113
|
+
# @example Refute Regexp
|
114
|
+
# "foo".refute!(/^ba/) # == "foo"
|
115
|
+
# "bad".refute!(/^ba/) # raises exception
|
116
|
+
#
|
117
|
+
# @example Refute Range
|
118
|
+
# 2.refute!(5..) # == 2
|
119
|
+
# 5.refute!(5..) # raises exception
|
120
|
+
#
|
121
|
+
# @example Refute truthy derivative value
|
122
|
+
# [2].refute!(&:empty?) # == [2]
|
123
|
+
# [].refute!(&:empty?) # raises exception
|
124
|
+
#
|
125
|
+
# @example Refute derivative value
|
126
|
+
# [2].refute!(1.., &:length) # == [2]
|
127
|
+
# [2, 5].refute!(1.., &:length) # raises exception
|
128
|
+
#
|
129
|
+
# @overload refute!(pattern = MiniSanity::TRUTHY, hint: nil)
|
130
|
+
# @param pattern [#===]
|
131
|
+
# @param hint [String]
|
132
|
+
# Hint to include in the error message
|
133
|
+
# @return [self]
|
134
|
+
# @raise [MiniSanity::Error]
|
135
|
+
# if +pattern+ matches the Object
|
136
|
+
#
|
137
|
+
# @overload refute!(pattern = MiniSanity::TRUTHY, hint: nil, &block)
|
138
|
+
# @param pattern [#===]
|
139
|
+
# @param hint [String]
|
140
|
+
# Hint to include in the error message
|
141
|
+
# @yieldparam itself [self]
|
142
|
+
# @yieldreturn [Object]
|
143
|
+
# Derivative value
|
144
|
+
# @return [self]
|
145
|
+
# @raise [MiniSanity::Error]
|
146
|
+
# if +pattern+ matches the value returned by +block+
|
147
|
+
def refute!(pattern = MiniSanity::TRUTHY, hint: nil, &block)
|
148
|
+
result = block ? block.call(self) : self
|
149
|
+
|
150
|
+
if pattern === result
|
151
|
+
raise MiniSanity::Error.new("Refute failed", {
|
152
|
+
"Value" => self.inspect,
|
153
|
+
"Derived value (from #{MiniSanity::Error.describe_block(&block) || "block"})" =>
|
154
|
+
(result.inspect if block),
|
155
|
+
"Refute matches" => pattern.inspect,
|
156
|
+
"Hint" => hint,
|
157
|
+
})
|
158
|
+
end
|
159
|
+
|
160
|
+
self
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require_relative "error"
|
2
|
+
|
3
|
+
|
4
|
+
class String
|
5
|
+
|
6
|
+
# Like {https://docs.ruby-lang.org/en/master/String.html#method-i-sub-21
|
7
|
+
# +String#sub!+}, but raises an exception if no substitution is
|
8
|
+
# performed.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# info = "library: LIB_NAME\nlanguage: LIB_LANG\n"
|
12
|
+
# info.change!(/\bLIB_NAME\b/, "mini_sanity") # == "library: mini_sanity\nlanguage: LIB_LANG\n"
|
13
|
+
# info # == "library: mini_sanity\nlanguage: LIB_LANG\n"
|
14
|
+
# info.change!(/\bLIB_LANGUAGE\b/, "Ruby") # raises exception
|
15
|
+
#
|
16
|
+
# @overload change!(pattern, replacement)
|
17
|
+
# @param pattern [Regexp, String]
|
18
|
+
# Pattern to search for
|
19
|
+
# @param replacement [String]
|
20
|
+
# Replacement String (see +String#sub+ documentation for more
|
21
|
+
# information)
|
22
|
+
# @return [String]
|
23
|
+
# @raise [MiniSanity::Error]
|
24
|
+
# if no substitution was performed
|
25
|
+
#
|
26
|
+
# @overload change!(pattern, hash)
|
27
|
+
# @param pattern [Regexp, String]
|
28
|
+
# Pattern to search for
|
29
|
+
# @param hash [Hash]
|
30
|
+
# Substitution Hash (see +String#sub+ documentation for more
|
31
|
+
# information)
|
32
|
+
# @return [String]
|
33
|
+
# @raise [MiniSanity::Error]
|
34
|
+
# if no substitution was performed
|
35
|
+
#
|
36
|
+
# @overload change!(pattern, &block)
|
37
|
+
# @param pattern [Regexp, String]
|
38
|
+
# Pattern to search for
|
39
|
+
# @yieldparam match [String]
|
40
|
+
# Matched String
|
41
|
+
# @yieldreturn [String]
|
42
|
+
# Replacement String
|
43
|
+
# @return [String]
|
44
|
+
# @raise [MiniSanity::Error]
|
45
|
+
# if no substitution was performed
|
46
|
+
def change!(pattern, replacement = nil, &block)
|
47
|
+
result = if replacement
|
48
|
+
self.sub!(pattern, replacement)
|
49
|
+
else
|
50
|
+
self.sub!(pattern, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
if !result
|
54
|
+
raise MiniSanity::Error.new("String does not match pattern", {
|
55
|
+
"String" => self.inspect,
|
56
|
+
"Pattern" => pattern.inspect,
|
57
|
+
})
|
58
|
+
end
|
59
|
+
|
60
|
+
result
|
61
|
+
end
|
62
|
+
|
63
|
+
# Like {https://docs.ruby-lang.org/en/master/String.html#method-i-sub
|
64
|
+
# +String#sub+}, but raises an exception if no substitution is
|
65
|
+
# performed.
|
66
|
+
#
|
67
|
+
# @example
|
68
|
+
# info = "library: LIB_NAME\nlanguage: LIB_LANG\n"
|
69
|
+
# info.change(/\bLIB_NAME\b/, "mini_sanity") # == "library: mini_sanity\nlanguage: LIB_LANG\n"
|
70
|
+
# info # == "library: LIB_NAME\nlanguage: LIB_LANG\n"
|
71
|
+
# info.change(/\bLIB_LANGUAGE\b/, "Ruby") # raises exception
|
72
|
+
#
|
73
|
+
# @overload change(pattern, replacement)
|
74
|
+
# @param pattern [Regexp, String]
|
75
|
+
# Pattern to search for
|
76
|
+
# @param replacement [String]
|
77
|
+
# Replacement String (see +String#sub+ documentation for more
|
78
|
+
# information)
|
79
|
+
# @return [String]
|
80
|
+
# @raise [MiniSanity::Error]
|
81
|
+
# if no substitution was performed
|
82
|
+
#
|
83
|
+
# @overload change(pattern, hash)
|
84
|
+
# @param pattern [Regexp, String]
|
85
|
+
# Pattern to search for
|
86
|
+
# @param hash [Hash]
|
87
|
+
# Substitution Hash (see +String#sub+ documentation for more
|
88
|
+
# information)
|
89
|
+
# @return [String]
|
90
|
+
# @raise [MiniSanity::Error]
|
91
|
+
# if no substitution was performed
|
92
|
+
#
|
93
|
+
# @overload change(pattern, &block)
|
94
|
+
# @param pattern [Regexp, String]
|
95
|
+
# Pattern to search for
|
96
|
+
# @yieldparam match [String]
|
97
|
+
# Matched String
|
98
|
+
# @yieldreturn [String]
|
99
|
+
# Replacement String
|
100
|
+
# @return [String]
|
101
|
+
# @raise [MiniSanity::Error]
|
102
|
+
# if no substitution was performed
|
103
|
+
def change(pattern, replacement = nil, &block)
|
104
|
+
self.dup.change!(pattern, replacement, &block)
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|