rails_edge_test 2.0.0 → 3.0.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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsEdgeTest
2
4
  class Configuration
3
5
  attr_accessor :elm_path, :edge_root_path, :printer
@@ -35,23 +37,17 @@ module RailsEdgeTest
35
37
  end
36
38
 
37
39
  def wrap_suite_execution(&block)
38
- @before_suite_blocks.each do |before_suit_block|
39
- before_suit_block.call
40
- end
40
+ @before_suite_blocks.each(&:call)
41
41
 
42
42
  block.call
43
43
  end
44
44
 
45
45
  def wrap_edge_execution(&edge)
46
- @before_each_blocks.each do |before_each_block|
47
- before_each_block.call
48
- end
46
+ @before_each_blocks.each(&:call)
49
47
 
50
48
  edge.call
51
49
 
52
- @after_each_blocks.each do |after_each_block|
53
- after_each_block.call
54
- end
50
+ @after_each_blocks.each(&:call)
55
51
  end
56
52
  end
57
53
  end
@@ -1,49 +1,53 @@
1
- module RailsEdgeTest::Dsl
2
- Action = Struct.new(:name, :controller) do
3
- def initialize(*args)
4
- super
5
- @edges = {}
6
- @let_handler = LetHandler.new
7
- end
1
+ # frozen_string_literal: true
8
2
 
9
- def edge(description, &block)
10
- edge = Edge.new(description, self)
11
- @edges[edge] = block
12
- end
3
+ module RailsEdgeTest
4
+ module Dsl
5
+ Action = Struct.new(:name, :controller) do
6
+ def initialize(*args)
7
+ super
8
+ @edges = {}
9
+ @let_handler = LetHandler.new
10
+ end
13
11
 
14
- def let(title, &block)
15
- @let_handler.add_definition(title, &block)
16
- end
12
+ def edge(description, &block)
13
+ edge = Edge.new(description, self)
14
+ @edges[edge] = block
15
+ end
17
16
 
18
- def generate(title, &block)
19
- @let_handler.add_definition("generate_#{title}", &block)
20
- end
17
+ def let(title, &block)
18
+ @let_handler.add_definition(title, &block)
19
+ end
21
20
 
22
- def __edges
23
- @edges
24
- end
21
+ def generate(title, &block)
22
+ @let_handler.add_definition("generate_#{title}", &block)
23
+ end
25
24
 
26
- def __let_handler
27
- @let_handler
28
- end
25
+ def __edges
26
+ @edges
27
+ end
29
28
 
30
- def controller_class
31
- controller.controller_class
32
- end
29
+ def __let_handler
30
+ @let_handler
31
+ end
33
32
 
34
- # support calling methods defined in controller
35
- def method_missing(method_name, *arguments, &block)
36
- if controller.respond_to?(method_name)
37
- controller.public_send(method_name, *arguments, &block)
38
- else
39
- super
33
+ def controller_class
34
+ controller.controller_class
35
+ end
36
+
37
+ # support calling methods defined in controller
38
+ def method_missing(method_name, ...)
39
+ if controller.respond_to?(method_name)
40
+ controller.public_send(method_name, ...)
41
+ else
42
+ super
43
+ end
40
44
  end
41
- end
42
45
 
43
- # always define respond_to_missing? when defining method_missing:
44
- # https://thoughtbot.com/blog/always-define-respond-to-missing-when-overriding
45
- def respond_to_missing?(method_name, include_private = false)
46
- controller.respond_to?(method_name) || super
46
+ # always define respond_to_missing? when defining method_missing:
47
+ # https://thoughtbot.com/blog/always-define-respond-to-missing-when-overriding
48
+ def respond_to_missing?(method_name, include_private = false)
49
+ controller.respond_to?(method_name) || super
50
+ end
47
51
  end
48
52
  end
49
53
  end
@@ -1,27 +1,31 @@
1
- module RailsEdgeTest::Dsl
2
- Controller = Struct.new(:controller_class) do
3
- def initialize(*args)
4
- super
5
- @actions = []
6
- @let_handler = LetHandler.new
7
- end
1
+ # frozen_string_literal: true
8
2
 
9
- def action(name, &block)
10
- new_action = Action.new(name, self)
11
- new_action.instance_exec(&block)
12
- @actions << new_action
13
- end
3
+ module RailsEdgeTest
4
+ module Dsl
5
+ Controller = Struct.new(:controller_class) do
6
+ def initialize(*args)
7
+ super
8
+ @actions = []
9
+ @let_handler = LetHandler.new
10
+ end
14
11
 
15
- def let(title, &block)
16
- @let_handler.add_definition(title, &block)
17
- end
12
+ def action(name, &block)
13
+ new_action = Action.new(name, self)
14
+ new_action.instance_exec(&block)
15
+ @actions << new_action
16
+ end
18
17
 
19
- def __actions
20
- @actions
21
- end
18
+ def let(title, &block)
19
+ @let_handler.add_definition(title, &block)
20
+ end
21
+
22
+ def __actions
23
+ @actions
24
+ end
22
25
 
23
- def __let_handler
24
- @let_handler
26
+ def __let_handler
27
+ @let_handler
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -1,134 +1,147 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'action_dispatch'
2
4
  require 'action_controller'
3
5
  require 'action_controller/test_case'
4
6
 
5
- module RailsEdgeTest::Dsl
6
- Edge = Struct.new(:description, :action) do
7
-
8
- def initialize(*args)
9
- super
10
- @let_cache = {}
11
- end
12
-
13
- delegate :controller_class, to: :action
14
- delegate :session, to: :request
15
-
16
- def request
17
- @request ||= ActionController::TestRequest.create(controller_class)
18
- end
7
+ module RailsEdgeTest
8
+ module Dsl
9
+ Edge = Struct.new(:description, :action) do
10
+ def initialize(*args)
11
+ super
12
+ @let_cache = {}
13
+ end
19
14
 
20
- # In the context of the edge, we want `controller` to be the rails controller
21
- # instead of our own RailsEdgeTest::Dsl::Controller. In this way the user can
22
- # directly access the rails controller within their edge.
23
- def controller
24
- @controller ||= controller_class.new
25
- end
15
+ delegate :controller_class, to: :action
16
+ delegate :session, to: :request
26
17
 
27
- def response
28
- @response
29
- end
18
+ def request
19
+ @request ||= ActionController::TestRequest.create(controller_class)
20
+ end
30
21
 
31
- def perform_get(parameters={})
32
- process(parameters)
33
- end
22
+ # In the context of the edge, we want `controller` to be the rails controller
23
+ # instead of our own RailsEdgeTest::Dsl::Controller. In this way the user can
24
+ # directly access the rails controller within their edge.
25
+ def controller
26
+ @controller ||= controller_class.new
27
+ end
34
28
 
35
- def perform_post(parameters={})
36
- request.instance_variable_set(:@method, "POST")
37
- request.env['REQUEST_METHOD'] = "POST"
38
- process(parameters)
39
- end
29
+ def response
30
+ @response
31
+ end
40
32
 
41
- def process(parameters={})
42
- request.assign_parameters(
43
- ::Rails.application.routes,
44
- controller_class.controller_path,
45
- action.name.to_s,
46
- parameters.stringify_keys!,
47
- '',
48
- ''
49
- )
50
-
51
- response = ActionDispatch::Response.new.tap do |res|
52
- res.request = request
33
+ def perform_get(parameters = {})
34
+ process(parameters)
53
35
  end
54
36
 
55
- @response = controller.dispatch(action.name, request, response)
56
- end
37
+ def perform_post(parameters = {})
38
+ request.instance_variable_set(:@method, 'POST')
39
+ request.env['REQUEST_METHOD'] = 'POST'
40
+ process(parameters)
41
+ end
57
42
 
58
- def produce_elm_file(module_name, ivar: nil)
59
- json = produce_json(ivar: ivar)
60
- json = json.gsub("\\", "\\\\\\\\") # unbelievably, this replaces \ with \\
43
+ def perform_delete(parameters = {})
44
+ request.instance_variable_set(:@method, 'DELETE')
45
+ request.env['REQUEST_METHOD'] = 'DELETE'
46
+ process(parameters)
47
+ end
61
48
 
62
- filepath = File.join(
63
- RailsEdgeTest.configuration.elm_path,
64
- 'Edge',
65
- controller_class.name.gsub('::','/')
66
- )
49
+ def set_authenticity_token
50
+ r = request
51
+ controller.instance_eval { @_request = r }
52
+ request.headers['X-CSRF-Token'] = controller.send :form_authenticity_token
53
+ end
67
54
 
68
- full_module_name =
69
- "#{controller_class.name.gsub('::','.')}.#{module_name}"
55
+ def process(parameters = {})
56
+ request.assign_parameters(
57
+ ::Rails.application.routes,
58
+ controller_class.controller_path,
59
+ action.name.to_s,
60
+ parameters.stringify_keys!,
61
+ '',
62
+ ''
63
+ )
64
+
65
+ response = ActionDispatch::Response.new.tap do |res|
66
+ res.request = request
67
+ end
68
+
69
+ @response = controller.dispatch(action.name, request, response)
70
+ end
70
71
 
71
- data = <<~ELM
72
- module Edge.#{full_module_name} exposing (json)
72
+ def produce_elm_file(module_name, ivar: nil)
73
+ json = produce_json(ivar: ivar)
74
+ json = json.gsub('\\', '\\\\\\\\') # unbelievably, this replaces \ with \\
73
75
 
76
+ filepath = File.join(
77
+ RailsEdgeTest.configuration.elm_path,
78
+ 'Edge',
79
+ controller_class.name.gsub('::', '/')
80
+ )
74
81
 
75
- json : String
76
- json =
77
- """
78
- #{json}
79
- """
80
- ELM
82
+ full_module_name =
83
+ "#{controller_class.name.gsub('::', '.')}.#{module_name}"
81
84
 
82
- write_file(filepath, module_name+'.elm', data)
83
- end
85
+ data = <<~ELM
86
+ module Edge.#{full_module_name} exposing (json)
84
87
 
85
- private
86
88
 
87
- def produce_json(ivar: nil)
88
- unless response
89
- fail "Must perform a request (for example `perform_get`) before attempting to produce a json file."
90
- end
89
+ json : String
90
+ json =
91
+ """
92
+ #{json}
93
+ """
94
+ ELM
91
95
 
92
- if response.is_a?(Array) && response[0] >= 300
93
- fail "Request did not result in a successful (2xx) response!"
96
+ write_file(filepath, "#{module_name}.elm", data)
94
97
  end
95
98
 
96
- ActiveSupport::JSON::Encoding.escape_html_entities_in_json = false
97
- if ivar
98
- value = controller.send(:instance_variable_get, ivar)
99
- JSON.pretty_unparse(value.as_json)
100
- elsif response[1]['Content-Type']&.starts_with?('application/json')
101
- value = JSON.parse(response[2].body)
102
- JSON.pretty_unparse(value)
103
- else
104
- response[2].body
99
+ private
100
+
101
+ def produce_json(ivar: nil)
102
+ unless response
103
+ raise 'Must perform a request (for example `perform_get`) before attempting to produce a json file.'
104
+ end
105
+
106
+ raise 'Request did not result in a successful (2xx) response!' if response.is_a?(Array) && response[0] >= 300
107
+
108
+ ActiveSupport::JSON::Encoding.escape_html_entities_in_json = false
109
+ if ivar
110
+ value = controller.send(:instance_variable_get, ivar)
111
+ JSON.pretty_unparse(value.as_json)
112
+ elsif response[1]['Content-Type']&.starts_with?('application/json')
113
+ value = JSON.parse(response[2].body)
114
+ JSON.pretty_unparse(value)
115
+ else
116
+ response[2].body
117
+ end
105
118
  end
106
- end
107
119
 
108
- def write_file(filepath, filename, data)
109
- FileUtils.mkdir_p(filepath)
120
+ def write_file(filepath, filename, data)
121
+ FileUtils.mkdir_p(filepath)
110
122
 
111
- filepath = File.join(filepath, filename)
123
+ filepath = File.join(filepath, filename)
112
124
 
113
- File.open(filepath, 'w') do |f|
114
- f.write(data)
115
- f.flush
125
+ File.open(filepath, 'w') do |f|
126
+ f.write(data)
127
+ f.flush
128
+ end
116
129
  end
117
- end
118
130
 
119
- # support calling methods defined in action
120
- def method_missing(method_name, *arguments, &block)
121
- if action.respond_to?(method_name)
122
- action.public_send(method_name, *arguments, &block)
123
- else
124
- super
131
+ # support calling methods defined in action
132
+ def method_missing(method_name, ...)
133
+ if action.respond_to?(method_name)
134
+ action.public_send(method_name, ...)
135
+ else
136
+ super
137
+ end
125
138
  end
126
- end
127
139
 
128
- # always define respond_to_missing? when defining method_missing:
129
- # https://thoughtbot.com/blog/always-define-respond-to-missing-when-overriding
130
- def respond_to_missing?(method_name, include_private = false)
131
- action.respond_to?(method_name) || super
140
+ # always define respond_to_missing? when defining method_missing:
141
+ # https://thoughtbot.com/blog/always-define-respond-to-missing-when-overriding
142
+ def respond_to_missing?(method_name, include_private = false)
143
+ action.respond_to?(method_name) || super
144
+ end
132
145
  end
133
146
  end
134
147
  end
@@ -1,22 +1,24 @@
1
- module RailsEdgeTest::Dsl
2
- class LetHandler
3
- attr_reader :let_blocks
1
+ # frozen_string_literal: true
4
2
 
5
- def initialize
6
- @let_blocks = {}
7
- end
3
+ module RailsEdgeTest
4
+ module Dsl
5
+ class LetHandler
6
+ attr_reader :let_blocks
8
7
 
9
- def add_definition(title, &block)
10
- @let_blocks[title] = block
11
- end
8
+ def initialize
9
+ @let_blocks = {}
10
+ end
12
11
 
13
- def execute(title)
14
- block = @let_blocks[title]
15
- unless block
16
- fail NoMethodError, "no method or let block defined with name #{title}"
12
+ def add_definition(title, &block)
13
+ @let_blocks[title] = block
17
14
  end
18
15
 
19
- block.call
16
+ def execute(title)
17
+ block = @let_blocks[title]
18
+ raise NoMethodError, "no method or let block defined with name #{title}" unless block
19
+
20
+ block.call
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RailsEdgeTest
2
4
  module Dsl
3
5
  def controller(controller_class, &block)
@@ -16,7 +18,6 @@ module RailsEdgeTest
16
18
  printer.begin_suite
17
19
 
18
20
  RailsEdgeTest.configuration.wrap_suite_execution do
19
-
20
21
  @controllers.each do |controller|
21
22
  printer.begin_controller(controller)
22
23
 
@@ -40,7 +41,6 @@ module RailsEdgeTest
40
41
 
41
42
  printer.end_controller
42
43
  end
43
-
44
44
  end
45
45
  printer.end_suite
46
46
  end
@@ -1,39 +1,39 @@
1
- module RailsEdgeTest::Printers
2
- class Boring
3
- def initialize
4
- @count = 0
5
- end
1
+ # frozen_string_literal: true
6
2
 
7
- def begin_suite
8
- puts ""
9
- end
3
+ module RailsEdgeTest
4
+ module Printers
5
+ class Boring
6
+ def initialize
7
+ @count = 0
8
+ end
10
9
 
11
- def end_suite
12
- puts "\n#{@count} edge specs executed."
13
- end
10
+ def begin_suite
11
+ puts ''
12
+ end
14
13
 
15
- def begin_controller(_controller)
16
- print "."
17
- end
14
+ def end_suite
15
+ puts "\n#{@count} edge specs executed."
16
+ end
18
17
 
19
- def end_controller
18
+ def begin_controller(_controller)
19
+ print '.'
20
+ end
20
21
 
21
- end
22
+ def end_controller; end
22
23
 
23
- def begin_action(_action)
24
- print "."
25
- end
26
-
27
- def end_action
24
+ def begin_action(_action)
25
+ print '.'
26
+ end
28
27
 
29
- end
28
+ def end_action; end
30
29
 
31
- def begin_edge(_edge)
32
- print "."
33
- end
30
+ def begin_edge(_edge)
31
+ print '.'
32
+ end
34
33
 
35
- def end_edge
36
- @count += 1
34
+ def end_edge
35
+ @count += 1
36
+ end
37
37
  end
38
38
  end
39
39
  end
@@ -1,27 +1,23 @@
1
- module RailsEdgeTest::Printers
2
- class Silent
3
- def begin_suite
4
- end
1
+ # frozen_string_literal: true
5
2
 
6
- def end_suite
7
- end
3
+ module RailsEdgeTest
4
+ module Printers
5
+ class Silent
6
+ def begin_suite; end
8
7
 
9
- def begin_controller(_controller)
10
- end
8
+ def end_suite; end
11
9
 
12
- def end_controller
13
- end
10
+ def begin_controller(_controller); end
14
11
 
15
- def begin_action(_action)
16
- end
12
+ def end_controller; end
17
13
 
18
- def end_action
19
- end
14
+ def begin_action(_action); end
20
15
 
21
- def begin_edge(_edge)
22
- end
16
+ def end_action; end
17
+
18
+ def begin_edge(_edge); end
23
19
 
24
- def end_edge
20
+ def end_edge; end
25
21
  end
26
22
  end
27
23
  end
@@ -1,42 +1,45 @@
1
- module RailsEdgeTest::Printers
2
- class Tree
3
- def initialize
4
- @count = 0
5
- end
6
-
7
- def begin_suite
8
- puts ""
9
- puts "Generating Edges..."
10
- puts "-------------------"
11
- puts ""
12
- end
13
-
14
- def end_suite
15
- puts "\n#{@count} edge specs executed."
16
- end
17
-
18
- def begin_controller(controller)
19
- puts controller.controller_class.name
20
- end
21
-
22
- def end_controller
23
- puts ""
24
- end
25
-
26
- def begin_action(action)
27
- puts " #{action.name}"
28
- end
29
-
30
- def end_action
31
- end
32
-
33
- def begin_edge(edge)
34
- print " #{edge.description}"
35
- end
36
-
37
- def end_edge
38
- puts " ... done"
39
- @count += 1
1
+ # frozen_string_literal: true
2
+
3
+ module RailsEdgeTest
4
+ module Printers
5
+ class Tree
6
+ def initialize
7
+ @count = 0
8
+ end
9
+
10
+ def begin_suite
11
+ puts ''
12
+ puts 'Generating Edges...'
13
+ puts '-------------------'
14
+ puts ''
15
+ end
16
+
17
+ def end_suite
18
+ puts "\n#{@count} edge specs executed."
19
+ end
20
+
21
+ def begin_controller(controller)
22
+ puts controller.controller_class.name
23
+ end
24
+
25
+ def end_controller
26
+ puts ''
27
+ end
28
+
29
+ def begin_action(action)
30
+ puts " #{action.name}"
31
+ end
32
+
33
+ def end_action; end
34
+
35
+ def begin_edge(edge)
36
+ print " #{edge.description}"
37
+ end
38
+
39
+ def end_edge
40
+ puts ' ... done'
41
+ @count += 1
42
+ end
40
43
  end
41
44
  end
42
45
  end