コード例 #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);
    }