public void RuleVisitorIgnoresRule() { // arrange var mockReporter = Substitute.For <IReporter>(); mockReporter.ReportViolation(Arg.Any <IRuleViolation>()); var mockConfigReader = Substitute.For <IConfigReader>(); mockConfigReader.GetRuleSeverity("select-star").Returns(RuleViolationSeverity.Error); var ignoredRuleList = new List <IRuleException> { new RuleException(typeof(SelectStarRule), 1, 10) }; var pathString = "DoesntExist.sql"; var RuleVisitorBuilder = new RuleVisitorBuilder(mockConfigReader, mockReporter); var ActiveRuleVisitors = RuleVisitorBuilder.BuildVisitors(pathString, ignoredRuleList); var testFileStream = ParsingUtility.GenerateStreamFromString("SELECT * FROM FOO;"); var textReader = new StreamReader(testFileStream); var sqlFragment = _fragmentBuilder.GetFragment(textReader, out _); // act foreach (var sqlFragmentVisitor in ActiveRuleVisitors) { sqlFragment.Accept(sqlFragmentVisitor); testFileStream.Seek(0, SeekOrigin.Begin); } // assert Assert.AreEqual(1, ActiveRuleVisitors.Count); Assert.IsTrue(ActiveRuleVisitors[0].GetType().Name == typeof(SelectStarRule).Name); mockReporter.DidNotReceive().ReportViolation(Arg.Any <IRuleViolation>()); }
public override void Load(XElement element, KeyMapping keyMapping) { base.Load(element, keyMapping); var dataElements = element .Elements(XName.Get("data", element.GetDefaultNamespace().NamespaceName)) .ToArray(); var initialElem = dataElements .Where(x => (string)x.Attribute("key") == keyMapping.InitialStateId) .FirstOrDefault(); var finalElem = dataElements .Where(x => (string)x.Attribute("key") == keyMapping.FinalStateId) .FirstOrDefault(); IsInitial = ParsingUtility.ElementContentToBoolean(initialElem, keyMapping.InitialStateDefaultValue); IsFinal = ParsingUtility.ElementContentToBoolean(finalElem, keyMapping.FinalStateDefaultValue); if (IsFinal) { if (Description == null) { Description = "[FINAL]"; } } CheckIdProperty(element); CheckDescriptionProperty(element); }
public static void RunDynamicSQLRulesTest(Type ruleType, string sql, List <RuleViolation> expectedRuleViolations) { // arrange var ruleViolations = new List <RuleViolation>(); var mockReporter = Substitute.For <IReporter>(); var mockPath = string.Empty; var compareer = new RuleViolationComparer(); var fragmentBuilder = new FragmentBuilder(); var sqlStream = ParsingUtility.GenerateStreamFromString(sql); void ErrorCallback(string ruleName, string ruleText, int startLine, int startColumn) { ruleViolations.Add(new RuleViolation(ruleName, startLine, startColumn)); } var visitors = new List <TSqlFragmentVisitor> { GetVisitor(ruleType, ErrorCallback) }; var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>(); mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <IEnumerable <IRuleException> >()).Returns(visitors); var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter); // act sqlRuleVisitor.VisitRules(mockPath, new List <IRuleException>(), sqlStream); ruleViolations = ruleViolations.OrderBy(o => o.Line).ThenBy(o => o.Column).ToList(); expectedRuleViolations = expectedRuleViolations.OrderBy(o => o.Line).ThenBy(o => o.Column).ToList(); // assert CollectionAssert.AreEqual(expectedRuleViolations, ruleViolations, compareer); }
public void VisitRules_InvalidSql_ShouldStillLint() { // arrange var mockReporter = Substitute.For <IReporter>(); var fragmentBuilder = new FragmentBuilder(120); var ruleViolations = new List <RuleViolation>(); void ErrorCallback(string ruleName, string ruleText, int startLine, int startColumn) { ruleViolations.Add(new RuleViolation(ruleName, startLine, startColumn)); } var visitors = new List <TSqlFragmentVisitor> { new KeywordCapitalizationRule(ErrorCallback) }; var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>(); mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors); var sqlStream = ParsingUtility.GenerateStreamFromString("select"); var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter); // act sqlRuleVisitor.VisitRules(Path, new List <IRuleException>(), sqlStream); // assert Assert.AreEqual(1, ruleViolations.Count); }
public void VisitRules_GlobalIgnoreNoFragments_ShouldNotReportErrors() { // arrange var sqlStream = ParsingUtility.GenerateStreamFromString(@"/* tsqllint-disable */ @@Scripts\dfu_setutp_import_cleanup.sql"); var mockReporter = Substitute.For <IReporter>(); var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>(); var visitors = new List <TSqlFragmentVisitor> { new SetAnsiNullsRule(null) }; mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors); var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, new FragmentBuilder(120), mockReporter); // act sqlRuleVisitor.VisitRules(Path, new List <IRuleException> { new GlobalRuleException(0, 99) }, sqlStream); // assert mockReporter.DidNotReceive().ReportViolation(Arg.Any <IRuleViolation>()); }
public void ProcessPath_SingleFile_ShouldProcessFile() { // arrange const string filePath = "c:\\dbscripts\\myfile.sql"; var ruleVisitor = Substitute.For <IRuleVisitor>(); var reporter = Substitute.For <IReporter>(); var fileSystem = Substitute.For <IFileSystem>(); var fileBase = Substitute.For <FileBase>(); var pluginHandler = Substitute.For <IPluginHandler>(); var processor = new SqlFileProcessor(ruleVisitor, pluginHandler, reporter, fileSystem); fileBase.Exists(filePath).Returns(true); fileBase.OpenRead(filePath).Returns(ParsingUtility.GenerateStreamFromString("Some Sql To Parse")); fileSystem.File.Returns(fileBase); // act processor.ProcessPath("\" " + filePath + " \""); // Also testing removal of quotes and leading/trailing spaces // assert fileBase.Received().Exists(filePath); fileBase.Received().OpenRead(filePath); ruleVisitor.Received().VisitRules(filePath, Arg.Any <Stream>()); Assert.AreEqual(1, processor.FileCount); }
public static new ApprovalBallot Parse(int candidateCount, string source) { var approvalsByCanidate = new bool[candidateCount]; foreach (var c in source) { approvalsByCanidate[ParsingUtility.DecodeCandidateIndex(c)] = true; } return(new ApprovalBallot(approvalsByCanidate)); }
public static TValue?GetNullableValueAs <TValue>(this NameValueCollection collection, string key) where TValue : struct { var resultStr = collection.GetValue(key, null); if (resultStr == null) { return(default(TValue?)); } return(ParsingUtility.Parse <TValue>(resultStr)); }
public async Task <object> Lint(string fileContents) { var sqlStream = ParsingUtility.GenerateStreamFromString("SELECT id FROM tbl_1"); return(new { errors = new string[] { "File length " + fileContents.Length } }); }
public void GetDefaultConfigRules_ShouldReturnJsonConfigWithRules() { // arrange var configFileGenerator = new ConfigFileGenerator(); // act var configString = configFileGenerator.GetDefaultConfigRules(); ParsingUtility.TryParseJson(configString, out var configJson); // assert Assert.IsNotNull(configJson["rules"]); }
public void CreateReader_WithoutPlaceholders_DoesNotChangeSql() { // arrange var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>(); var inputSql = "select 1;"; // act var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper) .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql)) .ReadToEnd(); // assert Assert.AreEqual(inputSql, outputSql); }
public void TestOverride( string message, string testString, List <IExtendedRuleException> expectedruleExceptionResult, List <IOverride> expectedOverrideResult) { var overrideResult = OverrideFinder.GetOverrideList(ParsingUtility.GenerateStreamFromString(testString)); CollectionAssert.AreEqual(overrideResult, expectedOverrideResult, OverrideComparer, message); var ruleExceptionResult = RuleExceptionFinder.GetIgnoredRuleList(ParsingUtility.GenerateStreamFromString(testString)); CollectionAssert.AreEqual(expectedruleExceptionResult, ruleExceptionResult, RuleExceptionComparer, message); }
public void Test(string description, string sql, int expectedOverrideCount) { var fragmentBuilder = new FragmentBuilder(Constants.DefaultCompatabilityLevel); var overrideFider = new OverrideFinder(); var sqlStream = ParsingUtility.GenerateStreamFromString(sql); var overrides = overrideFider.GetOverrideList(sqlStream); var fragment = fragmentBuilder.GetFragment(new StreamReader(sqlStream), out var errors, overrides); Assert.AreEqual(expectedOverrideCount, overrides.Count()); Assert.IsEmpty(errors); Assert.IsNotNull(fragment); }
private void LoadConfigFromJson(string jsonConfigString) { if (ParsingUtility.TryParseJson(jsonConfigString, out var token)) { SetupRules(token); SetupPlugins(token); SetupParser(token); IsConfigLoaded = true; } else { reporter.Report("Config file is not valid Json."); } }
public void VisitRules_InvalidSql_ShouldReportError() { // arrange var mockReporter = Substitute.For <IReporter>(); var fragmentBuilder = new FragmentBuilder(); var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>(); var sqlStream = ParsingUtility.GenerateStreamFromString("SELECT"); var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, fragmentBuilder, mockReporter); // act sqlRuleVisitor.VisitRules(Path, sqlStream); // assert mockReporter.Received().ReportViolation(Arg.Is <RuleViolation>(x => x.FileName == Path && x.Text == "TSQL not syntactically correct")); }
public void CreateReader_WithPlaceholdersButNoEnvironmentVariables_DoesNotChangeSql() { // arrange var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>(); mockEnvironmentWrapper.GetEnvironmentVariable("bar").ReturnsNull(); var inputSql = "select 1 where foo = $(bar);"; // act var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper) .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql)) .ReadToEnd(); // assert Assert.AreEqual(inputSql, outputSql); }
public static BucketBallot <T> Parse(int candidateCount, string source) { var bucketsByCandidate = new T[candidateCount]; foreach (var bucket in source.Split(' ')) { var splits = bucket.Split(':'); var bucketValue = Enum.Parse <T>(splits[0]); foreach (var c in splits[1]) { bucketsByCandidate[ParsingUtility.DecodeCandidateIndex(c)] = bucketValue; } } return(new BucketBallot <T>(bucketsByCandidate)); }
public void ActivatePlugins_PluginRuleViolations_ShouldCallReporter() { // arrange var filePath1 = TestHelper.GetTestFilePath(@"c:\pluginDirectory\plugin_one.dll"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filePath1, new MockFileData(string.Empty) } }); var assemblyWrapper = new TestAssemblyWrapper(); var pluginPaths = new Dictionary <string, string> { { "my-plugin", filePath1 } }; var reporter = Substitute.For <IReporter>(); var textReader = ParsingUtility.CreateTextReaderFromString("\tSELECT * FROM FOO"); var scriptPath = TestHelper.GetTestFilePath(@"c:\scripts\foo.sql"); var context = new PluginContext(scriptPath, new List <IRuleException>(), textReader); var versionWrapper = Substitute.For <IFileversionWrapper>(); versionWrapper.GetVersion(Arg.Any <Assembly>()).Returns("1.2.3"); // act var pluginHandler = new Infrastructure.Plugins.PluginHandler(reporter, fileSystem, assemblyWrapper, versionWrapper); pluginHandler.ProcessPaths(pluginPaths); // assert Assert.AreEqual(1, pluginHandler.Plugins.Count); Assert.DoesNotThrow(() => pluginHandler.ActivatePlugins(context)); reporter.Received().ReportViolation(Arg.Is <IRuleViolation>(x => x.FileName == context.FilePath && x.RuleName == "prefer-tabs" && x.Text == "Should use spaces rather than tabs" && x.Line == 1 && x.Column == 0 && x.Severity == RuleViolationSeverity.Warning)); }
public void CreateReader_WithPlaceholdersAndSomeEnvironmentVariables_ChangesSql() { // arrange var mockEnvironmentWrapper = Substitute.For <IEnvironmentWrapper>(); mockEnvironmentWrapper.GetEnvironmentVariable("foo").Returns("foo"); mockEnvironmentWrapper.GetEnvironmentVariable("bar").ReturnsNull(); var inputSql = "select 1 where foo = '$(foo)' and bar = '$(bar)';"; // act var outputSql = new SqlStreamReaderBuilder(mockEnvironmentWrapper) .CreateReader(ParsingUtility.GenerateStreamFromString(inputSql)) .ReadToEnd(); // assert Assert.AreEqual("select 1 where foo = 'foo' and bar = '$(bar)';", outputSql); }
public void ShouldIgnore(string description, string testString) { var stream = ParsingUtility.GenerateStreamFromString(testString); void DynamicCallback(string dynamicSQL, int dynamicStartLine, int dynamicStartColumn) { Assert.Fail("should not perform callback"); } var visitor = new DynamicSQLParser(DynamicCallback); var fragmentBuilder = new FragmentBuilder(); var textReader = new StreamReader(stream); var sqlFragment = fragmentBuilder.GetFragment(textReader, out var errors); sqlFragment.Accept(visitor); CollectionAssert.IsEmpty(errors, "parsing errors were generated"); }
public void RuleVisitorEnforcesOneRuleIgnoresAnother() { // arrange var mockReporter = Substitute.For <IReporter>(); mockReporter.ReportViolation(Arg.Any <IRuleViolation>()); var mockConfigReader = Substitute.For <IConfigReader>(); mockConfigReader.GetRuleSeverity("select-star").Returns(RuleViolationSeverity.Error); mockConfigReader.GetRuleSeverity("semicolon-termination").Returns(RuleViolationSeverity.Error); var ignoredRuleList = new List <IRuleException> { new RuleException(typeof(SelectStarRule), 1, 10) }; var pathString = "DoesntExist.sql"; var RuleVisitorBuilder = new RuleVisitorBuilder(mockConfigReader, mockReporter); var ActiveRuleVisitors = RuleVisitorBuilder.BuildVisitors(pathString, ignoredRuleList); var testFileStream = ParsingUtility.GenerateStreamFromString("SELECT * FROM FOO"); var textReader = new StreamReader(testFileStream); var sqlFragment = _fragmentBuilder.GetFragment(textReader, out _); // act foreach (var sqlFragmentVisitor in ActiveRuleVisitors) { sqlFragment.Accept(sqlFragmentVisitor); testFileStream.Seek(0, SeekOrigin.Begin); } // assert Assert.AreEqual(2, ActiveRuleVisitors.Count); mockReporter.Received().ReportViolation(Arg.Is <IRuleViolation>(x => x.FileName == pathString && x.RuleName == "semicolon-termination" && x.Line == 1 && x.Column == 18 && x.Severity == RuleViolationSeverity.Error)); }
public static ScoreBallot Parse(int candidateCount, string source) { var scoresByCandidate = new int[candidateCount]; foreach (var bucket in source.Split(' ')) { var splits = bucket.Split(':'); if (splits.Length != 2) { throw new InvalidOperationException($"Expected exactly one `:`, got '{bucket}' instead."); } var bucketValue = int.Parse(splits[0]); foreach (var c in splits[1]) { scoresByCandidate[ParsingUtility.DecodeCandidateIndex(c)] = bucketValue; } } return(new ScoreBallot(scoresByCandidate)); }
public void VisitRules_ValidSql_ShouldVisitRules() { // arrange var sqlStream = ParsingUtility.GenerateStreamFromString("SELECT 1"); var mockReporter = Substitute.For <IReporter>(); var mockFragment = Substitute.For <TSqlFragment>(); var mockFragmentBuilder = Substitute.For <IFragmentBuilder>(); IList <ParseError> mockErrors = new List <ParseError>(); mockFragmentBuilder.GetFragment(Arg.Any <TextReader>(), out var errors).Returns(x => { x[1] = mockErrors; return(mockFragment); }); var mockRuleExceptionFinder = Substitute.For <IRuleExceptionFinder>(); mockRuleExceptionFinder.GetIgnoredRuleList(Arg.Any <Stream>()).Returns(new List <IRuleException>()); var visitors = new List <TSqlFragmentVisitor> { new SemicolonTerminationRule(null) }; var mockRuleVisitorBuilder = Substitute.For <IRuleVisitorBuilder>(); mockRuleVisitorBuilder.BuildVisitors(Arg.Any <string>(), Arg.Any <List <IRuleException> >()).Returns(visitors); var sqlRuleVisitor = new SqlRuleVisitor(mockRuleVisitorBuilder, mockRuleExceptionFinder, mockFragmentBuilder, mockReporter); // act sqlRuleVisitor.VisitRules(Path, sqlStream); // assert mockFragment.Received().Accept(Arg.Any <TSqlFragmentVisitor>()); }
// ======================================================================== /// Parse project name. /// /// @param projectName The full name of the project. /// @return An array of the project name constituents. /// static string[] SplitPackageName(string projectName) { // -- Convert file path to namespace format. -- projectName = projectName.Replace('/', '.'); // -- Remove all illegal characters. -- var cleanName = new StringBuilder(64); var len = projectName.Length; for (int i = 0; i < len; ++i) { char c = projectName[i]; if (cleanName.Length == 0) { if (ParsingUtility.IsFirstIdent(c)) { cleanName.Append(c); } } else if (Char.IsWhiteSpace(c) || c == '.' || ParsingUtility.IsIdent(c)) { cleanName.Append(c); } else { cleanName.Append(' '); } } // -- Split the name into its parts. -- var splitName = cleanName.ToString().Split(new Char[] { '.' }); // -- Convert each part to a type format. -- for (int i = 0; i < splitName.Length; ++i) { splitName[i] = NameUtility.ToTypeName(splitName[i]); } return(splitName); }
public void ShouldParse(string description, string executeStatement, string innerSql, bool expectCallback = true) { var stream = ParsingUtility.GenerateStreamFromString(executeStatement); var receivedCallback = false; void DynamicCallback(string dynamicSQL, int dynamicStartLine, int dynamicStartColumn) { Assert.AreEqual(innerSql, dynamicSQL); receivedCallback = true; } var visitor = new DynamicSQLParser(DynamicCallback); var fragmentBuilder = new FragmentBuilder(); var textReader = new StreamReader(stream); var sqlFragment = fragmentBuilder.GetFragment(textReader, out var errors); CollectionAssert.IsEmpty(errors, "parsing errors were generated"); sqlFragment.Accept(visitor); Assert.AreEqual(receivedCallback, expectCallback); }
.Select(gp => gp.Key.ToString() + ":" + ParsingUtility.EncodeCandidates(gp.Select(a => a.Candidate))) .Join(" ");
public override string ToString() => ParsingUtility.EncodeCandidates(CandidateScores.Select(a => a.Candidate));
public void ValidJson(string testString) { Assert.IsTrue(ParsingUtility.TryParseJson(testString, out var token)); Assert.IsNotNull(token); }
public void InvalidJson(string testString) { Assert.IsFalse(ParsingUtility.TryParseJson(testString, out var token)); Assert.IsNull(token); }
public void TestOverride(string message, string testString, List <IOverride> expectedResult) { var overrides = testOverrideFinder.GetOverrideList(ParsingUtility.GenerateStreamFromString(testString)); CollectionAssert.AreEqual(overrides, expectedResult, overrideComparer, message); }