This repository was archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathLiquidEngineTests.cs
More file actions
2314 lines (1970 loc) · 103 KB
/
LiquidEngineTests.cs
File metadata and controls
2314 lines (1970 loc) · 103 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using DotLiquid;
using NSubstitute;
using Pretzel.Logic;
using Pretzel.Logic.Exceptions;
using Pretzel.Logic.Extensibility;
using Pretzel.Logic.Extensibility.Extensions;
using Pretzel.Logic.Extensions;
using Pretzel.Logic.Liquid;
using Pretzel.Logic.Templating.Context;
using Pretzel.Logic.Templating.Jekyll;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Extensions;
namespace Pretzel.Tests.Templating.Jekyll
{
public class LiquidEngineTests
{
private static SiteContextGenerator GetSiteContextGenerator(IFileSystem fileSystem, IConfiguration configuration = null)
{
return new SiteContextGenerator(fileSystem, new LinkHelper(), configuration ?? new Configuration());
}
public class When_Recieving_A_Folder_Containing_One_File : BakingEnvironment<LiquidEngine>
{
private const string FileContents = "<html><head></head><body></body></html>";
private const string Folder = @"C:\website\";
private const string OutputFolder = @"C:\website\_site";
private readonly SiteContext context = new SiteContext { SourceFolder = Folder, OutputFolder = OutputFolder };
public override LiquidEngine Given()
{
context.Pages.Add(new Page
{
Content = FileContents,
File = "index.html",
Filepath = @"C:\website\index.html",
Url = "index.html"
});
return new LiquidEngine();
}
public override void When()
{
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void That_Site_Folder_Is_Created()
{
Assert.True(FileSystem.Directory.Exists(@"C:\website\_site"));
}
[Fact]
public void The_File_Is_Added_At_The_Root()
{
Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html"));
}
[Fact]
public void The_File_Is_Identical()
{
Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
}
}
public class When_Recieving_A_Folder_Without_A_Trailing_Slash : BakingEnvironment<LiquidEngine>
{
private const string FileContents = "<html><head></head><body></body></html>";
private const string Folder = @"C:\website";
private const string OutputFolder = @"C:\website\_site";
private readonly SiteContext context = new SiteContext { SourceFolder = Folder, OutputFolder = OutputFolder };
public override LiquidEngine Given()
{
context.Pages.Add(new Page
{
Content = FileContents,
File = "index.html",
Filepath = @"C:\website\index.html",
Url = "index.html"
});
return new LiquidEngine();
}
public override void When()
{
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_File_Is_Added_At_The_Root()
{
Assert.True(FileSystem.File.Exists(@"C:\website\_site\index.html"));
}
}
public class When_Recieving_A_Folder_Containing_One_File_In_A_Subfolder : BakingEnvironment<LiquidEngine>
{
private const string FileContents = "<html><head></head><body></body></html>";
private const string Folder = @"C:\website";
private const string OutputFolder = @"C:\website\_site";
private readonly SiteContext context = new SiteContext { SourceFolder = Folder, OutputFolder = OutputFolder };
public override LiquidEngine Given()
{
context.Pages.Add(new Page
{
Content = FileContents,
File = @"content\index.html",
Filepath = @"C:\website\content\index.html",
Url = "content/index.html"
});
return new LiquidEngine();
}
public override void When()
{
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void That_Child_Folder_Is_Created()
{
Assert.True(FileSystem.Directory.Exists(@"C:\website\_site\content"));
}
[Fact]
public void The_File_Is_Added_At_The_Root()
{
Assert.True(FileSystem.File.Exists(@"C:\website\_site\content\index.html"));
}
[Fact]
public void The_File_Is_Identical()
{
Assert.Equal(FileContents, FileSystem.File.ReadAllText(@"C:\website\_site\content\index.html"));
}
}
public class When_Recieving_A_File_With_Metadata : BakingEnvironment<LiquidEngine>
{
private const string FileContents = "<html><head><title>{{ page.title }}</title></head><body></body></html>";
private const string OutputContents = "<html><head><title></title></head><body></body></html>";
private const string Folder = @"C:\website";
private const string OutputFolder = @"C:\website\_site";
private readonly SiteContext context = new SiteContext { SourceFolder = Folder, OutputFolder = OutputFolder };
public override LiquidEngine Given()
{
context.Pages.Add(new Page
{
Content = FileContents,
File = @"index.html",
Filepath = @"C:\website\index.html",
Url = "index.html"
});
return new LiquidEngine();
}
public override void When()
{
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_File_Is_Altered()
{
Assert.Equal(OutputContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
}
}
public class Given_A_Folder_Starting_With_Dot : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\.gems\file.txt", new MockFileData(PageContents));
var context = new SiteContext { SourceFolder = @"C:\website\", Title = "My Web Site" };
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Folder_Should_Be_Ignored()
{
Assert.False(FileSystem.File.Exists(@"C:\website\_site\.gems\file.txt"));
}
}
public class When_Paginate_With_No_Posts : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ page.title }}</title></head><body>{{ content }}</body></html>";
private const string IndexContents = "---\r\n layout: default \r\n paginate: 5 \r\n paginate_link: /blog/page:page/index.html \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<html><head><title>A different title</title></head><body><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Index_Is_Generated()
{
const string fileName = @"C:\website\_site\index.html";
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(fileName).RemoveWhiteSpace());
}
[Fact]
public void No_Pages_Should_Be_Generated()
{
Assert.False(FileSystem.File.Exists(@"C:\website\_site\page1\index.html"));
Assert.False(FileSystem.File.Exists(@"C:\website\_site\page2\index.html"));
}
}
public class When_Paginate_With_Default_Pagelink : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ page.title }}</title><link rel=\"canonical\" href=\"{{ page.url }}\" /></head><body>{{ content }}</body></html>";
private const string PostContents = "---\r\n layout: default \r\n title: 'Post'\r\n---\r\n\r\n## Hello World!";
private const string IndexContents = "---\r\n layout: default \r\n paginate: 1 \r\n title: 'A different title'\r\n---\r\n\r\n<h2>Hello World!</h2><p>{{ paginator.previous_page }} / {{ paginator.page }} / {{ paginator.next_page }}</p>";
private const string ExpectedfileContents = "<html><head><title>A different title</title><link rel=\"canonical\" href=\"{3}\" /></head><body><h2>Hello World!</h2><p>{0} / {1} / {2}</p></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.html", new MockFileData(IndexContents));
for (var i = 1; i <= 5; i++)
{
FileSystem.AddFile(String.Format(@"C:\website\_posts\2012-02-0{0}-p{0}.md", i), new MockFileData(PostContents));
}
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Four_Pages_Should_Be_Generated()
{
for (var i = 2; i <= 5; i++)
{
var fileName = String.Format(@"C:\website\_site\page\{0}\index.html", i);
var expectedContents = string.Format(ExpectedfileContents, i - 1, i, i + 1, "/page/" + i + "/index.html");
Assert.Equal(expectedContents, FileSystem.File.ReadAllText(fileName).RemoveWhiteSpace());
}
}
[Fact]
public void Page1_Is_Not_Generated()
{
Assert.False(FileSystem.File.Exists(@"C:\website\_site\page\1\index.html"));
}
[Fact]
public void But_Index_Is_Generated()
{
var expectedContents = string.Format(ExpectedfileContents, 0, 1, 2, "/index.html");
Assert.Equal(expectedContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class When_Paginate_With_Custom_Pagelink : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ page.title }}</title></head><body>{{ content }}</body></html>";
private const string PostContents = "---\r\n layout: default \r\n title: 'Post'\r\n---\r\n\r\n## Hello World!";
private const string IndexContents = "---\r\n layout: default \r\n paginate: 2 \r\n paginate_link: /blog/page:page/ \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<html><head><title>A different title</title></head><body><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents));
for (var i = 1; i <= 7; i++)
{
FileSystem.AddFile(String.Format(@"C:\website\_posts\2012-02-0{0}-p{0}.md", i), new MockFileData(PostContents));
}
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Four_Pages_Should_Be_Generated()
{
for (var i = 2; i <= 4; i++)
{
var fileName = String.Format(@"C:\website\_site\blog\page{0}\index.html", i);
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(fileName).RemoveWhiteSpace());
}
}
[Fact]
public void Page1_Is_Not_Generated()
{
Assert.False(FileSystem.File.Exists(@"C:\website\_site\blog\page1\index.html"));
}
}
public class When_SiteContext_Specifies_Title : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "<html><head><title>{{ page.title }}</title></head><body><h1>Hello World!</h1></body></html>";
private readonly SiteContext context = new SiteContext { SourceFolder = Folder, OutputFolder = OutputFolder, Title = "My Web Site" };
private readonly string expectedfileContents = PageContents.Replace(@"{{ page.title }}", "My Web Site");
private const string Folder = @"C:\website";
private const string OutputFolder = @"C:\website\_site";
public override LiquidEngine Given()
{
context.Pages.Add(new Page
{
Content = PageContents,
File = @"index.md",
Filepath = @"C:\website\index.html",
Url = "index.html"
});
return new LiquidEngine();
}
public override void When()
{
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Page_Includes_The_Value()
{
Assert.Equal(expectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
}
}
public class When_A_Template_Also_Has_A_Layout_Value : BakingEnvironment<LiquidEngine>
{
private const string ParentTemplateContents = "<html><head><title>{{ page.title }}</title></head><body>{{ content }}</body></html>";
private const string InnerTemplateContents = "---\r\n layout: parent\r\n---\r\n\r\n<h1>Title</h1>{{content}}";
private const string PageContents = "---\r\n layout: inner\r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<html><head><title>My Web Site</title></head><body><h1>Title</h1><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\parent.html", new MockFileData(ParentTemplateContents));
FileSystem.AddFile(@"C:\website\_layouts\inner.html", new MockFileData(InnerTemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
context.Title = "My Web Site";
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_File_Is_Applies_Data_To_The_Template()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
[Fact]
public void Does_Not_Copy_Template_To_Output()
{
Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\parent.html"));
Assert.False(FileSystem.File.Exists(@"C:\website\_site\_layouts\inner.html"));
}
}
public class When_Aeoth_Tests_The_Edge_Cases_Of_Handling_YAML_Front_Matter : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\n---";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\file.txt", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
context.Title = "My Web Site";
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Yaml_Matter_Should_Be_Cleared()
{
Assert.Equal("", FileSystem.File.ReadAllText(@"C:\website\_site\file.txt"));
}
}
public class Given_Markdown_Page_Has_A_Permalink : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\r\npermalink: /somepage.html\r\n---\r\n\r\n# Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
context.Title = "My Web Site";
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_File_Should_Be_At_A_Different_Path()
{
Assert.True(FileSystem.File.Exists(@"C:\website\_site\somepage.html"));
}
}
public class Given_Markdown_Page_Has_A_Title : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ page.title }}</title></head><body>{{ content }}</body></html>";
private const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<html><head><title>A different title</title></head><body><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
context.Title = "My Web Site";
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Override_The_Site_Title()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Page_Use_Filter : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><body>{{ '.NET C#' | slugify}}</body></html>";
private const string PageContents = "---\r\n layout: default \r\n title: 'A different title'\r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<html><body>net-csharp</body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine { Filters = new IFilter[] { new SlugifyFilter() } };
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Be_Slugified()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Config_File_Has_A_Title : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents_1 = "<html><head><title>{{ page.title }}</title></head><body>{{ content }}</body></html>";
private const string TemplateContents_2 = "<html><head><title>{{ site.title }}</title></head><body>{{ content }}</body></html>";
private const string PageContents_1 = "---\r\n layout: default \r\n---\r\n\r\n## Hello World!";
private const string PageContents_2 = "---\r\n layout: default2 \r\n---\r\n\r\n## Hello World!";
private const string ConfigContents = "---\r\n title: A different title\r\n---";
private const string ExpectedfileContents = "<html><head><title>A different title</title></head><body><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents_1));
FileSystem.AddFile(@"C:\website\_layouts\default2.html", new MockFileData(TemplateContents_2));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents_1));
FileSystem.AddFile(@"C:\website\index2.md", new MockFileData(PageContents_2));
var generator = GetSiteContextGenerator(FileSystem, new ConfigurationMock(ConfigContents));
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
context.Title = "My Web Site";
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Override_The_Page_Title()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
[Fact]
public void The_Output_Should_Override_The_Site_Title()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index2.html").RemoveWhiteSpace());
}
}
public class Given_Config_File_Has_A_Title_And_Context_Has_No_Title : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ site.title }}</title></head><body>{{ content }}</body></html>";
private const string PageContents = "---\r\n layout: default \r\n---\r\n\r\n## Hello World!";
private const string ConfigContents = "---\r\n title: A different title\r\n---";
private const string ExpectedfileContents = "<html><head><title>A different title</title></head><body><h2>Hello World!</h2></body></html>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem, new ConfigurationMock(ConfigContents));
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Use_The_Site_Title_From_Config()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Site_Comport_Non_Standard_Files : BakingEnvironment<LiquidEngine>
{
private const string TemplateContents = "<html><head><title>{{ site.title }}</title></head><body>{{ content }}</body></html>";
private const string NonProcessedPageContents = "## Hello World!";
private const string LayoutNilPageContents = "---\r\n layout: nil \r\n---\r\n\r\n## Hello World!";
private const string NoLayoutPageContents = "---\r\n \r\n---\r\n\r\n## Hello World!";
private const string NonExistingLayoutPageContents = "----\r\n layout: inexistant \r\n---\r\n\r\n## Hello World!";
private const string ExpectedfileContents = "<h2>Hello World!</h2>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\NoProcessed.md", new MockFileData(NonProcessedPageContents));
FileSystem.AddFile(@"C:\website\LayoutNil.md", new MockFileData(LayoutNilPageContents));
FileSystem.AddFile(@"C:\website\NoLayout.md", new MockFileData(NoLayoutPageContents));
FileSystem.AddFile(@"C:\website\NonExistingLayout.md", new MockFileData(NonExistingLayoutPageContents));
FileSystem.AddFile(@"C:\website\image.jpg", new MockFileData("jpg image"));
FileSystem.AddFile(@"C:\website\image.png", new MockFileData("png image"));
FileSystem.AddFile(@"C:\website\image.gif", new MockFileData("gif image"));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Non_Processed_Page_Should_Not_Be_Transformed()
{
Assert.Equal("## Hello World!", FileSystem.File.ReadAllText(@"C:\website\_site\NoProcessed.md").RemoveWhiteSpace());
}
[Fact]
public void Page_With_Layout_Nil_Should_Not_Have_Any_Layout()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\LayoutNil.html").RemoveWhiteSpace());
}
[Fact]
public void Page_With_No_Layout_Should_Not_Have_Any_Layout()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\NoLayout.html").RemoveWhiteSpace());
}
[Fact]
public void Page_With_Non_Existing_Layout_Should_Not_Have_Any_Layout()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\NonExistingLayout.html").RemoveWhiteSpace());
}
[Fact]
public void Images_Should_Be_Copied_In_Output_Directory()
{
Assert.Equal("jpg image", FileSystem.File.ReadAllText(@"C:\website\_site\image.jpg").RemoveWhiteSpace());
Assert.Equal("png image", FileSystem.File.ReadAllText(@"C:\website\_site\image.png").RemoveWhiteSpace());
Assert.Equal("gif image", FileSystem.File.ReadAllText(@"C:\website\_site\image.gif").RemoveWhiteSpace());
}
}
public class Given_Site_Comport_Bad_Formated_Layout : BakingEnvironment<LiquidEngine>
{
private SiteContext Context;
private const string TemplateContents = "----\r\n layout: default \r\n-----<html><head><title>{{ site.title }}</title>{{}</head><body>{{ content }}</body></html>";
private const string PageContents = "---\r\n layout: default \r\n---\r\n\r\n## Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
Context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
}
[Fact]
public void Layout_With_Bad_Header_Should_Throw_Exception()
{
var exception = Record.Exception(() => Subject.Process(Context));
Assert.IsType(typeof(PageProcessingException), exception);
Assert.Equal(@"Failed to process layout default for C:\website\_site\index.html, see inner exception for more details", exception.Message);
Assert.False(FileSystem.AllFiles.Contains(@"C:\website\_site\index.html"));
}
}
public class Given_Site_Comport_Bad_Formated_Layout_But_Skip_File_Error : BakingEnvironment<LiquidEngine>
{
private SiteContext Context;
private const string TemplateContents = "----\r\n layout: default \r\n-----<html><head><title>{{ site.title }}</title>{{}</head><body>{{ content }}</body></html>";
private const string PageContents = "---\r\n layout: default \r\n---\r\n\r\n## Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_layouts\default.html", new MockFileData(TemplateContents));
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
Context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
}
[Fact]
public void Layout_With_Bad_Header_Should_Not_Throw_Exception()
{
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
Subject.Process(Context, true);
Assert.Equal(@"Failed to process layout default for C:\website\_site\index.html because 'Variable '{{}' was not properly terminated with regexp: (?-mix:\}\})'. Skipping file" + Environment.NewLine, sw.ToString());
}
Assert.False(FileSystem.AllFiles.Contains(@"C:\website\_site\index.html"));
}
}
public class Given_Page_Is_Bad_Formated : BakingEnvironment<LiquidEngine>
{
private SiteContext Context;
private const string BadFormatPageContents = "---\r\n layout: default \r\n---\r\n\r\n## Hello World! {{}";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\BadFormat.md", new MockFileData(BadFormatPageContents));
var generator = GetSiteContextGenerator(FileSystem);
Context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
}
[Fact]
public void Layout_With_Bad_Header_Should_Throw_Exception()
{
var exception = Record.Exception(() => Subject.Process(Context));
Assert.IsType(typeof(PageProcessingException), exception);
Assert.Equal(@"Failed to process C:\website\_site\BadFormat.html, see inner exception for more details", exception.Message);
Assert.False(FileSystem.AllFiles.Contains(@"C:\website\_site\BadFormat.html"));
}
}
public class Given_Page_Is_Bad_Formated_But_Skip_File_Error : BakingEnvironment<LiquidEngine>
{
private SiteContext Context;
private const string BadFormatPageContents = "---\r\n layout: default \r\n---\r\n\r\n## Hello World! {{}";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\BadFormat.md", new MockFileData(BadFormatPageContents));
var generator = GetSiteContextGenerator(FileSystem);
Context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
}
[Fact]
public void File_With_Bad_Liquid_Format_Should_Be_Traced()
{
const string ExpectedStartFile = @"Failed to process C:\website\_site\BadFormat.html: DotLiquid.Exceptions.SyntaxException: Variable '{{}' was not properly terminated with regexp: (?-mix:\}\})";
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
Subject.Process(Context, true);
Assert.StartsWith(ExpectedStartFile, sw.ToString());
}
Assert.False(FileSystem.AllFiles.Contains(@"C:\website\_site\BadFormat.html"));
}
}
public class Given_Older_Non_Processed_Page_Already_Exists_In_Output_Directory : BakingEnvironment<LiquidEngine>
{
private const string OriginalPageContents = "## Hello Earth!";
private const string PageContents = "## Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_site\BadFormat.md", new MockFileData(OriginalPageContents) { LastWriteTime = new DateTime(2010, 01, 2) });
FileSystem.AddFile(@"C:\website\BadFormat.md", new MockFileData(PageContents) { LastWriteTime = new DateTime(2010, 01, 3) });
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Existing_File_Should_Be_Replaced()
{
Assert.Equal(PageContents, FileSystem.File.ReadAllText(@"C:\website\_site\BadFormat.md").RemoveWhiteSpace());
}
}
public class Given_Newer_Non_Processed_Page_Already_Exists_In_Output_Directory : BakingEnvironment<LiquidEngine>
{
private const string OriginalPageContents = "## Hello Earth!";
private const string PageContents = "## Hello World!";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\_site\BadFormat.md", new MockFileData(OriginalPageContents) { LastWriteTime = new DateTime(2011, 01, 5) });
FileSystem.AddFile(@"C:\website\BadFormat.md", new MockFileData(PageContents) { LastWriteTime = new DateTime(2010, 01, 3) });
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Existing_File_Should_Be_Not_Replaced()
{
Assert.Equal(OriginalPageContents, FileSystem.File.ReadAllText(@"C:\website\_site\BadFormat.md").RemoveWhiteSpace());
}
}
public class Given_Site_Engine_Is_Liquid : BakingEnvironment<LiquidEngine>
{
private SiteContext context;
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
context = new SiteContext();
context.Config = new ConfigurationMock(new Dictionary<string, object> { { "pretzel", new Dictionary<string, object> { { "engine", "liquid" } } } });
}
[Fact]
public void CanProcess_Should_Return_True()
{
Assert.True(Subject.CanProcess(context));
}
}
public class Given_Site_Engine_Is_Not_Liquid : BakingEnvironment<LiquidEngine>
{
private SiteContext context;
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
context = new SiteContext();
var configMock = Substitute.For<IConfiguration>();
configMock["pretzel"].Returns(new Dictionary<string, object> { { "engine", "myengine" } });
context.Config = configMock;
}
[Fact]
public void CanProcess_Should_Return_False()
{
Assert.False(Subject.CanProcess(context));
}
}
public class Given_Page_Use_PrettifyUrlFilter : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\r\n layout: nill \r\n---\r\n\r\n{{ 'http://mysite.com/index.html' | prettify_url}}";
private const string ExpectedfileContents = "http://mysite.com/";
public override LiquidEngine Given()
{
return new LiquidEngine { Filters = new IFilter[] { new PrettifyUrlFilter() } };
}
public override void When()
{
FileSystem.AddFile(@"C:\website\index.html", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Be_Prettifyed()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Page_Has_Comment : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\r\n layout: nil \r\n---\r\n\r\n## Hello World!{% comment %} This is a comment {% endcomment %}";
private const string ExpectedfileContents = "<h2>Hello World!</h2>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Not_Have_The_Comment()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Page_Has_PostUrlBlock : BakingEnvironment<LiquidEngine>
{
private const string PageContents = "---\r\n layout: nil \r\n---\r\n\r\n<p>{% post_url index.md %}</p>";
private const string ExpectedfileContents = "<p>/index.html</p>";
public override LiquidEngine Given()
{
return new LiquidEngine();
}
public override void When()
{
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(PageContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.TagFactories = new List<TagFactoryBase> { new PostUrlTagFactory() };
Subject.Process(context);
}
[Fact]
public void The_Output_Should_Have_Been_Transformed()
{
Assert.Equal(ExpectedfileContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Page_Has_Excerpt : BakingEnvironment<LiquidEngine>
{
private const string IndexContents = "---\r\n title: index\r\n show: true \r\n---\r\n\r\n{% for post in site.posts %}{{ post.excerpt }}{% endfor %}";
private const string PostContents = "---\r\n layout: nil\r\n title: post \r\n---\r\n\r\n<p>One {{ page.title }}<!--more-->Two</p>";
private const string ExpectedIndexContents = "<p>One post</p>";
public override LiquidEngine Given()
{
var engine = new LiquidEngine();
engine.Initialize();
return engine;
}
public override void When()
{
FileSystem.AddFile(@"C:\website\index.md", new MockFileData(IndexContents));
FileSystem.AddFile(@"C:\website\_posts\2015-02-03-post.md", new MockFileData(PostContents));
var generator = GetSiteContextGenerator(FileSystem);
var context = generator.BuildContext(@"C:\website\", @"C:\website\_site", false);
Subject.FileSystem = FileSystem;
Subject.Process(context);
}
[Fact]
public void Posts_Should_Have_Excerpt()
{
Assert.Equal(ExpectedIndexContents, FileSystem.File.ReadAllText(@"C:\website\_site\index.html").RemoveWhiteSpace());
}
}
public class Given_Page_Has_Excerpt_With_Layout : BakingEnvironment<LiquidEngine>