[runtime] Disable some tests in full-aot mode which cannot be AOTed because of type...
[mono.git] / mcs / class / monodoc / Test / Monodoc / HelpSourceTests.cs
1 using System;
2 using System.IO;
3 using System.Linq;
4 using System.Collections.Generic;
5
6 using NUnit.Framework;
7
8 using Monodoc;
9 using Monodoc.Generators;
10 using Monodoc.Providers;
11
12 // Used by ReachabilityWithCrefsTest
13 // using HtmlAgilityPack;
14
15 namespace MonoTests.Monodoc
16 {
17         [TestFixture]
18         public class HelpSourceTest
19         {
20                 const string BaseDir = "../../class/monodoc/Test/monodoc_test/";
21
22                 class CheckGenerator : IDocGenerator<bool>
23                 {
24                         public string LastCheckMessage { get; set; }
25
26                         public bool Generate (HelpSource hs, string id, Dictionary<string, string> context)
27                         {
28                                 LastCheckMessage = string.Format ("#1 : {0} {1}", hs, id);
29                                 if (hs == null || string.IsNullOrEmpty (id))
30                                         return false;
31
32                                 // Stripe the arguments parts since we don't need it
33                                 var argIdx = id.LastIndexOf ('?');
34                                 if (argIdx != -1)
35                                         id = id.Substring (0, argIdx);
36
37                                 LastCheckMessage = string.Format ("#2 : {0} {1}", hs, id);
38                                 if (hs.IsRawContent (id))
39                                         return hs.GetText (id) != null;
40
41                                 IEnumerable<string> parts;
42                                 if (hs.IsMultiPart (id, out parts)) {
43                                         LastCheckMessage = string.Format ("#4 : {0} {1} ({2})", hs, id, string.Join (", ", parts));
44                                         foreach (var partId in parts)
45                                                 if (!Generate (hs, partId, context))
46                                                         return false;
47                                 }
48
49                                 LastCheckMessage = string.Format ("#3 : {0} {1}", hs, id);
50                                 if (hs.IsGeneratedContent (id))
51                                         return hs.GetCachedText (id) != null;
52                                 else {
53                                         var s = hs.GetCachedHelpStream (id);
54                                         if (s != null) {
55                                                 s.Close ();
56                                                 return true;
57                                         } else {
58                                                 return false;
59                                         }
60                                 }
61                         }
62                 }
63
64                 /* This test verifies that for every node in our tree that possed a PublicUrl,
65                  * we can correctly access it back through RenderUrl
66                  */
67                 [Test]
68                 public void ReachabilityTest ()
69                 {
70                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
71                         Node result;
72                         var generator = new CheckGenerator ();
73                         int errorCount = 0;
74                         int testCount = 0;
75
76                         foreach (var leaf in GetLeaves (rootTree.RootNode)) {
77                                 if (!rootTree.RenderUrl (leaf.PublicUrl, generator, out result) || leaf != result) {
78                                         Console.WriteLine ("Error: {0} with HelpSource {1} ", leaf.PublicUrl, leaf.Tree.HelpSource.Name);
79                                         errorCount++;
80                                 }
81                                 testCount++;
82                         }
83
84                         //Assert.AreEqual (0, errorCount, errorCount + " / " + testCount.ToString ());
85
86                         // HACK: in reality we have currently 4 known issues which are due to duplicated namespaces across
87                         // doc sources, something that was never supported and that we need to improve/fix at some stage
88                         AssertHelper.LessOrEqual (4, errorCount, errorCount + " / " + testCount.ToString ());
89                 }
90
91                 IEnumerable<Node> GetLeaves (Node node)
92                 {
93                         if (node == null)
94                                 yield break;
95
96                         if (node.IsLeaf)
97                                 yield return node;
98                         else {
99                                 foreach (var child in node.ChildNodes) {
100                                         if (!string.IsNullOrEmpty (child.Element) && !child.Element.StartsWith ("root:/"))
101                                                 yield return child;
102                                         foreach (var childLeaf in GetLeaves (child))
103                                                 yield return childLeaf;
104                                 }
105                         }
106                 }
107
108                 [Test]
109                 public void ReachabilityWithShortGenericNotationTest ()
110                 {
111                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
112                         Node result;
113                         var generator = new CheckGenerator ();
114
115                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Collections.Concurrent.IProducerConsumerCollection`1", generator, out result), "#1");
116                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Collections.Generic.Dictionary`2", generator, out result), "#2");
117                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Action`4", generator, out result), "#3");
118                         Assert.IsTrue (rootTree.RenderUrl ("T:System.EventHandler`1", generator, out result), "#4");
119                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`5", generator, out result), "#5a");
120                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`4", generator, out result), "#5b");
121                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`6", generator, out result), "#5c");
122                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`7", generator, out result), "#5d");
123                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`3", generator, out result), "#5e");
124                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`2", generator, out result), "#5f");
125                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`1", generator, out result), "#5g");
126                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`8", generator, out result), "#5h");
127                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func`9", generator, out result), "#5i");
128                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Action`3", generator, out result), "#6a");
129                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Action`2", generator, out result), "#6b");
130                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Action`4", generator, out result), "#6c");
131                         Assert.IsTrue (rootTree.RenderUrl ("T:System.IComparable`1", generator, out result), "#7");
132                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Lazy`1", generator, out result), "#8");
133                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Tuple`1", generator, out result), "#9a");
134                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Tuple`2", generator, out result), "#9b");
135                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Tuple`3", generator, out result), "#9c");
136                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Tuple`4", generator, out result), "#9d");
137                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Collections.Generic.Dictionary`2+ValueCollection", generator, out result), "#10");
138                         Assert.IsFalse (rootTree.RenderUrl ("T:System.EventHandler`2", generator, out result), "#11");
139                         Assert.IsFalse (rootTree.RenderUrl ("T:System.Lazy`2", generator, out result), "#12");
140                 }
141
142                 [Test]
143                 public void AspNetStyleUrlReachabilityTest ()
144                 {
145                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
146                         Node result;
147                         var generator = new CheckGenerator ();
148
149                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Collections.Generic.Dictionary{TKey,TValue}", generator, out result), "#1");
150                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Action{T1,T2}", generator, out result), "#2");
151                         Assert.IsTrue (rootTree.RenderUrl ("T:System.EventHandler{TEventArgs}", generator, out result), "#3");
152                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Func{T1,T2,T3,TResult}", generator, out result), "#4");
153                         Assert.IsTrue (rootTree.RenderUrl ("T:System.Collections.Generic.Dictionary{TKey,TValue}+ValueCollection", generator, out result), "#5");
154                         Assert.IsTrue (rootTree.RenderUrl ("T:System.IComparable{T}", generator, out result), "#6");
155                 }
156
157                 [Test]
158                 public void PublicUrlOnUnattachedHelpSourceRoot ()
159                 {
160                         // Unattached help source have no root:/ URL attributed
161                         var hs = new EcmaHelpSource (Path.Combine (BaseDir, "sources", "netdocs"), false);
162                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
163                         hs.RootTree = rootTree;
164                         Assert.IsNull (hs.Tree.RootNode.PublicUrl);
165                         var nsChildUrl = hs.Tree.RootNode.ChildNodes.First ().PublicUrl;
166                         Assert.IsNotNull (nsChildUrl);
167                         StringAssert.StartsWith ("N:", nsChildUrl);
168                         // Verify GetNodeTypeParent
169                         var typeNode = hs.Tree.RootNode.ChildNodes.First ().ChildNodes.First ();
170                         var metaNode = typeNode.ChildNodes.First (cn => cn.Element == "M");
171                         StringAssert.StartsWith (typeNode.PublicUrl, metaNode.PublicUrl);
172                 }
173
174                 /*
175                 [Test, Ignore ("Mono documentation is full of syntax errors so we can't use it reliably for this test")]
176                 public void ReachabilityWithCrefsTest ()
177                 {
178                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
179                         Node result;
180                         var htmlGenerator = new HtmlGenerator (null);
181                         var crefs = new HashSet<string> ();
182                         var generator = new CheckGenerator ();
183                         int errorCount = 0;
184
185                         foreach (var leaf in GetLeaves (rootTree.RootNode)) {
186                                 Dictionary<string, string> context;
187                                 string internalId = leaf.Tree.HelpSource.GetInternalIdForUrl (leaf.PublicUrl, out result, out context);
188                                 if (leaf.Tree.HelpSource.GetDocumentTypeForId (internalId) != DocumentType.EcmaXml)
189                                         continue;
190
191                                 string content = null;
192                                 if (string.IsNullOrEmpty (content = rootTree.RenderUrl (leaf.PublicUrl, htmlGenerator, out result)) || leaf != result) {
193                                         Console.WriteLine ("Error: {0} with HelpSource {1} ", leaf.PublicUrl, leaf.Tree.HelpSource.Name);
194                                         continue;
195                                 }
196
197                                 HtmlDocument doc = new HtmlDocument();
198                                 try {
199                                         doc.LoadHtml (content);
200                                 } catch {
201                                         Console.WriteLine ("Couldn't load a HTML document for URL {0}", leaf.PublicUrl);
202                                         continue;
203                                 }
204
205                                 foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) {
206                                         var newUrl = link.Attributes["href"].Value;
207                                         var hashIndex = newUrl.IndexOf ('#');
208                                         if (hashIndex != -1)
209                                                 newUrl = newUrl.Substring (0, hashIndex);
210                                         if (newUrl.Length > 1 && newUrl[1] == ':' && char.IsLetter (newUrl, 0) && char.ToLowerInvariant (newUrl[0]) != 'c')
211                                                 crefs.Add (newUrl);
212                                 }
213
214                                 foreach (var cref in crefs) {
215                                         if (!rootTree.RenderUrl (cref, generator, out result) || result == null) {
216                                                 Console.WriteLine ("Error with cref: `{0}'", cref);
217                                                 errorCount++;
218                                         }
219                                 }
220
221                                 crefs.Clear ();
222                         }
223
224                         Assert.AreEqual (0, errorCount, errorCount + " / " + crefs.Count);
225                 }*/
226         }
227 }