public void TestFindElses()
        {
            {
                List <string> lines = new List <string>();
                try
                {
                    ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                    Assert.Fail();
                }
                catch { }
            }

            {
                List <string> lines = new List <string>();
                lines.Add("}");
                try
                {
                    ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                    Assert.Fail();
                }
                catch { }
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                try
                {
                    ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                    Assert.Fail();
                }
                catch { }
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("}");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 1 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("}");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 2 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 2 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("<>");
                lines.Add("    $ y = x + 3");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 2, 4 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("    $ a = y + 1");
                lines.Add("? x = 3");
                lines.Add("    $ y = x + 7");
                lines.Add("<>");
                lines.Add("    $ y = x + 3");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 3, 5, 7 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("    $ a = y + 1");
                lines.Add("? x = 3");
                lines.Add("    $ y = x + 7");
                lines.Add("<>");
                lines.Add("    $ y = x + 3");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 3, 5, 7 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("    O");
                lines.Add("        $ y = x + 7");
                lines.Add("        $ y = x + 3");
                lines.Add("    }");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 6 }, markers.ToArray());
            }

            {
                List <string> lines = new List <string>();
                lines.Add("? x = 1");
                lines.Add("    $ y = x + 1");
                lines.Add("    O");
                lines.Add("        ? x = 1");
                lines.Add("            $ y = x + 1");
                lines.Add("        <>");
                lines.Add("            $ y = x + 3");
                lines.Add("        }");
                lines.Add("    }");
                lines.Add("}");
                lines.Add("$ z = x + y");
                var markers = ScriptProcessor.FindElses(lines.ToArray(), 0, lines.Count - 1);
                ValidateIntLists(new[] { 9 }, markers.ToArray());
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.WriteLine("ERROR: Too many arguments, maybe put quotes around it?");
                return;
            }

            string specificTest = null;

            if (args.Length == 1)
            {
                specificTest = args[0];
            }

            string solutionDir    = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName;
            string testScriptsDir = Path.Combine(solutionDir, "scripttests");

            var testfilePaths =
                specificTest == null
                ? new List <string>(Directory.GetFiles(testScriptsDir, "*.*", SearchOption.AllDirectories))
                : new List <string>()
            {
                Path.Combine(testScriptsDir, specificTest + ".txt")
            };

            testfilePaths.Sort();

            int errorCount = 0;

#if !DEBUG
            try
#endif
            {
                foreach (string filePath in testfilePaths)
                {
                    string testName = Path.GetFileNameWithoutExtension(filePath);
                    Console.WriteLine("-- " + testName);

                    string fileText = File.ReadAllText(filePath);

                    int separatorIdx = fileText.IndexOf("===");
                    if (separatorIdx <= 0)
                    {
                        Console.WriteLine("ERROR: Test lacks == divider");
                        return;
                    }

                    string script   = fileText.Substring(0, separatorIdx).Trim();
                    string expected = fileText.Substring(separatorIdx + "===".Length).Trim().Replace("\r\n", "\n");

                    string output;
                    {
                        var symbols = new SymbolTable();
                        var stream  = new MemoryStream();
                        using (var proc = new ScriptProcessor(script, symbols, stream, "testScript", "execute", null, sm_scriptCtxtFunctions))
                            proc.ProcessAsync().Wait();
                        output = Encoding.UTF8.GetString(stream.ToArray()).Trim().Replace("\r\n", "\n");
                    }

                    if (expected != output)
                    {
                        ++errorCount;
                        Console.WriteLine("ERROR: Test '{0}' fails!", testName);
                        Console.WriteLine($" - Output:\n{output}");
                        Console.WriteLine($" - Expected:\n{expected}");
                        return;
                    }
                }
            }
#if !DEBUG
            catch (Exception exp)
            {
                if (exp is AggregateException)
                {
                    while (exp.InnerException != null)
                    {
                        exp = exp.InnerException;
                    }
                }

                if (exp is ScriptException)
                {
                    ScriptException scriptExp = (ScriptException)exp;
                    Console.WriteLine("SCRIPT ERROR:");
                    Console.WriteLine(scriptExp.Message);
                    Console.WriteLine(scriptExp.LineNumber + ": " + scriptExp.Line);
                }
                else
                {
                    Console.WriteLine("EXCEPTION:");
                    Console.WriteLine($"{exp.GetType().FullName}: {exp.Message}");
                }

                return;
            }
#endif
            Console.WriteLine("\nAll done.  Errors: {0}", errorCount);
        }
        public void TestFindMatchingEnd()
        {
            {
                List <string> lines = new List <string>();
                try
                {
                    ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                    Assert.Fail();
                }
                catch { }
            }

            {
                List <string> lines = new List <string>();
                lines.Add("}");
                try
                {
                    ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                    Assert.Fail();
                }
                catch { }
            }

            {
                List <string> lines = new List <string>();
                lines.Add("{");
                lines.Add("}");
                int endIndex = ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                Assert.AreEqual(1, endIndex);
            }

            {
                List <string> lines = new List <string>();
                lines.Add("O");
                lines.Add("    $ s = 1");
                lines.Add("}");
                int endIndex = ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                Assert.AreEqual(2, endIndex);
            }

            {
                List <string> lines = new List <string>();
                lines.Add("O");
                lines.Add("    $ s = 1");
                lines.Add("    $ q = 3");
                lines.Add("}");
                lines.Add("$ x = y + 5");
                int endIndex = ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                Assert.AreEqual(3, endIndex);
            }

            {
                List <string> lines = new List <string>();
                lines.Add("O");
                lines.Add("    O");
                lines.Add("        $ s = 1");
                lines.Add("        $ q = 3");
                lines.Add("    }");
                lines.Add("}");
                lines.Add("$ x = y + 5");
                int endIndex = ScriptProcessor.FindMatchingEnd(lines.ToArray(), 0, lines.Count - 1);
                Assert.AreEqual(5, endIndex);
            }
        }