Last active
June 9, 2019 11:07
-
-
Save NikiforovAll/1738fcc2fe9636ef9a4e35f8f04dce26 to your computer and use it in GitHub Desktop.
CommandImpl
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; | |
namespace Command { | |
public class Calculator { | |
private int _curr = 0; | |
public int CurrentResult { get => _curr; set => _curr = value; } | |
public int Operate (char @operator, int operand) { | |
// possibility to use interpreter pattern | |
switch (@operator) { | |
case '+': | |
CurrentResult += operand; | |
break; | |
case '-': | |
CurrentResult -= operand; | |
break; | |
case '*': | |
CurrentResult *= operand; | |
break; | |
case '/': | |
CurrentResult /= operand; | |
break; | |
} | |
return CurrentResult; | |
} | |
} | |
} | |
namespace Command { | |
public class CalculatorClient { | |
private Calculator calculator = new Calculator (); | |
private Stack<Command> _commands = new Stack<Command> (); | |
public int Result { get => calculator.CurrentResult; } | |
public double Compute (char @operator, int operand) { | |
var command = new CalculatorCommand (calculator, @operator, operand); | |
_commands.Push (command); | |
return command.Execute (); | |
} | |
public double Undo () { | |
return _commands.Pop ().UnExecute (); | |
} | |
} | |
} | |
namespace Command { | |
public class CalculatorCommand : Command { | |
private readonly Calculator calculator; | |
private readonly char @operator; | |
private readonly int operand; | |
public CalculatorCommand (Calculator calculator, char @operator, int operand) { | |
this.calculator = calculator; | |
this.@operator = @operator; | |
this.operand = operand; | |
} | |
public override double Execute () { | |
return calculator.Operate (@operator, operand); | |
} | |
public override double UnExecute () { | |
return calculator.Operate (GetUndoOperator (@operator), operand); | |
} | |
private char GetUndoOperator (char @operator) { | |
switch (@operator) { | |
case '+': | |
return '-'; | |
case '-': | |
return '+'; | |
case '*': | |
return '/'; | |
case '/': | |
return '*'; | |
default: | |
throw new ArgumentException (nameof (@operator) + "is not found"); | |
} | |
} | |
} | |
} | |
namespace Command { | |
public abstract class Command { | |
public abstract double Execute (); | |
public abstract double UnExecute (); | |
} | |
} |
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 Command; | |
public class Program { | |
public static void Main () { | |
var operations = new List < (char @operator, int operand) > { | |
('+', 10), | |
('-', 5), | |
('*', 3) | |
}; | |
CalculatorClient client = new CalculatorClient (); | |
foreach (var operation in operations) { | |
client.Compute (operation.@operator, operation.operand); | |
} | |
Assert.Equal (15, client.Result); | |
client.Undo (); | |
Assert.Equal (5, client.Result); | |
TestRunner.Print(); | |
} | |
} |
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.Runtime.CompilerServices; | |
public static class Assert { | |
public static void Equal<T> (T a, T b, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) { | |
if (!a.Equals (b)) { | |
// throw new Exception ($"{a} doesn't equal to {b}"); | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = false, | |
Assertion = "Equal<T>", | |
ErrorMessage = $"{a} doesn't equal to {b}" | |
}); | |
} else { | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = true, | |
Assertion = "Single<T>" | |
}); | |
} | |
} | |
public static void Equal<T> (IEnumerable<T> a, | |
IEnumerable<T> b, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) { | |
if (a.Count () != b.Count ()) { | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = false, | |
Assertion = "Equal<T>", | |
ErrorMessage = $"a.{a.Count()} != b.{b.Count()}" | |
}); | |
} else if (!a.SequenceEqual (b)) { | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = false, | |
Assertion = "SequenceEqual<T>", | |
ErrorMessage = $"Different sequences" | |
}); | |
} else { | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = true, | |
Assertion = "Equal<T>" | |
}); | |
} | |
} | |
public static void Single<T> (IEnumerable<T> collection, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) { | |
if (collection.Count () != 1) { | |
// throw new Exception ("Collection doesn't contain 1 element"); | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = false, | |
Assertion = "Single<T>", | |
ErrorMessage = "Collection doesn't contain 1 element" | |
}); | |
} | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = true, | |
Assertion = "Single<T>" | |
}); | |
} | |
public static void Contains<T> (T token1, T token2, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) { | |
if (typeof (T) == typeof (System.String)) { | |
if (!((token2 as string)).Contains (token1 as string)) { | |
// throw new Exception ($"{token2} doesn't contain {token1}"); | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = false, | |
Assertion = "Contains<T>", | |
ErrorMessage = $"{token2} doesn't contain {token1}" | |
}); | |
} else { | |
TestRunner.AddResult ( | |
new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = true, | |
Assertion = "Contains<T>" | |
}); | |
} | |
} else { | |
throw new Exception ("It is not possible to use .Contains() method"); | |
} | |
} | |
public static void All<T> (IEnumerable<T> collection, Func<T, bool> predicate, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) { | |
bool res = collection.All (predicate); | |
var tr = new TestRunResult () { | |
CallerName = callerName, | |
CallerLine = callerLine.ToString (), | |
Success = res, | |
Assertion = "All<T>" | |
}; | |
TestRunner.AddResult (tr); | |
if(!res) { | |
tr.ErrorMessage = "Condition is not met for all members"; | |
} | |
} | |
} | |
public static class TestRunner { | |
public static List<TestRunResult> Results { get; } = new List<TestRunResult> (); | |
public static void AddResult (TestRunResult res) { | |
Results.Add (res); | |
} | |
public static void Print () { | |
System.Console.WriteLine ("TestRunner.Print"); | |
System.Console.WriteLine ("============================================"); | |
foreach (var log in Results) { | |
System.Console.WriteLine (log); | |
} | |
System.Console.WriteLine ("============================================"); | |
} | |
} | |
public class TestRunResult { | |
public string CallerName { get; set; } | |
public string CallerLine { get; set; } | |
public string Assertion { get; set; } | |
public bool Success { get; set; } | |
public string ErrorMessage { get; set; } | |
public override string ToString () { | |
return $"Assertion: {Assertion, 15}; MethodName: {CallerName, 10}; Line: {CallerLine, 3}; " + (Success? $"Success: {Success}": $"ErrorMessage: {ErrorMessage}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment