templates support
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
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 InterpreterBindingTests
|
||||
{
|
||||
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_SimpleBinding_ResolvesValue()
|
||||
{
|
||||
var template = Parse("{Name}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Name": "John"}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "John");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_NestedBinding_ResolvesNestedValue()
|
||||
{
|
||||
var template = Parse("{Person.Name}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Person": {"Name": "Jane"}}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Jane");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_NumericBinding_OutputsNumber()
|
||||
{
|
||||
var template = Parse("{Amount}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Amount": 42}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "42");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_NumericBinding_WithFormat_FormatsNumber()
|
||||
{
|
||||
var template = Parse("{Price:F2}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Price": 19.5}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "19.50");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_DateBinding_WithFormat_FormatsDate()
|
||||
{
|
||||
var template = Parse("{Date:dd-MMM-yy}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Date": "2024-03-15T14:30:00"}""");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("Mar"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Contains("15", cmd.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_MissingBinding_OutputsEmptyString()
|
||||
{
|
||||
var template = Parse("{Missing}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Other": "value"}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_NullBinding_OutputsEmptyString()
|
||||
{
|
||||
var template = Parse("{Value}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Value": null}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_ArrayCountBinding_OutputsCount()
|
||||
{
|
||||
var template = Parse("{Items.count}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": [1, 2, 3, 4, 5]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "5");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_BoolBinding_OutputsTrue()
|
||||
{
|
||||
var template = Parse("{Flag}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Flag": true}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "true");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_BoolBinding_OutputsFalse()
|
||||
{
|
||||
var template = Parse("{Flag}");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Flag": false}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "false");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_BindingInStyledText_AppliesStyleToResolvedValue()
|
||||
{
|
||||
var template = Parse("#bold# {Name} #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Name": "Test"}""");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("Test"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsBold);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
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 InterpreterConditionTests
|
||||
{
|
||||
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_IfTruthy_ExecutesBody()
|
||||
{
|
||||
var template = Parse("@if HasValue\nYes\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"HasValue": true}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Yes");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfFalsy_SkipsBody()
|
||||
{
|
||||
var template = Parse("@if HasValue\nYes\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"HasValue": false}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => c.Text == "Yes");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfStringEmpty_IsFalsy()
|
||||
{
|
||||
var template = Parse("@if Name\nHas Name\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Name": ""}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => c.Text == "Has Name");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfStringNotEmpty_IsTruthy()
|
||||
{
|
||||
var template = Parse("@if Name\nHas Name\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Name": "John"}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Has Name");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfArrayEmpty_IsFalsy()
|
||||
{
|
||||
var template = Parse("@if Items\nHas Items\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": []}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => c.Text == "Has Items");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfArrayNotEmpty_IsTruthy()
|
||||
{
|
||||
var template = Parse("@if Items\nHas Items\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": [1, 2]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Has Items");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfNegation_InvertsCondition()
|
||||
{
|
||||
var template = Parse("@if !HasValue\nNo Value\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"HasValue": false}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "No Value");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfEqualsComparison_Works()
|
||||
{
|
||||
var template = Parse("@if Status == \"active\"\nActive\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Status": "active"}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Active");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfNotEqualsComparison_Works()
|
||||
{
|
||||
var template = Parse("@if Status != \"inactive\"\nNot Inactive\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Status": "active"}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Not Inactive");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfGreaterThanComparison_Works()
|
||||
{
|
||||
var template = Parse("@if Count > 5\nMany Items\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Count": 10}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Many Items");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfLessThanComparison_Works()
|
||||
{
|
||||
var template = Parse("@if Count < 5\nFew Items\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Count": 2}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Few Items");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfElse_ExecutesElseWhenFalse()
|
||||
{
|
||||
var template = Parse("@if HasValue\nYes\n@else\nNo\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"HasValue": false}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => c.Text == "Yes");
|
||||
Assert.Contains(commands, c => c.Text == "No");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_IfElseIf_ExecutesCorrectBranch()
|
||||
{
|
||||
var template = Parse("@if Value == 1\nOne\n@elseif Value == 2\nTwo\n@else\nOther\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Value": 2}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => c.Text == "One");
|
||||
Assert.Contains(commands, c => c.Text == "Two");
|
||||
Assert.DoesNotContain(commands, c => c.Text == "Other");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_ArrayCountComparison_Works()
|
||||
{
|
||||
var template = Parse("@if Items.count > 0\nHas Items\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": [1, 2, 3]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Has Items");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
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 InterpreterLoopTests
|
||||
{
|
||||
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_Foreach_IteratesOverArray()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{item}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": ["A", "B", "C"]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "A");
|
||||
Assert.Contains(commands, c => c.Text == "B");
|
||||
Assert.Contains(commands, c => c.Text == "C");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_AccessesObjectProperties()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{item.Name}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": [{"Name": "First"}, {"Name": "Second"}]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "First");
|
||||
Assert.Contains(commands, c => c.Text == "Second");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_EmptyArray_NoOutput()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{item}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": []}""");
|
||||
|
||||
Assert.DoesNotContain(commands, c => !string.IsNullOrWhiteSpace(c.Text));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_IndexMetadata_Works()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{_index}:{item}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": ["A", "B"]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "0");
|
||||
Assert.Contains(commands, c => c.Text == "1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_NumberMetadata_Works()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{_number}. {item}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": ["A", "B"]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "1");
|
||||
Assert.Contains(commands, c => c.Text == "2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_FirstMetadata_Works()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n@if _first\nFirst: {item}\n@end\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": ["A", "B", "C"]}""");
|
||||
|
||||
var firstCmds = commands.Where(c => c.Text.Contains("First")).ToList();
|
||||
Assert.Single(firstCmds);
|
||||
Assert.Contains(commands, c => c.Text == "A");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_LastMetadata_Works()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n@if _last\nLast: {item}\n@end\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Items": ["A", "B", "C"]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text.Contains("Last"));
|
||||
Assert.Contains(commands, c => c.Text == "C");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_NestedForeach_Works()
|
||||
{
|
||||
var template = Parse("@foreach group in Groups\n@foreach item in group.Items\n{item}\n@end\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template,
|
||||
"""{"Groups": [{"Items": ["A", "B"]}, {"Items": ["C", "D"]}]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "A");
|
||||
Assert.Contains(commands, c => c.Text == "B");
|
||||
Assert.Contains(commands, c => c.Text == "C");
|
||||
Assert.Contains(commands, c => c.Text == "D");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_AccessesParentContext()
|
||||
{
|
||||
var template = Parse("@foreach item in Items\n{Title}: {item}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, """{"Title": "List", "Items": ["A", "B"]}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "List");
|
||||
Assert.Contains(commands, c => c.Text == "A");
|
||||
Assert.Contains(commands, c => c.Text == "B");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_Foreach_NestedPath_Works()
|
||||
{
|
||||
var template = Parse("@foreach item in Data.Items\n{item.Name}\n@end");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template,
|
||||
"""{"Data": {"Items": [{"Name": "First"}, {"Name": "Second"}]}}""");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "First");
|
||||
Assert.Contains(commands, c => c.Text == "Second");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
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 InterpreterTextTests
|
||||
{
|
||||
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_PlainText_OutputsText()
|
||||
{
|
||||
var template = Parse("Hello World");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
Assert.Contains(commands, c => c.Text == "Hello World");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_BoldStyle_SetsBoldFlag()
|
||||
{
|
||||
var template = Parse("#bold# text #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsBold);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_BigStyle_SetsBigFlag()
|
||||
{
|
||||
var template = Parse("#big# text #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsBig);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_TallStyle_SetsTallFlag()
|
||||
{
|
||||
var template = Parse("#tall# text #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsTall);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_RedStyle_SetsRedFlag_WhenSupported()
|
||||
{
|
||||
var profileWithRed = new PrinterProfile("test", "Test", 48, 24, true);
|
||||
var template = Parse("#red# text #");
|
||||
var interpreter = new TemplateInterpreter(profileWithRed);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsRed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_RedStyle_IgnoresRedFlag_WhenNotSupported()
|
||||
{
|
||||
var profileNoRed = new PrinterProfile("test", "Test", 48, 24, false);
|
||||
var template = Parse("#red# text #");
|
||||
var interpreter = new TemplateInterpreter(profileNoRed);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.False(cmd.IsRed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_CenterStyle_CentersText()
|
||||
{
|
||||
var template = Parse("#center# Hi #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("Hi"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.StartsWith(" ", cmd.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_MultipleStyles_CombinesFlags()
|
||||
{
|
||||
var template = Parse("#bold,big,tall# text #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.True(cmd.IsBold);
|
||||
Assert.True(cmd.IsBig);
|
||||
Assert.True(cmd.IsTall);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_DashSeparator_OutputsFullWidthLine()
|
||||
{
|
||||
var template = Parse("---");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains('-'));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(48, cmd.Text.Length);
|
||||
Assert.True(cmd.Text.All(c => c == '-'));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_EqualsSeparator_OutputsFullWidthLine()
|
||||
{
|
||||
var template = Parse("===");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains('='));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(48, cmd.Text.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Interpret_SpacingStyle_SetsLineSpacing()
|
||||
{
|
||||
var template = Parse("#spacing:50# text #");
|
||||
var interpreter = new TemplateInterpreter(_profile);
|
||||
var commands = interpreter.Interpret(template, "{}");
|
||||
|
||||
var cmd = commands.FirstOrDefault(c => c.Text.Contains("text"));
|
||||
Assert.NotNull(cmd);
|
||||
Assert.Equal(50, cmd.SetLineSpacing);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user