public void TestParser(string filename, StringBuilder output)
        {
            try
            {
                // Create a new file by changing CRLFs to LFs and generate a new steam
                // or the tokens generated by the lexer will always have off by one errors
                TestCommandHandler commandHandler = new TestCommandHandler(output);
                string             input          = File.ReadAllText(filename).Replace("\r\n", "\n");
                var          inputStream          = GenerateStreamFromString(input);
                StreamReader streamReader         = new StreamReader(inputStream);

                using (Parser parser = new Parser(
                           commandHandler,
                           new TestVariableResolver(output),
                           streamReader,
                           filename))
                {
                    commandHandler.SetParser(parser);
                    parser.Parse();
                }
            }
            catch (BatchParserException ex)
            {
                output.AppendLine(string.Format(CultureInfo.CurrentCulture, "[PARSER ERROR: code {0} at {1} - {2} in {3}, token text: {4}, message: {5}]", ex.ErrorCode, GetPositionString(ex.Begin), GetPositionString(ex.End), GetFilenameOnly(ex.Begin.Filename), ex.Text, ex.Message));
            }
        }
Ejemplo n.º 2
0
        public void VerifyVariableResolverUsingVaribleParameter()
        {
            string query = @" Invoke-Sqlcmd -Query ""SELECT `$(calcOne)"" -Variable ""calcOne = 10 + 20"" ";

            TestCommandHandler handler  = new TestCommandHandler(new StringBuilder());
            IVariableResolver  resolver = new TestVariableResolver(new StringBuilder());

            using (Parser p = new Parser(
                       handler,
                       resolver,
                       new StringReader(query),
                       "test"))
            {
                p.ThrowOnUnresolvedVariable = true;
                handler.SetParser(p);
                Assert.Throws <BatchParserException>(() => p.Parse());
            }
        }
Ejemplo n.º 3
0
        public void VerifyVariableResolverIsStartIdentifierChar()
        {
            // instead of using variable calcOne, I purposely used In-variable 0alcOne
            string query = @" Invoke-Sqlcmd -Query ""SELECT `$(0alcOne)"" -Variable ""calcOne1 = 1"" ";

            TestCommandHandler handler  = new TestCommandHandler(new StringBuilder());
            IVariableResolver  resolver = new TestVariableResolver(new StringBuilder());

            using (Parser p = new Parser(
                       handler,
                       resolver,
                       new StringReader(query),
                       "test"))
            {
                p.ThrowOnUnresolvedVariable = true;
                handler.SetParser(p);
                Assert.Throws <BatchParserException>(() => p.Parse());
            }
        }
        public void VerifyThrowOnUnresolvedVariable()
        {
            string        script = "print '$(NotDefined)'";
            StringBuilder output = new StringBuilder();

            TestCommandHandler handler  = new TestCommandHandler(output);
            IVariableResolver  resolver = new TestVariableResolver(new StringBuilder());

            using (Parser p = new Parser(
                       handler,
                       resolver,
                       new StringReader(script),
                       "test"))
            {
                p.ThrowOnUnresolvedVariable = true;
                handler.SetParser(p);

                Assert.Throws <BatchParserException>(() => p.Parse());
            }
        }
Ejemplo n.º 5
0
        public void VerifySqlCmdExecute()
        {
            string             query       = ":!!if exist foo.txt del foo.txt";
            var                inputStream = GenerateStreamFromString(query);
            TestCommandHandler handler     = new TestCommandHandler(new StringBuilder());
            IVariableResolver  resolver    = new TestVariableResolver(new StringBuilder());

            using (Parser p = new Parser(
                       handler,
                       resolver,
                       new StringReader(query),
                       "test"))
            {
                p.ThrowOnUnresolvedVariable = true;
                handler.SetParser(p);

                var exception = Assert.Throws <BatchParserException>(() => p.Parse());
                // Verify the message should be "Command Execute is not supported."
                Assert.Equal("Command Execute is not supported.", exception.Message);
            }
        }
Ejemplo n.º 6
0
        public void VerifyInvalidNumber()
        {
            string query = @" SELECT 1+1
                           GO 999999999999999999999999999999999999999";

            TestCommandHandler handler  = new TestCommandHandler(new StringBuilder());
            IVariableResolver  resolver = new TestVariableResolver(new StringBuilder());

            using (Parser p = new Parser(
                       handler,
                       resolver,
                       new StringReader(query),
                       "test"))
            {
                p.ThrowOnUnresolvedVariable = true;
                handler.SetParser(p);
                // This test will fail because we are passing invalid number.
                // Exception will be raised from  ParseGo()
                Assert.Throws <BatchParserException>(() => p.Parse());
            }
        }