-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseClasses.cs
114 lines (93 loc) · 4 KB
/
BaseClasses.cs
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
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
namespace AdventOfCode
{
public abstract class SolutionBase
{
}
public abstract class SolutionBase<TPart1,TPart2> : SolutionBase
{
public virtual IEnumerable<TPart1> GetPart1SampleInputs() => Enumerable.Empty<TPart1>();
public virtual IEnumerable<TPart2> GetPart2SampleInputs()
=> typeof(TPart1) == typeof(TPart2)
? (IEnumerable<TPart2>) GetPart1SampleInputs()
: Enumerable.Empty<TPart2>();
public virtual IEnumerable<string> GetPart1SampleOutputs() => Enumerable.Empty<string>();
public virtual IEnumerable<string> GetPart2SampleOutputs() => Enumerable.Empty<string>();
public virtual TPart1 GetPart1Input() => default;
public virtual TPart2 GetPart2Input()
=> typeof(TPart1) == typeof(TPart2)
? (TPart2) (object) GetPart1Input()
: default;
public virtual object SolvePart1(TPart1 input) => null;
public virtual object SolvePart2(TPart2 input) => null;
protected string ReadInput(string postfix = ".txt") => File.ReadAllText(InputFileName(postfix));
protected string[] ReadInputLines(string postfix = ".txt") => File.ReadAllLines(InputFileName(postfix));
public string InputFileName(string postfix) => "InputFiles\\" + GetType().Name.Replace("Solution", "") + postfix;
}
public abstract class TestBase<TSolution, TPart1, TPart2> where TSolution : SolutionBase<TPart1, TPart2>, new()
{
private readonly ITestOutputHelper _outputHelper;
private SolutionBase<TPart1, TPart2> _solution = new TSolution();
public TestBase(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}
public static IEnumerable<object[]> Part1SampleData()
{
var solution = new TSolution();
var inputs = solution.GetPart1SampleInputs();
var outputs = solution.GetPart1SampleOutputs();
if (!outputs.Any()) return new List<object[]> {new object[] {default(TPart1), null}};
return inputs.Zip(outputs, (i, o) => new object[]{i, o});
}
public static IEnumerable<object[]> Part2SampleData()
{
var solution = new TSolution();
var inputs = solution.GetPart2SampleInputs();
var outputs = solution.GetPart2SampleOutputs();
if (!outputs.Any()) return new List<object[]> {new object[] {default(TPart2), null}};
return inputs.Zip(outputs, (i, o) => new object[]{i, o});
}
[SkippableFact]
public void Part1()
{
var input = _solution.GetPart1Input();
Skip.If(input == null, "No Input");
var output = _solution.SolvePart1(input);
Skip.If(output == null);
_outputHelper.WriteLine("Solution Part 1: " + output);
}
[SkippableFact]
public void Part2()
{
var input = _solution.GetPart2Input();
Skip.If(input == null, "No Input");
var output = _solution.SolvePart2(input);
Skip.If(output == null);
_outputHelper.WriteLine("Solution Part 2: " + output);
}
[SkippableTheory]
[MemberData(nameof(Part1SampleData))]
public void Part1Tests(TPart1 input, string expectedOutput)
{
Skip.If(expectedOutput == null);
var output = _solution.SolvePart1(input).ToString();
Skip.If(output == null);
Assert.Equal(expectedOutput, output);
}
[SkippableTheory]
[MemberData(nameof(Part2SampleData))]
public void Part2Tests(TPart2 input, string expectedOutput)
{
Skip.If(expectedOutput == null);
var output = _solution.SolvePart2(input).ToString();
Skip.If(output == null);
Assert.Equal(expectedOutput, output);
}
}
}