Sha256: c947694a20bc022e8f1e82aaccc0d9587bf74e0bfbfcb966c993603987edba8e

Contents?: true

Size: 1.49 KB

Versions: 59

Compression:

Stored size: 1.49 KB

Contents

module Test
  module Unit
    class CodeSnippetFetcher
      def initialize
        @sources = {}
      end

      def fetch(path, line, options={})
        n_context_line = options[:n_context_line] || 3
        lines = source(path)
        return [] if lines.nil?
        min_line = [line - n_context_line, 1].max
        max_line = [line + n_context_line, lines.length].min
        window = min_line..max_line
        window.collect do |n|
          attributes = {:target_line? => (n == line)}
          [n, lines[n - 1].chomp, attributes]
        end
      end

      def source(path)
        @sources[path] ||= read_source(path)
      end

      private
      def read_source(path)
        return nil unless File.exist?(path)
        lines = []
        File.open(path) do |file|
          first_line = file.gets
          break if first_line.nil?
          encoding = detect_encoding(first_line)
          if encoding
            first_line.force_encoding(encoding)
            file.set_encoding(encoding, encoding)
          end
          lines << first_line
          lines.concat(file.readlines)
        end
        lines
      end

      def detect_encoding(first_line)
        return nil unless first_line.respond_to?(:ascii_only?)
        return nil unless first_line.ascii_only?
        if /\b(?:en)?coding[:=]\s*([a-z\d_-]+)/i =~ first_line
          begin
            Encoding.find($1)
          rescue ArgumentError
            nil
          end
        else
          nil
        end
      end
    end
  end
end

Version data entries

59 entries across 53 versions & 10 rubygems

Version Path
postfinancecheckout-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.3.0 vendor/bundle/ruby/2.7.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.2.1 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.2.0 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
postfinancecheckout-ruby-sdk-3.2.0 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.1.5 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
postfinancecheckout-ruby-sdk-3.1.5 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.2.0 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.9 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.8 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.7 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.6 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.5 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
postfinancecheckout-ruby-sdk-3.1.4 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.4 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.1.4 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.1.3 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.3 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
whitelabelmachinename-ruby-sdk-3.1.2 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb
wallee-ruby-sdk-3.1.2 vendor/bundle/ruby/2.4.0/gems/test-unit-3.2.9/lib/test/unit/code-snippet-fetcher.rb