-
Notifications
You must be signed in to change notification settings - Fork 5
/
CompilerTests.cs
207 lines (164 loc) · 5.25 KB
/
CompilerTests.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Linq;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using Mulder.Base.Compilation;
using Mulder.Base.Domain;
using Mulder.Base.Filters;
using Mulder.Base.IO;
using Mulder.Base.Logging;
namespace Mulder.Tests.Base.Compilation
{
public class CompilerTests
{
[TestFixture]
public class when_compiling_a_site
{
Site site;
ILog log;
IFileSystem fileSystem;
IFilterFactory filterFactory;
IFilter filter;
Compiler compiler;
[SetUp]
public void SetUp()
{
site = LoadFakeSite();
log = Substitute.For<ILog>();
fileSystem = Substitute.For<IFileSystem>();
filterFactory = Substitute.For<IFilterFactory>();
filter = Substitute.For<IFilter>();
filterFactory.CreateFilter(Arg.Any<string>()).Returns(filter);
compiler = new Compiler(log, fileSystem, filterFactory);
}
[Test]
public void should_create_configured_output_directory()
{
compiler.Compile(site);
fileSystem.Received().CreateDirectory(site.Configuration.OutputDirectory);
}
[Test]
public void should_create_static_file_proxies_for_matched_items()
{
compiler.Compile(site);
foreach (Item item in site.Items) {
item.StaticFiles.Count().ShouldBe(1);
}
}
[Test]
public void should_set_path_on_each_static_file()
{
string expected = site.Configuration.OutputDirectory + "/file.html";
compiler.Compile(site);
var staticFiles = site
.Items
.SelectMany(item => item.StaticFiles)
.Where(staticFile => staticFile.Path == expected);
staticFiles.Count().ShouldBe(3);
}
[Test]
public void should_set_filter_on_each_static_file()
{
compiler.Compile(site);
var staticFiles = site.Items.SelectMany(item => item.StaticFiles);
foreach (StaticFile staticFile in staticFiles) {
string filterName = staticFile.GetNextFilterName();
filterName.ShouldBe("filter");
}
}
[Test]
public void should_set_layout_on_each_static_file()
{
var expectedLayout = site.Layouts.Single();
compiler.Compile(site);
var staticFiles = site.Items.SelectMany(item => item.StaticFiles);
foreach (StaticFile staticFile in staticFiles) {
staticFile.Layout.ShouldBe(expectedLayout);
}
}
[Test]
public void should_create_filter_for_static_file_filters()
{
compiler.Compile(site);
filterFactory.Received(3).CreateFilter("filter");
}
[Test]
public void should_execute_filter_for_static_file_filters()
{
compiler.Compile(site);
var staticFiles = site.Items.SelectMany(item => item.StaticFiles);
foreach (StaticFile staticFile in staticFiles) {
filter.Received().Execute("item-content", Arg.Any<FilterContext>());
}
}
[Test]
public void should_create_filter_for_static_file_layout_filter()
{
compiler.Compile(site);
filterFactory.Received(3).CreateFilter("layout-filter");
}
[Test]
public void should_execute_filter_for_static_file_layout_filter()
{
compiler.Compile(site);
var staticFiles = site.Items.SelectMany(item => item.StaticFiles);
foreach (StaticFile staticFile in staticFiles) {
filter.Received().Execute("layout-content", Arg.Any<FilterContext>());
}
}
[Test]
public void should_have_three_snapshots_in_each_static_file()
{
compiler.Compile(site);
var staticFiles = site.Items.SelectMany(item => item.StaticFiles);
foreach (StaticFile staticFile in staticFiles) {
staticFile.SnapShots.Count().ShouldBe(3);
}
}
[Test]
public void should_write_each_static_file_to_disk()
{
string expectedPath = site.Configuration.OutputDirectory + "/file.html";
string expectedContent = "";
compiler.Compile(site);
fileSystem.Received(3).WriteStringToFile(expectedPath, expectedContent);
}
[Test]
public void should_log_each_static_file_created()
{
string expectedPath = site.Configuration.OutputDirectory + "/file.html";
compiler.Compile(site);
log.Received(3).InfoMessage("\tcreate {0}", expectedPath);
}
static Site LoadFakeSite()
{
var configuration = Substitute.For<IConfiguration>();
configuration.OutputDirectory.Returns("SomeOutputDirectory");
var items = new List<Item> {
new Item("/a/", false, "item-content", new Dictionary<string, object>(), DateTime.Now),
new Item("/a/b/", false, "item-content", new Dictionary<string, object>(), DateTime.Now),
new Item("/a/b/c/", false, "item-content", new Dictionary<string, object>(), DateTime.Now)
};
var layouts = new List<Layout> {
new Layout("/layout/", "layout-content", new Dictionary<string, object>(), DateTime.Now)
};
var compileRules = new List<CompileRule> {
new CompileRule("*", (context) => {
context.WithFilter("filter").WithLayout("layout");
})
};
var routeRules = new List<RouteRule> {
new RouteRule("*", (context) => {
return "/file.html";
})
};
var layoutRules = new List<LayoutRule> {
new LayoutRule("*", "layout-filter")
};
return new Site(configuration, items, layouts, compileRules, routeRules, layoutRules);
}
}
}
}