Last active
August 29, 2015 14:20
-
-
Save Lysander/13ae5ab68815cd780827 to your computer and use it in GitHub Desktop.
Parameterized unit testing polymorphic types based upon the LSP
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ExpressionChecking | |
{ | |
public interface IExpressionChecker | |
{ | |
bool IsValid(string expression); | |
} | |
public class StackBasedExpressionChecker : IExpressionChecker | |
{ | |
public bool IsValid(string expression) | |
{ | |
var stack = new Stack<char>(); | |
try | |
{ | |
foreach (var c in expression) | |
{ | |
switch (c) | |
{ | |
case '(': | |
stack.Push(c); | |
break; | |
case ')': | |
stack.Pop(); | |
break; | |
} | |
} | |
} | |
catch (InvalidOperationException) | |
{ | |
return false; | |
} | |
return stack.Count() == 0; | |
} | |
} | |
public class SumBasedExpressionChecker : IExpressionChecker | |
{ | |
public bool IsValid(string expression) | |
{ | |
var bracketValues = new Dictionary<char, int> { { '(', 1 }, { ')', -1 } }; | |
var sum = 0; | |
foreach (var c in expression) | |
{ | |
try | |
{ | |
sum += bracketValues[c]; | |
} | |
catch (KeyNotFoundException) | |
{ | |
} | |
if (sum < 0) | |
{ | |
return false; | |
} | |
} | |
return sum == 0; | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using NUnit.Framework; | |
using ExpressionChecking; | |
namespace ExpressionCheckingUnitTests | |
{ | |
// Not a TestFixture!!! Just the template ;-) | |
public class ExpressionCheckerTests<TChecker> where TChecker : IExpressionChecker, new() | |
{ | |
[TestCase("((a+b)*c)")] | |
[TestCase("(()()((()())))")] | |
public void IsValid_ForValidExpressions_GivesTrue(string expression) | |
{ | |
var sut = new TChecker(); // our SUT remains generic! | |
var result = sut.IsValid(expression); | |
Assert.That(result, Is.True); | |
} | |
[TestCase("((a+b)(")] | |
[TestCase("(a+b)*c))")] | |
[TestCase("((())())))")] | |
public void IsValid_ForInvalidExpressions_GivesFalse(string expression) | |
{ | |
var sut = new TChecker(); // our SUT remains generic! | |
var result = sut.IsValid(expression); | |
Assert.That(result, Is.False); | |
} | |
} | |
[TestFixture] | |
public class StackBasedvarExpressionCheckerTests : ExpressionCheckerTests<StackBasedExpressionChecker> | |
{ | |
// here we just created a recognizeable test class for the NUnit faremwork with a concrete type | |
} | |
[TestFixture] | |
public class SumBasedvarExpressionCheckerTests : ExpressionCheckerTests<SumBasedExpressionChecker> | |
{ | |
// and here the other one | |
} | |
// every future implementation just needs a definition like the above ones as the following lines shows | |
// [TestFixture] | |
// public class SumBasedvarExpressionCheckerTests : ExpressionCheckerTests<SomeNotYetExistingExpressionChecker> | |
// { | |
// } | |
// clean and terse! 😎 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment