Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 98 lines (78 sloc) 3.925 kb
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Shouldly;

namespace Tests
{
[TestFixture]
    public class DocumentationBuilder
    {
[TestCase("TimeTests")]
[TestCase("DateTests")]
[Explicit("Only used to run to output some text to be used in documentation.")]
        public void Generate_examples_from_tests(string testfixture)
        {
            var startLine = string.Format("<!--- {0} start -->", testfixture);
            var endLine = string.Format("<!--- {0} end -->", testfixture);
            var fileName = "../../../../README.md";

            File.Exists(fileName).ShouldBe(true);

            var lines = File.ReadAllLines(fileName);

            try
            {
                var top = lines.TakeWhile(l => l != startLine).ToArray();
                var bottomAndEndLine = lines.SkipWhile(l => l != endLine).ToArray();

                var documentLines = GenerateDocumentationLinesFromTestData(testfixture);

                var updated = top.Concat(new[] { startLine })
                    .Concat(documentLines)
                    .Concat(bottomAndEndLine).ToArray();

                top.ShouldNotBeEmpty();
                bottomAndEndLine.ShouldNotBeEmpty();
                updated.ShouldNotBeEmpty();

                File.WriteAllLines(fileName, updated);
            }
            catch (Exception ex)
            {
                File.WriteAllLines(fileName, lines);
                Assert.Fail(ex.Message);
            }
        }

        private IEnumerable<string> GenerateDocumentationLinesFromTestData(string testfixture)
        {
            if (testfixture == "TimeTests")
                return TimeTestDocumentation();

            return DateTestDocumentation();
        }

        private IEnumerable<string> DateTestDocumentation()
        {
            Func<TestCaseData, string> format = td => string.Format(@" new Date(BaseDate, ""{0}"") // {2}{1}", td.Arguments[0], Environment.NewLine, td.Result);

            yield return "### Relative Date Values";
            yield return string.Format(" var BaseDate = new DateTime({0});{1}", DateTests.BaseDate.ToString("yyyy, M, d, H, m, s") , Environment.NewLine);
            foreach (var testData in DateTests.RelativeTimeTestData.Cast<TestCaseData>())
                yield return format(testData);
        }

        private IEnumerable<string> TimeTestDocumentation()
        {
            Func<TestCaseData, string> format = td => string.Format(@" new Time(""{0}"") // {2}{1}", td.Arguments[0], Environment.NewLine, td.Result);

            yield return "### Single Values";
            foreach (var testData in TimeTests.SingleValueTestData.Cast<TestCaseData>())
                yield return format(testData);

            yield return "### Multiple Values";
            foreach (var testData in TimeTests.MultiValueTestData.Cast<TestCaseData>())
                yield return format(testData);

            yield return "### Fractional Values";
            foreach (var testData in TimeTests.FractionalValueTestData.Cast<TestCaseData>())
                yield return format(testData);

            yield return "### Multiple and Fractional Values";
            foreach (var testData in TimeTests.MultipleAndFractionalValueTestData.Cast<TestCaseData>())
                yield return format(testData);

            yield return "### Mixed Case (upper/lower) Values";
            foreach (var testData in TimeTests.MixedCaseValueTestData.Cast<TestCaseData>())
                yield return format(testData);

            yield return "### English Words to Values";
            foreach (var testData in TimeTests.EnglishWordsValueTestData.Cast<TestCaseData>())
                yield return format(testData);
        }

    }
}
Something went wrong with that request. Please try again.