standard_assert 0.2.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3316ab8f33b749374cc006c40658940915e7098c827fd20ca7b2229429b205d4
4
- data.tar.gz: 6944543a4b9271ed2e7156d5f5f0bdd2d6a09842571e33dd3cdb8092013c6799
3
+ metadata.gz: 40f8957293fc716888d3893b998e99667ee02b8c2dfde688138eb921d19f4b84
4
+ data.tar.gz: 7cac84248babb63c2c0d9abfd9756820a8a8afd956360b2368edd3372cf3235f
5
5
  SHA512:
6
- metadata.gz: 939984e99601d6dfb8ee2ef1a72cd0ac76e4a54c6f2437bdde9c0d8b7e8881b02942059de9fc3a705c434a6e13ad8c8855494d491afe2fd42df75788f18b795a
7
- data.tar.gz: 045e55acb59fd7a97717b1776842cf8312d272b5a1c89f81424ae28d2069bacc4a8e3855677e86e2770f103fb71b693e1efd32ab182c4e5e5da56a7c380aea82
6
+ metadata.gz: 34e02026e2f5397ba6b9fea8728a2c95c65a741a314743a6d86bf7a558e9434e956158ac7ca99ecb729cdea9f5eb4040672043a9353ef017ed15a8dd09b8cf0a
7
+ data.tar.gz: 503ac256dbe5da0510e4b4a2e97469a11f9b2b2a98a8aeced91d08994ba6e5ecc9d11b5173e8d0797ef95b5875197f63093b638babd759c6d0527c3d1234a0b8
data/CHANGELOG.md CHANGED
@@ -1,11 +1,19 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## [0.3.0](https://github.com/yasaichi/standard_assert/releases/tag/v0.3.0) (December 21, 2019)
4
+
5
+ ### Breaking changes
6
+
7
+ - [Publish only `assert` and `assert_*` methods](https://github.com/yasaichi/standard_assert/pull/3)
8
+ - [Publish the assertion methods as module functions](https://github.com/yasaichi/standard_assert/pull/4)
9
+ - [Migrate the assertion methods' backend to `minitest`](https://github.com/yasaichi/standard_assert/pull/5)
10
+
3
11
  ## [0.2.1](https://github.com/yasaichi/standard_assert/releases/tag/v0.2.1) (December 14, 2019)
4
12
 
5
13
  ### Compatible changes
6
14
 
7
- - [Migration testing framework](https://github.com/yasaichi/standard_assert/commit/7d7f692234a099a85a9d79a0cd9b15047963eefc)
8
15
  - [Apply RuboCop autofix](https://github.com/yasaichi/standard_assert/commit/a7f0ddf984250fcec588a41b84c380b932af2a5f)
16
+ - [Migration testing framework](https://github.com/yasaichi/standard_assert/commit/7d7f692234a099a85a9d79a0cd9b15047963eefc)
9
17
 
10
18
  ## [0.2.0](https://github.com/yasaichi/standard_assert/releases/tag/v0.2.0) (December 14, 2019)
11
19
 
data/README.md CHANGED
@@ -24,28 +24,51 @@ $ bundle
24
24
 
25
25
  ## Usage
26
26
 
27
- Use `Assert` module with `extend` or `include` in classes or modules where you want to use assertion methods:
27
+ Since assertion methods are defined as module functions of `Assert`, you can use them as instance methods
28
+ to any classes that mix in the module:
28
29
 
29
30
  ```ruby
30
31
  require 'standard_assert'
31
32
 
32
- module MyMath
33
- extend ::Assert
33
+ class Stack
34
+ include ::Assert
34
35
 
35
- module_function
36
+ def initialize
37
+ @store = []
38
+ end
39
+
40
+ def pop
41
+ assert(!@store.empty?)
42
+ @store.pop
43
+ end
44
+
45
+ def push(element)
46
+ @store.push(element)
47
+ self
48
+ end
49
+ end
36
50
 
37
- def abs(num)
38
- assert_instance_of([::Float, ::Integer, ::Rational], num)
39
- num.positive? ? num : -num
51
+ Stack.new.pop #=> AssertionError (Expected false to be truthy.)
52
+ ```
53
+
54
+ Or just call them with the module as a receiver:
55
+
56
+ ```ruby
57
+ class Stack
58
+ def peek
59
+ ::Assert.assert(!@store.empty?)
60
+ @store.last
40
61
  end
41
62
  end
42
63
 
43
- MyMath.abs('42')
44
- #=> AssertionError: <"42"> was expected to be instance_of?
45
- # [<Float>, <Integer>, <Rational>] but was
46
- # <String>.
64
+ Stack.new.peek #=> AssertionError (Expected false to be truthy.)
47
65
  ```
48
66
 
67
+ Note that `Assert` provides `aseert` and `assert_*` methods same as in `Minitest::Assertions`
68
+ except they throw not `Minitest::Assertion` but `AssertionError` when an assertion fails.
69
+
70
+ See also: http://docs.seattlerb.org/minitest/Minitest/Assertions.html
71
+
49
72
  ## Contributing
50
73
 
51
74
  You should follow the steps below.
@@ -1,5 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "standard_assert/core_ext/assert"
4
- require_relative "standard_assert/core_ext/assertion_error"
3
+ require_relative "standard_assert/assertions"
5
4
  require_relative "standard_assert/version"
5
+
6
+ module StandardAssert
7
+ assertion_method_pattern = /\Aassert(?:_\w+)?\z/
8
+
9
+ ::StandardAssert::Assertions.instance
10
+ .public_methods
11
+ .select(&assertion_method_pattern.method(:match?))
12
+ .each do |name|
13
+ class_eval <<~CODE, __FILE__, __LINE__ + 1
14
+ module_function def #{name}(*args, &block)
15
+ ::StandardAssert::Assertions.instance.public_send(:#{name}, *args, &block)
16
+ end
17
+ CODE
18
+ end
19
+ end
20
+
21
+ # HACK: Call `dup` to make `inspect`, `name`, `to_s` and so on return `"Assert"`
22
+ Assert = StandardAssert.dup
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StandardAssert
4
+ class AssertionError < ::StandardError; end
5
+ end
6
+
7
+ # HACK: Call `dup` to make `inspect`, `name`, `to_s` and so on return `"AssertionError"`
8
+ AssertionError = StandardAssert::AssertionError.dup
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest"
4
+ require "singleton"
5
+ require_relative "assertion_error"
6
+
7
+ module StandardAssert
8
+ class Assertions # :nodoc:
9
+ include ::Minitest::Assertions
10
+ include ::Singleton
11
+
12
+ def initialize
13
+ @assertions = 0
14
+ end
15
+
16
+ # Override
17
+ def assert(test, msg = nil)
18
+ super
19
+ rescue ::Minitest::Assertion => e
20
+ raise ::AssertionError, e.message
21
+ end
22
+
23
+ protected # rubocop:disable Style/AccessModifierDeclarations
24
+
25
+ attr_reader :assertions
26
+
27
+ # NOTE: `Minitest::Assertions` expects to be able to increment an instance accessor named
28
+ # `assertions`, but we don't need the accessor because it's used for reporting test results.
29
+ # So define a writer that does nothing and always returns the same value.
30
+ def assertions=(_val)
31
+ @assertions
32
+ end
33
+ end
34
+ end
@@ -1,7 +1,7 @@
1
1
  # rubocop:disable Style/FrozenStringLiteralComment
2
2
 
3
3
  module StandardAssert
4
- VERSION = "0.2.1".freeze
4
+ VERSION = "0.3.0".freeze
5
5
  end
6
6
 
7
7
  # rubocop:enable Style/FrozenStringLiteralComment
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_assert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yasaichi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-14 00:00:00.000000000 Z
11
+ date: 2019-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,19 +95,19 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: test-unit
98
+ name: minitest
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '3.0'
103
+ version: '5.0'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '3.0'
110
+ version: '5.0'
111
111
  description: "`standard_assert` is a Standard Library-like library for assertions
112
112
  in Ruby. It is aimed at encouraging us to use assertion methods anywhere; Not only
113
113
  testing but also production."
@@ -122,8 +122,8 @@ files:
122
122
  - README.md
123
123
  - Rakefile
124
124
  - lib/standard_assert.rb
125
- - lib/standard_assert/core_ext/assert.rb
126
- - lib/standard_assert/core_ext/assertion_error.rb
125
+ - lib/standard_assert/assertion_error.rb
126
+ - lib/standard_assert/assertions.rb
127
127
  - lib/standard_assert/version.rb
128
128
  homepage: https://github.com/yasaichi/standard_assert
129
129
  licenses:
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test/unit/assertions"
4
-
5
- module Assert
6
- %i[public protected private].each do |visibility|
7
- ::Test::Unit::Assertions.public_send("#{visibility}_instance_methods").each do |name|
8
- define_method name, ::Test::Unit::Assertions.instance_method(name)
9
- __send__(visibility, name)
10
- end
11
- end
12
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
- require "test/unit/assertion-failed-error"
5
-
6
- class AssertionError < StandardError
7
- extend ::Forwardable
8
-
9
- OriginalError = ::Test::Unit::AssertionFailedError
10
- private_constant :OriginalError
11
-
12
- def_delegators(
13
- :@original_error,
14
- *(OriginalError.instance_methods - superclass.instance_methods)
15
- )
16
-
17
- def initialize(message = nil, options = nil)
18
- @original_error = OriginalError.new(message, options)
19
- super(@original_error.message)
20
- end
21
- end
22
-
23
- Test::Unit.class_exec do
24
- remove_const :AssertionFailedError
25
- const_set :AssertionFailedError, AssertionError
26
- end