foobara-spec-helpers 0.0.3

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: b0f6a1006e69b2a292438a457245727c5cf82c3ba46d4d9fbc591b5f2f6cc8ea
4
+ data.tar.gz: 5b9bfe81386aac886b40afb214a7e552e1f9da9136f3d32c61a9d98c5f4b57a7
5
+ SHA512:
6
+ metadata.gz: 400e0ef3c6c607a5da9b09bf81c1b1dc5bc5aefdbc28509566ee6c6541e6817aa6ecac3f4c382fdd4e27625773094d9efb1ff0b290cd95125044fc2d55f3e9bb
7
+ data.tar.gz: f94444dc58209f31d597969507bd49173e506b3257e7b526924ed0f3548891a2141b03abd84f5aed23adc8dab14810a3178af98a430f5a722c2daaea71d2b7e1
data/LICENSE.txt ADDED
@@ -0,0 +1,8 @@
1
+ foobara-spec-helpers is dual licensed under your choice of the Apache-2.0 license and the MIT license.
2
+
3
+ Apache-2.0 License: LICENSE-APACHE.txt or https://www.apache.org/licenses/LICENSE-2.0.txt
4
+ MIT License: LICENSE-MIT.txt or https://opensource.org/licenses/MIT
5
+
6
+ This is equivalent to the following SPDX License Expression: Apache-2.0 OR MIT
7
+
8
+ Copyright (c) 2023 Miles Georgi
@@ -0,0 +1,2 @@
1
+ # TODO: replace this with Util.require_directory when Util is in its own repository.
2
+ Dir["#{__dir__}/**/*.rb"].each { |f| require f }
@@ -0,0 +1,9 @@
1
+ RSpec::Matchers.define :any do |expectation|
2
+ match do |actual|
3
+ actual.any? { |item| expectation === item }
4
+ end
5
+
6
+ failure_message do |actual|
7
+ "expected that at least one element in #{actual} would match #{expected}"
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ module RspecHelpers
2
+ module StubClass
3
+ module ClassMethods
4
+ def stub_class(name, superclass = nil, which: :class, &)
5
+ name = name.to_s if name.is_a?(::Symbol)
6
+
7
+ unless metadata.key?(:foobara_stubbed_modules)
8
+ set = metadata[:foobara_stubbed_modules] = Set.new
9
+
10
+ after do
11
+ names = set.sort_by { |s| -s.count(":") }
12
+
13
+ names.each do |module_name|
14
+ Foobara::Util.remove_constant(module_name)
15
+ end
16
+
17
+ set.clear
18
+ end
19
+ end
20
+
21
+ metadata[:foobara_stubbed_modules] << name
22
+
23
+ Foobara::Util.make_class(name, superclass, which:, &)
24
+ end
25
+
26
+ def stub_module(name, &)
27
+ stub_class(name, which: :module, &)
28
+ end
29
+ end
30
+
31
+ def stub_class(name, superclass = nil, &)
32
+ self.class.stub_class(name, superclass, &)
33
+ end
34
+
35
+ def stub_module(name, &)
36
+ self.class.stub_module(name, &)
37
+ end
38
+ end
39
+ end
40
+
41
+ RSpec.configure do |c|
42
+ c.include RspecHelpers::StubClass
43
+ c.extend RspecHelpers::StubClass::ClassMethods
44
+ end
@@ -0,0 +1,23 @@
1
+ module RspecHelpers
2
+ module Expectations
3
+ def is_expected_to_raise(*, &)
4
+ is_expected_to raise_error(*, &)
5
+ end
6
+
7
+ def is_expected_to_not_raise(*, &)
8
+ is_expected_to_not raise_error(*, &)
9
+ end
10
+
11
+ def is_expected_to(expectation)
12
+ expect { subject }.to expectation
13
+ end
14
+
15
+ def is_expected_to_not(expectation)
16
+ expect { subject }.to_not expectation
17
+ end
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |c|
22
+ c.include RspecHelpers::Expectations
23
+ end
@@ -0,0 +1,88 @@
1
+ module RspecHelpers
2
+ module Stubs
3
+ module ClassMethods
4
+ def stub_env_vars(env_vars)
5
+ env_vars.each_pair do |env_var, value|
6
+ stub_env_var(env_var, value)
7
+ end
8
+ end
9
+
10
+ def stub_env_var(env_var, value)
11
+ before do
12
+ stub_env_var(env_var, value)
13
+ end
14
+ end
15
+ end
16
+
17
+ def stub_env_vars(env_vars)
18
+ env_vars.each_pair do |env_var, value|
19
+ stub_env_var(env_var, value)
20
+ end
21
+ end
22
+
23
+ def stub_env_var(env_var, value)
24
+ env_var = env_var.to_s
25
+
26
+ unless self.class.metadata[:env_stubbed]
27
+ install_env_stubs
28
+ end
29
+
30
+ self.class.metadata[:env_stubbed][env_var] = value
31
+ end
32
+
33
+ private
34
+
35
+ def install_env_stubs
36
+ env_stubbed = {}
37
+
38
+ self.class.metadata[:env_stubbed] = env_stubbed
39
+ self.class.after do
40
+ self.class.metadata.delete(:env_stubbed)
41
+ end
42
+
43
+ allow(ENV).to receive(:fetch).and_wrap_original do |original_method, key, default|
44
+ if env_stubbed.key?(key)
45
+ env_stubbed[key]
46
+ else
47
+ original_method.call(key, default)
48
+ end
49
+ end
50
+ allow(ENV).to receive(:[]).and_wrap_original do |original_method, key|
51
+ if env_stubbed.key?(key)
52
+ env_stubbed[key]
53
+ else
54
+ original_method.call(key)
55
+ end
56
+ end
57
+ allow(ENV).to receive(:each).and_wrap_original do |original_method, &block|
58
+ original_method.call(&block)
59
+ env_stubbed.each(&block)
60
+
61
+ ENV
62
+ end
63
+ allow(ENV).to receive(:each_key).and_wrap_original do |original_method, &block|
64
+ original_method.call(&block)
65
+ env_stubbed.each_key(&block)
66
+
67
+ ENV
68
+ end
69
+ allow(ENV).to receive(:each_value).and_wrap_original do |original_method, &block|
70
+ original_method.call(&block)
71
+ env_stubbed.each_value(&block)
72
+
73
+ ENV
74
+ end
75
+ allow(ENV).to receive(:keys).and_wrap_original do |original_method, &block|
76
+ original_method.call(&block) | env_stubbed.keys
77
+ end
78
+ allow(ENV).to receive(:values).and_wrap_original do |original_method, &block|
79
+ original_method.call(&block) | env_stubbed.values
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ RSpec.configure do |c|
86
+ c.extend RspecHelpers::Stubs::ClassMethods
87
+ c.include RspecHelpers::Stubs
88
+ end
@@ -0,0 +1,5 @@
1
+ module Foobara
2
+ module SpecHelpers
3
+ VERSION = "0.0.3".freeze
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "spec_helpers/version"
2
+
3
+ module Foobara
4
+ module SpecHelpers
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foobara-spec-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Miles Georgi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: foobara-util
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - azimux@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - lib/foobara/spec_helpers.rb
36
+ - lib/foobara/spec_helpers/all.rb
37
+ - lib/foobara/spec_helpers/any.rb
38
+ - lib/foobara/spec_helpers/class.rb
39
+ - lib/foobara/spec_helpers/expectations.rb
40
+ - lib/foobara/spec_helpers/stubs.rb
41
+ - lib/foobara/spec_helpers/version.rb
42
+ homepage: https://github.com/foobara/spec-helpers
43
+ licenses:
44
+ - Apache-2.0
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/foobara/spec-helpers
48
+ source_code_uri: https://github.com/foobara/spec-helpers
49
+ changelog_uri: https://github.com/foobara/spec-helpers/blob/main/CHANGELOG.md
50
+ rubygems_mfa_required: 'true'
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.2.2
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.4.10
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Common spec helpers used in various Foobara projects.
70
+ test_files: []