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

ValidationTests.cs « mdoc.Test « mdoc - github.com/mono/api-doc-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 54827d71389f5f9735bdac70c170fbc437c87318 (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
using Mono.Documentation;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;

namespace mdoc.Test
{
    [TestFixture ()]
    public class ValidationTests
    {

        [Test ()]
        public void ValidationFailure ()
        {
            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader ("<random></random>"));

            Assert.AreEqual (1, context.Errors.Count);
            Assert.IsTrue (context.Errors.Any (e => e.Message.Contains ("random")));
        }

        [Test ()]
        public void ValidationSuccess ()
        {
            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader ("<Type Name=\"AVKitError\" FullName=\"AVKit.AVKitError\"></Type>"));

            Assert.AreEqual (0, context.Errors.Count);
        }

        [Test]
        public void FrameworkAlternate_Attributes_Type ()
        {
            string xmlString = @"<Type Name=""AVKitError"" FullName=""AVKit.AVKitError"">
  <Attributes>
    <Attribute>
      <AttributeName>ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 9, 0, ObjCRuntime.PlatformArchitecture.None, null)</AttributeName>
    </Attribute>
    <Attribute FrameworkAlternate=""One;Two"">
      <AttributeName>ObjCRuntime.Native</AttributeName>
    </Attribute>
  </Attributes>
</Type>";


            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader (xmlString));


            Assert.AreEqual (0, context.Errors.Count, context.ErrorText);
        }

        [Test]
        public void FrameworkAlternate_MemberSignature ()
        {
            string xmlString = @"<Type Name=""blah"" FullName=""blah.blah"">
            <Members><Member MemberName=""Meth"">
      <MemberSignature Language=""C#"" Value=""public void Meth (int a, string d, int c);"" FrameworkAlternate=""One;Three"" />
      <MemberSignature Language=""C#"" Value=""public void Meth (int a, string b, int c);"" FrameworkAlternate=""Two"" />
      <MemberType>b</MemberType>
      <Docs></Docs>
    </Member></Members></Type>";


            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader (xmlString));


            Assert.AreEqual (0, context.Errors.Count, context.ErrorText);
        }

        [Test]
        public void FrameworkAlternate_TypeSignature ()
        {
            string xmlString = @"<Type Name=""blah"" FullName=""blah.blah"">
      <TypeSignature Language=""C#"" Value=""blah"" FrameworkAlternate=""One;Three"" />
      <TypeSignature Language=""C#"" Value=""blah2"" FrameworkAlternate=""Two"" />
</Type>";


            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader (xmlString));


            Assert.AreEqual (0, context.Errors.Count, context.ErrorText);
        }

        [Test]
        public void FrameworkAlternate_Parameter ()
        {
            string xmlString = @"<Member MemberName=""Meth"">
      <MemberSignature Language=""C#"" Value=""blah"" />
      <MemberType>Method</MemberType>
      <Parameters>
        <Parameter Name = ""a"" Type=""System.Int32"" Index=""0"" FrameworkAlternate=""One"" />
        <Parameter Name = ""d"" Type=""System.Int32"" Index=""0"" FrameworkAlternate=""Three"" />
      </Parameters>
      <Docs></Docs>
    </Member>";


            var context = InitializeTestContext ();

            context.Validator.ValidateFile (new StringReader (xmlString));


            Assert.AreEqual (0, context.Errors.Count, context.ErrorText);
        }

        #region Test Context Stuff
        struct ValidationContext
        {
            public MDocValidator Validator;
            public List<Exception> Errors;
            public string ErrorText{
                get => Errors.Aggregate (
                    new StringBuilder (),
                    (sb, e) =>
                    {
                        sb.Append (e.Message + ";");
                        return sb;
                    }
                    ).ToString ();
            }
        }

        private static ValidationContext InitializeTestContext ()
        {
            List<Exception> errors = new List<Exception> ();

            MDocValidator validator = new MDocValidator ();
            validator.InitializeSchema (
                "ecma",
                (a, b) => errors.Add (b.Exception)
            );
            ValidationContext context = new ValidationContext
            {
                Validator = validator,
                Errors = errors
            };
            return context;
        }
        #endregion
    }
}