Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / tools / monkeydoc / Test / Monkeydoc / 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 MonkeyDoc;
9
10 namespace MonoTests.MonkeyDoc
11 {
12         [TestFixture]
13         public class HelpSourceTest
14         {
15                 const string BaseDir = "../../tools/monkeydoc/Test/monodoc/";
16
17                 class CheckGenerator : IDocGenerator<bool>
18                 {
19                         public string LastCheckMessage { get; set; }
20
21                         public bool Generate (HelpSource hs, string id)
22                         {
23                                 LastCheckMessage = string.Format ("#1 : {0} {1}", hs, id);
24                                 if (hs == null || string.IsNullOrEmpty (id))
25                                         return false;
26
27                                 // Stripe the arguments parts since we don't need it
28                                 var argIdx = id.LastIndexOf ('?');
29                                 if (argIdx != -1)
30                                         id = id.Substring (0, argIdx);
31
32                                 LastCheckMessage = string.Format ("#2 : {0} {1}", hs, id);
33                                 if (hs.IsRawContent (id))
34                                         return hs.GetText (id) != null;
35
36                                 IEnumerable<string> parts;
37                                 if (hs.IsMultiPart (id, out parts)) {
38                                         LastCheckMessage = string.Format ("#4 : {0} {1} ({2})", hs, id, string.Join (", ", parts));
39                                         foreach (var partId in parts)
40                                                 if (!Generate (hs, partId))
41                                                         return false;
42                                 }
43
44                                 LastCheckMessage = string.Format ("#3 : {0} {1}", hs, id);
45                                 if (hs.IsGeneratedContent (id))
46                                         return hs.GetCachedText (id) != null;
47                                 else {
48                                         var s = hs.GetCachedHelpStream (id);
49                                         if (s != null) {
50                                                 s.Close ();
51                                                 return true;
52                                         } else {
53                                                 return false;
54                                         }
55                                 }
56                         }
57                 }
58
59                 /* This test verifies that for every node in our tree that possed a PublicUrl,
60                  * we can correctly access it back through RenderUrl
61                  */
62                 [Test]
63                 public void ReachabilityTest ()
64                 {
65                         var rootTree = RootTree.LoadTree (Path.GetFullPath (BaseDir), false);
66                         Node result;
67                         var generator = new CheckGenerator ();
68                         int errorCount = 0;
69                         int testCount = 0;
70
71                         foreach (var leaf in GetLeaves (rootTree.RootNode)) {
72                                 if (!rootTree.RenderUrl (leaf.PublicUrl, generator, out result) || leaf != result) {
73                                         Console.WriteLine ("Error: {0} with HelpSource {1} ", leaf.PublicUrl, leaf.Tree.HelpSource.Name);
74                                         errorCount++;
75                                 }
76                                 testCount++;
77                         }
78
79                         Assert.AreEqual (0, errorCount, errorCount + " / " + testCount.ToString ());
80                 }
81
82                 IEnumerable<Node> GetLeaves (Node node)
83                 {
84                         if (node == null)
85                                 yield break;
86
87                         if (node.IsLeaf)
88                                 yield return node;
89                         else {
90                                 foreach (var child in node.Nodes) {
91                                         if (!string.IsNullOrEmpty (child.Element) && !child.Element.StartsWith ("root:/"))
92                                                 yield return child;
93                                         foreach (var childLeaf in GetLeaves (child))
94                                                 yield return childLeaf;
95                                 }
96                         }
97                 }
98         }
99 }