sspec-support 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/Changelog.md +242 -0
  3. data/LICENSE.md +23 -0
  4. data/README.md +40 -0
  5. data/lib/rspec/support.rb +149 -0
  6. data/lib/rspec/support/caller_filter.rb +83 -0
  7. data/lib/rspec/support/comparable_version.rb +46 -0
  8. data/lib/rspec/support/differ.rb +215 -0
  9. data/lib/rspec/support/directory_maker.rb +63 -0
  10. data/lib/rspec/support/encoded_string.rb +165 -0
  11. data/lib/rspec/support/fuzzy_matcher.rb +48 -0
  12. data/lib/rspec/support/hunk_generator.rb +47 -0
  13. data/lib/rspec/support/matcher_definition.rb +42 -0
  14. data/lib/rspec/support/method_signature_verifier.rb +426 -0
  15. data/lib/rspec/support/mutex.rb +73 -0
  16. data/lib/rspec/support/object_formatter.rb +275 -0
  17. data/lib/rspec/support/recursive_const_methods.rb +76 -0
  18. data/lib/rspec/support/reentrant_mutex.rb +53 -0
  19. data/lib/rspec/support/ruby_features.rb +176 -0
  20. data/lib/rspec/support/source.rb +75 -0
  21. data/lib/rspec/support/source/location.rb +21 -0
  22. data/lib/rspec/support/source/node.rb +110 -0
  23. data/lib/rspec/support/source/token.rb +87 -0
  24. data/lib/rspec/support/spec.rb +81 -0
  25. data/lib/rspec/support/spec/deprecation_helpers.rb +64 -0
  26. data/lib/rspec/support/spec/formatting_support.rb +9 -0
  27. data/lib/rspec/support/spec/in_sub_process.rb +69 -0
  28. data/lib/rspec/support/spec/library_wide_checks.rb +150 -0
  29. data/lib/rspec/support/spec/shell_out.rb +84 -0
  30. data/lib/rspec/support/spec/stderr_splitter.rb +63 -0
  31. data/lib/rspec/support/spec/string_matcher.rb +46 -0
  32. data/lib/rspec/support/spec/with_isolated_directory.rb +13 -0
  33. data/lib/rspec/support/spec/with_isolated_stderr.rb +13 -0
  34. data/lib/rspec/support/version.rb +7 -0
  35. data/lib/rspec/support/warnings.rb +39 -0
  36. metadata +115 -0
@@ -0,0 +1,176 @@
1
+ require 'rbconfig'
2
+ RSpec::Support.require_rspec_support "comparable_version"
3
+
4
+ module RSpec
5
+ module Support
6
+ # @api private
7
+ #
8
+ # Provides query methods for different OS or OS features.
9
+ module OS
10
+ module_function
11
+
12
+ def windows?
13
+ !!(RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/)
14
+ end
15
+
16
+ def windows_file_path?
17
+ ::File::ALT_SEPARATOR == '\\'
18
+ end
19
+ end
20
+
21
+ # @api private
22
+ #
23
+ # Provides query methods for different rubies
24
+ module Ruby
25
+ module_function
26
+
27
+ def jruby?
28
+ RUBY_PLATFORM == 'java'
29
+ end
30
+
31
+ def jruby_version
32
+ @jruby_version ||= ComparableVersion.new(JRUBY_VERSION)
33
+ end
34
+
35
+ def jruby_9000?
36
+ jruby? && JRUBY_VERSION >= '9.0.0.0'
37
+ end
38
+
39
+ def rbx?
40
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
41
+ end
42
+
43
+ def non_mri?
44
+ !mri?
45
+ end
46
+
47
+ def mri?
48
+ !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby'
49
+ end
50
+ end
51
+
52
+ # @api private
53
+ #
54
+ # Provides query methods for ruby features that differ among
55
+ # implementations.
56
+ module RubyFeatures
57
+ module_function
58
+
59
+ if Ruby.jruby?
60
+ # On JRuby 1.7 `--1.8` mode, `Process.respond_to?(:fork)` returns true,
61
+ # but when you try to fork, it raises an error:
62
+ # NotImplementedError: fork is not available on this platform
63
+ #
64
+ # When we drop support for JRuby 1.7 and/or Ruby 1.8, we can drop
65
+ # this special case.
66
+ def fork_supported?
67
+ false
68
+ end
69
+ else
70
+ def fork_supported?
71
+ Process.respond_to?(:fork)
72
+ end
73
+ end
74
+
75
+ def optional_and_splat_args_supported?
76
+ Method.method_defined?(:parameters)
77
+ end
78
+
79
+ def caller_locations_supported?
80
+ respond_to?(:caller_locations, true)
81
+ end
82
+
83
+ if Exception.method_defined?(:cause)
84
+ def supports_exception_cause?
85
+ true
86
+ end
87
+ else
88
+ def supports_exception_cause?
89
+ false
90
+ end
91
+ end
92
+
93
+ ripper_requirements = [ComparableVersion.new(RUBY_VERSION) >= '1.9.2']
94
+
95
+ ripper_requirements.push(false) if Ruby.rbx?
96
+
97
+ if Ruby.jruby?
98
+ ripper_requirements.push(Ruby.jruby_version >= '1.7.5')
99
+ # Ripper on JRuby 9.0.0.0.rc1 - 9.1.8.0 reports wrong line number
100
+ # or cannot parse source including `:if`.
101
+ ripper_requirements.push(!Ruby.jruby_version.between?('9.0.0.0.rc1', '9.1.8.0'))
102
+ end
103
+
104
+ if ripper_requirements.all?
105
+ def ripper_supported?
106
+ true
107
+ end
108
+ else
109
+ def ripper_supported?
110
+ false
111
+ end
112
+ end
113
+
114
+ if Ruby.mri?
115
+ def kw_args_supported?
116
+ RUBY_VERSION >= '2.0.0'
117
+ end
118
+
119
+ def required_kw_args_supported?
120
+ RUBY_VERSION >= '2.1.0'
121
+ end
122
+
123
+ def supports_rebinding_module_methods?
124
+ RUBY_VERSION.to_i >= 2
125
+ end
126
+ else
127
+ # RBX / JRuby et al support is unknown for keyword arguments
128
+ begin
129
+ eval("o = Object.new; def o.m(a: 1); end;"\
130
+ " raise SyntaxError unless o.method(:m).parameters.include?([:key, :a])")
131
+
132
+ def kw_args_supported?
133
+ true
134
+ end
135
+ rescue SyntaxError
136
+ def kw_args_supported?
137
+ false
138
+ end
139
+ end
140
+
141
+ begin
142
+ eval("o = Object.new; def o.m(a: ); end;"\
143
+ "raise SyntaxError unless o.method(:m).parameters.include?([:keyreq, :a])")
144
+
145
+ def required_kw_args_supported?
146
+ true
147
+ end
148
+ rescue SyntaxError
149
+ def required_kw_args_supported?
150
+ false
151
+ end
152
+ end
153
+
154
+ begin
155
+ Module.new { def foo; end }.instance_method(:foo).bind(Object.new)
156
+
157
+ def supports_rebinding_module_methods?
158
+ true
159
+ end
160
+ rescue TypeError
161
+ def supports_rebinding_module_methods?
162
+ false
163
+ end
164
+ end
165
+ end
166
+
167
+ def module_refinement_supported?
168
+ Module.method_defined?(:refine) || Module.private_method_defined?(:refine)
169
+ end
170
+
171
+ def module_prepends_supported?
172
+ Module.method_defined?(:prepend) || Module.private_method_defined?(:prepend)
173
+ end
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,75 @@
1
+ RSpec::Support.require_rspec_support 'encoded_string'
2
+ RSpec::Support.require_rspec_support 'ruby_features'
3
+
4
+ module RSpec
5
+ module Support
6
+ # @private
7
+ # Represents a Ruby source file and provides access to AST and tokens.
8
+ class Source
9
+ attr_reader :source, :path
10
+
11
+ def self.from_file(path)
12
+ source = File.read(path)
13
+ new(source, path)
14
+ end
15
+
16
+ if String.method_defined?(:encoding)
17
+ def initialize(source_string, path=nil)
18
+ @source = RSpec::Support::EncodedString.new(source_string, Encoding.default_external)
19
+ @path = path ? File.expand_path(path) : '(string)'
20
+ end
21
+ else # for 1.8.7
22
+ # :nocov:
23
+ def initialize(source_string, path=nil)
24
+ @source = RSpec::Support::EncodedString.new(source_string)
25
+ @path = path ? File.expand_path(path) : '(string)'
26
+ end
27
+ # :nocov:
28
+ end
29
+
30
+ def lines
31
+ @lines ||= source.split("\n")
32
+ end
33
+
34
+ def inspect
35
+ "#<#{self.class} #{path}>"
36
+ end
37
+
38
+ if RSpec::Support::RubyFeatures.ripper_supported?
39
+ RSpec::Support.require_rspec_support 'source/node'
40
+ RSpec::Support.require_rspec_support 'source/token'
41
+
42
+ def ast
43
+ @ast ||= begin
44
+ require 'ripper'
45
+ sexp = Ripper.sexp(source)
46
+ raise SyntaxError unless sexp
47
+ Node.new(sexp)
48
+ end
49
+ end
50
+
51
+ def tokens
52
+ @tokens ||= begin
53
+ require 'ripper'
54
+ tokens = Ripper.lex(source)
55
+ Token.tokens_from_ripper_tokens(tokens)
56
+ end
57
+ end
58
+
59
+ def nodes_by_line_number
60
+ @nodes_by_line_number ||= begin
61
+ nodes_by_line_number = ast.select(&:location).group_by { |node| node.location.line }
62
+ Hash.new { |hash, key| hash[key] = [] }.merge(nodes_by_line_number)
63
+ end
64
+ end
65
+
66
+ def tokens_by_line_number
67
+ @tokens_by_line_number ||= begin
68
+ nodes_by_line_number = tokens.group_by { |token| token.location.line }
69
+ Hash.new { |hash, key| hash[key] = [] }.merge(nodes_by_line_number)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,21 @@
1
+ module RSpec
2
+ module Support
3
+ class Source
4
+ # @private
5
+ # Represents a source location of node or token.
6
+ Location = Struct.new(:line, :column) do
7
+ include Comparable
8
+
9
+ def self.location?(array)
10
+ array.is_a?(Array) && array.size == 2 && array.all? { |e| e.is_a?(Integer) }
11
+ end
12
+
13
+ def <=>(other)
14
+ line_comparison = (line <=> other.line)
15
+ return line_comparison unless line_comparison == 0
16
+ column <=> other.column
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,110 @@
1
+ RSpec::Support.require_rspec_support 'source/location'
2
+
3
+ module RSpec
4
+ module Support
5
+ class Source
6
+ # @private
7
+ # A wrapper for Ripper AST node which is generated with `Ripper.sexp`.
8
+ class Node
9
+ include Enumerable
10
+
11
+ attr_reader :sexp, :parent
12
+
13
+ def self.sexp?(array)
14
+ array.is_a?(Array) && array.first.is_a?(Symbol)
15
+ end
16
+
17
+ def initialize(ripper_sexp, parent=nil)
18
+ @sexp = ripper_sexp.freeze
19
+ @parent = parent
20
+ end
21
+
22
+ def type
23
+ sexp[0]
24
+ end
25
+
26
+ def args
27
+ @args ||= raw_args.map do |raw_arg|
28
+ if Node.sexp?(raw_arg)
29
+ Node.new(raw_arg, self)
30
+ elsif Location.location?(raw_arg)
31
+ Location.new(*raw_arg)
32
+ elsif raw_arg.is_a?(Array)
33
+ ExpressionSequenceNode.new(raw_arg, self)
34
+ else
35
+ raw_arg
36
+ end
37
+ end.freeze
38
+ end
39
+
40
+ def children
41
+ @children ||= args.select { |arg| arg.is_a?(Node) }.freeze
42
+ end
43
+
44
+ def location
45
+ @location ||= args.find { |arg| arg.is_a?(Location) }
46
+ end
47
+
48
+ # We use a loop here (instead of recursion) to prevent SystemStackError
49
+ def each
50
+ return to_enum(__method__) unless block_given?
51
+
52
+ node_queue = []
53
+ node_queue << self
54
+
55
+ while (current_node = node_queue.shift)
56
+ yield current_node
57
+ node_queue.concat(current_node.children)
58
+ end
59
+ end
60
+
61
+ def each_ancestor
62
+ return to_enum(__method__) unless block_given?
63
+
64
+ current_node = self
65
+
66
+ while (current_node = current_node.parent)
67
+ yield current_node
68
+ end
69
+ end
70
+
71
+ def inspect
72
+ "#<#{self.class} #{type}>"
73
+ end
74
+
75
+ private
76
+
77
+ def raw_args
78
+ sexp[1..-1] || []
79
+ end
80
+ end
81
+
82
+ # @private
83
+ # Basically `Ripper.sexp` generates arrays whose first element is a symbol (type of sexp),
84
+ # but it exceptionally generates typeless arrays for expression sequence:
85
+ #
86
+ # Ripper.sexp('foo; bar')
87
+ # => [
88
+ # :program,
89
+ # [ # Typeless array
90
+ # [:vcall, [:@ident, "foo", [1, 0]]],
91
+ # [:vcall, [:@ident, "bar", [1, 5]]]
92
+ # ]
93
+ # ]
94
+ #
95
+ # We wrap typeless arrays in this pseudo type node
96
+ # so that it can be handled in the same way as other type node.
97
+ class ExpressionSequenceNode < Node
98
+ def type
99
+ :_expression_sequence
100
+ end
101
+
102
+ private
103
+
104
+ def raw_args
105
+ sexp
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,87 @@
1
+ RSpec::Support.require_rspec_support 'source/location'
2
+
3
+ module RSpec
4
+ module Support
5
+ class Source
6
+ # @private
7
+ # A wrapper for Ripper token which is generated with `Ripper.lex`.
8
+ class Token
9
+ CLOSING_TYPES_BY_OPENING_TYPE = {
10
+ :on_lbracket => :on_rbracket,
11
+ :on_lparen => :on_rparen,
12
+ :on_lbrace => :on_rbrace,
13
+ :on_heredoc_beg => :on_heredoc_end
14
+ }.freeze
15
+
16
+ CLOSING_KEYWORDS_BY_OPENING_KEYWORD = {
17
+ 'def' => 'end',
18
+ 'do' => 'end',
19
+ }.freeze
20
+
21
+ attr_reader :token
22
+
23
+ def self.tokens_from_ripper_tokens(ripper_tokens)
24
+ ripper_tokens.map { |ripper_token| new(ripper_token) }.freeze
25
+ end
26
+
27
+ def initialize(ripper_token)
28
+ @token = ripper_token.freeze
29
+ end
30
+
31
+ def location
32
+ @location ||= Location.new(*token[0])
33
+ end
34
+
35
+ def type
36
+ token[1]
37
+ end
38
+
39
+ def string
40
+ token[2]
41
+ end
42
+
43
+ def ==(other)
44
+ token == other.token
45
+ end
46
+
47
+ alias_method :eql?, :==
48
+
49
+ def inspect
50
+ "#<#{self.class} #{type} #{string.inspect}>"
51
+ end
52
+
53
+ def keyword?
54
+ type == :on_kw
55
+ end
56
+
57
+ def opening?
58
+ opening_delimiter? || opening_keyword?
59
+ end
60
+
61
+ def closed_by?(other)
62
+ closed_by_delimiter?(other) || closed_by_keyword?(other)
63
+ end
64
+
65
+ private
66
+
67
+ def opening_delimiter?
68
+ CLOSING_TYPES_BY_OPENING_TYPE.key?(type)
69
+ end
70
+
71
+ def opening_keyword?
72
+ return false unless keyword?
73
+ CLOSING_KEYWORDS_BY_OPENING_KEYWORD.key?(string)
74
+ end
75
+
76
+ def closed_by_delimiter?(other)
77
+ other.type == CLOSING_TYPES_BY_OPENING_TYPE[type]
78
+ end
79
+
80
+ def closed_by_keyword?(other)
81
+ return false unless other.keyword?
82
+ other.string == CLOSING_KEYWORDS_BY_OPENING_KEYWORD[string]
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end