Sha256: ac6488ce26248f20dc1993a01ef96c0ef1e6e0c4a6fbf83dbe5f9e2099508567

Contents?: true

Size: 1009 Bytes

Versions: 4

Compression:

Stored size: 1009 Bytes

Contents

require "petitest/texts/base_text"

module Petitest
  module Texts
    class RaisedCodeText < ::Petitest::Texts::BaseText
      # @return [Petitest::TestCase]
      attr_reader :test_case

      # @param test_case [Petitest::TestCase]
      def initialize(test_case:)
        @test_case = test_case
      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 ::FileTest.file?(caller_path)
          ::File.read(caller_path)
        end
      end

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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
petitest-0.1.3 lib/petitest/texts/raised_code_text.rb
petitest-0.1.2 lib/petitest/texts/raised_code_text.rb
petitest-0.1.1 lib/petitest/texts/raised_code_text.rb
petitest-0.1.0 lib/petitest/texts/raised_code_text.rb