Skip to content

Commit 6f21378

Browse files
committed
fix: make rspec dsl show source test code snippets
1 parent 8c7a8b9 commit 6f21378

File tree

3 files changed

+75
-24
lines changed

3 files changed

+75
-24
lines changed

lib/action_policy/rspec/dsl.rb

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,27 @@ module RSpec # :nodoc: all
55
module DSL
66
%w[describe fdescribe xdescribe].each do |meth|
77
class_eval <<~CODE, __FILE__, __LINE__ + 1
8-
def #{meth}_rule(policy_rule, *args, **kwargs)
9-
#{meth}("#\#{policy_rule}", *args, **kwargs) do
10-
let(:rule) { policy_rule }
11-
12-
subject do
13-
policy.apply(rule)
14-
policy.result
15-
end
16-
17-
instance_eval(&Proc.new) if block_given?
18-
end
8+
def #{meth}_rule(rule, *args, &block)
9+
find_and_eval_shared("context", "action_policy:policy_rule_context", caller.first, rule, *args, method: :#{meth}, block: block)
1910
end
2011
CODE
2112
end
2213

2314
["", "f", "x"].each do |prefix|
2415
class_eval <<~CODE, __FILE__, __LINE__ + 1
25-
def #{prefix}succeed(*args, **kwargs)
26-
context(*args) do
16+
def #{prefix}succeed(msg = "succeeds", *args, **kwargs)
17+
the_caller = caller
18+
#{prefix}context(msg, *args, **kwargs) do
2719
instance_eval(&Proc.new) if block_given?
28-
#{prefix}it "succeeds", kwargs do
29-
is_expected.to be_success, "Expected succeed but failed:\n\#{formatted_policy(policy)}"
30-
end
20+
find_and_eval_shared("examples", "action_policy:policy_rule_example", the_caller.first, true, the_caller)
3121
end
3222
end
3323
34-
def #{prefix}failed(*args, **kwargs)
35-
context(*args) do
24+
def #{prefix}failed(msg = "fails", *args, **kwargs)
25+
the_caller = caller
26+
#{prefix}context(msg, *args, **kwargs) do
3627
instance_eval(&Proc.new) if block_given?
37-
#{prefix}it "fails", kwargs do
38-
is_expected.to be_fail, "Expected to fail but succeed:\n\#{formatted_policy(policy)}"
39-
end
28+
find_and_eval_shared("examples", "action_policy:policy_rule_example", the_caller.first, false, the_caller)
4029
end
4130
end
4231
CODE
@@ -47,8 +36,6 @@ module PolicyExampleGroup
4736
def self.included(base)
4837
base.metadata[:type] = :policy
4938
base.extend ActionPolicy::RSpec::DSL
50-
base.let(:record) { nil }
51-
base.let(:policy) { described_class.new(record, context) }
5239
super
5340
end
5441

@@ -60,11 +47,57 @@ def formatted_policy(policy)
6047
end
6148

6249
if defined?(::RSpec)
63-
RSpec.configure do |config|
50+
::RSpec.shared_context "action_policy:policy_context" do
51+
let(:record) { nil }
52+
let(:context) { {} }
53+
let(:policy) { described_class.new(record, context) }
54+
end
55+
56+
::RSpec.shared_context "action_policy:policy_rule_context" do |policy_rule, *args, method: "describe", block: nil|
57+
public_send(method, policy_rule.to_s, *args) do
58+
let(:rule) { policy_rule }
59+
60+
let(:subject) do
61+
policy.apply(rule)
62+
policy.result
63+
end
64+
65+
instance_eval(&block) if block
66+
end
67+
end
68+
69+
::RSpec.shared_examples_for "action_policy:policy_rule_example" do |success, the_caller|
70+
if success
71+
specify do
72+
next if subject.success?
73+
raise(
74+
RSpec::Expectations::ExpectationNotMetError,
75+
"Expected to succeed but failed:\n#{formatted_policy(policy)}",
76+
the_caller
77+
)
78+
end
79+
else
80+
specify do
81+
next if subject.fail?
82+
raise(
83+
RSpec::Expectations::ExpectationNotMetError,
84+
"Expected to fail but succeed:\n#{formatted_policy(policy)}",
85+
the_caller
86+
)
87+
end
88+
end
89+
end
90+
91+
::RSpec.configure do |config|
6492
config.include(
6593
ActionPolicy::RSpec::PolicyExampleGroup,
6694
type: :policy,
6795
file_path: %r{spec/policies}
6896
)
97+
config.include_context(
98+
"action_policy:policy_context",
99+
type: :policy,
100+
file_path: %r{spec/policies}
101+
)
69102
end
70103
end

lib/action_policy/utils/pretty_print.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# frozen_string_literal: true
22

3+
old_verbose = $VERBOSE
4+
35
begin
46
require "method_source"
7+
# Ignore parse warnings when patch
8+
# Ruby version mismatches
9+
$VERBOSE = nil
510
require "parser/current"
611
require "unparser"
712
rescue LoadError
813
# do nothing
14+
ensure
15+
$VERBOSE = old_verbose
916
end
1017

1118
module ActionPolicy

spec/action_policy/dsl_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def manage?
3535
let(:admin) { User.new("admin") }
3636
let(:context) { {user: user} }
3737

38+
# test skip rule
39+
xdescribe_rule :manage? do
40+
succeed
41+
end
42+
3843
describe_rule :manage? do
3944
let(:record) { User.new("guest") }
4045

@@ -62,5 +67,11 @@ def manage?
6267
let(:user) { admin }
6368
end
6469
end
70+
71+
context "test skip" do
72+
xfailed do
73+
let(:user) { admin }
74+
end
75+
end
6576
end
6677
end

0 commit comments

Comments
 (0)