Files
sql-utilities/tests/Strata.SqlTools.Markdown.Tests/Expressions/ExpressionGeneratorTests.cs
Thom LambandClaude Opus 4.7 f55d8130e6
SonarQube Analysis / sonarqube (pull_request) Successful in 3m41s
chore(sonar): clear residual test-file smells (NUnit2046, NUnit2045, CS8604)
- **NUnit2046 (15)**: extend the prior regex sweep to also catch the
  `Is.GreaterThan(n)` / `Is.GreaterThanOrEqualTo(n)` variants on
  `.Count` and `.Length` — the previous pass only handled `Is.EqualTo`.
  Affects PG/Snowflake `QueryBreakdownTests`, PG `StatementReaderTests`,
  `LinqToSql` `QueryComparatorTests` / `QueryValidatorTests`,
  `Markdown.Tests` `ExpressionGeneratorTests` /
  `QueryBreakdownGeneratorTests`, and `JsonTokenReaderTests`.

- **CS8604 (8)**: `merged[someKey]` access inside `Assert.Multiple(() =>
  { ... })` lambdas where `someKey` is `string?` from `FirstOrDefault`.
  The preceding `Assert.That(someKey, Is.Not.Null)` does not propagate
  null-narrowing into the lambda scope, so add `!` to the dictionary
  index. (Tests fail loud with a meaningful message if the key really
  is null, so this is safe.)

- **NUnit2045 (1)**: wrap a four-assert block in
  `JsonTokenReaderTests.cs:65-68` in `Assert.Multiple`. Mirrors the
  surrounding two `Assert.Multiple` groups in that method.

All 1180 tests stay green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 15:29:20 -05:00

280 lines
9.1 KiB
C#

using Strata.SqlTools.SqlBreakdown.Expressions;
using Strata.SqlTools.SqlBreakdown.Expressions.Conditional.Logical;
using Strata.SqlTools.SqlBreakdown.Expressions.Literals;
using Strata.SqlTools.Markdown.Expressions;
namespace Strata.SqlTools.Markdown.Tests.Expressions;
[TestFixture]
public class ExpressionGeneratorTests
{
private ExpressionGenerator _generator = null!;
[SetUp]
public void Setup()
{
_generator = new ExpressionGenerator();
}
[Test]
public void GenerateMarkdown_WithSimpleExpression_GeneratesMarkdown()
{
// Arrange
var expression = new NumberLiteralExpression(42);
// Act
var result = _generator.GenerateMarkdown(expression);
// Assert
Assert.That(result, Does.Contain("## Expression Structure"));
Assert.That(result, Does.Contain("## Expression Type"));
Assert.That(result, Does.Contain("## Mermaid Diagram"));
Assert.That(result, Does.Contain("NumberLiteralExpression"));
}
[Test]
public void GenerateMarkdown_WithComparisonExpression_IncludesComparison()
{
// Arrange
Expression left = new NumberLiteralExpression(100);
Expression right = new NumberLiteralExpression(50);
var expression = left > right;
// Act
var result = _generator.GenerateMarkdown(expression, "Price Comparison");
// Assert
Assert.That(result, Does.Contain("# Price Comparison"));
Assert.That(result, Does.Contain("Comparison"));
Assert.That(result, Does.Contain("```"));
}
[Test]
public void GenerateMarkdown_WithArithmeticExpression_IncludesArithmetic()
{
// Arrange
Expression price = new NumberLiteralExpression(100);
Expression discount = new NumberLiteralExpression(0.1m);
var expression = price * (1 - discount);
// Act
var result = _generator.GenerateMarkdown(expression);
// Assert
Assert.That(result, Does.Contain("Arithmetic"));
Assert.That(result, Does.Contain("```mermaid"));
Assert.That(result, Does.Contain("graph TD"));
}
[Test]
public void GenerateMarkdown_WithLogicalExpression_IncludesLogic()
{
// Arrange
var expr1 = new NumberLiteralExpression(5) > new NumberLiteralExpression(3);
var expr2 = new NumberLiteralExpression(10) < new NumberLiteralExpression(20);
var expression = new AndExpression(expr1, expr2);
// Act
var result = _generator.GenerateMarkdown(expression);
// Assert
Assert.That(result, Does.Contain("AND"));
Assert.That(result, Does.Contain("mermaid"));
}
[Test]
public void GenerateMarkdown_WithTitle_IncludesTitle()
{
// Arrange
var expression = new StringLiteralExpression("test");
// Act
var result = _generator.GenerateMarkdown(expression, "Test Expression");
// Assert
Assert.That(result, Does.StartWith("# Test Expression"));
}
[Test]
public void GenerateMarkdown_ComplexExpression_GeneratesCompleteDoc()
{
// Arrange
Expression quantity = new NumberLiteralExpression(10);
Expression price = new NumberLiteralExpression(25.50m);
Expression discount = new NumberLiteralExpression(0.15m);
var expression = quantity * price * (1 - discount);
// Act
var result = _generator.GenerateMarkdown(expression, "Order Total");
// Assert
Assert.That(result, Does.Contain("# Order Total"));
Assert.That(result, Does.Contain("## Expression Structure"));
Assert.That(result, Does.Contain("## Expression Type"));
Assert.That(result, Does.Contain("## Mermaid Diagram"));
Assert.That(result, Does.Contain("## Mathematical Expression"));
Assert.That(result, Does.Contain("Arithmetic"));
Assert.That(result, Has.Length.GreaterThan(200));
}
[Test]
public void GenerateMathematicalExpression_WithArithmetic_GeneratesLatex()
{
// Arrange
Expression a = new NumberLiteralExpression(5);
Expression b = new NumberLiteralExpression(3);
var expression = a + b;
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression);
// Assert
Assert.That(result, Does.Contain("$$"));
Assert.That(result, Does.Contain("+"));
Assert.That(result, Does.Contain("5"));
Assert.That(result, Does.Contain("3"));
}
[Test]
public void GenerateMathematicalExpression_WithMultiplication_UsesTimesSymbol()
{
// Arrange
Expression x = new NumberLiteralExpression(10);
Expression y = new NumberLiteralExpression(20);
var expression = x * y;
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression);
// Assert
Assert.That(result, Does.Contain("\\times"));
}
[Test]
public void GenerateMathematicalExpression_WithComparison_UsesCorrectSymbol()
{
// Arrange
Expression a = new NumberLiteralExpression(10);
Expression b = new NumberLiteralExpression(20);
var expression = a >= b;
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression);
// Assert
Assert.That(result, Does.Contain("\\geq"));
}
[Test]
public void GenerateMathematicalExpression_WithLogicalAnd_UsesLandSymbol()
{
// Arrange
var expr1 = new NumberLiteralExpression(5) > new NumberLiteralExpression(3);
var expr2 = new NumberLiteralExpression(10) < new NumberLiteralExpression(20);
var expression = new AndExpression(expr1, expr2);
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression);
// Assert
Assert.That(result, Does.Contain("\\land"));
}
[Test]
public void GenerateMathematicalExpression_Inline_UsesSingleDollar()
{
// Arrange
var expression = new NumberLiteralExpression(42);
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression, inline: true);
// Assert
Assert.That(result, Does.StartWith("$"));
Assert.That(result, Does.EndWith("$"));
Assert.That(result, Does.Not.Contain("$$"));
}
[Test]
public void GenerateMathematicalExpression_Block_UsesDoubleDollar()
{
// Arrange
var expression = new NumberLiteralExpression(42);
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression, inline: false);
// Assert
Assert.That(result, Does.Contain("$$"));
}
[Test]
public void GenerateMathematicalExpression_ComplexArithmetic_GeneratesCorrectLatex()
{
// Arrange
Expression quantity = new NumberLiteralExpression(10);
Expression price = new NumberLiteralExpression(25);
Expression discount = new NumberLiteralExpression(0.1m);
var expression = quantity * price * (1 - discount);
// Act
var result = ExpressionGenerator.GenerateMathematicalExpression(expression);
// Assert
Assert.That(result, Does.Contain("\\times"));
Assert.That(result, Does.Contain("10"));
Assert.That(result, Does.Contain("25"));
Assert.That(result, Does.Contain("0.1"));
}
[Test]
public void GenerateRawMathematicalExpression_CauchySchwarzInequality_WithDollarFormat()
{
// Arrange
var latex = @"\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)";
// Act
var result = ExpressionGenerator.GenerateRawMathematicalExpression(latex, format: "dollar", inline: false);
// Assert
Assert.That(result, Does.StartWith("$$"));
Assert.That(result, Does.EndWith("$$"));
Assert.That(result, Does.Contain("\\sum_{k=1}^n"));
Assert.That(result, Does.Contain("a_k b_k"));
Assert.That(result, Does.Contain("\\leq"));
}
[Test]
public void GenerateRawMathematicalExpression_CauchySchwarzInequality_WithMathFormat()
{
// Arrange
var latex = @"\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)";
// Act
var result = ExpressionGenerator.GenerateRawMathematicalExpression(latex, format: "math");
// Assert
Assert.That(result, Does.StartWith("```math"));
Assert.That(result, Does.EndWith("```"));
Assert.That(result, Does.Contain("\\sum_{k=1}^n"));
Assert.That(result, Does.Contain("a_k b_k"));
Assert.That(result, Does.Contain("\\leq"));
}
[Test]
public void GenerateRawMathematicalExpression_Inline_UsesSingleDollar()
{
// Arrange
var latex = "E = mc^2";
// Act
var result = ExpressionGenerator.GenerateRawMathematicalExpression(latex, inline: true);
// Assert
Assert.That(result, Does.StartWith("$"));
Assert.That(result, Does.EndWith("$"));
Assert.That(result, Does.Not.Contain("```"));
Assert.That(result, Is.EqualTo("$E = mc^2$"));
}
}