Skip to content

Commit 80ea8db

Browse files
committed
Add tests covering more paths in compilation renderer
1 parent 0eb0a23 commit 80ea8db

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

test/Stubble.Compilation.Tests/RenderTests.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Dynamic;
88
using System.Linq;
99
using System.Threading.Tasks;
10+
using Stubble.Core.Loaders;
1011
using Xunit;
1112

1213
namespace Stubble.Compilation.Tests
@@ -172,6 +173,141 @@ public void You_Should_Be_Able_To_Build_Using_Builder()
172173
Assert.Equal("Bar", func(input));
173174
}
174175

176+
[Fact]
177+
public void You_Should_Be_Able_To_Create_A_Renderer()
178+
{
179+
var stubble = new StubbleCompilationRenderer();
180+
181+
var input = new { Foo = "Bar" };
182+
var func = stubble.Compile("{{Foo}}", input);
183+
184+
Assert.Equal("Bar", func(input));
185+
}
186+
187+
[Fact]
188+
public async Task You_Should_Be_Able_To_Compile_Without_An_Example_Object()
189+
{
190+
var stubble = new StubbleCompilationRenderer();
191+
192+
var input = new ExampleClass { Foo = "Bar" };
193+
var func = stubble.Compile<ExampleClass>("{{Foo}}");
194+
var funcAsync = await stubble.CompileAsync<ExampleClass>("{{Foo}}");
195+
196+
Assert.Equal("Bar", func(input));
197+
Assert.Equal("Bar", funcAsync(input));
198+
}
199+
200+
[Fact]
201+
public async Task You_Should_Be_Able_To_Compile_Without_An_Example_Object_And_CompilationSettings()
202+
{
203+
var stubble = new StubbleCompilationRenderer();
204+
var settings = new CompilationSettings
205+
{
206+
ThrowOnDataMiss = true
207+
};
208+
209+
var input = new ExampleClass { Foo = "Bar" };
210+
var func = stubble.Compile<ExampleClass>("{{Foo}}", settings);
211+
var funcAsync = await stubble.CompileAsync<ExampleClass>("{{Foo}}", settings);
212+
213+
Assert.Equal("Bar", func(input));
214+
Assert.Equal("Bar", funcAsync(input));
215+
}
216+
217+
[Fact]
218+
public async Task You_Should_Be_Able_To_Compile_Without_An_Example_Object_With_Partials()
219+
{
220+
var stubble = new StubbleCompilationRenderer();
221+
var partials = new Dictionary<string, string>
222+
{
223+
{"Partial", "{{Foo}}"}
224+
};
225+
226+
var input = new ExampleClass { Foo = "Bar" };
227+
var func = stubble.Compile<ExampleClass>("{{> Partial}}", partials);
228+
var funcAsync = await stubble.CompileAsync<ExampleClass>("{{> Partial}}", partials);
229+
230+
Assert.Equal("Bar", func(input));
231+
Assert.Equal("Bar", funcAsync(input));
232+
}
233+
234+
[Fact]
235+
public async Task You_Should_Be_Able_To_Compile_Without_An_Example_Object_With_Partials_And_Settings()
236+
{
237+
var stubble = new StubbleCompilationRenderer();
238+
var settings = new CompilationSettings
239+
{
240+
ThrowOnDataMiss = true
241+
};
242+
var partials = new Dictionary<string, string>
243+
{
244+
{"Partial", "{{Foo}}"}
245+
};
246+
247+
var input = new ExampleClass { Foo = "Bar" };
248+
var func = stubble.Compile<ExampleClass>("{{> Partial}}", partials, settings);
249+
var funcAsync = await stubble.CompileAsync<ExampleClass>("{{> Partial}}", partials, settings);
250+
251+
Assert.Equal("Bar", func(input));
252+
Assert.Equal("Bar", funcAsync(input));
253+
}
254+
255+
[Fact]
256+
public async Task You_Should_Be_Able_To_Compile_With_An_Example_Object_With_Settings()
257+
{
258+
var stubble = new StubbleCompilationRenderer();
259+
var settings = new CompilationSettings
260+
{
261+
ThrowOnDataMiss = true
262+
};
263+
264+
var input = new ExampleClass { Foo = "Bar" };
265+
var func = stubble.Compile("{{Foo}}", input, settings);
266+
var funcAsync = await stubble.CompileAsync("{{Foo}}", input, settings);
267+
268+
Assert.Equal("Bar", func(input));
269+
Assert.Equal("Bar", funcAsync(input));
270+
}
271+
272+
[Fact]
273+
public async Task You_Should_Be_Able_To_Compile_With_An_Example_Object_With_Partials_And_Settings()
274+
{
275+
var stubble = new StubbleCompilationRenderer();
276+
var settings = new CompilationSettings
277+
{
278+
ThrowOnDataMiss = true
279+
};
280+
var partials = new Dictionary<string, string>
281+
{
282+
{"Partial", "{{Foo}}"}
283+
};
284+
285+
var input = new ExampleClass { Foo = "Bar" };
286+
var func = stubble.Compile("{{> Partial}}", input, partials, settings);
287+
var funcAsync = await stubble.CompileAsync("{{> Partial}}", input, partials, settings);
288+
289+
Assert.Equal("Bar", func(input));
290+
Assert.Equal("Bar", funcAsync(input));
291+
}
292+
293+
[Fact]
294+
public async Task Unknown_Exceptions_Are_Thrown()
295+
{
296+
var partials = new Dictionary<string, string>
297+
{
298+
{"Partial", "{{Foo}}"}
299+
};
300+
301+
var stubble = new StubbleCompilationBuilder()
302+
.Configure(configure => { configure.SetTemplateLoader(new DictionaryLoader(partials)); })
303+
.Build();
304+
305+
var input = new ExampleClass { Foo = "Bar" };
306+
307+
Assert.Throws<UnknownTemplateException>(() => stubble.Compile("MissingPartial", input));
308+
await Assert.ThrowsAsync<UnknownTemplateException>(async () => await stubble.CompileAsync("MissingPartial", input));
309+
}
310+
175311
public static IEnumerable<object[]> Data => new List<SpecTest>
176312
{
177313
new SpecTest
@@ -245,5 +381,10 @@ public void You_Should_Be_Able_To_Build_Using_Builder()
245381
Expected = "That should totally be true"
246382
}
247383
}.Select(s => new[] { s });
384+
385+
private class ExampleClass
386+
{
387+
public string Foo { get; set; }
388+
}
248389
}
249390
}

0 commit comments

Comments
 (0)