Sha256: 892379130560c339fb53ab732dadf4b80e3a207211eaee49fe9a83fbc700c663

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

module Petitest
  module Assertions
    # @param actual_or_message [Object]
    # @param message [String, nil]
    def assert(actual_or_message = nil, message = nil, &block)
      if block
        assert_block(actual_or_message, &block)
      else
        actual = actual_or_message
        check(
          message: message,
          template: "%{actual} is not truthy",
          template_variables: {
            actual: actual,
          },
        ) do
          actual
        end
      end
    end

    # @param message [String, nil]
    def assert_block(message = nil, &block)
      check(
        message: message,
        template: "Given block returned falsy",
        &block
      )
    end

    # @param expected [Object]
    # @param actual [Object]
    # @param message [String, nil]
    def assert_equal(expected, actual, message = nil)
      check(
        message: message,
        template: "%{expected} expected but was %{actual}",
        template_variables: {
          actual: actual,
          expected: expected,
        },
      ) do
        expected == actual
      end
    end

    private

    # @param message [String, nil]
    # @param template [String]
    # @param template_variables [Hash, nil]
    def check(
      message:,
      template:,
      template_variables: {}
    )
      unless yield
        raise ::Petitest::AssertionFailureError.new(
          additional_message: message,
          template: template,
          template_variables: template_variables,
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
petitest-0.1.3 lib/petitest/assertions.rb
petitest-0.1.2 lib/petitest/assertions.rb
petitest-0.1.1 lib/petitest/assertions.rb