public void TestLoadRuleWithLoop() { IProjectNode p = ProjectSerializer.LoadString("rule root {\r\n\tloop 1 2 {\r\n\t\tliteral a\r\n\t}\r\n}\r\n", null, null); Assert.AreEqual(1, p.Children.Count, "Didn't find the rule."); IProjectNode rule = p.Children[0]; Assert.AreEqual(ProjectNodeType.RuleDeclaration, rule.NodeType); RuleNode rn = rule as RuleNode; Assert.IsNotNull(rn); Assert.AreEqual(1, rn.Children.Count, "Didn't find the command block."); Assert.AreEqual("root", rn.Name); CommandBlockNode cbn = rn.Children[0] as CommandBlockNode; Assert.IsNotNull(cbn); Assert.AreEqual(1, cbn.Children.Count); Whee.WordBuilder.Model.Commands.CommandBase cb = cbn.Children[0] as Whee.WordBuilder.Model.Commands.CommandBase; Assert.IsInstanceOfType(typeof(Whee.WordBuilder.Model.Commands.LoopCommand), cb); Assert.AreEqual(1, cb.Children.Count); }
public override void LoadCommand(Whee.WordBuilder.ProjectV2.IProjectSerializer serializer) { double num = 0; bool found = true; Token amount = null; while (found) { amount = serializer.ReadNumericToken(this, ref num, out found); if (amount != null) { _Repetitions.Add((int)num); } if (amount == null && found) { int reps = 0; string data; amount = serializer.ReadRepeatingToken(this, out reps, out data); if (amount != null) { List <int> numbers = new List <int>(); foreach (string numstring in data.Split(' ')) { int number = 0; if (int.TryParse(numstring, out number)) { numbers.Add(number); } else { serializer.Warn("Expected numbers only", this); } } for (int i = 0; i < reps; i++) { _Repetitions.AddRange(numbers); } } else { found = false; } } } CommandBlockNode cbn = new CommandBlockNode(serializer); Children.Add(cbn); foreach (CommandBase cmd in cbn.Commands) { Commands.Add(cmd); } }
public void TestLoadCommandBlockCStyle() { Mockery mockery = new Mockery(); IProjectSerializer mockSerializer = mockery.NewMock <IProjectSerializer>(); using (mockery.Ordered) { Expect.Once.On(mockSerializer).GetProperty("Position").Will(Return.Value(0)); Expect.Once.On(mockSerializer).Method("ReadLineBreakToken").Will(Return.Value(new Token(TokenType.LineBreak, "\r\n", 0, 2))); Expect.Once.On(mockSerializer).Method("ReadIndentationToken").Will(Return.Value(new Token(TokenType.Indentation, "\t", 2, 1))); Expect.Once.On(mockSerializer).Method("GetIndentationLevel").Will(Return.Value(1)); Expect.Once.On(mockSerializer).Method("ReadBlockStarterToken").Will(Return.Value(new Token(TokenType.BlockStarter, "{", 3, 1))); Expect.Once.On(mockSerializer).Method("ReadLineBreakToken").Will(Return.Value(new Token(TokenType.LineBreak, "\r\n", 4, 2))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadIndentationToken").Will(Return.Value(new Token(TokenType.Indentation, "\t", 6, 1))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(new Token(TokenType.Text, "token", 7, 5))); Expect.Once.On(mockSerializer).GetProperty("LineNumber").Will(Return.Value(3)); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(new Token(TokenType.Text, "consonants", 12, 10))); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).Method("ReadLineBreakToken").Will(Return.Value(new Token(TokenType.LineBreak, "\r\n", 22, 2))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadIndentationToken").Will(Return.Value(new Token(TokenType.Indentation, "\t", 24, 1))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(new Token(TokenType.Command, "rule", 25, 4))); Expect.Once.On(mockSerializer).GetProperty("LineNumber").Will(Return.Value(4)); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(new Token(TokenType.Text, "second", 29, 6))); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).Method("ReadLineBreakToken").Will(Return.Value(new Token(TokenType.LineBreak, "\r\n", 35, 2))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadIndentationToken").Will(Return.Value(new Token(TokenType.Indentation, "\t", 37, 1))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).GetProperty("Done").Will(Return.Value(false)); Expect.Once.On(mockSerializer).Method("ReadTextToken").Will(Return.Value(null)); Expect.Once.On(mockSerializer).Method("ReadLineBreakToken").Will(Return.Value(new Token(TokenType.LineBreak, "\r\n", 38, 2))); Expect.Once.On(mockSerializer).Method("ReadBlockEnderToken").Will(Return.Value(new Token(TokenType.BlockEnder, "}", 40, 1))); } CommandBlockNode cbn = new CommandBlockNode(mockSerializer); Assert.AreEqual(2, cbn.Commands.Count); mockery.VerifyAllExpectationsHaveBeenMet(); }
public void TestLoadRule() { IProjectNode p = ProjectSerializer.LoadString("rule root {\r\n\trule blah\r\n\tliteral a\r\n}\r\n", null, null); Assert.AreEqual(1, p.Children.Count, "Didn't find the rule."); IProjectNode rule = p.Children[0]; Assert.AreEqual(ProjectNodeType.RuleDeclaration, rule.NodeType); RuleNode rn = rule as RuleNode; Assert.IsNotNull(rn); Assert.AreEqual(1, rn.Children.Count, "Didn't find the command block."); Assert.AreEqual("root", rn.Name); CommandBlockNode cbn = rn.Children[0] as CommandBlockNode; Assert.IsNotNull(cbn); Assert.AreEqual(2, cbn.Commands.Count); }