using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; using Mono.Cecil.Rocks; namespace N { /// /// ID string generated is "T:N.X". /// public class X : IX> { /// /// ID string generated is "M:N.X.#ctor". /// public X() { } /// /// ID string generated is "M:N.X.#ctor(System.Int32)". /// /// Describe parameter. public X(int i) { } /// /// ID string generated is "F:N.X.q". /// public string q; /// /// ID string generated is "F:N.X.PI". /// public const double PI = 3.14; /// /// ID string generated is "M:N.X.f". /// public int f() { return 1; } /// /// ID string generated is "M:N.X.bb(System.String,System.Int32@)". /// public int bb(string s, ref int y) { return 1; } /// /// ID string generated is "M:N.X.gg(System.Int16[],System.Int32[0:,0:])". /// public int gg(short[] array1, int[,] array) { return 0; } /// /// ID string generated is "M:N.X.op_Addition(N.X,N.X)". /// public static X operator +(X x, X xx) { return x; } /// /// ID string generated is "P:N.X.prop". /// public int prop { get { return 1; } set { } } /// /// ID string generated is "E:N.X.d". /// #pragma warning disable 67 public event D d; #pragma warning restore 67 /// /// ID string generated is "P:N.X.Item(System.String)". /// public int this[string s] { get { return 1; } } /// /// ID string generated is "T:N.X.Nested". /// public class Nested { } /// /// ID string generated is "T:N.X.D". /// public delegate void D(int i); /// /// ID string generated is "M:N.X.op_Explicit(N.X)~System.Int32". /// public static explicit operator int(X x) { return 1; } public static void Linq (IEnumerable enumerable, Func selector) { } /// /// ID string generated is "M:N.X.N#IX{N#KVP{System#String,System#Int32}}#IXA(N.KVP{System.String,System.Int32})" /// void IX>.IXA (KVP k) { } } public interface IX { void IXA (K k); } public class KVP { } } namespace Mono.Cecil.Tests { [TestFixture] public class DocCommentIdTests { [Test] public void TypeDef () { AssertDocumentID ("T:N.X", GetTestType ()); } [Test] public void ParameterlessCtor () { var type = GetTestType (); var ctor = type.GetConstructors ().Single (m => m.Parameters.Count == 0); AssertDocumentID ("M:N.X.#ctor", ctor); } [Test] public void CtorWithParameters () { var type = GetTestType (); var ctor = type.GetConstructors ().Single (m => m.Parameters.Count == 1); AssertDocumentID ("M:N.X.#ctor(System.Int32)", ctor); } [Test] public void Field () { var type = GetTestType (); var field = type.Fields.Single (m => m.Name == "q"); AssertDocumentID ("F:N.X.q", field); } [Test] public void ConstField () { var type = GetTestType (); var field = type.Fields.Single (m => m.Name == "PI"); AssertDocumentID ("F:N.X.PI", field); } [Test] public void ParameterlessMethod () { var type = GetTestType (); var method = type.Methods.Single (m => m.Name == "f"); AssertDocumentID ("M:N.X.f", method); } [Test] public void MethodWithByRefParameters () { var type = GetTestType (); var method = type.Methods.Single (m => m.Name == "bb"); AssertDocumentID ("M:N.X.bb(System.String,System.Int32@)", method); } [Test] public void MethodWithArrayParameters () { var type = GetTestType (); var method = type.Methods.Single (m => m.Name == "gg"); AssertDocumentID ("M:N.X.gg(System.Int16[],System.Int32[0:,0:])", method); } [Test] public void OpAddition () { var type = GetTestType (); var op = type.Methods.Single (m => m.Name == "op_Addition"); AssertDocumentID ("M:N.X.op_Addition(N.X,N.X)", op); } [Test] public void OpExplicit () { var type = GetTestType (); var op = type.Methods.Single (m => m.Name == "op_Explicit"); AssertDocumentID ("M:N.X.op_Explicit(N.X)~System.Int32", op); } [Test] public void Property () { var type = GetTestType (); var property = type.Properties.Single (p => p.Name == "prop"); AssertDocumentID ("P:N.X.prop", property); } [Test] public void Indexer () { var type = GetTestType (); var indexer = type.Properties.Single (p => p.Name == "Item"); AssertDocumentID ("P:N.X.Item(System.String)", indexer); } [Test] public void Event () { var type = GetTestType (); var @event = type.Events.Single (e => e.Name == "d"); AssertDocumentID ("E:N.X.d", @event); } [Test] public void Delegate () { var type = GetTestType (); var @delegate = type.NestedTypes.Single (t => t.Name == "D"); AssertDocumentID ("T:N.X.D", @delegate); } [Test] public void NestedType () { var type = GetTestType (); var nestedType = type.NestedTypes.Single (t => t.Name == "Nested"); AssertDocumentID ("T:N.X.Nested", nestedType); } [Test] public void Linq () { var type = GetTestType (); var method = type.GetMethod ("Linq"); AssertDocumentID ("M:N.X.Linq(System.Collections.Generic.IEnumerable{System.String},System.Func{System.String})", method); } [Test] public void EII () { var type = GetTestType (); var method = type.Methods.Where (m => m.Name.Contains("IXA")).First (); AssertDocumentID ("M:N.X.N#IX{N#KVP{System#String,System#Int32}}#IXA(N.KVP{System.String,System.Int32})", method); } TypeDefinition GetTestType () { return typeof (N.X).ToDefinition (); } static void AssertDocumentID (string docId, IMemberDefinition member) { Assert.AreEqual (docId, DocCommentId.GetDocCommentId (member)); } } }