ClassLib_Test.MyClass.TestGetCustomAttribute_Type2 C# (CSharp) Method

TestGetCustomAttribute_Type2() public method

public TestGetCustomAttribute_Type2 ( MemberInfo mem ) : bool
mem System.Reflection.MemberInfo
return bool
 virtual public bool TestGetCustomAttribute_Type2 (MemberInfo mem)
   {
   Util.print ("\n\n" + this.GetType ().ToString () + " - TestGetCustomAttribute_Type2() started.");
   Util.print ("For: " + mem.GetType ().ToString () + "\n");
   string strLoc="L_111_2_000";
   String str = null;
   Object[] attrs = null;
   try  
     {
     do
       {
       iCountTestcases++;
       strLoc="L_111_2_001";
       iCountTestcases++;
       strLoc="L_111_2_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMfalse_INfalse), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_2_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_2_002.3";
       if (attrs.Length != iCountAMfalseINfalseAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_2_jhd3 - attrs.Length != iCountAMfalseINfalseAttrs");
       Util.print ("Expected: " + iCountAMfalseINfalseAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_2_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMfalse_INfalse[])) {
       iCountErrors++;
       Util.printerr ("E_111_2_jhd32 - attrs is ClassLib_Attributes.CA_AMfalse_INfalse[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_2_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_2_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMfalse_INfalse) {
       ClassLib_Attributes.CA_AMfalse_INfalse attr = (ClassLib_Attributes.CA_AMfalse_INfalse) attrs[i];
       if (attr.name.Equals ("CA_AMfalse_INfalse")) {
       iCountTestcases++;
       strLoc="L_111_2_001.5";
       if (!caAmfInf.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_2_8d2p- caAmfInf2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_2_iu2h- UnExpected attr.name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_2_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_3_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMfalse_INtrue), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_3_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_3_002.3";
       if (attrs.Length != iCountAMfalseINtrueAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_3_jhd3 - attrs.Length != iCountAMfalseINtrueAttrs");
       Util.print ("Expected: " + iCountAMfalseINtrueAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_3_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMfalse_INtrue[])) {
       iCountErrors++;
       Util.printerr ("E_111_3_jhd32 - attrs is ClassLib_Attributes.CA_AMfalse_INtrue[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_3_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_3_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMfalse_INtrue) {
       ClassLib_Attributes.CA_AMfalse_INtrue attr = (ClassLib_Attributes.CA_AMfalse_INtrue) attrs[i];
       if (attr.name.Equals ("CA_AMfalse_INtrue")) {
       iCountTestcases++;
       strLoc="L_111_3_001.5";
       if (!caAmfInt.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_3_8d2p- caAmfInt2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_3_iu2h- UnExpected attr.name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_3_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_4_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMtrue_INtrue), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_4_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_4_002.3";
       if (attrs.Length != iCountAMtrueINtrueAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_4_jhd3 - attrs.Length != iCountAMtrueINtrueAttrs");
       Util.print ("Expected: " + iCountAMtrueINtrueAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_4_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMtrue_INtrue[])) {
       iCountErrors++;
       Util.printerr ("E_111_4_jhd32 - attrs is ClassLib_Attributes.CA_AMtrue_INtrue[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_4_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_4_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMtrue_INtrue) {
       ClassLib_Attributes.CA_AMtrue_INtrue attr = (ClassLib_Attributes.CA_AMtrue_INtrue) attrs[i];
       if (attr.name.Equals ("CA_AMtrue_INtrue")) {
       iCountTestcases++;
       strLoc="L_111_4_001.5";
       if (!caAmtInt.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_4_8d2p5- caAmtInt.Equals FAiLed!");
       }
       }
       else if (attr.name.Equals ("CA_AMtrue_INtrue2")) {
       iCountTestcases++;
       strLoc="L_111_4_001.6";
       if (!caAmtInt2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_4_8d2p6- caAmtInt2.Equals FAiLed!");
       }
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_4_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_5_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMtrue_INfalse), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_5_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_5_002.3";
       if (attrs.Length != iCountAMtrueINfalseAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_5_jhd3 - attrs.Length != iCountAMtrueINfalseAttrs");
       Util.print ("Expected: " + iCountAMtrueINfalseAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_5_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMtrue_INfalse[])) {
       iCountErrors++;
       Util.printerr ("E_111_5_jhd32 - attrs is ClassLib_Attributes.CA_AMtrue_INfalse[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_5_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_5_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMtrue_INfalse) {
       ClassLib_Attributes.CA_AMtrue_INfalse attr = (ClassLib_Attributes.CA_AMtrue_INfalse) attrs[i];
       if (attr.name.Equals ("CA_AMtrue_INfalse")) {
       iCountTestcases++;
       strLoc="L_111_5_001.5";
       if (!caAmtInf.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_5_8d2p5- caAmtInf.Equals FAiLed!");
       }
       }
       else if (attr.name.Equals ("CA_AMtrue_INfalse2")) {
       iCountTestcases++;
       strLoc="L_111_5_001.5";
       if (!caAmtInf2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_5_8d2p5- caAmtInf2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_5_djfh - UnExpected attr name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_5_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_6_002.2";
       attrs = mem.GetCustomAttributes (typeof (System.Attribute), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_6_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_6_002.3";
        
        if (attrs.Length != (iCountTotalAttrs)) {
        iCountErrors++;
        Util.printerr ("E_111_6_jhd3 - attrs.Length != iCountTotalAttrs+1");
        Util.print ("Expected: " + (iCountTotalAttrs+1) + ";Returned: " + attrs.Length);					
        }
                
       iCountTestcases++;
       strLoc="L_111_6_002.4";
       TestAttributes (attrs, mem);
       } while ( false );
     }
   catch( Exception exc_runTest ) {
   ++iCountErrors;
   Util.printerr ("E_111_333un! - Uncaught Exception caught in TestGetCustomAttribute_Type(); strLoc == " + strLoc);
   Util.printexc (exc_runTest);
   Util.print (exc_runTest.StackTrace);
   }
   if ( iCountErrors == 0 ) {   return true; }
   else {
   return false;
   }
   }
}	

Usage Example

 public bool RunTest()
   {
   print (strPath + strTest + "  RunTest() started.");
   string strLoc="L_000";
   MyClass myclass = new MyClass ();
   MyClass2 myclass2 = new MyClass2 ();
   MemberInfo mem = null;
   MemberInfo mem2 = null;
   Attribute[] attrs = null;
   Type type = null;
   Type type2 = null;
   try  
     {
     strLoc="L_200_001";
     iCountTestcases++;
     strLoc="L_200_002";
     type = myclass.GetType ();
     mem = type.GetMethod ("MyMethod");
     if (mem == null) {
     iCountErrors++;
     Util.printerr ("E_200_75yhg - mem == null");
     }
     iCountTestcases++;
     strLoc="L_200_002.1";
     try {
     attrs = Attribute.GetCustomAttributes (mem, null, true);
     iCountErrors++;
     Util.printerr ("E_200_oi32 - Should 've thrown ArgNullExc");
     }
     catch (ArgumentException ) {
     }
     iCountTestcases++;
     strLoc="L_200_002.1.2";
     try {
     attrs = Attribute.GetCustomAttributes ((MemberInfo) null, typeof (ClassLib_Attributes.CA_AMfalse_INfalse), true);
     iCountErrors++;
     Util.printerr ("E_200_oi322 - Should 've thrown ArgNullExc");
     }
     catch (ArgumentException ) {
     }
     iCountTestcases++;
     strLoc="L_200_002.5";
     type = myclass.GetType ();
     mem = type;
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_002.5.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_200_003.4";
     mem = type.GetConstructor (Type.EmptyTypes);
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_003.4.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_200_003.5";
     mem = type.GetMethod ("MyMethod");
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_003.5.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_200_004.5";
     mem = type.GetField ("MyField");
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_004.5.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_200_005.5";
     mem = type.GetProperty ("MyProp");
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_005.5.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_200_005.6";
     mem = type.GetEvent ("MyEvent");
     myclass.TestGetCustomAttribute_Type (mem);
     iCountTestcases++;
     strLoc="L_200_005.6.2";
     myclass.TestGetCustomAttribute_Type2 (mem);
     iCountTestcases++;
     strLoc="L_2_200_002.5";
     type2 = myclass2.GetType ();
     mem2 = type2;
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_002.5.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     iCountTestcases++;
     strLoc="L_2_200_003.4";
     mem2 = type2.GetConstructor (Type.EmptyTypes);
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_003.4.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     iCountTestcases++;
     strLoc="L_2_200_003.5";
     mem2 = type2.GetMethod ("MyMethod");
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_003.5.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     iCountTestcases++;
     strLoc="L_2_200_004.5";
     mem2 = type2.GetField ("MyField");
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_004.5.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     iCountTestcases++;
     strLoc="L_2_200_005.5";
     mem2 = type2.GetProperty ("MyProp");
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_005.5.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     iCountTestcases++;
     strLoc="L_2_200_005.6";
     mem2 = type2.GetEvent ("MyEvent");
     myclass2.TestGetCustomAttribute_Type (mem2);
     iCountTestcases++;
     strLoc="L_2_200_005.6.2";
     myclass2.TestGetCustomAttribute_Type2 (mem2);
     }
   catch( Exception exc_runTest ) {
   ++myclass2.iCountErrors;
   printerr ("Err_888un! - Uncaught Exception caught in runTest(); strLoc == " + strLoc);
   printexc (exc_runTest);
   print (exc_runTest.StackTrace);
   }
   iCountErrors = myclass.iCountErrors + myclass2.iCountErrors;
   iCountTestcases = myclass.iCountTestcases + myclass2.iCountTestcases;
   if ( iCountErrors == 0 ) {   return true; }
   else {
   print  ("Related Bugs: " + strBug);
   print ("FAiL!   " + strPath +strTest + "  iCountErrors==" +  iCountErrors.ToString ());
   print ("iCountTestcases==" +  iCountTestcases.ToString ());
   return false;
   }
   }