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: f754d222825324928ffd5e142452265af9c0d1c8 (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
151
152
153
154
155
156
157
using System;
using System.Linq;
using Mono.Cecil;
using Mono.Documentation.Updater;
using NUnit.Framework;

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

        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 MemberReference GetProperty(TypeDefinition type, string memberName)
        {
            var property = type.Properties.FirstOrDefault(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);
        }

        protected void TestTypeSignature(string libPath, string fullTypeName, string expected)
        {
            expected = FormatEndings(expected);
            var type = GetType(libPath, fullTypeName);
            var signature = formatter.GetDeclaration(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);
        }

        protected void TestMethodSignature(string libPath, string fullTypeName, string memberName, string expected)
        {
            var type = GetType(libPath, fullTypeName);
            var method = GetMethod(type, i => i.Name == memberName);
            var signature = formatter.GetDeclaration(method);
            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 TestPropertySignature(string libPath, string fullTypeName, string memberName, string expected)
        {
            var type = GetType(libPath, fullTypeName);
            var property = GetProperty(type, memberName);
            var signature = formatter.GetDeclaration(property);
            Assert.AreEqual(FormatEndings(expected), signature);
        }

        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 TestEventSignature(string libPath, string fullTypeName, string memberName, string expected)
        {
            var type = GetType(libPath, fullTypeName);
            var @event = GetEvent(type, memberName);
            var signature = formatter.GetDeclaration(@event);
            Assert.AreEqual(FormatEndings(expected), signature);
        }

        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 void TestFieldSignature(string libPath, string fullTypeName, string memberName, string expected)
        {
            var type = GetType(libPath, fullTypeName);
            var field = GetField(type, memberName);
            var signature = formatter.GetDeclaration(field);
            Assert.AreEqual(FormatEndings(expected), signature);
        }

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