1

I have overloaded several compare operators like operator== in my class CXmlAttr. Then I wrote generic functions where I used these operators and was shocked that this didn’t work in C# or not in every case. Then I recoded some generic functions to explicit functions which work with the CXmlAttr type as argument. Additional I decided to use extensive unit test functions to check the behavior in several situations. And there was the next problem. I wanted to debug these unit test and recognized that Assert.AreEqual<> didn’t call my overloaded== operators nor my Equal functions. I set breakpoints in the appropriate functions but the debugger didn’t stop the execution. Then I tried to step into in the code, but this didn’t work too even I have configured to debug .NET code.

Question: So does anybody know how Assert.AreEqual<> works and why my breakpoint didn’t work?

Assert.IsTrue(…==…) works fine.
I’m very unsure about how and if Assert.AreEqual<> works and if I can trust the results

public static void CompareCXmlAttr(LibKat.CXmlAttr left_attr, LibKat.CXmlAttr right_attr, EOper oper)
   {
       //  just for debugging
       System.Type gen_type = typeof(LibKat.CXmlAttr);
       string s_gen_name = gen_type.FullName;
       dynamic left = left_attr;
       dynamic right = right_attr;

       switch (oper)
       {
           case EOper.equal:
               //  operator== left == right -> true
               //debugger doesn't stop and no exception
               Assert.AreEqual<LibKat.CXmlAttr>(left, right, "typeof(T) == {0}, operator== (left == right) == false - but should be true", s_gen_name);
               //debugger stops in operator== function
               Assert.IsTrue((left == right), "typeof(T) == {0}, operator== (left == right) == false - but should be true", s_gen_name);
               //debugger doesn't stop and no exception
               Assert.AreEqual<LibKat.CXmlAttr>(left_attr, right_attr, "typeof(T) == {0}, operator== (left_attr == right_attr) == false - but should be true", s_gen_name);
               ////debugger stops in operator== function
               Assert.IsTrue((left_attr == right_attr), "typeof(T) == {0}, operator== (left_attr == right_attr) == false - but should be true", s_gen_name);

               break;
           case EOper.not_equal:
               //  operator!= left != right -> true


               break;
           case EOper.less:
           //result = (left < right ? true : false);
           case EOper.less_equal:
           //result = (left <= right ? true : false);
           case EOper.greater:
           //result = (left > right ? true : false);
           case EOper.greater_equal:
           //result = (left >= right ? true : false);
           default:
               System.Type type_oper = oper.GetType();
               string s_type_name = type_oper.FullName;
               string s_message = string.Format("typeof(T) == {0}, wrong (operator == {1}.{2}), only operator EOper.equal, EOper.not_equal allowed", s_gen_name, s_type_name, oper);
               string s_identifier_name = IdentifierAsStringGetFromLinqExpression<EOper>(() => oper); //prepare code for expression to parse and get the identifier name as string
               string s_ident_name = IdentifierAsStringGetFromTypeProperty(new { oper }); //generic call to get type info and name of identifier inside the initializer list - the faster version
               throw new System.ArgumentException(s_message, s_ident_name);
       }

   }
melgoth
  • 11
  • 4

1 Answers1

0

I had a very similar situation but I can't be sure it's the same cause because your code appears incomplete (I don't find LibKat.CXmlAttr anywhere on the internet other than in this question). However, you might look at whether or not you (or LibKat) implemented operator == but did not override Object.Equals. This would cause similar behavior and should generate a tell-tale CS0660 compiler warning.

bronzenose
  • 631
  • 5
  • 6