From 690f9445146101eff2d98bb22e04f571f90a75bc Mon Sep 17 00:00:00 2001 From: LoxSmoke <34951331+loxsmoke@users.noreply.github.com> Date: Sat, 15 Jun 2024 22:34:58 -0700 Subject: [PATCH] Unit test formatting and the fix for ubuntu newlines --- .../DocXmlOtherLibForUnitTests.csproj | 2 +- test/DocXmlUnitTests/BaseTestClass.cs | 2 - test/DocXmlUnitTests/DocXmlReaderUnitTests.cs | 4 +- test/DocXmlUnitTests/DocXmlUnitTests.csproj | 2 +- test/DocXmlUnitTests/EnumCommentsUnitTests.cs | 83 ++--- test/DocXmlUnitTests/MemberInfoUnitTests.cs | 4 +- .../MethodCommentsUnitTests.cs | 295 ++++++++++-------- test/DocXmlUnitTests/PropertyInfoUnitTests.cs | 28 +- test/DocXmlUnitTests/TypeCommentsUnitTests.cs | 39 ++- 9 files changed, 250 insertions(+), 209 deletions(-) diff --git a/test/DocXmlOtherLibForUnitTests/DocXmlOtherLibForUnitTests.csproj b/test/DocXmlOtherLibForUnitTests/DocXmlOtherLibForUnitTests.csproj index 0b7e33a..d399495 100644 --- a/test/DocXmlOtherLibForUnitTests/DocXmlOtherLibForUnitTests.csproj +++ b/test/DocXmlOtherLibForUnitTests/DocXmlOtherLibForUnitTests.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 Debug;Release;Coverage diff --git a/test/DocXmlUnitTests/BaseTestClass.cs b/test/DocXmlUnitTests/BaseTestClass.cs index d4a997b..5ed1ad0 100644 --- a/test/DocXmlUnitTests/BaseTestClass.cs +++ b/test/DocXmlUnitTests/BaseTestClass.cs @@ -11,13 +11,11 @@ public class BaseTestClass { public DocXmlReader Reader { get; set; } public DocXmlReader MultiAssemblyReader { get; set; } - public Type MyClass_Type; public void Setup() { Reader = new DocXmlReader("DocXmlUnitTests.xml"); MultiAssemblyReader = new DocXmlReader((a) => Path.GetFileNameWithoutExtension(a.Location) + ".xml"); - MyClass_Type = typeof(MyClass); } public void AssertParam(MethodComments comments, int paramIndex, string name, string text) diff --git a/test/DocXmlUnitTests/DocXmlReaderUnitTests.cs b/test/DocXmlUnitTests/DocXmlReaderUnitTests.cs index c4e7e6d..5d44d41 100644 --- a/test/DocXmlUnitTests/DocXmlReaderUnitTests.cs +++ b/test/DocXmlUnitTests/DocXmlReaderUnitTests.cs @@ -52,8 +52,8 @@ First paragraph. Second paragraph. -", - summary); +".ReplaceLineEndings(), + summary.ReplaceLineEndings()); } [TestMethod] diff --git a/test/DocXmlUnitTests/DocXmlUnitTests.csproj b/test/DocXmlUnitTests/DocXmlUnitTests.csproj index bd0f2c4..2af1ec6 100644 --- a/test/DocXmlUnitTests/DocXmlUnitTests.csproj +++ b/test/DocXmlUnitTests/DocXmlUnitTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 false diff --git a/test/DocXmlUnitTests/EnumCommentsUnitTests.cs b/test/DocXmlUnitTests/EnumCommentsUnitTests.cs index 97485ee..aaa29a2 100644 --- a/test/DocXmlUnitTests/EnumCommentsUnitTests.cs +++ b/test/DocXmlUnitTests/EnumCommentsUnitTests.cs @@ -38,33 +38,36 @@ void AssertBigEnumComment(BigInteger expectedValue, string expectedName, string [TestMethod] public void GetEnumComments_NotEnumType() { - Assert.ThrowsException(() => Reader.GetEnumComments(MyClass_Type)); + Assert.ThrowsException(() => Reader.GetEnumComments(typeof(MyClass))); } [TestMethod] public void GetEnumComments() { - var mm = Reader.GetEnumComments(typeof(TestEnum2)); - Assert.AreEqual("Enum 2 type description", mm.Summary); - Assert.AreEqual(0, mm.ValueComments.Count); + var comments = Reader.GetEnumComments(typeof(TestEnum2)); + + Assert.AreEqual("Enum 2 type description", comments.Summary); + Assert.AreEqual(0, comments.ValueComments.Count); } [TestMethod] public void GetEnumComments_NoComment() { - var mm = Reader.GetEnumComments(typeof(MyNoCommentClass.TestEnumNoComments)); - Assert.IsNull(mm.Summary); - Assert.AreEqual(0, mm.ValueComments.Count); + var comments = Reader.GetEnumComments(typeof(MyNoCommentClass.TestEnumNoComments)); + + Assert.IsNull(comments.Summary); + Assert.AreEqual(0, comments.ValueComments.Count); } [TestMethod] public void GetEnumComments_Values_NoComments() { - var mm = Reader.GetEnumComments(typeof(TestEnum2), true); - Assert.AreEqual("Enum 2 type description", mm.Summary); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertEnumComment(0, "Value21", string.Empty, mm.ValueComments[0]); - AssertEnumComment(1, "Value22", string.Empty, mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnum2), true); + + Assert.AreEqual("Enum 2 type description", comments.Summary); + Assert.AreEqual(2, comments.ValueComments.Count); + AssertEnumComment(0, "Value21", string.Empty, comments.ValueComments[0]); + AssertEnumComment(1, "Value22", string.Empty, comments.ValueComments[1]); } [TestMethod] @@ -89,56 +92,62 @@ public void EnumValueComments_ToString() [TestMethod] public void GetEnumComments_WithValues_Comments() { - var mm = Reader.GetEnumComments(typeof(TestEnumWithValueComments)); - Assert.AreEqual("Enum type description", mm.Summary); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertEnumComment(10, "Value1", "Enum value one", mm.ValueComments[0]); - AssertEnumComment(20, "Value2", "Enum value two", mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnumWithValueComments)); + + Assert.AreEqual("Enum type description", comments.Summary); + Assert.AreEqual(2, comments.ValueComments.Count); + AssertEnumComment(10, "Value1", "Enum value one", comments.ValueComments[0]); + AssertEnumComment(20, "Value2", "Enum value two", comments.ValueComments[1]); } [TestMethod] public void GetEnumComments_WithValue_Comments_OtherAssembly() { - var mm = new DocXmlReader().GetEnumComments(typeof(OtherEnum)); - Assert.AreEqual("Other enum", mm.Summary); - Assert.AreEqual(1, mm.ValueComments.Count); - AssertEnumComment(1, "Value1", "Enum value one", mm.ValueComments[0]); + var comments = new DocXmlReader().GetEnumComments(typeof(OtherEnum)); + + Assert.AreEqual("Other enum", comments.Summary); + Assert.AreEqual(1, comments.ValueComments.Count); + AssertEnumComment(1, "Value1", "Enum value one", comments.ValueComments[0]); } [TestMethod] public void GetEnumComments_WithValues_UInt8() { - var mm = Reader.GetEnumComments(typeof(TestEnumUInt8)); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertEnumComment(10, "Value1", "Enum value one", mm.ValueComments[0]); - AssertEnumComment(20, "Value2", "Enum value two", mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnumUInt8)); + + Assert.AreEqual(2, comments.ValueComments.Count); + AssertEnumComment(10, "Value1", "Enum value one", comments.ValueComments[0]); + AssertEnumComment(20, "Value2", "Enum value two", comments.ValueComments[1]); } [TestMethod] public void GetEnumComments_WithValues_UInt64() { - var mm = Reader.GetEnumComments(typeof(TestEnumUInt64)); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", mm.ValueComments[0]); - AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnumUInt64)); + + Assert.AreEqual(2, comments.ValueComments.Count); + AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", comments.ValueComments[0]); + AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", comments.ValueComments[1]); } [TestMethod] public void GetEnumComments_WithValues_Int64() { - var mm = Reader.GetEnumComments(typeof(TestEnumInt64)); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", mm.ValueComments[0]); - AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnumInt64)); + + Assert.AreEqual(2, comments.ValueComments.Count); + AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", comments.ValueComments[0]); + AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", comments.ValueComments[1]); } [TestMethod] public void GetEnumComments_WithNegativeValues() { - var mm = Reader.GetEnumComments(typeof(TestEnumWithNegativeValues)); - Assert.AreEqual(2, mm.ValueComments.Count); - AssertEnumComment(-20, "Value1", "Enum value one", mm.ValueComments[0]); - AssertEnumComment(-10, "Value2", "Enum value two", mm.ValueComments[1]); + var comments = Reader.GetEnumComments(typeof(TestEnumWithNegativeValues)); + + Assert.AreEqual(2, comments.ValueComments.Count); + AssertEnumComment(-20, "Value1", "Enum value one", comments.ValueComments[0]); + AssertEnumComment(-10, "Value2", "Enum value two", comments.ValueComments[1]); } } } diff --git a/test/DocXmlUnitTests/MemberInfoUnitTests.cs b/test/DocXmlUnitTests/MemberInfoUnitTests.cs index 0c8dc84..c24761c 100644 --- a/test/DocXmlUnitTests/MemberInfoUnitTests.cs +++ b/test/DocXmlUnitTests/MemberInfoUnitTests.cs @@ -25,8 +25,10 @@ public class MemberInfoUnitTests : BaseTestClass [DataRow(nameof(MyClass.genericTypeField), "Generic field description")] public void GetMemberComment(string memberName, string expectedComment) { - var memberInfo = MyClass_Type.GetMember(memberName).First(); + var memberInfo = typeof(MyClass).GetMember(memberName).First(); + var comment = Reader.GetMemberComment(memberInfo); + Assert.AreEqual(expectedComment, comment); } } diff --git a/test/DocXmlUnitTests/MethodCommentsUnitTests.cs b/test/DocXmlUnitTests/MethodCommentsUnitTests.cs index dc13c35..06b1209 100644 --- a/test/DocXmlUnitTests/MethodCommentsUnitTests.cs +++ b/test/DocXmlUnitTests/MethodCommentsUnitTests.cs @@ -14,72 +14,41 @@ namespace DocXmlUnitTests [TestClass] public class MethodCommentsUnitTests : BaseTestClass { - public MethodInfo MyClass_MemberFunction; - public MethodInfo MyClass_MemberFunction2_string; - public MethodInfo MyClass_MemberFunction2_int; - public MethodInfo MyClass_MemberFunctionWithArray; - public MethodInfo MyClass_TemplateMethod; - public MethodInfo MyClass_TemplateMethod2; - public MethodInfo MyClass_TemplateMethod3; - public MethodInfo MyClass_PlusOperator; - public MethodInfo MyClass_ItemProperty; - public MethodInfo MyClass_explicitOperator; - - public MethodInfo MyNoCommentClass_Method; - - public MethodInfo MySubClass_TemplateMethod3; - public MethodInfo MySubClass_MethodWithComments; - [TestInitialize] public new void Setup() { base.Setup(); - MyClass_MemberFunction = MyClass_Type.GetMethod(nameof(MyClass.MemberFunction)); - MyClass_MemberFunction2_string = MyClass_Type.GetMethod( - nameof(MyClass.MemberFunction2), - new[] { typeof(string), typeof(int).MakeByRefType() }); - MyClass_MemberFunction2_int = MyClass_Type.GetMethod( - nameof(MyClass.MemberFunction2), - new[] { typeof(int), typeof(int).MakeByRefType() }); - MyClass_MemberFunctionWithArray = MyClass_Type.GetMethod(nameof(MyClass.MemberFunctionWithArray)); - MyClass_TemplateMethod = MyClass_Type.GetMethod(nameof(MyClass.TemplateMethod)); - MyClass_TemplateMethod2 = MyClass_Type.GetMethod(nameof(MyClass.TemplateMethod2)); - MyClass_TemplateMethod3 = MyClass_Type.GetMethod(nameof(MyClass.TemplateMethod3)); - MyClass_PlusOperator = MyClass_Type.GetMethods().FirstOrDefault( - mt => mt.IsSpecialName && mt.Name == "op_Addition"); - MyClass_ItemProperty = MyClass_Type.GetMethods().FirstOrDefault( - mt => mt.IsSpecialName && mt.Name == "get_Item"); - MyClass_explicitOperator = MyClass_Type.GetMethods().FirstOrDefault( - mt => mt.IsSpecialName && mt.Name == "op_Explicit"); - - MyNoCommentClass_Method = typeof(MyNoCommentClass).GetMethod(nameof(MyNoCommentClass.Method)); - - MySubClass_TemplateMethod3 = typeof(MySubClass).GetMethod(nameof(MySubClass.TemplateMethod3)); - MySubClass_MethodWithComments = typeof(MySubClass).GetMethod(nameof(MySubClass.MethodWithComments)); } [TestMethod] public void GetMethodComments_Constructor_NoParams() { - var constr = MyClass_Type.GetConstructor(Array.Empty()); - var mm = Reader.GetMethodComments(constr); - Assert.AreEqual(0, mm.Parameters.Count); - Assert.AreEqual("Constructor with no parameters", mm.Summary); + var constructor = typeof(MyClass).GetConstructor(Array.Empty()); + + var comments = Reader.GetMethodComments(constructor); + + Assert.AreEqual(0, comments.Parameters.Count); + Assert.AreEqual("Constructor with no parameters", comments.Summary); } [TestMethod] public void GetMethodComments_Constructor_OneParam() { - var constr = MyClass_Type.GetConstructor(new[] { typeof(int) }); - var mm = Reader.GetMethodComments(constr); - AssertParam(mm, 0, "one", "Parameter one"); - Assert.AreEqual("Constructor with one parameter", mm.Summary); + var constructor = typeof(MyClass).GetConstructor(new[] { typeof(int) }); + + var comments = Reader.GetMethodComments(constructor); + + AssertParam(comments, 0, "one", "Parameter one"); + Assert.AreEqual("Constructor with one parameter", comments.Summary); } [TestMethod] public void GetMethodComments() { - var comments = Reader.GetMethodComments(MyClass_MemberFunction); + var method = typeof(MyClass).GetMethod(nameof(MyClass.MemberFunction)); + + var comments = Reader.GetMethodComments(method); + Assert.AreEqual("Member function description", comments.Summary); Assert.AreEqual(0, comments.Parameters.Count); Assert.AreEqual("Return value description", comments.Returns); @@ -104,141 +73,184 @@ public void GetMethodComments() [TestMethod] public void GetMethodComments_Empty() { - var mm = Reader.GetMethodComments(MyNoCommentClass_Method); - Assert.IsNull(mm.Summary); - Assert.AreEqual(0, mm.Parameters.Count); - Assert.IsNull(mm.Returns); + var method = typeof(MyNoCommentClass).GetMethod(nameof(MyNoCommentClass.Method)); + + var comments = Reader.GetMethodComments(method); + + Assert.IsNull(comments.Summary); + Assert.AreEqual(0, comments.Parameters.Count); + Assert.IsNull(comments.Returns); } [TestMethod] public void GetMethodComments_1_Param() { - var mm = Reader.GetMethodComments(MyClass_MemberFunction2_string); + var method = typeof(MyClass).GetMethod( + nameof(MyClass.MemberFunction2), + new[] { typeof(string), typeof(int).MakeByRefType() }); + + var comments = Reader.GetMethodComments(method); - Assert.AreEqual("Member function description. 2", mm.Summary); - Assert.AreEqual(2, mm.Parameters.Count); - AssertParam(mm, 0, "one", "Parameter one"); - AssertParam(mm, 1, "two", "Parameter two"); - Assert.AreEqual("Return value description", mm.Returns); + Assert.AreEqual("Member function description. 2", comments.Summary); + Assert.AreEqual(2, comments.Parameters.Count); + AssertParam(comments, 0, "one", "Parameter one"); + AssertParam(comments, 1, "two", "Parameter two"); + Assert.AreEqual("Return value description", comments.Returns); } [TestMethod] public void GetMethodComments_2_Params() { - var mm = Reader.GetMethodComments(MyClass_MemberFunction2_int); + var method = typeof(MyClass).GetMethod( + nameof(MyClass.MemberFunction2), + new[] { typeof(int), typeof(int).MakeByRefType() }); + + var comments = Reader.GetMethodComments(method); - Assert.AreEqual("Member function description. Overload 2", mm.Summary); - Assert.AreEqual(2, mm.Parameters.Count); - AssertParam(mm, 0, "one", "Parameter one"); - AssertParam(mm, 1, "two", "Parameter two"); - Assert.AreEqual("Return value description", mm.Returns); + Assert.AreEqual("Member function description. Overload 2", comments.Summary); + Assert.AreEqual(2, comments.Parameters.Count); + AssertParam(comments, 0, "one", "Parameter one"); + AssertParam(comments, 1, "two", "Parameter two"); + Assert.AreEqual("Return value description", comments.Returns); } [TestMethod] public void GetMethodComments_ArrayParams() { - var mm = Reader.GetMethodComments(MyClass_MemberFunctionWithArray); + var method = typeof(MyClass).GetMethod(nameof(MyClass.MemberFunctionWithArray)); + + var comments = Reader.GetMethodComments(method); - Assert.AreEqual("MemberFunctionWithArray description", mm.Summary); - Assert.AreEqual(2, mm.Parameters.Count); - AssertParam(mm, 0, "array1", "Parameter array1"); - AssertParam(mm, 1, "array2", "Parameter array2"); - Assert.AreEqual("Return value description", mm.Returns); + Assert.AreEqual("MemberFunctionWithArray description", comments.Summary); + Assert.AreEqual(2, comments.Parameters.Count); + AssertParam(comments, 0, "array1", "Parameter array1"); + AssertParam(comments, 1, "array2", "Parameter array2"); + Assert.AreEqual("Return value description", comments.Returns); } [TestMethod] public void GetMethodComments_StaticOperator() { - var mm = Reader.GetMethodComments(MyClass_PlusOperator); - Assert.AreEqual("Operator description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(2, mm.Parameters.Count); - AssertParam(mm, 0, "param1", "Parameter param1"); - AssertParam(mm, 1, "param2", "Parameter param2"); + var method = typeof(MyClass).GetMethods().FirstOrDefault( + mt => mt.IsSpecialName && mt.Name == "op_Addition"); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual("Operator description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(2, comments.Parameters.Count); + AssertParam(comments, 0, "param1", "Parameter param1"); + AssertParam(comments, 1, "param2", "Parameter param2"); } [TestMethod] public void GetMethodComments_IndexerProperty() { - var mm = Reader.GetMethodComments(MyClass_ItemProperty); - Assert.AreEqual("Property description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(1, mm.Parameters.Count); - AssertParam(mm, 0, "parameter", "Parameter description"); + var itemProperty = typeof(MyClass).GetMethods().FirstOrDefault( + mt => mt.IsSpecialName && mt.Name == "get_Item"); + + var comments = Reader.GetMethodComments(itemProperty); + + Assert.AreEqual("Property description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(1, comments.Parameters.Count); + AssertParam(comments, 0, "parameter", "Parameter description"); } [TestMethod] public void GetMethodComments_ExplicitOperator() { - var mm = Reader.GetMethodComments(MyClass_explicitOperator); - Assert.AreEqual("Operator description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(1, mm.Parameters.Count); - AssertParam(mm, 0, "parameter", "Parameter description"); + var explicitOperator = typeof(MyClass).GetMethods().FirstOrDefault( + mt => mt.IsSpecialName && mt.Name == "op_Explicit"); + + var comments = Reader.GetMethodComments(explicitOperator); + + Assert.AreEqual("Operator description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(1, comments.Parameters.Count); + AssertParam(comments, 0, "parameter", "Parameter description"); } [TestMethod] public void GetMethodComments_TemplateMethod() { - var mm = Reader.GetMethodComments(MyClass_TemplateMethod); - Assert.AreEqual("TemplateMethod description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns.Trim()); - Assert.AreEqual(0, mm.Parameters.Count); - Assert.AreEqual(1, mm.TypeParameters.Count); - AssertTypeParam(mm, 0, "T", "Type parameter"); + var method = typeof(MyClass).GetMethod(nameof(MyClass.TemplateMethod)); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual("TemplateMethod description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns.Trim()); + Assert.AreEqual(0, comments.Parameters.Count); + Assert.AreEqual(1, comments.TypeParameters.Count); + AssertTypeParam(comments, 0, "T", "Type parameter"); } [TestMethod] public void GetMethodComments_TemplateMethod_GenericParameter() { - var mm = Reader.GetMethodComments(MyClass_TemplateMethod2); - Assert.AreEqual("TemplateMethod2 description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(1, mm.Parameters.Count); - AssertParam(mm, 0, "parameter", "Parameter description"); - Assert.AreEqual(mm.TypeParameters.Count, 1); - AssertTypeParam(mm, 0, "T", "Type parameter"); + var method = typeof(MyClass).GetMethod(nameof(MyClass.TemplateMethod2)); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual("TemplateMethod2 description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(1, comments.Parameters.Count); + AssertParam(comments, 0, "parameter", "Parameter description"); + Assert.AreEqual(comments.TypeParameters.Count, 1); + AssertTypeParam(comments, 0, "T", "Type parameter"); } [TestMethod] public void GetMethodComments_TemplateMethod_2_GenericParameters() { - var mm = Reader.GetMethodComments(MyClass_TemplateMethod3); - Assert.AreEqual("TemplateMethod3 description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(2, mm.Parameters.Count, 2); - AssertParam(mm, 0, "parameter1", "Parameter description"); - AssertParam(mm, 1, "parameter2", "Parameter description"); - Assert.AreEqual(2, mm.TypeParameters.Count); - AssertTypeParam(mm, 0, "X", "Type parameter"); - AssertTypeParam(mm, 1, "Y", "Type parameter"); + var method = typeof(MyClass).GetMethod(nameof(MyClass.TemplateMethod3)); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual("TemplateMethod3 description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(2, comments.Parameters.Count, 2); + AssertParam(comments, 0, "parameter1", "Parameter description"); + AssertParam(comments, 1, "parameter2", "Parameter description"); + Assert.AreEqual(2, comments.TypeParameters.Count); + AssertTypeParam(comments, 0, "X", "Type parameter"); + AssertTypeParam(comments, 1, "Y", "Type parameter"); } [TestMethod] public void GetMethodComments_TemplateMethod_BaseClass() { - var mm = Reader.GetMethodComments(MySubClass_TemplateMethod3); - Assert.AreEqual("TemplateMethod3 description", mm.Summary); - Assert.AreEqual("Return value description", mm.Returns); - Assert.AreEqual(2, mm.Parameters.Count); - AssertParam(mm, 0, "parameter1", "Parameter description"); - AssertParam(mm, 1, "parameter2", "Parameter description"); + var method = typeof(MySubClass).GetMethod(nameof(MySubClass.TemplateMethod3)); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual("TemplateMethod3 description", comments.Summary); + Assert.AreEqual("Return value description", comments.Returns); + Assert.AreEqual(2, comments.Parameters.Count); + AssertParam(comments, 0, "parameter1", "Parameter description"); + AssertParam(comments, 1, "parameter2", "Parameter description"); } [TestMethod] public void GetMethodComment() { var constructors = typeof(MySubClass).GetConstructors(); + + var comment = Reader.GetMemberComment(constructors.First()); + Assert.AreEqual(1, constructors.Length); - Assert.AreEqual("Constructor comment", Reader.GetMemberComment(constructors.First())); + Assert.AreEqual("Constructor comment", comment); } [TestMethod] public void GetMethodComments_SubClass() { - var comments = Reader.GetMemberComments(MySubClass_MethodWithComments); + var method = typeof(MySubClass).GetMethod(nameof(MySubClass.MethodWithComments)); + + var comments = Reader.GetMemberComments(method); + Assert.AreEqual("Method summary", comments.Summary); Assert.AreEqual("Method example", comments.Example); Assert.AreEqual("Method remarks", comments.Remarks); @@ -259,16 +271,21 @@ public void GetMethodComments_SubClass() [DataRow(typeof(MyClass), nameof(MyClass.MemberFunctionWithReadOnlyStringCollection), "MemberFunctionWithReadOnlyStringCollection description")] public void GetMethodComments_InParamter(Type type, string methodName, string expectedSummary) { - var comments = Reader.GetMethodComments(type.GetMethod(methodName)); - Assert.AreEqual(expectedSummary, comments.Summary); + var method = type.GetMethod(methodName); + + var comments = Reader.GetMethodComments(method); + + Assert.AreEqual(expectedSummary.ReplaceLineEndings(), comments.Summary.ReplaceLineEndings()); } [TestMethod] public void GetMethodComments_Inheritdoc_Constructor() { - var comments = - Reader.GetMethodComments(typeof(ClassForInheritdoc) - .GetConstructor(new Type[] { typeof(int) })); + var method = typeof(ClassForInheritdoc) + .GetConstructor(new Type[] { typeof(int) }); + + var comments = Reader.GetMethodComments(method); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual("Constructor2", comments.Summary); Assert.IsNotNull(comments.Parameters); @@ -279,9 +296,11 @@ public void GetMethodComments_Inheritdoc_Constructor() [TestMethod] public void GetMethodComments_Inheritdoc_VirtualOverride() { - var comments = - Reader.GetMethodComments(typeof(ClassForInheritdoc) - .GetMethod(nameof(ClassForInheritdoc.Method))); + var method = typeof(ClassForInheritdoc) + .GetMethod(nameof(ClassForInheritdoc.Method)); + + var comments = Reader.GetMethodComments(method); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual("Method for Inheritdoc", comments.Summary); } @@ -289,9 +308,11 @@ public void GetMethodComments_Inheritdoc_VirtualOverride() [TestMethod] public void GetMethodComments_Inheritdoc_InterfaceImplementation() { - var comments = - Reader.GetMethodComments(typeof(ClassForInheritdoc) - .GetMethod(nameof(ClassForInheritdoc.InterfaceMethod))); + var method = typeof(ClassForInheritdoc) + .GetMethod(nameof(ClassForInheritdoc.InterfaceMethod)); + + var comments = Reader.GetMethodComments(method); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual("Interface method", comments.Summary); } @@ -299,9 +320,11 @@ public void GetMethodComments_Inheritdoc_InterfaceImplementation() [TestMethod] public void GetMethodComments_Inheritdoc_Cref() { - var comments = - Reader.GetMethodComments(typeof(ClassForInheritdocCref) - .GetMethod(nameof(ClassForInheritdocCref.Method))); + var method = typeof(ClassForInheritdocCref) + .GetMethod(nameof(ClassForInheritdocCref.Method)); + + var comments = Reader.GetMethodComments(method); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual("M:DocXmlUnitTests.TestData.BaseClassForInheritdoc.Method", comments.Inheritdoc.Cref); Assert.AreEqual("Method for Inheritdoc", comments.Summary); @@ -314,9 +337,11 @@ public void GetMethodComments_Inheritdoc_Cref_OtherAssembly() new Assembly[] { typeof(MyClass).Assembly, typeof(OtherClass).Assembly}, (a) => Path.GetFileNameWithoutExtension(a.Location) + ".xml"); - var comments = - docReader.GetMethodComments(typeof(ClassForInheritdocCref) - .GetMethod(nameof(ClassForInheritdocCref.OtherLibMethod))); + var method = typeof(ClassForInheritdocCref) + .GetMethod(nameof(ClassForInheritdocCref.OtherLibMethod)); + + var comments = docReader.GetMethodComments(method); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual("OtherLibMethod summary", comments.Summary); } @@ -324,8 +349,10 @@ public void GetMethodComments_Inheritdoc_Cref_OtherAssembly() [TestMethod] public void GetMemberComments_Property_Inheritdoc() { - var comments = - Reader.GetMemberComments(typeof(ClassForInheritdoc).GetProperty(nameof(ClassForInheritdoc.Property))); + var property = typeof(ClassForInheritdoc).GetProperty(nameof(ClassForInheritdoc.Property)); + + var comments =Reader.GetMemberComments(property); + Assert.IsNotNull(comments.Inheritdoc); } } diff --git a/test/DocXmlUnitTests/PropertyInfoUnitTests.cs b/test/DocXmlUnitTests/PropertyInfoUnitTests.cs index 30d855c..5b03fc6 100644 --- a/test/DocXmlUnitTests/PropertyInfoUnitTests.cs +++ b/test/DocXmlUnitTests/PropertyInfoUnitTests.cs @@ -10,41 +10,41 @@ namespace DocXmlUnitTests [TestClass] public class PropertyInfoUnitTests : BaseTestClass { - public PropertyInfo MyClass_ItemProperty_GetOnly; - public PropertyInfo MyClass_ItemProperty_GetSet; - public PropertyInfo MyClass_GetSetProperty; - [TestInitialize] public new void Setup() { base.Setup(); - MyClass_ItemProperty_GetOnly = MyClass_Type.GetProperties().FirstOrDefault( - mt => mt.Name == "Item" && mt.GetMethod != null && mt.SetMethod == null); - MyClass_ItemProperty_GetSet = MyClass_Type.GetProperties().FirstOrDefault( - mt => mt.Name == "Item" && mt.GetMethod != null && mt.SetMethod != null); - MyClass_GetSetProperty = MyClass_Type.GetProperties().FirstOrDefault( - mt => mt.Name == nameof(MyClass.GetSetProperty)); - } [TestMethod] public void GetMemberComments_GetOnly() { - var comments = Reader.GetMemberComments(MyClass_ItemProperty_GetOnly); + var itemGetProperty = typeof(MyClass).GetProperties().FirstOrDefault( + mt => mt.Name == "Item" && mt.GetMethod != null && mt.SetMethod == null); + + var comments = Reader.GetMemberComments(itemGetProperty); + Assert.AreEqual("Property description", comments.Summary); } [TestMethod] public void GetMemberComments_GetSet() { - var comments = Reader.GetMemberComments(MyClass_ItemProperty_GetSet); + var itemGetSetProperty = typeof(MyClass).GetProperties().FirstOrDefault( + mt => mt.Name == "Item" && mt.GetMethod != null && mt.SetMethod != null); + + var comments = Reader.GetMemberComments(itemGetSetProperty); + Assert.AreEqual("ItemGetSetProperty description", comments.Summary); } [TestMethod] public void GetMemberComments_GetSetAndCommon() { - var comments = Reader.GetMemberComments(MyClass_GetSetProperty); + var getSetProperty = typeof(MyClass).GetProperties().FirstOrDefault( + mt => mt.Name == nameof(MyClass.GetSetProperty)); + + var comments = Reader.GetMemberComments(getSetProperty); Assert.AreEqual("GetSetProperty comment", comments.Summary); } } diff --git a/test/DocXmlUnitTests/TypeCommentsUnitTests.cs b/test/DocXmlUnitTests/TypeCommentsUnitTests.cs index f7c0655..306d0c3 100644 --- a/test/DocXmlUnitTests/TypeCommentsUnitTests.cs +++ b/test/DocXmlUnitTests/TypeCommentsUnitTests.cs @@ -29,34 +29,37 @@ public class TypeCommentsUnitTests : BaseTestClass [DataRow(typeof(MyClass.Nested<>.DoubleNested<>), "Double nested generic class")] public void GetTypeComments(Type type, string expectedSummaryComment) { - var mm = Reader.GetTypeComments(type); - Assert.AreEqual(expectedSummaryComment, mm.Summary); + var comments = Reader.GetTypeComments(type); + Assert.AreEqual(expectedSummaryComment, comments.Summary); } [TestMethod] public void GetTypeComments_OtherAssembly() { - var mm = MultiAssemblyReader.GetTypeComments(typeof(OtherClass)); - Assert.AreEqual("Other class", mm.Summary); + var comments = MultiAssemblyReader.GetTypeComments(typeof(OtherClass)); + Assert.AreEqual("Other class", comments.Summary); } [TestMethod] public void GetTypeComments_XPathDocument() { - var m = new DocXmlReader(new XPathDocument("DocXmlUnitTests.xml")); - var mm = m.GetTypeComments(typeof(MyClass)); - Assert.AreEqual("This is MyClass", mm.Summary); + var reader = new DocXmlReader(new XPathDocument("DocXmlUnitTests.xml")); + + var comments = reader.GetTypeComments(typeof(MyClass)); + + Assert.AreEqual("This is MyClass", comments.Summary); } [TestMethod] public void GetTypeComments_DelegateType() { - var mm = Reader.GetTypeComments(MyClass_Type.GetNestedType(nameof(MyClass.DelegateType))); - Assert.AreEqual("Delegate type description", mm.Summary); - Assert.AreEqual(1, mm.Parameters.Count); - AssertParam(mm, 0, "parameter", "Parameter description"); + var comments = Reader.GetTypeComments(typeof(MyClass).GetNestedType(nameof(MyClass.DelegateType))); + + Assert.AreEqual("Delegate type description", comments.Summary); + Assert.AreEqual(1, comments.Parameters.Count); + AssertParam(comments, 0, "parameter", "Parameter description"); } [DataTestMethod] @@ -65,6 +68,7 @@ public void GetTypeComments_DelegateType() public void GetTypeComments_Inheritdoc(Type type, string expectedComment) { var comments = Reader.GetTypeComments(type); + Assert.IsNotNull(comments.Inheritdoc); Assert.AreEqual(string.Empty, comments.Inheritdoc.Cref); Assert.AreEqual(expectedComment, comments.Summary); @@ -73,12 +77,13 @@ public void GetTypeComments_Inheritdoc(Type type, string expectedComment) [TestMethod] public void GetTypeComments_TypeParam() { - var mm = Reader.GetTypeComments(typeof(ClassWithTypeParams)); - Assert.AreEqual(2, mm.TypeParameters.Count); - Assert.AreEqual("T1", mm.TypeParameters[0].Name); - Assert.AreEqual("Type param1", mm.TypeParameters[0].Text); - Assert.AreEqual("T2", mm.TypeParameters[1].Name); - Assert.AreEqual("Type param2", mm.TypeParameters[1].Text); + var comments = Reader.GetTypeComments(typeof(ClassWithTypeParams)); + + Assert.AreEqual(2, comments.TypeParameters.Count); + Assert.AreEqual("T1", comments.TypeParameters[0].Name); + Assert.AreEqual("Type param1", comments.TypeParameters[0].Text); + Assert.AreEqual("T2", comments.TypeParameters[1].Name); + Assert.AreEqual("Type param2", comments.TypeParameters[1].Text); } } }