1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
using NUnit.Framework;
using System.Linq;
using mdoc.Test.SampleClasses;
using Mono.Documentation.Updater;
namespace mdoc.Test
{
[TestFixture ()]
public class FormatterTests : BasicFormatterTests<CSharpMemberFormatter>
{
private CSharpMemberFormatter csharpMemberFormatter = new CSharpMemberFormatter();
protected override CSharpMemberFormatter formatter => csharpMemberFormatter;
[Test ()]
public void Formatters_VerifyPrivateConstructorNull ()
{
// this is a private constructor
var method = GetMethod (typeof(TestClass), m => m.IsConstructor && !m.IsPublic && m.Parameters.Count () == 1);
MemberFormatter[] formatters = new MemberFormatter[]
{
new CSharpFullMemberFormatter (),
new CSharpMemberFormatter(),
new ILMemberFormatter(),
new ILFullMemberFormatter(),
new VBFullMemberFormatter (),
new VBMemberFormatter(),
new FSharpMemberFormatter(),
new FSharpFullMemberFormatter(),
};
var sigs = formatters.Select (f => f.GetDeclaration (method));
foreach (var sig in sigs)
Assert.IsNull (sig);
}
[Test]
public void CSharp_op_Addition () =>
TestBinaryOp ("Addition", "+");
[Test]
public void CSharp_op_Subtraction () =>
TestBinaryOp ("Subtraction", "-");
[Test]
public void CSharp_op_Division () =>
TestBinaryOp ("Division", "/");
[Test]
public void CSharp_op_Multiplication () =>
TestBinaryOp ("Multiply", "*");
[Test]
public void CSharp_op_Modulus () =>
TestBinaryOp ("Modulus", "%");
[Test]
public void CSharp_op_BitwiseAnd () =>
TestBinaryOp ("BitwiseAnd", "&");
[Test]
public void CSharp_op_BitwiseOr () =>
TestBinaryOp ("BitwiseOr", "|");
[Test]
public void CSharp_op_ExclusiveOr () =>
TestBinaryOp ("ExclusiveOr", "^");
[Test]
public void CSharp_op_LeftShift () =>
TestBinaryOp ("LeftShift", "<<", secondType: "int");
[Test]
public void CSharp_op_RightShift () =>
TestBinaryOp ("RightShift", ">>", secondType: "int");
[Test]
public void CSharp_op_UnaryPlus () =>
TestUnaryOp ("UnaryPlus", "+");
[Test]
public void CSharp_op_UnaryNegation () =>
TestUnaryOp ("UnaryNegation", "-");
[Test]
public void CSharp_op_LogicalNot () =>
TestUnaryOp ("LogicalNot", "!");
[Test]
public void CSharp_op_OnesComplement () =>
TestUnaryOp ("OnesComplement", "~");
[Test]
public void CSharp_op_Decrement () =>
TestUnaryOp ("Decrement", "--");
[Test]
public void CSharp_op_Increment () =>
TestUnaryOp ("Increment", "++");
[Test]
public void CSharp_op_True () =>
TestUnaryOp ("True", "true", returnType: "bool");
[Test]
public void CSharp_op_False () =>
TestUnaryOp ("False", "false", returnType: "bool");
[Test]
public void CSharp_op_Equality () =>
TestComparisonOp ("Equality", "==");
[Test]
public void CSharp_op_Inequality () =>
TestComparisonOp ("Inequality", "!=");
[Test]
public void CSharp_op_LessThan () =>
TestComparisonOp ("LessThan", "<");
[Test]
public void CSharp_op_GreaterThan () =>
TestComparisonOp ("GreaterThan", ">");
[Test]
public void CSharp_op_LessThanOrEqual () =>
TestComparisonOp ("LessThanOrEqual", "<=");
[Test]
public void CSharp_op_GreaterThanOrEqual () =>
TestComparisonOp ("GreaterThanOrEqual", ">=");
[Test]
public void CSharp_op_Implicit () =>
TestConversionOp ("Implicit", "implicit", "TestClass", "TestClassTwo");
[Test]
public void CSharp_op_Implicit_inverse () =>
TestConversionOp ("Implicit", "implicit", "TestClassTwo", "TestClass");
[Test]
public void CSharp_op_Explicit () =>
TestConversionOp ("Explicit", "explicit", "int", "TestClass");
[Test]
public void CSharp_op_Explicit_inverse () =>
TestConversionOp ("Explicit", "explicit", "TestClass", "int");
[Test]
public void CSharp_modopt () =>
TestMod ("SomeFunc2", "public SomeClass* SomeFunc2 (SomeClass param);", returnType: "SomeClass*");
[Test]
public void CSharp_modreq () =>
TestMod ("SomeFunc", "public int SomeFunc (SomeClass* param);", returnType: "int");
[Test]
public void CSharp_doublepointer () =>
TestMod ("SomeFunc3", "public SomeClass** SomeFunc3 (int param);", returnType: "cppcli.SomeClass**");
[Test]
public void CSharp_pointerref_modreqparam () =>
TestMod ("SomeFunc4", "public int SomeFunc4 (SomeClass** param, int param2);", returnType: "int");
[Test]
public void DoubleMod ()
{
string doubledUp = "System.ValueType modopt(System.DateTime) modopt(System.Runtime.CompilerServices.IsBoxed)";
string result = MemberFormatter.RemoveMod (doubledUp);
Assert.AreEqual ("System.ValueType", result);
}
[Test]
public void DoubleMod_Mixed ()
{
string doubledUp = "System.ValueType modreq(System.DateTime) modopt(System.Runtime.CompilerServices.IsBoxed)";
string result = MemberFormatter.RemoveMod (doubledUp);
Assert.AreEqual ("System.ValueType", result);
}
[Test]
public void DoubleMod_Pointer ()
{
string doubledUp = "System.ValueType modreq(System.DateTime) modopt(System.Runtime.CompilerServices.IsBoxed)*";
string result = MemberFormatter.RemoveMod (doubledUp);
Assert.AreEqual ("System.ValueType*", result);
}
[Test]
public void Params()
{
var member = GetMethod (typeof(TestClass), m => m.Name == "DoSomethingWithParams");
var sig = formatter.GetDeclaration (member);
Assert.AreEqual ("public void DoSomethingWithParams (params int[] values);", sig);
}
[Test]
public void IL_RefAndOut ()
{
var member = GetMethod (typeof(TestClass), m => m.Name == "RefAndOut");
var formatter = new ILFullMemberFormatter ();
var sig = formatter.GetDeclaration (member);
Assert.AreEqual (".method public hidebysig instance void RefAndOut(int32& a, [out] int32& b) cil managed", sig);
}
[Test]
public void MethodSignature_Finalize() =>
TestMethodSignature(typeof(SomeGenericClass<>),
"~SomeGenericClass ();",
"Finalize");
[Test]
public void CSharpReadonlyRefReturn()
{
var member = GetMethod(typeof(ReadonlyRefClass), m => m.Name == "ReadonlyRef");
var formatter = new CSharpFullMemberFormatter();
var sig = formatter.GetDeclaration(member);
Assert.AreEqual("public ref readonly int ReadonlyRef ();", sig);
}
[Test]
public void CSharpRefReturn()
{
var member = GetMethod(typeof(ReadonlyRefClass), m => m.Name == "Ref");
var formatter = new CSharpFullMemberFormatter();
var sig = formatter.GetDeclaration(member);
Assert.AreEqual("public ref int Ref ();", sig);
}
#region Helper Methods
string RealTypeName(string name){
switch (name) {
case "bool": return "Boolean";
case "int": return "Int32";
default: return name;
}
}
void TestConversionOp (string name, string type, string leftType, string rightType) {
TestOp (name, $"public static {type} operator {leftType} ({rightType} c1);", argCount: 1, returnType: leftType);
}
void TestComparisonOp (string name, string op)
{
TestOp (name, $"public static bool operator {op} (TestClass c1, TestClass c2);", argCount: 2, returnType: "Boolean");
}
void TestUnaryOp (string name, string op, string returnType = "TestClass")
{
TestOp (name, $"public static {returnType} operator {op} (TestClass c1);", argCount: 1, returnType: returnType);
}
void TestBinaryOp (string name, string op, string returnType = "TestClass", string secondType = "TestClass")
{
TestOp (name, $"public static {returnType} operator {op} (TestClass c1, {secondType} c2);", argCount: 2, returnType: returnType);
}
void TestOp (string name, string expectedSig, int argCount, string returnType = "TestClass")
{
var member = GetMethod (typeof(TestClass), m => m.Name == $"op_{name}" && m.Parameters.Count == argCount && m.ReturnType.Name == RealTypeName (returnType));
var formatter = new CSharpMemberFormatter ();
var sig = formatter.GetDeclaration (member);
Assert.AreEqual (expectedSig, sig);
}
void TestMod (string name, string expectedSig, int argCount = 1, string returnType = "SomeClass")
{
var member = GetMethod (
GetType ("SampleClasses/cppcli.dll", "cppcli.SomeInterface"),
m => m.Name == name
);
var formatter = new CSharpMemberFormatter ();
var sig = formatter.GetDeclaration (member);
Assert.AreEqual (expectedSig, sig);
}
#endregion
}
}
|