|
| 1 | +using NSubstitute; |
| 2 | +using Pretzel.Logic.Extensibility; |
| 3 | +using Pretzel.Logic.Extensibility.Extensions; |
| 4 | +using Pretzel.Logic.Extensions; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Globalization; |
| 8 | +using System.IO; |
| 9 | +using System.IO.Abstractions; |
| 10 | +using System.IO.Abstractions.TestingHelpers; |
| 11 | +using System.Linq; |
| 12 | +using System.Text; |
| 13 | +using System.Threading; |
| 14 | +using Xunit; |
| 15 | +using Xunit.Extensions; |
| 16 | + |
| 17 | +namespace Pretzel.Tests.Recipe |
| 18 | +{ |
| 19 | + public class IngredientTests |
| 20 | + { |
| 21 | + private MockFileSystem fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); |
| 22 | + private const string BaseSite = @"c:\site\"; |
| 23 | + private const string PostsFolder = @"_posts"; |
| 24 | + private const string DraftsFolder = @"_drafts"; |
| 25 | + |
| 26 | + private readonly StringBuilder sb = new StringBuilder(); |
| 27 | + private readonly TextWriter writer; |
| 28 | + |
| 29 | + public IngredientTests() |
| 30 | + { |
| 31 | + writer = new StringWriter(sb); |
| 32 | + Tracing.Logger.SetWriter(writer); |
| 33 | + Tracing.Logger.AddCategory(Tracing.Category.Info); |
| 34 | + Tracing.Logger.AddCategory(Tracing.Category.Error); |
| 35 | + Tracing.Logger.AddCategory(Tracing.Category.Debug); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Post_Is_Created() |
| 40 | + { |
| 41 | + fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); |
| 42 | + var postTitle = "Post title"; |
| 43 | + var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); |
| 44 | + |
| 45 | + var ingredient = new Logic.Recipe.Ingredient(fileSystem, postTitle, BaseSite, false); |
| 46 | + ingredient.Create(); |
| 47 | + |
| 48 | + Assert.True(fileSystem.File.Exists(fileSystem.Path.Combine(BaseSite + PostsFolder, postName))); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void Post_Has_Correct_Content() |
| 53 | + { |
| 54 | + fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); |
| 55 | + var postTitle = "Post title"; |
| 56 | + var expectedContent = string.Format("---\r\n layout: post \r\n title: {0}\r\n comments: true\r\n---\r\n", postTitle); |
| 57 | + var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); |
| 58 | + |
| 59 | + var ingredient = new Logic.Recipe.Ingredient(fileSystem, "Post title", BaseSite, false); |
| 60 | + ingredient.Create(); |
| 61 | + |
| 62 | + Assert.Equal(expectedContent, fileSystem.File.ReadAllText(fileSystem.Path.Combine(BaseSite + PostsFolder, postName))); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Post_Already_Exists() |
| 67 | + { |
| 68 | + fileSystem.Directory.CreateDirectory(BaseSite + PostsFolder); |
| 69 | + var postTitle = "Post title"; |
| 70 | + var postName = string.Format("{0}-{1}.md", DateTime.Today.ToString("yyyy-MM-dd"), SlugifyFilter.Slugify(postTitle)); |
| 71 | + |
| 72 | + var ingredient = new Logic.Recipe.Ingredient(fileSystem, postTitle, BaseSite, false); |
| 73 | + ingredient.Create(); |
| 74 | + ingredient.Create(); |
| 75 | + |
| 76 | + Assert.Contains(string.Format("The \"{0}\" file already exists", postName), writer.ToString()); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void Post_Folder_Not_Found() |
| 81 | + { |
| 82 | + var ingredient = new Logic.Recipe.Ingredient(fileSystem, string.Empty, BaseSite, false); |
| 83 | + ingredient.Create(); |
| 84 | + |
| 85 | + Assert.Contains(string.Format(@"{0} folder not found", BaseSite + PostsFolder), writer.ToString()); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Draft_Folder_Not_Found() |
| 90 | + { |
| 91 | + var ingredient = new Logic.Recipe.Ingredient(fileSystem, string.Empty, BaseSite, true); |
| 92 | + ingredient.Create(); |
| 93 | + |
| 94 | + Assert.Contains(string.Format(@"{0} folder not found", BaseSite + DraftsFolder), writer.ToString()); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments