aygabtu 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ require 'rails_application_helper'
2
+
3
+ require 'aygabtu/rspec'
4
+
5
+ require 'support/aygabtu_sees_routes'
6
+
7
+ describe "nesting and chaining scopes" do
8
+ extend AygabtuSeesRoutes
9
+
10
+ include Aygabtu::RSpec.example_group_module
11
+
12
+ aygabtu_sees_routes do
13
+ scope module: :namespace do
14
+ get 'for_action1', to: 'bogus#action1'
15
+ get 'for_action2', to: 'bogus#action2'
16
+ end
17
+ end
18
+
19
+ context "asserting the right route is being visited" do
20
+ before do
21
+ @spy = double
22
+ expect(@spy).to receive(:visit).with(be_path_for_this_route)
23
+ end
24
+ attr_reader :spy
25
+
26
+ def visit(path)
27
+ spy.visit(path)
28
+ end
29
+
30
+ def aygabtu_assertions
31
+ end
32
+
33
+ context "visiting one route" do
34
+ def be_path_for_this_route
35
+ include('for_action1')
36
+ end
37
+
38
+ namespace(:namespace) do
39
+ action(:action1).visit
40
+ end
41
+ end
42
+
43
+ context "visiting another route" do
44
+ def be_path_for_this_route
45
+ include('for_action2')
46
+ end
47
+
48
+ namespace(:namespace).action(:action2).visit
49
+ end
50
+ end
51
+
52
+ remaining do
53
+ # sanity check.
54
+ it "has covered routes and thus created two examples" do
55
+ expect(self.class.aygabtu_matching_routes).to be_empty
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,8 @@
1
+ require "action_controller/railtie"
2
+
3
+ class Application < Rails::Application
4
+ config.secret_key_base = 'bogus'
5
+ end
6
+
7
+ require 'rspec/rails'
8
+ require 'rspec/rails/example'
@@ -0,0 +1,17 @@
1
+ require 'support/identifies_routes'
2
+
3
+ module AygabtuSeesRoutes
4
+ def aygabtu_sees_routes(&block)
5
+ engine = Class.new(Rails::Engine)
6
+ engine.instance.routes.draw do
7
+ # Rails evaluates the block given inside the context of
8
+ # some routing related object. Since we do not want to repeat
9
+ # the following line every time this method is called,
10
+ # we need to to this instance_eval trick.
11
+ extend IdentifiesRoutes
12
+ instance_eval(&Proc.new)
13
+ end
14
+ aygabtu_handle.send(:rails_application_routes=, engine.routes)
15
+ end
16
+ end
17
+
@@ -0,0 +1,18 @@
1
+ module IdentifiesRoutes
2
+ def identified_by(identifier)
3
+ { defaults: { route_identifier: identifier } }
4
+ end
5
+
6
+ def be_identified_by(identifier)
7
+ satisfy { |rw| rw.journey_route.defaults[:route_identifier] == identifier }
8
+ end
9
+
10
+ def route_identified_by(identifier, all_routes = all_routes)
11
+ identified_routes = all_routes.select do |rw|
12
+ rw.journey_route.defaults[:route_identifier] == identifier
13
+ end
14
+ expect(identified_routes.length).to be == 1
15
+ identified_routes.first
16
+ end
17
+ end
18
+
@@ -0,0 +1,93 @@
1
+ require 'pathname'
2
+ require 'json'
3
+
4
+ module InvokesRspec
5
+ private
6
+
7
+ def rspec_result(specfile = nil)
8
+ specfile ||= prepare_specfile
9
+
10
+ arglist = ['rspec', '--format', 'json', specfile.to_s]
11
+ output = `#{arglist.shelljoin}`
12
+ raise "rspec gave no output, file not found?, syntax error in spec file? excption outside example?" if output.empty?
13
+
14
+ # rspec-rails 2.99 pollutes STDOUT with a deprecation warning. Work around that.
15
+ # This workaround assumes the warning does not contain a '{', while the JSON starts with it
16
+ output = output[%r{\A[^\{]*(.*)}m, 1]
17
+
18
+ _convert_raw_rspec_result(JSON.parse(output))
19
+ end
20
+
21
+ def prepare_specfile
22
+ path = Pathname(__FILE__).dirname.join('../../_generated_spec.rb')
23
+ path.open('w') do |file|
24
+ file << rspec_file_content
25
+ end
26
+
27
+ root_path = Pathname(__FILE__).parent.parent.parent
28
+
29
+ path.relative_path_from(root_path)
30
+ end
31
+
32
+ def _convert_raw_rspec_result(json)
33
+ examples = json['examples'].map { |raw| Example.new(raw) }
34
+ RSpecResult.new(json, examples)
35
+ end
36
+
37
+ RSpecResult = Struct.new(:original_json, :examples)
38
+
39
+ class Example
40
+ attr_reader :raw
41
+
42
+ def initialize(raw)
43
+ @raw = raw
44
+ end
45
+
46
+ def full_description
47
+ raw['full_description']
48
+ end
49
+
50
+ def line_number
51
+ raw['line_number']
52
+ end
53
+
54
+ def status
55
+ raw['status'].to_sym
56
+ end
57
+
58
+ def passed?
59
+ status == :passed
60
+ end
61
+
62
+ def pending?
63
+ status == :pending
64
+ end
65
+
66
+ def failed?
67
+ status == :failed
68
+ end
69
+
70
+ def exception_message
71
+ raw.fetch('exception').fetch('message')
72
+ end
73
+
74
+ def payload
75
+ Marshal.load(exception_message)
76
+ end
77
+
78
+ def inspect
79
+ hash = {
80
+ line: line_number,
81
+ status: status
82
+ }
83
+ hash[:exception_message] = exception_message if failed?
84
+
85
+ segments = [
86
+ 'Example',
87
+ *hash.map { |key, value| "#{key}: #{value.inspect}" }
88
+ ]
89
+ segments.join(' ')
90
+ end
91
+ end
92
+ end
93
+
@@ -0,0 +1,39 @@
1
+ module MatcherShims
2
+ def contain_exactly(*objects_or_matchers)
3
+ return super if defined?(super)
4
+
5
+ satisfy do |actual|
6
+ next false unless actual.length == objects_or_matchers.length
7
+
8
+ objects_or_matchers.permutation.any? do |permutation|
9
+ permutation.zip(actual).all? do |object_or_matcher, actual_item|
10
+ if object_or_matcher.respond_to?(:matches?)
11
+ object_or_matcher.matches?(actual_item)
12
+ else
13
+ object_or_matcher == actual_item
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def all(matcher)
21
+ return super if defined?(super)
22
+
23
+ satisfy do |collection|
24
+ collection.all? { |item| matcher.matches?(item) }
25
+ end
26
+ end
27
+
28
+ module And
29
+ def and(another_matcher)
30
+ return super if defined?(super)
31
+
32
+ RSpec::Matchers::BuiltIn::Satisfy.new do |actual|
33
+ matches?(actual) && another_matcher.matches?(actual)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ RSpec::Matchers::BuiltIn::BaseMatcher.send :include, MatcherShims::And
@@ -0,0 +1,49 @@
1
+ require 'rails_application_helper'
2
+
3
+ require 'aygabtu/rspec'
4
+
5
+ require 'support/identifies_routes'
6
+ require 'support/aygabtu_sees_routes'
7
+ require 'support/matcher_shims'
8
+
9
+ describe "test mechanism for identifying routes independently of controller, name and action" do
10
+ extend AygabtuSeesRoutes
11
+
12
+ include Aygabtu::RSpec.example_group_module
13
+
14
+ aygabtu_sees_routes do
15
+ get 'bogus', identified_by(:an_identifier).merge(to: 'bogus#action1')
16
+ get 'bogus', identified_by(:another_identifier).merge(to: 'bogus#action2')
17
+ end
18
+
19
+ include IdentifiesRoutes
20
+ include MatcherShims
21
+
22
+ describe "route_identified_by" do
23
+ it "returns the correct route" do
24
+ expect(route_identified_by(:an_identifier).action).to be == 'action1'
25
+ expect(route_identified_by(:another_identifier).action).to be == 'action2'
26
+ end
27
+ end
28
+
29
+ describe "be_identified_by" do
30
+ it "works inside our matcher" do
31
+ expect(all_routes).to contain_exactly(
32
+ be_identified_by(:an_identifier),
33
+ be_identified_by(:another_identifier)
34
+ )
35
+
36
+ # assert difference is really taken into account
37
+ expect(all_routes).not_to contain_exactly(
38
+ be_identified_by(:an_identifier),
39
+ be_identified_by(:an_identifier)
40
+ )
41
+ end
42
+ end
43
+
44
+ # interface expected by IdentifiesRoutes
45
+ def all_routes
46
+ self.class.aygabtu_matching_routes
47
+ end
48
+ end
49
+
@@ -0,0 +1,57 @@
1
+ require 'rails_application_helper'
2
+
3
+ require 'aygabtu/rspec'
4
+
5
+ require 'support/aygabtu_sees_routes'
6
+ require 'support/identifies_routes'
7
+
8
+ describe "path construction when visiting a route", type: :feature do
9
+ extend AygabtuSeesRoutes
10
+
11
+ include Aygabtu::RSpec.example_group_module
12
+
13
+ aygabtu_sees_routes do
14
+ get 'bogus', to: 'bogus#route_without'
15
+ get 'bogus/:segment1', to: 'bogus#route_with'
16
+ get 'bogus/:segment1/:segment2', to: 'bogus#route_with_two'
17
+ end
18
+
19
+ def assert_path(path)
20
+ # The fact that the path has already been formed means
21
+ # that the router accepted the route arguments, so no segment
22
+ # can be missing.
23
+ # We just asssert here that no excess parameters have been passed.
24
+ expect(path).not_to include('?')
25
+ end
26
+
27
+ def visit(path)
28
+ assert_path path
29
+ end
30
+
31
+ def aygabtu_assertions
32
+ end
33
+
34
+ action(:route_without).visit
35
+ context "with an additional parameter" do
36
+ def assert_path(path)
37
+ expect(URI.parse(path).query).to be == "additional_parameter=bogus"
38
+ end
39
+
40
+ action(:route_without).visit_with(additional_parameter: "bogus")
41
+ end
42
+
43
+ action(:route_with).visit_with(segment1: "bogus")
44
+ action(:route_with).visiting_with(segment1: "bogus") do
45
+ visit
46
+ end
47
+
48
+ action(:route_with) do
49
+ visit_with(segment1: "bogus")
50
+ end
51
+
52
+ action(:route_with_two).visit_with(segment1: "bogus", segment2: "bogus")
53
+ action(:route_with_two).visiting_with(segment1: "bogus") do
54
+ visit_with(segment2: "bogus")
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aygabtu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Stratmann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-rails
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
+ - !ruby/object:Gem::Dependency
28
+ name: capybara
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Feature test generator for GET requests, using Capybara and RSpec
42
+ email:
43
+ - thomas.stratmann@9elements.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - aygabtu.gemspec
56
+ - lib/aygabtu.rb
57
+ - lib/aygabtu/generator.rb
58
+ - lib/aygabtu/handle.rb
59
+ - lib/aygabtu/point_of_call.rb
60
+ - lib/aygabtu/route_mark.rb
61
+ - lib/aygabtu/route_wrapper.rb
62
+ - lib/aygabtu/rspec.rb
63
+ - lib/aygabtu/scope/action.rb
64
+ - lib/aygabtu/scope/base.rb
65
+ - lib/aygabtu/scope/named.rb
66
+ - lib/aygabtu/scope/namespace_controller.rb
67
+ - lib/aygabtu/scope/remaining.rb
68
+ - lib/aygabtu/scope/requiring.rb
69
+ - lib/aygabtu/scope/static_dynamic.rb
70
+ - lib/aygabtu/scope/visiting_with.rb
71
+ - lib/aygabtu/scope_actor.rb
72
+ - lib/aygabtu/scope_chain.rb
73
+ - lib/aygabtu/version.rb
74
+ - spec/actions_spec.rb
75
+ - spec/example_spec.rb
76
+ - spec/lib/route_wrapper_spec.rb
77
+ - spec/matching_routes_spec.rb
78
+ - spec/nesting_spec.rb
79
+ - spec/rails_application_helper.rb
80
+ - spec/support/aygabtu_sees_routes.rb
81
+ - spec/support/identifies_routes.rb
82
+ - spec/support/invokes_rspec.rb
83
+ - spec/support/matcher_shims.rb
84
+ - spec/support_spec/identifies_routes_spec.rb
85
+ - spec/visiting_routes_spec.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Feature test generator for GET requests
110
+ test_files:
111
+ - spec/actions_spec.rb
112
+ - spec/example_spec.rb
113
+ - spec/lib/route_wrapper_spec.rb
114
+ - spec/matching_routes_spec.rb
115
+ - spec/nesting_spec.rb
116
+ - spec/rails_application_helper.rb
117
+ - spec/support/aygabtu_sees_routes.rb
118
+ - spec/support/identifies_routes.rb
119
+ - spec/support/invokes_rspec.rb
120
+ - spec/support/matcher_shims.rb
121
+ - spec/support_spec/identifies_routes_spec.rb
122
+ - spec/visiting_routes_spec.rb
123
+ has_rdoc: