lib/ass_tests/minitest/assertions.rb in ass_tests-1.2.0.alpha vs lib/ass_tests/minitest/assertions.rb in ass_tests-2.0.0.alpha

- old
+ new

@@ -10,102 +10,120 @@ end end end module AssTests - module Assertions - UNKNOWN_XML_TYPE = 'UNKNOWN_XML_TYPE' + module Minitest + module Assertions + # @api private + module Comparable + UNKNOWN_XML_TYPE = 'UNKNOWN_XML_TYPE' + SRV_CONNECTORS = [AssLauncher::Enterprise::Ole::IbConnection, + AssLauncher::Enterprise::Ole::ThickApplication] - GOOD_OLE_CONNECTORS = [ - AssLauncher::Enterprise::Ole::IbConnection, - AssLauncher::Enterprise::Ole::ThickApplication - ] - def _assert_xml_type(exp, obj, mess = nil) - act = xml_type_get(obj) - mess = message(mess) do - "Expected #{exp} xml type but #{act} given" - end - assert exp == act, mess - end + class ClientContext + attr_reader :obj, :ole_connector + def initialize(obj, ole_connector) + @obj = obj + @ole_connector = ole_connector + end - def _assert_ref_empty(obj, mess = nil) - mess = message(mess) {"Ref must be empty"} - assert obj.ref.IsEmpty, mess - end + def string_internal + fail NotImplementedError, + "You should patch method #string_internal in class: #{self.class.name}" + end - def _refute_ref_empty(obj, mess = nil) - mess = message(mess) {"Ref must not be empty"} - refute obj.ref.IsEmpty, mess - end + def xml_type + fail NotImplementedError, + "You should patch method #xml_type in class: #{self.class.name}" + end - def _assert_equal(exp, act, mess = nil) - fail_not_comparable(exp, act) if not_comparable?(exp, act) - exp_ = to_comparable(exp) - act_ = to_comparable(act) - mess = message(mess, Minitest::Assertions::E){diff exp_, act_} - assert exp_ == act_, mess - end + def as_string + ole_connector.sTring obj + end - def to_comparable(obj) - return obj unless obj.is_a? WIN32OLE - return obj.__real_obj__ if obj.__ruby__? - # TODO: make comparsation ruby object from internal Ass string + def make + r = {} + r[:as_string] = as_string + r[:xml_type] = xml_type + r[:as_string_internal] = string_internal + r + end + end - return to_comparable_srv_context(obj) if\ - GOOD_OLE_CONNECTORS.include? ole_connector.class - to_comparable_client_context(obj) - end - private :to_comparable + class SrvContext < ClientContext + def string_internal + ole_connector.ValueToStringInternal obj + end - def to_comparable_client_context(obj) - fail 'You should define own method #to_comparable_client_context'\ - ' and returns #new_comparable Hash' - end - private :to_comparable_client_context + def xml_type + return UNKNOWN_XML_TYPE if ole_connector.xmlTypeOf(obj).nil? + ole_connector.xmlTypeOf(obj).typeName + end + end - def to_comparable_srv_context(obj) - new_comparable ole_connector.sTring(obj), - xml_type_get(obj), - ole_connector.ValueToStringInternal(obj) - end - private :to_comparable_srv_context + def self.new(obj, ole_connector) + return SrvContext.new(obj, ole_connector) if\ + SRV_CONNECTORS.include? ole_connector.class + ClientContext.new(obj, ole_connector) + end + end - def new_comparable(as_string, xml_type, as_string_internal) - r = {} - r[:as_string] = as_string - r[:xml_type] = xml_type - r[:as_string_internal] = as_string_internal - r - end - private :new_comparable + def _assert_xml_type(exp, obj, mess = nil) + act = Comparable.new(obj, ole_connector).xml_type + mess = message(mess) do + "Expected #{exp} xml type but #{act} given" + end + assert exp == act, mess + end - def xml_type_get(obj) - return UNKNOWN_XML_TYPE if ole_connector.xmlTypeOf(obj).nil? - ole_connector.xmlTypeOf(obj).typeName - end - private :xml_type_get + def _assert_ref_empty(obj, mess = nil) + mess = message(mess) {"Ref must be empty"} + assert obj.ref.IsEmpty, mess + end - def not_comparable?(exp, act) - is_ruby?(exp) ^ is_ruby?(act) - end - private :not_comparable? + def _refute_ref_empty(obj, mess = nil) + mess = message(mess) {"Ref must not be empty"} + refute obj.ref.IsEmpty, mess + end - def is_ruby?(obj) - !obj.is_a? WIN32OLE or obj.__ruby__? - end - private :is_ruby? + def _assert_equal(exp, act, mess = nil) + fail_not_comparable(exp, act) if not_comparable?(exp, act) + exp_ = to_comparable(exp) + act_ = to_comparable(act) + mess = message(mess, ::Minitest::Assertions::E){diff exp_, act_} + assert exp_ == act_, mess + end - def fail_not_comparable(exp, act) - fail ArgumentError, - "Not comparable types `#{not_comparable_class exp}'"\ - " and `#{not_comparable_class act}'" - end - private :fail_not_comparable + def to_comparable(obj) + return obj unless obj.is_a? WIN32OLE + return obj.__real_obj__ if obj.__ruby__? + Comparable.new(obj, ole_connector).make + end + private :to_comparable - def not_comparable_class(obj) - return obj.__real_obj__.class if obj.respond_to? :__real_obj__ - obj.class + def not_comparable?(exp, act) + ruby?(exp) ^ ruby?(act) + end + private :not_comparable? + + def ruby?(obj) + !obj.is_a? WIN32OLE or obj.__ruby__? + end + private :ruby? + + def fail_not_comparable(exp, act) + fail ArgumentError, + "Not comparable types `#{not_comparable_class exp}'"\ + " and `#{not_comparable_class act}'" + end + private :fail_not_comparable + + def not_comparable_class(obj) + return obj.__real_obj__.class if obj.respond_to? :__real_obj__ + obj.class + end + private :not_comparable_class end - private :not_comparable_class end end