|
| 1 | +using System; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Zad2; |
| 4 | + |
| 5 | +namespace Tests |
| 6 | +{ |
| 7 | + [TestClass] |
| 8 | + public class Zad2Test |
| 9 | + { |
| 10 | + const string TRUE_TOKEN = "true"; |
| 11 | + const string FALSE_TOKEN = "false"; |
| 12 | + |
| 13 | + [TestMethod] |
| 14 | + public void TestUnaryVar() |
| 15 | + { |
| 16 | + const string VAR_NAME = "x"; |
| 17 | + |
| 18 | + var ctx = new Context(); |
| 19 | + ctx.SetValue(VAR_NAME, false); |
| 20 | + |
| 21 | + var unary = new UnaryExpression(VAR_NAME); |
| 22 | + Assert.AreEqual(true, unary.Interpret(ctx)); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void TestConst() |
| 27 | + { |
| 28 | + var ctx = new Context(); |
| 29 | + |
| 30 | + var constExp = new ConstExpression(TRUE_TOKEN); |
| 31 | + Assert.AreEqual(true, constExp.Interpret(ctx)); |
| 32 | + |
| 33 | + constExp = new ConstExpression(FALSE_TOKEN); |
| 34 | + Assert.AreEqual(false, constExp.Interpret(ctx)); |
| 35 | + } |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void TestBinaryAndVar() |
| 39 | + { |
| 40 | + const string VAR_X = "x"; |
| 41 | + const string VAR_Y = "y"; |
| 42 | + const string VAR_Z = "z"; |
| 43 | + |
| 44 | + var ctx = new Context(); |
| 45 | + ctx.SetValue(VAR_X, false); |
| 46 | + ctx.SetValue(VAR_Y, true); |
| 47 | + ctx.SetValue(VAR_Z, true); |
| 48 | + |
| 49 | + var binary = new BinaryExpression(VAR_X, BinaryOp.And, VAR_Y); |
| 50 | + Assert.AreEqual(false, binary.Interpret(ctx)); |
| 51 | + |
| 52 | + binary = new BinaryExpression(VAR_Y, BinaryOp.And, VAR_Z); |
| 53 | + Assert.AreEqual(true, binary.Interpret(ctx)); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void TestBinaryOrVar() |
| 58 | + { |
| 59 | + const string VAR_X = "x"; |
| 60 | + const string VAR_Y = "y"; |
| 61 | + const string VAR_Z = "z"; |
| 62 | + |
| 63 | + var ctx = new Context(); |
| 64 | + ctx.SetValue(VAR_X, false); |
| 65 | + ctx.SetValue(VAR_Y, true); |
| 66 | + ctx.SetValue(VAR_Z, false); |
| 67 | + |
| 68 | + var binary = new BinaryExpression(VAR_X, BinaryOp.Or, VAR_Y); |
| 69 | + Assert.AreEqual(true, binary.Interpret(ctx)); |
| 70 | + |
| 71 | + binary = new BinaryExpression(VAR_X, BinaryOp.Or, VAR_Z); |
| 72 | + Assert.AreEqual(false, binary.Interpret(ctx)); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + public void TestComplexExpression() |
| 77 | + { |
| 78 | + const string VAR_X = "x"; |
| 79 | + const string VAR_Y = "y"; |
| 80 | + const string VAR_Z = "z"; |
| 81 | + |
| 82 | + var ctx = new Context(); |
| 83 | + ctx.SetValue(VAR_X, true); |
| 84 | + ctx.SetValue(VAR_Y, true); |
| 85 | + ctx.SetValue(VAR_Z, false); |
| 86 | + |
| 87 | + var constExp = new ConstExpression(TRUE_TOKEN); |
| 88 | + var unaryExp = new UnaryExpression(constExp); |
| 89 | + Assert.AreEqual(false, unaryExp.Interpret(ctx)); |
| 90 | + |
| 91 | + var binaryExp = |
| 92 | + new BinaryExpression( |
| 93 | + new BinaryExpression(VAR_X, |
| 94 | + BinaryOp.And, |
| 95 | + unaryExp), |
| 96 | + BinaryOp.Or, |
| 97 | + new BinaryExpression(new UnaryExpression(VAR_Y), |
| 98 | + BinaryOp.And, |
| 99 | + VAR_Z)); |
| 100 | + |
| 101 | + Assert.AreEqual(false, binaryExp.Interpret(ctx)); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + [ExpectedException(typeof(InvalidOperationException))] |
| 106 | + public void TestVarNotFoundExc() |
| 107 | + { |
| 108 | + const string VAR_NAME = "x"; |
| 109 | + |
| 110 | + var ctx = new Context(); |
| 111 | + var unary = new UnaryExpression(VAR_NAME); |
| 112 | + unary.Interpret(ctx); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments