162 lines
5.5 KiB
C#
162 lines
5.5 KiB
C#
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");
|
|
}
|
|
}
|