StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
401 stars 58 forks source link

Add Assert Template compile correctly for unit testing #54

Closed Bchir closed 5 years ago

Bchir commented 5 years ago

Add Assert Template compile correctly for unit testing

Romanx commented 5 years ago

Hi there,

Would you mind explaining what use case you could see for this that we could help with?

We already throw exceptions when parsing and rendering that you can use for such tracking so you could easily write an extension for your preferred asserting framework.

Bchir commented 5 years ago

Hi, Yes sure, here is what am expecting the assert function should do : the assert function will need the template and the class that we would pass as view

it doesn't have to be used throw asserting framework for exmeple automapper framework has it's own assert method Mapper.Configuration.AssertConfigurationIsValid(); so it would be something similar to this like StubbleVisitorRenderer.AssertRenderAsync<TSource>(string template,TSource view)

Romanx commented 5 years ago

Hey there,

I've thought about this request and think it's probably best to live separately from the core library. If you'd be interested in creating an asserting project I can give you an example of something building on Xunit's existing functionality which would cover some of the basic cases you mentioned.

public static void AssertTemplateValidAsync<TSource>(string template, TSource view)
{
    if (assert == null)
    {
        throw new ArgumentNullException(nameof(assert));
    }

    var exception = Record.Exception(() =>
    {
        StaticStubbleRenderer.Instance.Render(template, view, new Settings.RenderSettings
        {
            ThrowOnDataMiss = true,
        });
    });

    if (exception is StubbleDataMissException dataMissException)
    {
        throw new Exception("Data was missing in template", dataMissException);
    }
    else if (exception is StubbleException stubbleException)
    {
        throw new Exception("An error occured validating template", stubbleException);
    }

    Assert.Null(exception);
}

Please let me know if you have any issues, thanks for using the library!