Sha256: ad8a2f0b80b2b2ecf1f39a65bd16b23810c5e1f4656ea6f316f66f5df63c20d2

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'colorize'
require 'diffy'

module Asciidoctor
  module DocTest
    ##
    # Module to be included into +Minitest::Test+ to use Diffy for diff.
    module MinitestDiffy

      # @private
      def self.included(base)
        base.make_my_diffs_pretty!
      end

      ##
      # Returns diff between +exp+ and +act+ (if needed) using Diffy.
      #
      # @note Overrides method from +Minitest::Assertions+.
      def diff(exp, act)
        expected = mu_pp_for_diff(exp)
        actual = mu_pp_for_diff(act)

        if need_diff? expected, actual
          ::Diffy::Diff.new(expected, actual, context: 3).to_s
        else
          "Expected: #{mu_pp(exp)}\n  Actual: #{mu_pp(act)}"
        end
      end

      ##
      # Returns +true+ if diff should be printed (using Diffy) for the given
      # content, +false+ otherwise.
      #
      # @param expected [String]
      # @param actual [String]
      #
      def need_diff?(expected, actual)
        expected.include?("\n") ||
          actual.include?("\n") ||
          expected.size > 30    ||
          actual.size > 30      ||
          expected == actual
      end
    end
  end
end

module Diffy
  module Format

    ##
    # ANSI color output suitable for terminal, customized for minitest.
    def minitest
      padding = ' ' * 2
      ary = map do |line|
        case line
        when /^(---|\+\+\+|\\\\)/
          # ignore
        when /^\\\s*No newline at end of file/
          # ignore
        when /^\+/
          line.chomp.sub(/^\+/, 'A' + padding).red
        when /^-/
          line.chomp.sub(/^\-/, 'E' + padding).green
        else
          padding + line.chomp
        end
      end
      "\n" + ary.compact.join("\n")
    end
  end
end

Diffy::Diff.default_format = :minitest

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
asciidoctor-doctest-1.5.1.2 lib/asciidoctor/doctest/minitest_diffy.rb
asciidoctor-doctest-1.5.1.1 lib/asciidoctor/doctest/minitest_diffy.rb
asciidoctor-doctest-1.5.1 lib/asciidoctor/doctest/minitest_diffy.rb