Created
August 30, 2022 22:24
-
-
Save escalonn/376d41dc81a6c319db188f45725e92d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run with dotnet run | |
using System.Reflection; | |
using P0; | |
Console.WriteLine("Running P0 tests..."); | |
int passed = 0, failed = 0; | |
IEnumerable<MethodInfo> testMethods = typeof(UnitTests).GetMethods() | |
.Where(m => m.GetCustomAttributes(typeof(UnitTestAttribute)).Any()); | |
foreach (MethodInfo method in testMethods) | |
{ | |
try | |
{ | |
object? tests = Activator.CreateInstance(typeof(UnitTests)); | |
method.Invoke(obj: tests, parameters: Array.Empty<object>()); | |
passed++; | |
Console.WriteLine($"Test {method.Name} passed!"); | |
} | |
catch (TargetInvocationException ex) | |
{ | |
failed++; | |
Console.Error.WriteLine( | |
$"Test {method.Name} failed: {ex.InnerException}"); | |
} | |
} | |
Console.WriteLine($"Passed: {passed}, Failed: {failed}"); | |
/// <summary> | |
/// Marks a method in the UnitTests class as a unit test for P0Tests | |
/// </summary> | |
[AttributeUsage(AttributeTargets.Method)] | |
class UnitTestAttribute : Attribute {} | |
/// <summary> | |
/// Contains unit tests for P0 with the UnitTestAttribute meant to be invoked | |
/// with reflection. | |
/// </summary> | |
class UnitTests | |
{ | |
[UnitTest] | |
public void TestConstructor() | |
{ | |
_ = new Scrabble(); | |
} | |
[UnitTest] | |
public void TestAlphabetize() | |
{ | |
string expected = "act"; | |
string actual = Scrabble.Alphabetize("cat"); | |
if (expected != actual) | |
throw new Exception("Fail"); | |
} | |
[UnitTest] | |
public void TestDictionaryScan() | |
{ | |
using FileStream file = | |
File.Open("../data/words_alpha.txt", FileMode.Open); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment