Sha256: fe60f7c5f2065cc48c99a99c9a08f8b88e36932eccbccc84467352d5a70406c0

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'minitest/autorun'
require 'hana'
require 'json'

module Hana
  class TestCase < Minitest::Test
    TESTDIR = File.dirname File.expand_path __FILE__

    def self.read_test_json_file file
      Module.new {
        tests = JSON.load File.read file
        tests.each_with_index do |test, i|
          next unless test['doc']

          define_method("test_#{test['comment'] || i }") do
            skip "disabled" if test['disabled']

            doc   = test['doc']
            patch = test['patch']

            patch = Hana::Patch.new patch

            if test['error']
              assert_raises(*ex(test['error'])) do
                patch.apply doc
              end
            else
              assert_equal(test['expected'] || doc, patch.apply(doc))
            end
          end
        end
      }
    end

    def self.skip regexp, message
      instance_methods.grep(regexp).each do |method|
        define_method(method) { skip message }
      end
    end

    private

    def ex msg
      case msg
      when /Out of bounds/i then [Hana::Patch::OutOfBoundsException]
      when /Object operation on array/ then
        [Hana::Patch::ObjectOperationOnArrayException]
      when /test op shouldn't get array element/ then
        [Hana::Patch::IndexError, Hana::Patch::ObjectOperationOnArrayException]
      when /bad number$/ then
        [Hana::Patch::IndexError, Hana::Patch::ObjectOperationOnArrayException]
      when /missing '(from|value)' parameter/ then
        [KeyError]
      when /Unrecognized op 'spam'/ then
        [Hana::Patch::Exception]
      when /missing|non-existent/ then
        [Hana::Patch::MissingTargetException]
      else
        [Hana::Patch::FailedTestException]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hana-1.3.1 test/helper.rb