4 using System.Collections.Generic;
9 using Monodoc.Generators;
10 using Monodoc.Providers;
12 // Used by ReachabilityWithCrefsTest
13 // using HtmlAgilityPack;
15 namespace MonoTests.Monodoc
18 public class HelpSourceTest
20 const string BaseDir = "../../class/monodoc/Test/monodoc_test/";
22 class CheckGenerator : IDocGenerator<bool>
24 public string LastCheckMessage { get; set; }
26 public bool Generate (HelpSource hs, string id, Dictionary<string, string> context)
28 LastCheckMessage = string.Format ("#1 : {0} {1}", hs, id);
29 if (hs == null || string.IsNullOrEmpty (id))
32 // Stripe the arguments parts since we don't need it
33 var argIdx = id.LastIndexOf ('?');
35 id = id.Substring (0, argIdx);
37 LastCheckMessage = string.Format ("#2 : {0} {1}", hs, id);
38 if (hs.IsRawContent (id))
39 return hs.GetText (id) != null;
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))
49 LastCheckMessage = string.Format ("#3 : {0} {1}", hs, id);
50 if (hs.IsGeneratedContent (id))
51 return hs.GetCachedText (id) != null;
53 var s = hs.GetCachedHelpStream (id);
64 /* This test verifies that for every node in our tree that possed a PublicUrl,
65 * we can correctly access it back through RenderUrl
68 public void ReachabilityTest ()
70 var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
72 var generator = new CheckGenerator ();
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);
84 //Assert.AreEqual (0, errorCount, errorCount + " / " + testCount.ToString ());
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 Assert.LessOrEqual (4, errorCount, errorCount + " / " + testCount.ToString ());
91 IEnumerable<Node> GetLeaves (Node node)
99 foreach (var child in node.ChildNodes) {
100 if (!string.IsNullOrEmpty (child.Element) && !child.Element.StartsWith ("root:/"))
102 foreach (var childLeaf in GetLeaves (child))
103 yield return childLeaf;
109 public void ReachabilityWithShortGenericNotationTest ()
111 var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
113 var generator = new CheckGenerator ();
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");
143 public void AspNetStyleUrlReachabilityTest ()
145 var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
147 var generator = new CheckGenerator ();
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");
158 public void PublicUrlOnUnattachedHelpSourceRoot ()
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);
175 [Test, Ignore ("Mono documentation is full of syntax errors so we can't use it reliably for this test")]
176 public void ReachabilityWithCrefsTest ()
178 var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
180 var htmlGenerator = new HtmlGenerator (null);
181 var crefs = new HashSet<string> ();
182 var generator = new CheckGenerator ();
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)
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);
197 HtmlDocument doc = new HtmlDocument();
199 doc.LoadHtml (content);
201 Console.WriteLine ("Couldn't load a HTML document for URL {0}", leaf.PublicUrl);
205 foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]")) {
206 var newUrl = link.Attributes["href"].Value;
207 var hashIndex = newUrl.IndexOf ('#');
209 newUrl = newUrl.Substring (0, hashIndex);
210 if (newUrl.Length > 1 && newUrl[1] == ':' && char.IsLetter (newUrl, 0) && char.ToLowerInvariant (newUrl[0]) != 'c')
214 foreach (var cref in crefs) {
215 if (!rootTree.RenderUrl (cref, generator, out result) || result == null) {
216 Console.WriteLine ("Error with cref: `{0}'", cref);
224 Assert.AreEqual (0, errorCount, errorCount + " / " + crefs.Count);