Sha256: 3d9ad51781f3bf6e0fc5e847fe56d42e715d86e94ec348af47422bb5256d7c2e

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require "petitest/texts/base_text"

module Petitest
  module Texts
    class RaisedCodeText < ::Petitest::Texts::BaseText
      # @return [Petitest::Test]
      attr_reader :test

      # @param test [Petitest::Test]
      def initialize(test:)
        @test = test
      end

      # @note Override
      def to_s
        if content = caller_file_content
          content.lines[caller_line_number - 1].strip
        else
          ""
        end
      end

      private

      # @return [String, nil]
      def caller_file_content
        if caller_path && ::FileTest.file?(caller_path)
          ::File.read(caller_path)
        end
      end

      # @return [String, nil]
      def caller_line_number
        if caller_segments[1]
          caller_segments[1].to_i
        end
      end

      # @return [String, nil]
      def caller_path
        caller_segments[0]
      end

      # @return [Array<String>]
      def caller_segments
        @caller_segments ||= begin
          if test.runner.filtered_backtrace[0]
            test.runner.filtered_backtrace[0].split(":", 3)
          else
            []
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
petitest-0.3.1 lib/petitest/texts/raised_code_text.rb
petitest-0.3.0 lib/petitest/texts/raised_code_text.rb