public void TokenPipeline_MathExpressionException_Thrown_By_Rewriter_Is_Not_Wrapped_In_MathExpressionException()
 {
     var processor1 = new TestActionProcessor(() => { throw new MathExpressionException("An error."); });
     var processor2 = new TestActionProcessor(() => { });
     TokenPipeline pipeline = new TokenPipeline(processor1, processor2);
     var exception = Assert.Throws<MathExpressionException>(() => pipeline.Process(new TokenStream(new Token[] { }, TokenNotation.Infix)));
     Assert.IsNull(exception.InnerException);
     Assert.AreEqual("An error.", exception.Message);
 }
 public void TokenPipeline_Executes_Processors_In_Same_Order_As_Added()
 {
     DateTime processor1InvokedAt = DateTime.MinValue;
     DateTime processor2InvokedAt = DateTime.MinValue;
     var processor1 = new TestActionProcessor(() => { processor1InvokedAt = DateTime.Now; Thread.Sleep(5); });
     var processor2 = new TestActionProcessor(() => { processor2InvokedAt = DateTime.Now; });
     TokenPipeline pipeline = new TokenPipeline(processor1, processor2);
     pipeline.Process(new TokenStream(new Token[] { }, TokenNotation.Infix));
     Assert.Less(processor1InvokedAt, processor2InvokedAt);
 }
 public void TokenPipeline_Exceptions_Thrown_By_Processor_Is_Wrapped_In_MathExpressionException()
 {
     var processor1 = new TestActionProcessor(() => { throw new InvalidOperationException("An error."); });
     var processor2 = new TestActionProcessor(() => { });
     TokenPipeline pipeline = new TokenPipeline(processor1, processor2);
     var exception = Assert.Throws<MathExpressionException>(() => pipeline.Process(new TokenStream(new Token[] { }, TokenNotation.Infix)));
     Assert.IsInstanceOf<InvalidOperationException>(exception.InnerException);
     Assert.AreEqual("An error occured while processing tokens. See inner exception for details.", exception.Message);
     Assert.AreEqual("An error.", exception.InnerException.Message);
 }
 public void NegationRewriter_Can_Rewrite_Subtraction_Token_If_First_In_Stream()
 {
     // -3
     List<Token> tokens = new List<Token> {
         new Token(TokenType.Subtraction, "-"),
         new Token(TokenType.Numeric, "3")
     };
     TokenStream stream = new TokenStream(tokens, TokenNotation.Infix);
     TokenPipeline pipeline = new TokenPipeline(new NegationRewriter());
     var result = pipeline.Process(stream).ToArray();
     Assert.AreEqual(2, result.Length);
     Assert.AreEqual(TokenType.Negation, result[0].Type);
 }
 public void NegationRewriter_Can_Rewrite_Subtraction_Token_If_Preceeded_By_Opening_Parenthesis()
 {
     // 3+(-2)
     List<Token> tokens = new List<Token> {
         new Token(TokenType.Numeric, 3),
         new Token(TokenType.Addition, "+"),
         new Token(TokenType.OpeningParenthesis, "("),
         new Token(TokenType.Subtraction, "-"),
         new Token(TokenType.Numeric, "3"),
         new Token(TokenType.ClosingParenthesis, ")")
     };
     TokenStream stream = new TokenStream(tokens, TokenNotation.Infix);
     TokenPipeline pipeline = new TokenPipeline(new NegationRewriter());
     var result = pipeline.Process(stream);
     Assert.AreEqual(6, result.Count);
     Assert.AreEqual(TokenType.Negation, result[3].Type);
 }
 public void NegationRewriter_Subtraction_Is_Not_Rewritten()
 {
     // 3+(-2)
     List<Token> tokens = new List<Token> {
         new Token(TokenType.Numeric, 3),
         new Token(TokenType.Subtraction, "-"),
         new Token(TokenType.OpeningParenthesis, "4"),
     };
     TokenStream stream = new TokenStream(tokens, TokenNotation.Infix);
     TokenPipeline pipeline = new TokenPipeline(new NegationRewriter());
     var result = pipeline.Process(stream).ToArray();
     Assert.AreEqual(3, result.Length);
     Assert.AreEqual(TokenType.Subtraction, result[1].Type);
 }
 public void NegationRewriter_Can_Rewrite_Subtraction_Token_If_Preceeded_By_Operator()
 {
     // 2+-3
     List<Token> tokens = new List<Token> {
         new Token(TokenType.Numeric, 2),
         new Token(TokenType.Addition, "+"),
         new Token(TokenType.Subtraction, "-"),
         new Token(TokenType.Numeric, "3")
     };
     TokenStream stream = new TokenStream(tokens, TokenNotation.Infix);
     TokenPipeline pipeline = new TokenPipeline(new NegationRewriter());
     var result = pipeline.Process(stream).ToArray();
     Assert.AreEqual(4, result.Length);
     Assert.AreEqual(TokenType.Negation, result[2].Type);
 }