summaryrefslogtreecommitdiff
path: root/src/marlin.tests/VariableExpanderTests.cs
blob: c04152ec3c263a3afce9caeb6f84702b63ec10ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace marlin.tests;

using core.util;

public class VariableExpanderTests
{
    private Dictionary<string, string> _variables = new();

    [SetUp]
    public void Setup()
    {
        this._variables["name"] = "john";
        this._variables["branch"] = "master";
    }

    [TestCase("hello world", "hello world")] // passthroughs
    [TestCase("hi {name}!", "hi john!")] // classic variable expansion
    [TestCase("hi {name=bob}!", "hi john!")] // existent variable with fallback
    [TestCase("hi {nonvar=bob}!", "hi bob!")] // non-existent variable with fallback
    [TestCase("hi {name? plane}!", "hi plane!")] // use value if set
    [TestCase("hi {nonvar? plane}!", "hi !")] // omit value if not set
    [TestCase("mycommand {--checkout branch}", "mycommand --checkout master")] // inject cli arg when set
    [TestCase("mycommand {--checkout nonvar}", "mycommand")] // omit cli arg when not set
    [TestCase("i will {{escape}}", "i will {escape}")] // escaping works
    public void ExpansionMatches(string input, string output)
    {
        var result = VariableExpander.Expand(input, this._variables);
        Assert.That(result, Is.EqualTo(output));
    }

    [TestCase("un {", typeof(UnmatchedBraceException))]       // unmatched leading
    [TestCase("un }", typeof(UnmatchedBraceException))]       // unmatched trailing
    [TestCase("{nonvar}", typeof(VariableNotFoundException))] // non existent variable
    public void ExpansionFails(string input, Type exceptionType)
    {
        Assert.That(() => VariableExpander.Expand(input, this._variables),
            Throws.TypeOf(exceptionType));
    }
}