Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (94 sloc) 4.592 kb
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using NUnit.Framework;
6 using Shouldly;
7
8 namespace Tests
9 {
10 [TestFixture]
11 public class DocumentationBuilder
12 {
79d400c @ChrisMissal Clean up formatting on readme generation
authored
13 const string OpenCodeBlock = "```csharp";
14 const string CloseCodeBlock = "```";
15
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
16 [TestCase("TimeTests")]
17 [TestCase("DateTests")]
18 [Explicit("Only used to run to output some text to be used in documentation.")]
19 public void Generate_examples_from_tests(string testfixture)
20 {
21 var startLine = string.Format("<!--- {0} start -->", testfixture);
22 var endLine = string.Format("<!--- {0} end -->", testfixture);
23 var fileName = "../../../../README.md";
24
25 File.Exists(fileName).ShouldBe(true);
26
27 var lines = File.ReadAllLines(fileName);
28
29 try
30 {
31 var top = lines.TakeWhile(l => l != startLine).ToArray();
32 var bottomAndEndLine = lines.SkipWhile(l => l != endLine).ToArray();
33
34 var documentLines = GenerateDocumentationLinesFromTestData(testfixture);
35
36 var updated = top.Concat(new[] { startLine })
37 .Concat(documentLines)
38 .Concat(bottomAndEndLine).ToArray();
39
40 top.ShouldNotBeEmpty();
41 bottomAndEndLine.ShouldNotBeEmpty();
42 updated.ShouldNotBeEmpty();
43
44 File.WriteAllLines(fileName, updated);
45 }
46 catch (Exception ex)
47 {
48 File.WriteAllLines(fileName, lines);
49 Assert.Fail(ex.Message);
50 }
51 }
52
53 private IEnumerable<string> GenerateDocumentationLinesFromTestData(string testfixture)
54 {
55 if (testfixture == "TimeTests")
56 return TimeTestDocumentation();
57
58 return DateTestDocumentation();
59 }
60
61 private IEnumerable<string> DateTestDocumentation()
62 {
79d400c @ChrisMissal Clean up formatting on readme generation
authored
63 Func<TestCaseData, string> format = td => string.Format(@"new Date(BaseDate, ""{0}"") // {2}{1}", td.Arguments[0], Environment.NewLine, td.Result);
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
64
65 yield return "### Relative Date Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
66 yield return OpenCodeBlock;
67 yield return string.Format("var BaseDate = new DateTime({0});{1}", DateTests.BaseDate.ToString("yyyy, M, d, H, m, s"), Environment.NewLine);
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
68 foreach (var testData in DateTests.RelativeTimeTestData.Cast<TestCaseData>())
69 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
70 yield return CloseCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
71 }
72
73 private IEnumerable<string> TimeTestDocumentation()
74 {
79d400c @ChrisMissal Clean up formatting on readme generation
authored
75 Func<TestCaseData, string> format = td => string.Format(@"new Time(""{0}"") // {2}{1}", td.Arguments[0], Environment.NewLine, td.Result);
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
76
77 yield return "### Single Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
78 yield return OpenCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
79 foreach (var testData in TimeTests.SingleValueTestData.Cast<TestCaseData>())
80 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
81 yield return CloseCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
82
83 yield return "### Multiple Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
84 yield return OpenCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
85 foreach (var testData in TimeTests.MultiValueTestData.Cast<TestCaseData>())
86 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
87 yield return CloseCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
88
89 yield return "### Fractional Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
90 yield return OpenCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
91 foreach (var testData in TimeTests.FractionalValueTestData.Cast<TestCaseData>())
92 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
93 yield return CloseCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
94
95 yield return "### Multiple and Fractional Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
96 yield return OpenCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
97 foreach (var testData in TimeTests.MultipleAndFractionalValueTestData.Cast<TestCaseData>())
98 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
99 yield return CloseCodeBlock;
4d32029 @ChrisMissal upgraded to Parsley 0.0.4 and added case insensitivity
authored
100
101 yield return "### Mixed Case (upper/lower) Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
102 yield return OpenCodeBlock;
4d32029 @ChrisMissal upgraded to Parsley 0.0.4 and added case insensitivity
authored
103 foreach (var testData in TimeTests.MixedCaseValueTestData.Cast<TestCaseData>())
104 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
105 yield return CloseCodeBlock;
077b7c3 @ChrisMissal added words for digits 'one' through 'ten'
authored
106
107 yield return "### English Words to Values";
79d400c @ChrisMissal Clean up formatting on readme generation
authored
108 yield return OpenCodeBlock;
077b7c3 @ChrisMissal added words for digits 'one' through 'ten'
authored
109 foreach (var testData in TimeTests.EnglishWordsValueTestData.Cast<TestCaseData>())
110 yield return format(testData);
79d400c @ChrisMissal Clean up formatting on readme generation
authored
111 yield return CloseCodeBlock;
9e8ddc6 @ChrisMissal cleaned up some code and added Date examples
authored
112 }
113
114 }
115 }
Something went wrong with that request. Please try again.