Files
Matteo Taveggia ffed8313d9 Exposing a DisableVariableSubstitution on the Parser object (#1941)
* Cleaning up a few tests; fixing one

* Exposing DisableVariableSubstitution in Parser; fixing NRE; adding tests

* Added another test

* Addressing CR feedback; added tests
2023-03-13 22:04:04 -07:00

94 lines
3.9 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using System;
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.SqlTools.ServiceLayer.BatchParser;
namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
{
public class TestCommandHandler : ICommandHandler
{
private Parser parser;
private StringBuilder outputString;
public TestCommandHandler(StringBuilder outputString)
{
this.outputString = outputString;
}
public void SetParser(Parser parser)
{
this.parser = parser;
}
public BatchParserAction Go(TextBlock batch, int repeatCount, SqlCmdCommand command)
{
string textWithVariablesResolved;
string textWithVariablesUnresolved;
LineInfo lineInfoVarsResolved;
LineInfo lineInfoVarsUnresolved;
batch.GetText(true, out textWithVariablesResolved, out lineInfoVarsResolved);
batch.GetText(false, out textWithVariablesUnresolved, out lineInfoVarsUnresolved);
outputString.AppendFormat(CultureInfo.InvariantCulture, "*** Execute batch ({0})\n", repeatCount);
if (string.Compare(textWithVariablesUnresolved, textWithVariablesResolved, StringComparison.Ordinal) != 0)
{
outputString.AppendLine("Text with variables resolved:");
outputString.AppendLine(textWithVariablesResolved);
outputString.AppendLine("Text with variables not resolved:");
outputString.AppendLine(textWithVariablesUnresolved);
int i = 0;
outputString.AppendLine("Mapping from resolved string to unresolved:");
while (i <= textWithVariablesResolved.Length)
{
PositionStruct pos = lineInfoVarsResolved.GetStreamPositionForOffset(i);
string character = i < textWithVariablesResolved.Length ? ("" + textWithVariablesResolved[i]).Replace("\n", @"\n").Replace("\r", @"\r") : "EOF";
outputString.AppendFormat(CultureInfo.InvariantCulture, "Pos [{0}] {1}:{2} \"{3}\"",
i,
BatchParserTests.GetFilenameOnly(pos.Filename),
pos.Offset,
character);
outputString.AppendLine();
i++;
}
}
else
{
outputString.AppendLine("Batch text:");
outputString.AppendLine(textWithVariablesUnresolved);
}
outputString.AppendLine();
return BatchParserAction.Continue;
}
public BatchParserAction OnError(Token token, OnErrorAction action)
{
outputString.AppendFormat(CultureInfo.InvariantCulture, "*** PARSER: On error: {0}", action.ToString());
outputString.AppendLine();
return BatchParserAction.Continue;
}
public BatchParserAction Include(TextBlock filename, out TextReader stream, out string newFilename)
{
string resolvedFilename;
LineInfo lineInfo;
filename.GetText(true, out resolvedFilename, out lineInfo);
outputString.AppendFormat(CultureInfo.InvariantCulture, "*** PARSER: Include file \"{0}\"\n", resolvedFilename);
outputString.AppendLine();
string currentFilename = lineInfo.GetStreamPositionForOffset(0).Filename;
string currentFilePath = Path.Combine(Path.GetDirectoryName(currentFilename), resolvedFilename);
stream = new StreamReader(File.Open(currentFilePath, FileMode.Open));
newFilename = resolvedFilename;
return BatchParserAction.Continue;
}
}
}