templates support

This commit is contained in:
EugeneTes
2026-01-21 15:56:37 +01:00
parent 729679d283
commit 9fbc61dede
60 changed files with 5678 additions and 14 deletions

View File

@@ -0,0 +1,105 @@
using Inspectron.Epson.Templates.Configuration;
using Inspectron.Epson.Templates.Interpreter;
using Inspectron.Epson.Templates.Language;
using Inspectron.Epson.Templates.Language.Nodes;
namespace Inspectron.Epson.Templates.Tests.Interpreter;
public class InterpreterRowTests
{
private readonly PrinterProfile _profile = new("test", "Test Printer", 48, 24, false);
private TemplateNode Parse(string source)
{
var lexer = new Lexer(source);
var parser = new Parser(lexer.Tokenize().Tokens);
return parser.Parse().Template;
}
[Fact]
public void Interpret_Row_CombinesColumns()
{
var template = Parse("@row\n|20|Name|10|Value\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
// Should have a single command with combined column text
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Name") && c.Text.Contains("Value"));
Assert.NotNull(rowCmd);
}
[Fact]
public void Interpret_Row_LeftAlignsPaddsRight()
{
var template = Parse("@row\n|10|Hi\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Hi"));
Assert.NotNull(rowCmd);
Assert.Equal(10, rowCmd.Text.Length);
Assert.StartsWith("Hi", rowCmd.Text);
}
[Fact]
public void Interpret_Row_RightAlignsPaddsLeft()
{
var template = Parse("@row\n|10,right|Hi\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Hi"));
Assert.NotNull(rowCmd);
Assert.Equal(10, rowCmd.Text.Length);
Assert.EndsWith("Hi", rowCmd.Text);
}
[Fact]
public void Interpret_Row_CenterAlignsCentersText()
{
var template = Parse("@row\n|10,center|Hi\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Hi"));
Assert.NotNull(rowCmd);
Assert.Equal(10, rowCmd.Text.Length);
Assert.StartsWith(" Hi", rowCmd.Text);
}
[Fact]
public void Interpret_Row_TruncatesLongText()
{
var template = Parse("@row\n|5|HelloWorld\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Hello"));
Assert.NotNull(rowCmd);
Assert.Equal(5, rowCmd.Text.Length);
Assert.Equal("Hello", rowCmd.Text);
}
[Fact]
public void Interpret_Row_WithBindings_ResolvesValues()
{
var template = Parse("@row\n|20|{Name}|10,right|{Price:F2}\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, """{"Name": "Item", "Price": 9.99}""");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("Item") && c.Text.Contains("9.99"));
Assert.NotNull(rowCmd);
}
[Fact]
public void Interpret_Row_MultipleColumns_CorrectTotalWidth()
{
var template = Parse("@row\n|10|A|10|B|10|C\n@endrow");
var interpreter = new TemplateInterpreter(_profile);
var commands = interpreter.Interpret(template, "{}");
var rowCmd = commands.FirstOrDefault(c => c.Text.Contains("A") && c.Text.Contains("B") && c.Text.Contains("C"));
Assert.NotNull(rowCmd);
Assert.Equal(30, rowCmd.Text.Length);
}
}