expresenter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fb63ac6ae40169d1d98b61f79fc0b4f90fe2977d80baf5949c0ba6c9853cfe59
4
+ data.tar.gz: 313bc8ef04d5916a19c0bef468835fb8b59c9dfb5b16a58b3cd11ea4b85c07f3
5
+ SHA512:
6
+ metadata.gz: f79e27247f4ec36d4e77ac7dd34f77486dac59b57a917992887eb0e8d7cfd308a39b429557e23bffaebc0d5e8799ed5584031c06dad043385755cd9f4340263a
7
+ data.tar.gz: fa66a109000fbc04de1122d92727b1c410e85f2d12cf3072e994a601bf4b6f8349086459398c7bcefd5ccb1f15d80b747c2e2c2df9e77e63fe820f54d2f72fd3
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Cyril Kato
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Expresenter
2
+
3
+ [![Build Status](https://api.travis-ci.org/fixrb/expresenter.svg?branch=main)][travis]
4
+ [![Code Climate](https://codeclimate.com/github/fixrb/expresenter/badges/gpa.svg)][codeclimate]
5
+ [![Gem Version](https://badge.fury.io/rb/expresenter.svg)][gem]
6
+ [![Inline docs](https://inch-ci.org/github/fixrb/expresenter.svg?branch=main)][inchpages]
7
+ [![Documentation](https://img.shields.io/:yard-docs-38c800.svg)][rubydoc]
8
+
9
+ > Expectation result presenter.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "expresenter"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install expresenter
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ result = Expresenter.call(true, actual: "FOO", error: nil, expected: "foo", got: true, negate: true, valid: true, matcher: :eql, level: :MUST)
31
+
32
+ actual.failed? # => false
33
+ actual.failure? # => false
34
+ actual.info? # => false
35
+ actual.warning? # => false
36
+ actual.to_sym # => :success
37
+ actual.char # => "."
38
+ actual.emoji # => "✅"
39
+ actual.passed? # => true
40
+ actual.negate? # => true
41
+ actual.error? # => false
42
+ actual.success? # => true
43
+ actual.valid? # => true
44
+ actual.inspect # => "Expresenter::Pass(actual: \"FOO\", error: nil, expected: \"foo\", got: true, matcher: :eql, negate: true, level: :MUST, valid: true)"
45
+ actual.definition # => "eql \"foo\""
46
+ actual.maybe_negate # => " not"
47
+ actual.summary # => "expected \"FOO\" not to eql \"foo\""
48
+ actual.colored_char # => "\e[32m.\e[0m"
49
+ actual.colored_string # => "\e[32mSuccess: expected \"FOO\" not to eql \"foo\".\e[0m"
50
+ result.message # => "Success: expected \"FOO\" not to eql \"foo\"."
51
+ actual.to_s # => "Success: expected \"FOO\" not to eql \"foo\"."
52
+ actual.titre # => "Success"
53
+ ```
54
+
55
+ ## Contact
56
+
57
+ * Home page: https://github.com/fixrb/expresenter
58
+ * Bugs/issues: https://github.com/fixrb/expresenter/issues
59
+
60
+ ## Rubies
61
+
62
+ * [MRI](https://www.ruby-lang.org/)
63
+ * [Rubinius](https://rubinius.com/)
64
+ * [JRuby](https://www.jruby.org/)
65
+
66
+ ## Versioning
67
+
68
+ __Expresenter__ follows [Semantic Versioning 2.0](https://semver.org/).
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
73
+
74
+ ***
75
+
76
+ <p>
77
+ This project is sponsored by:<br />
78
+ <a href="https://sashite.com/"><img
79
+ src="https://github.com/fixrb/expresenter/raw/main/img/sashite.png"
80
+ alt="Sashite" /></a>
81
+ </p>
82
+
83
+ [gem]: https://rubygems.org/gems/expresenter
84
+ [travis]: https://travis-ci.org/fixrb/expresenter
85
+ [codeclimate]: https://codeclimate.com/github/fixrb/expresenter
86
+ [inchpages]: https://inch-ci.org/github/fixrb/expresenter
87
+ [rubydoc]: https://rubydoc.info/gems/expresenter/frames
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Namespace for the Expresenter library.
4
+ module Expresenter
5
+ # @raise [Fail] A failed spec result.
6
+ # @return [Pass] A passed spec result.
7
+ def self.call(is_passed)
8
+ is_passed ? Pass : Fail
9
+ end
10
+ end
11
+
12
+ require_relative File.join("expresenter", "fail")
13
+ require_relative File.join("expresenter", "pass")
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Expresenter
4
+ # Common collection of methods for Result's classes.
5
+ module Base
6
+ # @return [#object_id] Returned value by the challenged subject.
7
+ attr_reader :actual
8
+
9
+ # @return [Exception, nil] Any possible raised exception.
10
+ attr_reader :error
11
+
12
+ # @return [#object_id] The expected value.
13
+ attr_reader :expected
14
+
15
+ # @return [#object_id] The result of the boolean comparison between the
16
+ # actual value and the expected value through the matcher.
17
+ attr_reader :got
18
+
19
+ # @return [#object_id] The matcher.
20
+ attr_reader :matcher
21
+
22
+ # @return [:MUST, :SHOULD, :MAY] The requirement level of the expectation.
23
+ attr_reader :level
24
+
25
+ # Common initialize method.
26
+ #
27
+ # @param actual [#object_id] Returned value by the challenged subject.
28
+ # @param error [Exception, nil] Any possible raised exception.
29
+ # @param expected [#object_id] The expected value.
30
+ # @param got [Boolean, nil] The result of the boolean comparison
31
+ # between the actual value and the expected value through the matcher.
32
+ # @param negate [Boolean] Evaluated to a negative assertion?
33
+ # @param valid [Boolean] Report if the test was true or false?
34
+ # @param matcher [Symbol] The matcher.
35
+ # @param level [:MUST, :SHOULD, :MAY] The requirement level.
36
+ def initialize(actual:, error:, expected:, got:, negate:, valid:,
37
+ matcher:, level:)
38
+
39
+ @actual = actual
40
+ @error = error
41
+ @expected = expected
42
+ @got = got
43
+ @negate = negate
44
+ @valid = valid
45
+ @matcher = matcher
46
+ @level = level
47
+
48
+ super(to_s) if failed?
49
+ end
50
+
51
+ # Did the test pass?
52
+ #
53
+ # @return [Boolean] The spec passed or failed?
54
+ def passed?
55
+ !failed?
56
+ end
57
+
58
+ # The value of the negate instance variable.
59
+ #
60
+ # @return [Boolean] Evaluated to a negative assertion?
61
+ def negate?
62
+ @negate
63
+ end
64
+
65
+ # The state of error.
66
+ #
67
+ # @return [Boolean] The test raised an error?
68
+ def error?
69
+ !error.nil?
70
+ end
71
+
72
+ # The state of success.
73
+ #
74
+ # @return [Boolean] The test was a success?
75
+ def success?
76
+ got.equal?(true)
77
+ end
78
+
79
+ # The value of the boolean comparison between the actual value and the
80
+ # expected value.
81
+ #
82
+ # @return [Boolean] The test was true or false?
83
+ def valid?
84
+ @valid
85
+ end
86
+
87
+ # A string containing a human-readable representation of the result.
88
+ #
89
+ # @return [String] The human-readable representation of the result.
90
+ def inspect
91
+ "#{self.class}(actual: #{actual.inspect}, " \
92
+ "error: #{error.inspect}, " \
93
+ "expected: #{expected.inspect}, " \
94
+ "got: #{got.inspect}, " \
95
+ "matcher: #{matcher.inspect}, " \
96
+ "negate: #{negate?.inspect}, " \
97
+ "level: #{level.inspect}, " \
98
+ "valid: #{valid?.inspect})" \
99
+ end
100
+
101
+ # The readable definition.
102
+ #
103
+ # @return [String] A readable string of the definition.
104
+ def definition
105
+ [matcher, expected&.inspect].compact.join(" ")
106
+ end
107
+
108
+ # The negation, if any.
109
+ #
110
+ # @return [String] The negation, or an empty string.
111
+ def maybe_negate
112
+ negate? ? " not" : ""
113
+ end
114
+
115
+ # The summary of the result.
116
+ #
117
+ # @return [String] A string representing the summary of the result.
118
+ def summary
119
+ if error?
120
+ error.message
121
+ elsif actual.is_a?(::Exception)
122
+ actual.message
123
+ elsif actual == expected
124
+ "expected#{maybe_negate} to #{definition}"
125
+ else
126
+ "expected #{actual.inspect}#{maybe_negate} to #{definition}"
127
+ end
128
+ end
129
+
130
+ # Express the result with one colored char.
131
+ #
132
+ # @return [String] The colored char that identify the result.
133
+ def colored_char
134
+ color(char)
135
+ end
136
+
137
+ # The colored string representation of the result.
138
+ #
139
+ # @return [String] A string representing the result.
140
+ def colored_string
141
+ color(to_s)
142
+ end
143
+
144
+ # The representation of the result.
145
+ #
146
+ # @return [String] A string representing the result.
147
+ def to_s
148
+ "#{titre}: #{summary}."
149
+ end
150
+
151
+ # Titre for the result.
152
+ #
153
+ # @return [String] A string representing the titre.
154
+ def titre
155
+ if error?
156
+ error.class.name
157
+ else
158
+ to_sym.to_s.capitalize
159
+ end
160
+ end
161
+
162
+ protected
163
+
164
+ def color(str)
165
+ if success?
166
+ "\e[32m#{str}\e[0m"
167
+ elsif info?
168
+ "\e[36m#{str}\e[0m"
169
+ elsif warning?
170
+ "\e[33m#{str}\e[0m"
171
+ elsif failure?
172
+ "\e[35m#{str}\e[0m"
173
+ else
174
+ "\e[31m#{str}\e[0m"
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Expresenter
6
+ # The class that is responsible for reporting that the expectation is false.
7
+ class Fail < ::StandardError
8
+ include Base
9
+
10
+ # @raise [Fail] A failed spec result.
11
+ def self.with(**details)
12
+ raise new(**details)
13
+ end
14
+
15
+ # Did the test fail?
16
+ #
17
+ # @return [Boolean] The spec passed or failed?
18
+ def failed?
19
+ true
20
+ end
21
+
22
+ # The state of failure.
23
+ #
24
+ # @return [Boolean] The test was a failure?
25
+ def failure?
26
+ !error?
27
+ end
28
+
29
+ # The state of info.
30
+ #
31
+ # @return [Boolean] The test was an info?
32
+ def info?
33
+ false
34
+ end
35
+
36
+ # The state of warning.
37
+ #
38
+ # @return [Boolean] The test was a warning?
39
+ def warning?
40
+ false
41
+ end
42
+
43
+ # Identify the state of the result.
44
+ #
45
+ # @return [Symbol] The identifier of the state.
46
+ def to_sym
47
+ failure? ? :failure : :error
48
+ end
49
+
50
+ # Express the result with one char.
51
+ #
52
+ # @return [String] The char that identify the result.
53
+ def char
54
+ if failure?
55
+ "F"
56
+ else
57
+ "E"
58
+ end
59
+ end
60
+
61
+ # Express the result with one emoji.
62
+ #
63
+ # @return [String] The emoji that identify the result.
64
+ def emoji
65
+ if failure?
66
+ "❌"
67
+ else
68
+ "💥"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Expresenter
6
+ # The class that is responsible for reporting that the expectation is true.
7
+ class Pass
8
+ include Base
9
+
10
+ # @return [Pass] A passed spec result.
11
+ def self.with(**details)
12
+ new(**details)
13
+ end
14
+
15
+ alias message to_s
16
+
17
+ # Did the test fail?
18
+ #
19
+ # @return [Boolean] The spec passed or failed?
20
+ def failed?
21
+ false
22
+ end
23
+
24
+ # The state of failure.
25
+ #
26
+ # @return [Boolean] The test was a failure?
27
+ def failure?
28
+ false
29
+ end
30
+
31
+ # The state of info.
32
+ #
33
+ # @return [Boolean] The test was an info?
34
+ def info?
35
+ !error.nil?
36
+ end
37
+
38
+ # The state of warning.
39
+ #
40
+ # @return [Boolean] The test was a warning?
41
+ def warning?
42
+ got.equal?(false)
43
+ end
44
+
45
+ # Identify the state of the result.
46
+ #
47
+ # @return [Symbol] The identifier of the state.
48
+ def to_sym
49
+ return :success if success?
50
+ return :warning if warning?
51
+
52
+ :info
53
+ end
54
+
55
+ # Express the result with one char.
56
+ #
57
+ # @return [String] The char that identify the result.
58
+ def char
59
+ if success?
60
+ "."
61
+ elsif warning?
62
+ "W"
63
+ else
64
+ "I"
65
+ end
66
+ end
67
+
68
+ # Express the result with one emoji.
69
+ #
70
+ # @return [String] The emoji that identify the result.
71
+ def emoji
72
+ if success?
73
+ "✅"
74
+ elsif warning?
75
+ "⚠️"
76
+ else
77
+ "💡"
78
+ end
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expresenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: brutal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-md
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-thread_safety
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Expectation result presenter.
140
+ email: contact@cyril.email
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - LICENSE.md
146
+ - README.md
147
+ - lib/expresenter.rb
148
+ - lib/expresenter/base.rb
149
+ - lib/expresenter/fail.rb
150
+ - lib/expresenter/pass.rb
151
+ homepage: https://github.com/fixrb/expresenter
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: 2.7.0
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.1.4
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Expectation result presenter.
174
+ test_files: []