예제 #1
0
    public void Assembly_works()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            @"T:\test.tt",
            @"<#@ assembly name=""Microsoft.EntityFrameworkCore"" #><#= nameof(Microsoft.EntityFrameworkCore.DbContext) #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal("DbContext", result);
    }
예제 #2
0
    public void Directive_throws_when_processor_unknown()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        var ex = Assert.Throws <FileNotFoundException>(
            () => host.ProcessTemplate(
                @"T:\test.tt",
                @"<#@ test processor=""TestDirectiveProcessor"" #>",
                callback));

        Assert.Equal(DesignStrings.UnknownDirectiveProcessor("TestDirectiveProcessor"), ex.Message);
    }
예제 #3
0
    public void Error_works()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        host.ProcessTemplate(
            @"T:\test.tt",
            @"<# Error(""Hello, Error!""); #>",
            callback);

        var error = Assert.Single(callback.Errors.Cast <CompilerError>());

        Assert.Equal("Hello, Error!", error.ErrorText);
    }
예제 #4
0
    public void Output_works()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        host.ProcessTemplate(
            @"T:\test.tt",
            @"<#@ output extension="".txt"" encoding=""us-ascii"" #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal(".txt", callback.Extension);
        Assert.Equal(Encoding.ASCII, callback.OutputEncoding);
    }
예제 #5
0
    public void Service_works()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .AddSingleton("Hello, Services!")
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            @"T:\test.tt",
            @"<#@ template hostSpecific=""true"" #><#= ((IServiceProvider)Host).GetService(typeof(string)) #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal("Hello, Services!", result);
    }
예제 #6
0
    public void ResolvePath_work()
    {
        using var dir = new TempDirectory();

        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            Path.Combine(dir, "test.tt"),
            @"<#@ template hostSpecific=""true"" #><#= Host.ResolvePath(""data.json"") #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal(Path.Combine(dir, "data.json"), result);
    }
예제 #7
0
    public void Include_works()
    {
        using var dir = new TempDirectory();
        File.WriteAllText(
            Path.Combine(dir, "test.ttinclude"),
            "Hello, Include!");

        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            Path.Combine(dir, "test.tt"),
            @"<#@ include file=""test.ttinclude"" #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal("Hello, Include!", result);
    }
예제 #8
0
    public void Session_works_with_parameter()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());

        host.Session = new TextTemplatingSession
        {
            ["Value"] = "Hello, Session!"
        };
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            @"T:\test.tt",
            @"<#@ parameter name=""Value"" type=""System.String"" #><#= Value #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal("Hello, Session!", result);
    }
예제 #9
0
    public void Session_works()
    {
        var host = new TextTemplatingService(
            new ServiceCollection()
            .BuildServiceProvider());

        host.Session = new TextTemplatingSession
        {
            ["Value"] = "Hello, Session!"
        };
        var callback = new TextTemplatingCallback();

        var result = host.ProcessTemplate(
            @"T:\test.tt",
            @"<#= Session[""Value""] #>",
            callback);

        Assert.Empty(callback.Errors);
        Assert.Equal("Hello, Session!", result);
    }