예제 #1
0
        public void Process_Include_Nested(string eol, string eof)
        {
            using var file0 = new TemporaryFile();
            using var file1 = new TemporaryFile();

            file0.Write(Lines(eol, eof,
                              "b",
                              $":r {file1.Path}",
                              "c"
                              ));

            file1.Write(Lines(eol, eof,
                              "included"
                              ));

            var preprocessor = new SqlCmdPreprocessor {
            };

            var batches = preprocessor.Process(
                Lines(eol, eof, "a", $":r {file0.Path}", "d")
                );

            batches.Should().Equal(
                Batch(
                    "a", eol,
                    "b", eol,
                    "included", eof,
                    "c", eof,
                    "d", eof
                    )
                );
        }
예제 #2
0
        public void Process_Include_BatchSeparatorAfterInclusionBoundary(string eol, string eof)
        {
            using var file = new TemporaryFile();

            file.Write(
                Lines(eol, eof,
                      "GO",
                      "included"
                      )
                );

            var preprocessor = new SqlCmdPreprocessor {
            };

            var batches = preprocessor.Process(
                Lines(eol, eof,
                      $":r {file.Path}",
                      "GO",
                      "main"
                      )
                );

            batches.Should().Equal(
                Batch("included", eof),
                Batch("main", eof)
                );
        }
예제 #3
0
        public void Process_SetVariable_Unquoted(string eol, string eof)
        {
            // NOTE: This test contains German, Japanese, and Russian characters.

            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["f0ö_Бар-baß"] = "original value" }
            };

            var batches = preprocessor.Process(
                Lines(eol, eof,
                      @"a",
                      @":setvar f0ö_Бар-baß qux~`!@#$%^&*()-_=+[{]}\|;:',<.>/?ほげ",
                      @"b $(F0ö_Бар-baß) c"
                      )
                );

            batches.Should().Equal(
                Batch(
                    @"a", eol,
                    @"b qux~`!@#$%^&*()-_=+[{]}\|;:',<.>/?ほげ c", eof
                    )
                );

            preprocessor.Variables["f0ö_Бар-baß"].Should().Be(
                @"qux~`!@#$%^&*()-_=+[{]}\|;:',<.>/?ほげ"
                );
        }
예제 #4
0
        public void Process_EmptyText()
        {
            var preprocessor = new SqlCmdPreprocessor();

            var batches = preprocessor.Process("");

            batches.Should().BeEmpty();
        }
예제 #5
0
        public void Process_BracketQuotedIdentifier_Unterminated(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            var text = Lines(eol, eof, "a [foo]]bar");

            var batches = preprocessor.Process(text);

            batches.Should().Equal(text);
        }
예제 #6
0
        public void Process_VariableReplacement_Undefined()
        {
            var preprocessor = new SqlCmdPreprocessor {
            };

            preprocessor
            .Invoking(p => p.Process("$(foo)").Count())
            .Should().Throw <SqlCmdException>()
            .WithMessage("SqlCmd variable 'foo' is not defined.");
        }
예제 #7
0
        public void Process_BlockComment_Unterminated(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            var text = Lines(eol, eof, "a /* foo");

            var batches = preprocessor.Process(text);

            batches.Should().Equal(text);
        }
예제 #8
0
        public void Process_LineComment(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            var text = Lines(eol, eof, "a", "b -- foo", "c");

            var batches = preprocessor.Process(text);

            batches.Should().Equal(text);
        }
예제 #9
0
        public void Process_TokenlessText()
        {
            var preprocessor = new SqlCmdPreprocessor();

            var text = "no tokens here";

            var batches = preprocessor.Process(text);

            batches.Should().Equal(text);
        }
예제 #10
0
        public void Process_SingleQuotedString_Unterminated(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            var text = Lines(eol, eof, "a 'foo''bar");

            var batches = preprocessor.Process(text);

            batches.Should().Equal(text);
        }
예제 #11
0
        public void Process_VariableReplacement_Unterminated()
        {
            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["foo"] = "bar" }
            };

            preprocessor
            .Invoking(p => p.Process("$(foo").Count())
            .Should().Throw <SqlCmdException>()
            .WithMessage("Unterminated reference to SqlCmd variable 'foo'.");
        }
예제 #12
0
        public void Process_BatchSeparator_EmptyBatchAtEnd(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            foreach (var separator in BatchSeparators)
            {
                var text = Lines(eol, eof, "a", separator);

                var batches = preprocessor.Process(text);

                batches.Should().Equal("a" + eol);
            }
        }
예제 #13
0
        public void Process_BatchSeparator_AllEmptyBatches(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor();

            foreach (var separator in BatchSeparators)
            {
                var text = Lines(eol, eof, separator, separator);

                var batches = preprocessor.Process(text);

                batches.Should().BeEmpty();
            }
        }
예제 #14
0
        public void Process_SetVariable_DoubleQuoted_Unterminated(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor {
            };

            var text = Lines(eol, eof, @":setvar foo ""bar");

            preprocessor
            .Invoking(p => p.Process(text).Count())
            .Should().Throw <SqlCmdException>()
            .WithMessage("Unterminated double-quoted string.");

            preprocessor.Variables.Should().NotContainKey("foo");
        }
예제 #15
0
        public void Process_SetVariable_Unquoted_Invalid(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor {
            };

            var text = Lines(eol, eof, ":setvar foo bar baz");

            preprocessor
            .Invoking(p => p.Process(text).Count())
            .Should().Throw <SqlCmdException>()
            .WithMessage("Invalid syntax in :setvar directive.");

            preprocessor.Variables.Should().NotContainKey("foo");
        }
예제 #16
0
        public void Process_VariableReplacement_Normal(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["foo"] = "bar" }
            };

            var batches = preprocessor.Process(
                Lines(eol, eof, "a $(FOO) b")
                );

            batches.Should().Equal(
                Lines(eol, eof, "a bar b")
                );
        }
예제 #17
0
        public void Process_VariableReplacement_InBracketQuotedIdentifier(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["foo"] = "bar" }
            };

            var batches = preprocessor.Process(
                Lines(eol, eof, "a [b ]] $(fOo) c] d")
                );

            batches.Should().Equal(
                Lines(eol, eof, "a [b ]] bar c] d")
                );
        }
예제 #18
0
        public void Process_VariableReplacement_InSingleQuotedString(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["foo"] = "bar" }
            };

            var batches = preprocessor.Process(
                Lines(eol, eof, "a 'b '' $(fOO) c' d")
                );

            batches.Should().Equal(
                Lines(eol, eof, "a 'b '' bar c' d")
                );
        }
예제 #19
0
        public void Process_SetVariable_Unset(string eol, string eof)
        {
            var preprocessor = new SqlCmdPreprocessor
            {
                Variables = { ["foo"] = "bar" }
            };

            var text = Lines(eol, eof, ":setvar foo", "$(foo)");

            preprocessor
            .Invoking(p => p.Process(text).Count())
            .Should().Throw <SqlCmdException>()
            .WithMessage("SqlCmd variable 'foo' is not defined.");

            preprocessor.Variables.Should().NotContainKey("foo");
        }
예제 #20
0
        public void Process_Include_CrossInclusionBatches(string eol, string eof)
        {
            using var file0 = new TemporaryFile();
            using var file1 = new TemporaryFile();

            file0.Write(Lines(eol, eof,
                              "file0.a",
                              "GO",
                              "file0.b"
                              ));

            file1.Write(Lines(eol, eof,
                              "file1.a",
                              "GO",
                              "file1.b"
                              ));

            var preprocessor = new SqlCmdPreprocessor {
            };

            var batches = preprocessor.Process(Lines(eol, eof,
                                                     "beg",
                                                     $":r {file0.Path}",
                                                     "mid",
                                                     $":r {file1.Path}",
                                                     "end"
                                                     ));

            batches.Should().Equal(
                // Batch begins in top level and ends in included
                Batch(
                    "beg", eol,
                    "file0.a", eol
                    ),
                // Batch begins in included, continues to top level, and ends in another included
                Batch(
                    "file0.b", eof,
                    "mid", eol,
                    "file1.a", eol
                    ),
                // Batch begins in included and ends in top level
                Batch(
                    "file1.b", eof,
                    "end", eof
                    )
                );
        }
예제 #21
0
    public static SqlCmdPreprocessor WithVariables(
        this SqlCmdPreprocessor preprocessor, IDictionary?entries)
    {
        if (preprocessor is null)
        {
            throw new ArgumentNullException(nameof(preprocessor));
        }

        if (entries == null)
        {
            return(preprocessor);
        }

        var variables = preprocessor.Variables;

        foreach (var obj in entries)
        {
            if (obj is not DictionaryEntry entry)
            {
                continue;
            }

            if (entry.Key is null)
            {
                continue;
            }

            var key = Convert.ToString(entry.Key, CultureInfo.InvariantCulture);

            if (string.IsNullOrEmpty(key))
            {
                continue;
            }

            var value = Convert.ToString(entry.Value, CultureInfo.InvariantCulture);

            variables[key] = value ?? string.Empty;
        }

        return(preprocessor);
    }
예제 #22
0
        public void Process_Include_BatchInsideInclude(string eol, string eof)
        {
            using var file = new TemporaryFile();

            file.Write(Lines(eol, eof,
                             "file.a",
                             "GO",
                             "file.b",
                             "GO",
                             "file.c"
                             ));

            var preprocessor = new SqlCmdPreprocessor {
            };

            var batches = preprocessor.Process(Lines(eol, eof,
                                                     "beg",
                                                     $":r {file.Path}",
                                                     "end"
                                                     ));

            batches.Should().Equal(
                // Batch begins in top level and ends in included
                Batch(
                    "beg", eol,
                    "file.a", eol
                    ),
                // Batch begins in included and ends in same included
                Batch(
                    "file.b", eol
                    ),
                // Batch begins in included and ends in top level
                Batch(
                    "file.c", eof,
                    "end", eof
                    )
                );
        }