Skip to content

Commit ea793b3

Browse files
committed
add test cases
1 parent c32d54f commit ea793b3

File tree

239 files changed

+19464
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+19464
-2
lines changed

src/Typewriter/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace Typewriter
1313
{
14-
class Program
14+
public class Program
1515
{
1616
internal static Logger Logger => LogManager.GetLogger("Typewriter");
1717

18-
static void Main(string[] args)
18+
public static void Main(string[] args)
1919
{
2020
Parser.Default.ParseArguments<Options>(args)
2121
.WithParsed(opts => GenerateSDK(opts))
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.IO;
4+
using System.Text;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Collections.Generic;
8+
9+
namespace Typewriter.Test
10+
{
11+
[TestClass]
12+
public class CSharpMultipleNamespaceSupportTests
13+
{
14+
private const string OutputDirectoryName = "OutputDirectory";
15+
private const string TestDataCSharpDirectoryName = "TestDataCSharp";
16+
private const string MetadataDirectoryName = "Metadata";
17+
18+
[TestMethod]
19+
public void Test()
20+
{
21+
// Arrange
22+
var currentDirectory = Directory.GetCurrentDirectory();
23+
var outputDirectory = Path.Combine(currentDirectory, OutputDirectoryName);
24+
var dataDirectory = Path.Combine(currentDirectory, TestDataCSharpDirectoryName);
25+
var metadataFile = Path.Combine(currentDirectory, MetadataDirectoryName, "MetadataMultipleNamespaces.xml");
26+
var typewriterParameters = $"-v Info -m {metadataFile} -o {outputDirectory} -g Files";
27+
28+
// Act
29+
if (Directory.Exists(outputDirectory))
30+
{
31+
Directory.Delete(outputDirectory, recursive: true); // clean up any previous runs
32+
}
33+
34+
Program.Main(typewriterParameters.Split(' '));
35+
36+
// Assert
37+
var testOutputBuilder = new StringBuilder();
38+
var errorCounter = 0;
39+
foreach (var (expectedFilePath, actualOutputFilePath) in GetFilePaths(dataDirectory))
40+
{
41+
if (File.Exists(actualOutputFilePath))
42+
{
43+
CompareFiles(testOutputBuilder, expectedFilePath, actualOutputFilePath, ref errorCounter);
44+
}
45+
else
46+
{
47+
testOutputBuilder.AppendLine();
48+
testOutputBuilder.AppendLine($"{++errorCounter}. Output file is not generated: {actualOutputFilePath}");
49+
}
50+
}
51+
52+
if (errorCounter > 0)
53+
{
54+
string message = string.Join(Environment.NewLine,
55+
"A diff between following folders are strongly encouraged to see if the changes are intended:",
56+
dataDirectory,
57+
outputDirectory,
58+
string.Empty,
59+
$"If the changes are expected, please replace the contents of {TestDataCSharpDirectoryName} with the contents of {OutputDirectoryName}.",
60+
string.Empty,
61+
"Details of failures:");
62+
63+
Assert.Fail(message + testOutputBuilder.ToString());
64+
}
65+
}
66+
67+
/// <summary>
68+
/// Compares the contents of expected file to actual output from Typewriter.
69+
/// </summary>
70+
/// <param name="testOutputBuilder">String builder to append into the test output</param>
71+
/// <param name="expectedFilePath">Path to the expected file</param>
72+
/// <param name="actualOutputFilePath">Path to the actual output file from Typewriter</param>
73+
/// <param name="errorCounter">Error counter for the test run</param>
74+
private static void CompareFiles(StringBuilder testOutputBuilder, string expectedFilePath, string actualOutputFilePath, ref int errorCounter)
75+
{
76+
var expectedFileContents = File.ReadAllText(expectedFilePath);
77+
var actualFileContents = File.ReadAllText(actualOutputFilePath);
78+
if (expectedFileContents != actualFileContents)
79+
{
80+
testOutputBuilder.AppendLine();
81+
testOutputBuilder.AppendLine($"{++errorCounter}. File contents do not match.");
82+
testOutputBuilder.AppendLine($"\tExpected file: {expectedFilePath}");
83+
testOutputBuilder.AppendLine($"\tActual file: {actualOutputFilePath}");
84+
}
85+
}
86+
87+
/// <summary>
88+
/// Takes a data directory, traverses the entire subdirectory structure,
89+
/// extracts full file paths as expected file paths,
90+
/// converts expected file paths into actual output file paths as well for a later diff.
91+
/// </summary>
92+
/// <param name="dataDirectory">Data directory full path</param>
93+
/// <returns>Pairs of expected and actual file paths as an enumerable</returns>
94+
private static IEnumerable<(string, string)> GetFilePaths(string dataDirectory)
95+
{
96+
return from file in new DirectoryInfo(dataDirectory).GetFiles("*.cs", SearchOption.AllDirectories)
97+
let actualOutputFilePath = file.FullName.Replace(TestDataCSharpDirectoryName, OutputDirectoryName)
98+
let expectedFilePath = file.FullName
99+
select (expectedFilePath, actualOutputFilePath);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)