File size: 4,498 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
using System.Runtime.CompilerServices;
using VersOne.Epub.Test.Comparers;
using VersOne.Epub.Test.Integration.CustomSerialization;
using VersOne.Epub.Test.Integration.Types;

namespace VersOne.Epub.Test.Integration.Runner
{
    public class TestRunner
    {
        private const string TEST_CASES_DIRECTORY_NAME = "TestCases";
        private const string TEST_CASES_FILE_NAME = "testcases.json";
        private const string TEST_EPUB_FILE_NAME = "test.epub";

        private static readonly string rootTestCasesDirectory;
        private static readonly TestCaseSerializer testCaseSerializer;

        static TestRunner()
        {
            rootTestCasesDirectory = GetRootTestCasesDirectory();
            testCaseSerializer = new TestCaseSerializer();
        }

        [Theory(DisplayName = "Integration test")]
        [MemberData(nameof(GetTestCaseDirectories))]
        public void Run(string testCaseDirectoryPath)
        {
            string testCaseDirectoryAbsolutePath = Path.Combine(rootTestCasesDirectory, testCaseDirectoryPath);
            string testCasesPath = Path.Combine(testCaseDirectoryAbsolutePath, TEST_CASES_FILE_NAME);
            string testEpubPath = Path.Combine(testCaseDirectoryAbsolutePath, TEST_EPUB_FILE_NAME);
            List<TestCase>? testCases = ReadTestCases(testCasesPath, testEpubPath);
            Assert.NotNull(testCases);
            foreach (TestCase testCase in testCases)
            {
                if (testCase.ExpectedResult != null)
                {
                    EpubBook epubBook = EpubReader.ReadBook(testEpubPath, testCase.Options);
                    EpubBookComparer.CompareEpubBooks(testCase.ExpectedResult, epubBook);
                }
                else if (testCase.ExpectedException != null)
                {
                    bool exceptionThrown = false;
                    try
                    {
                        EpubReader.ReadBook(testEpubPath, testCase.Options);
                    }
                    catch (Exception actualException)
                    {
                        exceptionThrown = true;
                        Assert.Equal(actualException.GetType().Name, testCase.ExpectedException.Type);
                        if (testCase.ExpectedException.Message != null)
                        {
                            Assert.Equal(actualException.Message, testCase.ExpectedException.Message);
                        }
                    }
                    Assert.True(exceptionThrown, $"Expected exception was not thrown for the test case: {testCase.Name}");
                }
            }
        }

        [Fact(Skip = "This method should be run manually")]
        public void GenerateTestCaseTemplates()
        {
            TestCaseWriter testCaseWriter = new(testCaseSerializer, rootTestCasesDirectory, TEST_CASES_FILE_NAME, TEST_EPUB_FILE_NAME);
            testCaseWriter.WriteAllTestCases();
        }

        private static List<TestCase>? ReadTestCases(string testCasePath, string testEpubPath)
        {
            return testCaseSerializer.Deserialize(testCasePath, testEpubPath);
        }

        public static TheoryData<string> GetTestCaseDirectories()
        {
            List<string> directories = new();
            GetTestCaseDirectories(rootTestCasesDirectory, directories);
            return new(directories);
        }

        private static void GetTestCaseDirectories(string currentDirectory, List<string> directories)
        {
            if (File.Exists(Path.Combine(currentDirectory, TEST_CASES_FILE_NAME)))
            {
                directories.Add(currentDirectory[(rootTestCasesDirectory.Length + 1)..]);
            }
            else
            {
                foreach (string subdirectory in Directory.GetDirectories(currentDirectory))
                {
                    GetTestCaseDirectories(subdirectory, directories);
                }
            }
        }

        private static string GetRootTestCasesDirectory([CallerFilePath] string? callerFilePath = null)
        {
            string? runnerDirectory = Path.GetDirectoryName(callerFilePath);
            Assert.NotNull(runnerDirectory);
            DirectoryInfo? runnerDirectoryInfo = Directory.GetParent(runnerDirectory);
            Assert.NotNull(runnerDirectoryInfo);
            string integrationDirectory = runnerDirectoryInfo.FullName;
            return Path.Combine(integrationDirectory, TEST_CASES_DIRECTORY_NAME);
        }
    }
}