smartest 0.2.0.alpha4 → 0.2.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: 944d35bc0a1be2293c8e276d16436693ff7dd6a2d37218f9f65af3b7a47f90f5
4
- data.tar.gz: edd55fb6442a92e74146eaffc76a3b881463a16b4ce3c72a877734e0f6e36f52
3
+ metadata.gz: 9891230284d4770c57adc2ec2d28356aca9323ba07cd825c6a7f2fb219d6415a
4
+ data.tar.gz: 2cd3e5badccdc21d42a9b3ffd303053e75aa886dcaffefcb903cf84fc6483c90
5
5
  SHA512:
6
- metadata.gz: 5406cfcb57b56c71140c98565da884a184b8d14dcd41f8d6f65f218010d5ef7672eee6c3ad874dcc22a2d415097c9f9926f3a22c31ea3f5dafcf6dd00c2c795d
7
- data.tar.gz: '0666928711e5ec22a5f9d671714597eb9e0a7e98edcf11d334bf72301c42739a8ae71104dff5784fa4728c10c186188e9f5278137ebbf7d22f6e14e745964927'
6
+ metadata.gz: 1fe246549fe47ebb020820690b66937c6fa558be8444e8641f2c176a3ef9886370c17a260b25598cba8d3817adb37cdf331d7de7348c82561dedc993c8dc303e
7
+ data.tar.gz: bcb7e23e7fb872e3001d6683577981a03d5c5482d1d548d9f8f07d1e4f3b3d5766d08b4b3ccbb2c8f17be85c9c553e84c2f2983732e8ecc2966509704839f792
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smartest
4
+ class HelperRegistry
5
+ include Enumerable
6
+
7
+ def self.validate!(helper_module)
8
+ unless helper_module.is_a?(Module) && !helper_module.is_a?(Class)
9
+ raise ArgumentError, "helper must be a module"
10
+ end
11
+ end
12
+
13
+ def initialize
14
+ @helper_modules = []
15
+ end
16
+
17
+ def add(helper_module)
18
+ self.class.validate!(helper_module)
19
+
20
+ @helper_modules << helper_module unless @helper_modules.include?(helper_module)
21
+ end
22
+
23
+ def each(&block)
24
+ @helper_modules.each(&block)
25
+ end
26
+
27
+ def to_a
28
+ @helper_modules.dup
29
+ end
30
+ end
31
+ end
@@ -49,6 +49,10 @@ module Smartest
49
49
  @test_run.add_matcher_module(matcher_module)
50
50
  end
51
51
 
52
+ def use_helper(helper_module)
53
+ @test_run.add_helper_module(helper_module)
54
+ end
55
+
52
56
  def skip(reason = nil)
53
57
  raise Skipped, reason
54
58
  end
@@ -73,8 +73,8 @@ module Smartest
73
73
  test_run = TestRun.new(
74
74
  fixture_classes: @suite.fixture_classes,
75
75
  matcher_modules: @suite.matcher_modules
76
- ) do |fixture_classes:, matcher_modules:|
77
- run_test_body(test_case, fixture_classes, matcher_modules, run_state, cleanup_errors)
76
+ ) do |fixture_classes:, matcher_modules:, helper_modules:|
77
+ run_test_body(test_case, fixture_classes, matcher_modules, helper_modules, run_state, cleanup_errors)
78
78
  end
79
79
 
80
80
  begin
@@ -114,8 +114,8 @@ module Smartest
114
114
  end
115
115
  end
116
116
 
117
- def run_test_body(test_case, fixture_classes, matcher_modules, run_state, cleanup_errors)
118
- context = build_context(matcher_modules, run_state)
117
+ def run_test_body(test_case, fixture_classes, matcher_modules, helper_modules, run_state, cleanup_errors)
118
+ context = build_context(matcher_modules, run_state, helper_modules)
119
119
  fixture_set = nil
120
120
 
121
121
  begin
@@ -152,12 +152,22 @@ module Smartest
152
152
  )
153
153
  end
154
154
 
155
- def build_context(matcher_modules = @suite.matcher_modules, run_state = TestRunState.new)
155
+ def build_context(matcher_modules = @suite.matcher_modules, run_state = TestRunState.new, helper_modules = [])
156
156
  ExecutionContext.new(run_state: run_state).tap do |context|
157
+ helper_modules.each { |helper_module| extend_helper_module(context, helper_module) }
157
158
  matcher_modules.each { |matcher_module| context.extend(matcher_module) }
158
159
  end
159
160
  end
160
161
 
162
+ def extend_helper_module(context, helper_module)
163
+ context.extend(helper_module)
164
+
165
+ helper_methods = helper_module.public_instance_methods + helper_module.protected_instance_methods
166
+ return if helper_methods.empty?
167
+
168
+ context.singleton_class.class_eval { private(*helper_methods) }
169
+ end
170
+
161
171
  def around_test_protocol_error?(error)
162
172
  error.is_a?(AroundTestRunError)
163
173
  end
@@ -4,13 +4,16 @@ module Smartest
4
4
  class TestRun
5
5
  attr_reader :result
6
6
 
7
- def initialize(fixture_classes:, matcher_modules:, &block)
7
+ def initialize(fixture_classes:, matcher_modules:, helper_modules: [], &block)
8
8
  @fixture_classes = FixtureClassRegistry.new
9
9
  fixture_classes.each { |fixture_class| @fixture_classes.add(fixture_class) }
10
10
 
11
11
  @matcher_modules = MatcherRegistry.new
12
12
  matcher_modules.each { |matcher_module| @matcher_modules.add(matcher_module) }
13
13
 
14
+ @helper_modules = HelperRegistry.new
15
+ helper_modules.each { |helper_module| @helper_modules.add(helper_module) }
16
+
14
17
  @block = block
15
18
  @ran = false
16
19
  @result = nil
@@ -22,7 +25,8 @@ module Smartest
22
25
  @ran = true
23
26
  @result = @block.call(
24
27
  fixture_classes: @fixture_classes,
25
- matcher_modules: @matcher_modules
28
+ matcher_modules: @matcher_modules,
29
+ helper_modules: @helper_modules
26
30
  )
27
31
  end
28
32
 
@@ -43,6 +47,12 @@ module Smartest
43
47
  @matcher_modules.add(matcher_module)
44
48
  end
45
49
 
50
+ def add_helper_module(helper_module)
51
+ raise AroundTestRunError, "use_helper must be called before test.run" if ran?
52
+
53
+ @helper_modules.add(helper_module)
54
+ end
55
+
46
56
  private
47
57
 
48
58
  def reject_suite_fixture_class!(klass)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartest
4
- VERSION = "0.2.0.alpha4"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/smartest.rb CHANGED
@@ -9,6 +9,7 @@ require_relative "smartest/fixture_definition"
9
9
  require_relative "smartest/fixture"
10
10
  require_relative "smartest/fixture_class_registry"
11
11
  require_relative "smartest/matcher_registry"
12
+ require_relative "smartest/helper_registry"
12
13
  require_relative "smartest/fixture_set"
13
14
  require_relative "smartest/suite"
14
15
  require_relative "smartest/suite_run"
@@ -517,6 +517,22 @@ test("rejects class matcher registrations") do
517
517
  expect(error.message).to include("matcher must be a module")
518
518
  end
519
519
 
520
+ test("rejects non-module helper registrations") do
521
+ error = SmartestSelfTest.capture_error(ArgumentError) do
522
+ Smartest::HelperRegistry.new.add(Object.new)
523
+ end
524
+
525
+ expect(error.message).to include("helper must be a module")
526
+ end
527
+
528
+ test("rejects class helper registrations") do
529
+ error = SmartestSelfTest.capture_error(ArgumentError) do
530
+ Smartest::HelperRegistry.new.add(Class.new)
531
+ end
532
+
533
+ expect(error.message).to include("helper must be a module")
534
+ end
535
+
520
536
  test("resolves keyword fixture dependencies per test") do
521
537
  calls = []
522
538
 
@@ -912,6 +928,73 @@ test("around_test can register matchers for one test run") do
912
928
  expect(status).to eq(0)
913
929
  end
914
930
 
931
+ test("around_test can register private helpers for one test run") do
932
+ helper_module = Module.new do
933
+ def local_helper_value
934
+ "local"
935
+ end
936
+ end
937
+
938
+ suite = Smartest::Suite.new
939
+ suite.tests.add(
940
+ Smartest::TestCase.new(
941
+ name: "uses local helper",
942
+ metadata: {},
943
+ location: caller_locations(1, 1).first,
944
+ around_test_hooks: [
945
+ proc do |test_run|
946
+ use_helper helper_module
947
+ test_run.run
948
+ end
949
+ ],
950
+ block: proc do
951
+ expect(local_helper_value).to eq("local")
952
+ expect(private_methods).to include(:local_helper_value)
953
+ expect(public_methods).not_to include(:local_helper_value)
954
+ end
955
+ )
956
+ )
957
+ suite.tests.add(
958
+ SmartestSelfTest.test_case(
959
+ "without helper",
960
+ proc { expect(respond_to?(:local_helper_value, true)).to eq(false) }
961
+ )
962
+ )
963
+
964
+ status, = SmartestSelfTest.run_suite(suite)
965
+
966
+ expect(status).to eq(0)
967
+ end
968
+
969
+ test("around_test rejects helper registrations after test.run") do
970
+ helper_module = Module.new do
971
+ def late_helper_value
972
+ "late"
973
+ end
974
+ end
975
+
976
+ suite = Smartest::Suite.new
977
+ suite.tests.add(
978
+ Smartest::TestCase.new(
979
+ name: "late helper",
980
+ metadata: {},
981
+ location: caller_locations(1, 1).first,
982
+ around_test_hooks: [
983
+ proc do |test_run|
984
+ test_run.run
985
+ use_helper helper_module
986
+ end
987
+ ],
988
+ block: proc { expect(true).to eq(true) }
989
+ )
990
+ )
991
+
992
+ status, output = SmartestSelfTest.run_suite(suite)
993
+
994
+ expect(status).to eq(1)
995
+ expect(output).to include("Smartest::AroundTestRunError: use_helper must be called before test.run")
996
+ end
997
+
915
998
  test("around_test must call test.run") do
916
999
  suite = Smartest::Suite.new
917
1000
  suite.tests.add(
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.alpha4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Iwaki
@@ -52,6 +52,7 @@ files:
52
52
  - lib/smartest/fixture_class_registry.rb
53
53
  - lib/smartest/fixture_definition.rb
54
54
  - lib/smartest/fixture_set.rb
55
+ - lib/smartest/helper_registry.rb
55
56
  - lib/smartest/hook_contexts.rb
56
57
  - lib/smartest/init_generator.rb
57
58
  - lib/smartest/matcher_registry.rb
@@ -90,9 +91,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
91
  version: '3.1'
91
92
  required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  requirements:
93
- - - ">"
94
+ - - ">="
94
95
  - !ruby/object:Gem::Version
95
- version: 1.3.1
96
+ version: '0'
96
97
  requirements: []
97
98
  rubygems_version: 3.4.19
98
99
  signing_key: