public void TestStatementParser_InvalidNumber() { const string line = "glob glob Silver is 3a Credits"; Context context = new Context(); context.RomanMapping["glob"] = RomanPrimitive.Parse('I'); new StatementParser().Parse(line, context); }
public void TestRoman_NotDefined() { const string line = "pish tegj glob glob"; Dictionary <string, RomanPrimitive> romanMapping = new Dictionary <string, RomanPrimitive> { { "pish", RomanPrimitive.Parse('X') }, { "tegj", RomanPrimitive.Parse('L') } }; Roman.Parse(line, romanMapping); }
public void TestRomanPrimitive() { RomanPrimitive primitive = RomanPrimitive.Parse('I'); Assert.IsNotNull(primitive); Assert.AreEqual("I", primitive.Symbol); Assert.AreEqual(1, primitive.Value); Assert.IsTrue(primitive.AllowRepeat); Assert.IsTrue(primitive.AllowSubtract); Assert.AreEqual("VX", primitive.SubtractFrom); }
public void TestRoman() { const string line = "pish tegj glob glob"; Dictionary <string, RomanPrimitive> romanMapping = new Dictionary <string, RomanPrimitive> { { "glob", RomanPrimitive.Parse('I') }, { "pish", RomanPrimitive.Parse('X') }, { "tegj", RomanPrimitive.Parse('L') } }; Assert.AreEqual(42, Roman.Parse(line, romanMapping)); }
public void TestStatementSolver_ItemNotFound() { const string question = "how many Credits is glob prok Silver "; string answer; Context context = new Context { RomanMapping = new Dictionary <string, RomanPrimitive> { { "glob", RomanPrimitive.Parse('I') }, { "prok", RomanPrimitive.Parse('V') } } }; new StatementSolver().Solve(question, context, out answer); }
public void TestStatementParser() { const string line = "glob glob Silver is 34 Credits"; Context context = new Context(); context.RomanMapping["glob"] = RomanPrimitive.Parse('I'); Assert.IsFalse(new DefinitionParser().Parse(line, context)); Assert.IsFalse(new QuestionParser().Parse(line, context)); Assert.IsTrue(new StatementParser().Parse(line, context)); Assert.AreEqual(1, context.ItemPrice.Count); Assert.AreEqual(17m, context.ItemPrice["Silver"]); }
public void TestDefinitionSolver() { const string question = "how much is pish tegj glob glob "; string answer; Context context = new Context { RomanMapping = new Dictionary <string, RomanPrimitive> { { "glob", RomanPrimitive.Parse('I') }, { "pish", RomanPrimitive.Parse('X') }, { "tegj", RomanPrimitive.Parse('L') } } }; Assert.IsFalse(new StatementSolver().Solve(question, context, out answer)); Assert.IsNull(answer); const string expected = "pish tegj glob glob is 42"; Assert.IsTrue(new DefinitionSolver().Solve(question, context, out answer)); Assert.AreEqual(expected, answer); }
public override bool Parse(string input) { string[] lexers = input.Split(new[] { " is " }, StringSplitOptions.RemoveEmptyEntries); if (lexers.Count() != 2) { return(false); } if (lexers[1].Length > 1) { return(false); } RomanPrimitive roman = RomanPrimitive.Parse(lexers[1][0]); if (roman == null) { throw new Exception("syntex error."); } string name = lexers[0].Trim(); Context.Primitives[name] = roman; return(true); }
public void TestStatementSolver() { const string question = "how many Credits is glob prok Silver "; string answer; Context context = new Context { RomanMapping = new Dictionary <string, RomanPrimitive> { { "glob", RomanPrimitive.Parse('I') }, { "prok", RomanPrimitive.Parse('V') } } }; context.ItemPrice["Silver"] = 17m; Assert.IsFalse(new DefinitionSolver().Solve(question, context, out answer)); Assert.IsNull(answer); const string expected = "glob prok Silver is 68 Credits"; Assert.IsTrue(new StatementSolver().Solve(question, context, out answer)); Assert.AreEqual(expected, answer); }
// Format: [Name] is [Primitive] public bool Parse(string line, Context context) { if (line.EndsWith(Constants.QuestionMark)) { return(false); } string[] fields = line.Split(new[] { Constants.ParserDelimiter }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length != 2) { return(false); } string right = fields[1].Trim(); if (right.Length != 1) { return(false); } string[] left = fields[0].Split(new[] { Constants.Space }, StringSplitOptions.RemoveEmptyEntries); if (left.Length != 1) { return(false); } RomanPrimitive primitive = RomanPrimitive.Parse(right[0]); if (primitive == null) { throw new ArgumentException(Constants.InvalidRomanPrimitive, right); } context.RomanMapping[left[0]] = primitive; return(true); }
public void TestRomanPrimitive_NotFound() { RomanPrimitive primitive = RomanPrimitive.Parse('A'); Assert.IsNull(primitive); }