@@ -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)
6047end
6148
6249if 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
70103end
0 commit comments