Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BasicFormatterTests.cs « mdoc.Test « mdoc - github.com/mono/api-doc-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3cb521e61a190c036e7b5e0ceee43983cc07145c (plain)
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
using System;
using System.Linq;
using Mono.Cecil;
using Mono.Documentation.Updater;
using NUnit.Framework;

namespace mdoc.Test
{
    public abstract class BasicFormatterTests<T> where T : MemberFormatter
    {
        protected abstract T formatter { get; }

        protected MethodDefinition GetMethod(Type type, Func<MethodDefinition, bool> query)
        {
            return GetMethod(GetType(type), query);
        }

        protected MethodDefinition GetMethod(TypeDefinition testclass, Func<MethodDefinition, bool> query)
        {
            var methods = testclass.Methods;
            var member = methods.FirstOrDefault(query).Resolve();
            if (member == null)
                throw new Exception("Did not find the member in the test class");
            return member;
        }

        protected MethodDefinition GetProperty(TypeDefinition testclass, Func<MethodDefinition, bool> query)
        {
            var methods = testclass.Methods;
            var member = methods.FirstOrDefault(query).Resolve();
            if (member == null)
                throw new Exception("Did not find the member in the test class");
            return member;
        }

        protected TypeDefinition GetType(string filepath, string classname)
        {
            var module = ModuleDefinition.ReadModule(filepath);
            var types = module.GetTypes();
            var testclass = types
                .SingleOrDefault(t => t.FullName == classname);
            if (testclass == null)
            {
                throw new Exception($"Test was unable to find type {classname}");
            }
            return testclass.Resolve();
        }

        protected virtual TypeDefinition GetType(Type type)
        {
            var moduleName = type.Module.FullyQualifiedName;
            return GetType(moduleName, type.FullName);
        }

        protected MemberReference GetProperty(TypeDefinition type, string memberName)
        {
            var property = type.Properties.SingleOrDefault(i => i.Name == memberName);
            if (property == null)
                throw new Exception($"Can't find property {memberName}");
            return property;
        }

        protected MemberReference GetField(TypeDefinition type, string eventName)
        {
            var property = type.Fields.SingleOrDefault(i => i.Name == eventName);
            if (property == null)
                throw new Exception($"Can't find field {eventName}");
            return property;
        }

        protected MemberReference GetEvent(TypeDefinition type, string eventName)
        {
            var property = type.Events.SingleOrDefault(i => i.Name == eventName);
            if (property == null)
                throw new Exception($"Can't find field {eventName}");
            return property;
        }

        protected void TestTypeSignature(Type type, string expected)
        {
            expected = FormatEndings(expected);
            var signature = GetTypeSignature(type);
            Assert.AreEqual(expected, signature);
        }

        private string GetTypeSignature(Type type)
        {
            var tref = GetType(type);
            var signature = formatter.GetDeclaration(tref);
            return signature;
        }

        protected void TestMethodSignature(Type type, string expected, string memberName)
        {
            var signature = GetMethodSignature(type, memberName);
            Assert.AreEqual(FormatEndings(expected), signature);
        }

        private string GetMethodSignature(Type type, string memberName)
        {
            var tref = GetType(type);
            var method = GetMethod(tref, i => i.Name == memberName);
            var signature = formatter.GetDeclaration(method);
            return signature;
        }

        protected void TestPropertySignature(Type type, string expected, string memberName)
        {
            var signature = GetPropertySignature(type, memberName);
            Assert.AreEqual(FormatEndings(expected), signature);
        }

        private string GetPropertySignature(Type type, string memberName)
        {
            var tref = GetType(type);
            return formatter.GetDeclaration(GetProperty(tref, memberName));
        }

        protected void TestEventSignature(Type type, string expected, string memberName)
        {
            var signature = GetEventSignature(type, memberName);
            Assert.AreEqual(expected, signature);
        }

        private string GetEventSignature(Type type, string memberName)
        {
            var tref = GetType(type);
            return formatter.GetDeclaration(GetEvent(tref, memberName));
        }

        protected void TestFieldSignature(Type type, string expected, string memberName)
        {
            var usage = GetFieldUsage(type, memberName);
            Assert.AreEqual(FormatEndings(expected), usage);
        }

        private string GetFieldUsage(Type type, string memberName)
        {
            var tref = GetType(type);
            var field = GetField(tref, memberName);
            var signature = formatter.GetDeclaration(field);
            return signature;
        }

        protected static string FormatEndings(string s)
        {
            return s?.Replace("\r\n", MemberFormatter.GetLineEnding());
        }
    }
}