Skip to content

Commit a9d7b97

Browse files
authored
Merge pull request rspec#2420 from rspec/export-source-classes-to-rspec-support
Export source classes to rspec-support
2 parents ba2019a + dfe55d3 commit a9d7b97

15 files changed

+48
-613
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# encoding: utf-8
22
RSpec::Support.require_rspec_core "formatters/console_codes"
33
RSpec::Support.require_rspec_core "formatters/snippet_extractor"
4+
RSpec::Support.require_rspec_core 'formatters/syntax_highlighter'
45
RSpec::Support.require_rspec_support "encoded_string"
56

67
module RSpec
@@ -215,7 +216,7 @@ def read_failed_lines
215216
file_path, line_number = file_and_line_number[1..2]
216217
max_line_count = RSpec.configuration.max_displayed_failure_line_count
217218
lines = SnippetExtractor.extract_expression_lines_at(file_path, line_number.to_i, max_line_count)
218-
RSpec.world.source_cache.syntax_highlighter.highlight(lines)
219+
RSpec.world.syntax_highlighter.highlight(lines)
219220
rescue SnippetExtractor::NoSuchFileError
220221
["Unable to find #{file_path} to read failed line"]
221222
rescue SnippetExtractor::NoSuchLineError

lib/rspec/core/formatters/html_snippet_extractor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def self.convert(code)
2424
@@converter = NullConverter
2525
begin
2626
require 'coderay'
27-
RSpec::Support.require_rspec_core 'source/syntax_highlighter'
28-
RSpec::Core::Source::SyntaxHighlighter.attempt_to_add_rspec_terms_to_coderay_keywords
27+
RSpec::Support.require_rspec_core 'formatters/syntax_highlighter'
28+
RSpec::Core::Formatters::SyntaxHighlighter.attempt_to_add_rspec_terms_to_coderay_keywords
2929
@@converter = CoderayConverter
3030
# rubocop:disable Lint/HandleExceptions
3131
rescue LoadError

lib/rspec/core/formatters/snippet_extractor.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
RSpec::Support.require_rspec_core "source"
2-
31
module RSpec
42
module Core
53
module Formatters
@@ -17,7 +15,7 @@ def self.extract_line_at(file_path, line_number)
1715

1816
def self.source_from_file(path)
1917
raise NoSuchFileError unless File.exist?(path)
20-
RSpec.world.source_cache.source_from_file(path)
18+
RSpec.world.source_from_file(path)
2119
end
2220

2321
if RSpec::Support::RubyFeatures.ripper_supported?

lib/rspec/core/source/syntax_highlighter.rb renamed to lib/rspec/core/formatters/syntax_highlighter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module RSpec
22
module Core
3-
class Source
3+
module Formatters
44
# @private
55
# Provides terminal syntax highlighting of code snippets
66
# when coderay is available.

lib/rspec/core/source.rb

Lines changed: 0 additions & 86 deletions
This file was deleted.

lib/rspec/core/source/location.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/rspec/core/source/node.rb

Lines changed: 0 additions & 93 deletions
This file was deleted.

lib/rspec/core/source/token.rb

Lines changed: 0 additions & 87 deletions
This file was deleted.

lib/rspec/core/world.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def ordered_example_groups
4040
def reset
4141
RSpec::ExampleGroups.remove_all_constants
4242
example_groups.clear
43+
@sources_by_path.clear if defined?(@sources_by_path)
44+
@syntax_highlighter = nil
4345
end
4446

4547
# @private
@@ -128,11 +130,18 @@ def reporter
128130
end
129131

130132
# @private
131-
def source_cache
132-
@source_cache ||= begin
133-
RSpec::Support.require_rspec_core "source"
134-
Source::Cache.new(@configuration)
133+
def source_from_file(path)
134+
unless defined?(@sources_by_path)
135+
RSpec::Support.require_rspec_support 'source'
136+
@sources_by_path = {}
135137
end
138+
139+
@sources_by_path[path] ||= Support::Source.from_file(path)
140+
end
141+
142+
# @private
143+
def syntax_highlighter
144+
@syntax_highlighter ||= Formatters::SyntaxHighlighter.new(@configuration)
136145
end
137146

138147
# @api private

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ module RSpec::Core
247247
end
248248

249249
it 'uses our syntax highlighter on the code snippet to format it nicely' do
250-
syntax_highlighter = instance_double(Source::SyntaxHighlighter)
250+
syntax_highlighter = instance_double(Formatters::SyntaxHighlighter)
251251
allow(syntax_highlighter).to receive(:highlight) do |lines|
252252
lines.map { |l| "<highlighted>#{l.strip}</highlighted>" }
253253
end
254254

255-
allow(RSpec.world.source_cache).to receive_messages(:syntax_highlighter => syntax_highlighter)
255+
allow(RSpec.world).to receive_messages(:syntax_highlighter => syntax_highlighter)
256256

257257
formatted = presenter.fully_formatted(1)
258258
expect(formatted).to include("<highlighted>expect('RSpec').to be_a(Integer)</highlighted>")

spec/rspec/core/source/syntax_highlighter_spec.rb renamed to spec/rspec/core/formatters/syntax_highlighter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require 'rspec/core/source/syntax_highlighter'
1+
require 'rspec/core/formatters/syntax_highlighter'
22

3-
class RSpec::Core::Source
3+
module RSpec::Core::Formatters
44
RSpec.describe SyntaxHighlighter do
55
let(:config) { RSpec::Core::Configuration.new.tap { |config| config.color_mode = :on } }
66
let(:highlighter) { SyntaxHighlighter.new(config) }

0 commit comments

Comments
 (0)