From 3af9529d96899e9ff3a6f99b15dbcc570edbd9d3 Mon Sep 17 00:00:00 2001 From: Chui Tey Date: Sun, 5 Feb 2023 08:50:29 +1000 Subject: [PATCH] #117 Case statement should include whitespace if IncludeWhiteSpace=true --- .../Parsers/TSQLCaseExpressionParser.cs | 4 ++++ .../Tests/Expressions/CaseExpressionTests.cs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/TSQL_Parser/TSQL_Parser/Expressions/Parsers/TSQLCaseExpressionParser.cs b/TSQL_Parser/TSQL_Parser/Expressions/Parsers/TSQLCaseExpressionParser.cs index 9205ea0..794fa5b 100644 --- a/TSQL_Parser/TSQL_Parser/Expressions/Parsers/TSQLCaseExpressionParser.cs +++ b/TSQL_Parser/TSQL_Parser/Expressions/Parsers/TSQLCaseExpressionParser.cs @@ -39,6 +39,10 @@ public TSQLCaseExpression Parse(ITSQLTokenizer tokenizer) tokenizer.MoveNext(); + TSQLTokenParserHelper.ReadCommentsAndWhitespace( + tokenizer, + caseExpression.Tokens); + return caseExpression; } } diff --git a/TSQL_Parser/Tests/Expressions/CaseExpressionTests.cs b/TSQL_Parser/Tests/Expressions/CaseExpressionTests.cs index 69e9075..b6a923d 100644 --- a/TSQL_Parser/Tests/Expressions/CaseExpressionTests.cs +++ b/TSQL_Parser/Tests/Expressions/CaseExpressionTests.cs @@ -8,6 +8,7 @@ using TSQL.Tokens; using Tests.Tokens; +using TSQL.Expressions.Parsers; namespace Tests.Expressions { @@ -81,5 +82,26 @@ public void CaseExpression_Regression_Duplicated_Case() }, tokens); } + + [TestCase(false, 30)] + [TestCase(true, 31)] + public void CaseExpression_Should_Include_TrailingWhitespace_If_Flag_Is_True(bool includeWhiteSpace, int expectedEndPosition) + { + const string sql = + // 0123456789012345678901234567890123456789 + "CASE 1 WHEN 2 THEN 2 ELSE 3 END "; + + TSQLTokenizer tokenizer = new TSQLTokenizer(sql) + { + IncludeWhitespace = includeWhiteSpace + }; + + Assert.IsTrue(tokenizer.MoveNext()); + + var expression = new TSQLCaseExpressionParser().Parse(tokenizer); + Assert.AreEqual(0, expression.BeginPosition); + Assert.AreEqual(expectedEndPosition, expression.EndPosition); + + } } }