From b80279791ee4ddf779e2ab7d7024ee9755b06764 Mon Sep 17 00:00:00 2001 From: Wolfgang Ziegler Date: Tue, 4 Oct 2022 10:36:29 +0200 Subject: [PATCH] Use json-specs/sql_token_examples.json test input --- test/Elastic.Apm.Tests/DbSpanNameTests.cs | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/Elastic.Apm.Tests/DbSpanNameTests.cs b/test/Elastic.Apm.Tests/DbSpanNameTests.cs index 364391281..a4424860e 100644 --- a/test/Elastic.Apm.Tests/DbSpanNameTests.cs +++ b/test/Elastic.Apm.Tests/DbSpanNameTests.cs @@ -11,6 +11,7 @@ using Elastic.Apm.Libraries.Newtonsoft.Json; using Elastic.Apm.Libraries.Newtonsoft.Json.Linq; using Elastic.Apm.Model; +using Elastic.Apm.Tests.Utilities; using FluentAssertions; using Xunit; @@ -51,4 +52,44 @@ public static IEnumerable SqlSignatureExamplesTestData } } } + + public class SqlTokenTestData + { + public string Name; + public string Comment; + public string Input; + public List Tokens = new(); + } + + public struct SqlToken + { + public string Kind; + public string Text; + } + + [Theory] + [JsonFileData("./TestResources/json-specs/sql_token_examples.json", typeof(SqlTokenTestData))] + public void TestSqlTokenParsing(SqlTokenTestData data) + { + var parsedTokens = Helper_ParseSql(data.Input); + parsedTokens.Count.Should().Be(data.Tokens.Count); + for (var i = 0; i < parsedTokens.Count; i++) + parsedTokens[i].ToString().Should().BeEquivalentTo(data.Tokens[i].Kind); + } + + private static List Helper_ParseSql(string sql) + { + var scanner = new Scanner(); + scanner.SetQuery(sql); + var tokens = new List(); + while (true) + { + var token = scanner.Scan(); + if (token == Scanner.Token.Eof) + break; + tokens.Add(token); + } + + return tokens; + } }