New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HandlebarsConverter test #56
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using FluentAssertions; | ||
using Sitegen.Extensions; | ||
using Sitegen.Models.Config; | ||
using Sitegen.Services; | ||
using Xunit; | ||
|
||
namespace Sitegen.Tests | ||
{ | ||
public class HandlebarsConverterTest | ||
{ | ||
private static readonly HandlebarsConverter Subject; | ||
|
||
static HandlebarsConverterTest() | ||
{ | ||
Subject = new HandlebarsConverter(new TopLevelConfig()); | ||
} | ||
|
||
// | ||
// Handlebars helpers tests | ||
// | ||
|
||
[Fact] | ||
void include_includes_given_file() | ||
{ | ||
string result = Subject.Convert(@" | ||
Content before include | ||
{{include 'fixtures/include.hbs'}} | ||
Content after include | ||
".TrimLines()).Trim(); | ||
|
||
result.Should().Be( | ||
"Content before include\n" + | ||
"Content from include file\n" + | ||
"\n" + // This extra newline comes from the newline in the {{include}} line above. | ||
"Content after include"); | ||
} | ||
|
||
[Fact] | ||
void markdown_renders_inline_markdown_content() | ||
{ | ||
string result = Subject.Convert(@" | ||
{{#markdown}} | ||
This is **Markdown** content. Way cool, eh? | ||
{{/markdown}} | ||
".TrimLines()).Trim(); | ||
|
||
result.Should().Be("<p>This is <strong>Markdown</strong> content. Way cool, eh?</p>"); | ||
} | ||
|
||
[Fact] | ||
void set_variable_can_be_accessed_later_in_template() | ||
{ | ||
string result = Subject.Convert(@" | ||
{{set foo='bar'}} | ||
Value of foo is {{foo}} | ||
".TrimLines()).Trim(); | ||
|
||
result.Should().Be("Value of foo is bar"); | ||
} | ||
|
||
[Fact] | ||
void ifeq_executes_block_if_condition_is_truthy() | ||
{ | ||
string result = Subject.Convert(@" | ||
{{set language='en'}} | ||
{{#ifeq language 'en'}}Language is English{{/ifeq}} | ||
{{#ifeq language 'sv'}}Language is Swedish{{/ifeq}} | ||
".TrimLines()).Trim(); | ||
|
||
result.Should().Be("Language is English"); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,78 +6,88 @@ | |
|
||
namespace Sitegen.Tests | ||
{ | ||
public static class ReadConfig | ||
public static class ProgramTest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Added missing outer class wrapping here, to make it clearer that these tests are testing the |
||
{ | ||
public class WithEmptyConfig | ||
public static class ReadConfig | ||
{ | ||
[Fact] | ||
void throws_expected_exception() | ||
public class WithEmptyConfig | ||
{ | ||
Action action = () => Program.ReadConfig("fixtures/empty_config.yaml"); | ||
[Fact] | ||
void throws_expected_exception() | ||
{ | ||
Action action = () => Program.ReadConfig("fixtures/empty_config.yaml"); | ||
|
||
action.Should() | ||
.Throw<ConfigurationException>() | ||
.WithMessage("config.yaml does not contain any settings"); | ||
action.Should() | ||
.Throw<ConfigurationException>() | ||
.WithMessage("config.yaml does not contain any settings"); | ||
} | ||
} | ||
} | ||
|
||
public class WithNonExistentConfig | ||
{ | ||
[Fact] | ||
void throws_expected_exception() | ||
public class WithNonExistentConfig | ||
{ | ||
Action action = () => Program.ReadConfig("fixtures/non_existent_config.yaml"); | ||
[Fact] | ||
void throws_expected_exception() | ||
{ | ||
Action action = () => Program.ReadConfig("fixtures/non_existent_config.yaml"); | ||
|
||
action.Should() | ||
.Throw<ConfigurationException>() | ||
.WithMessage("fixtures/non_existent_config.yaml does not exist"); | ||
action.Should() | ||
.Throw<ConfigurationException>() | ||
.WithMessage("fixtures/non_existent_config.yaml does not exist"); | ||
} | ||
} | ||
} | ||
|
||
public class ConfigWithEmptyDictionaries | ||
{ | ||
private static readonly TopLevelConfig TopLevelConfig = Program.ReadConfig("fixtures/config_with_empty_dictionaries.yaml"); | ||
|
||
[Fact] | ||
void creates_a_non_null_TopLevelConfig_Config() | ||
public class ConfigWithEmptyDictionaries | ||
{ | ||
Assert.NotNull(TopLevelConfig.Config); | ||
} | ||
private static readonly TopLevelConfig TopLevelConfig = | ||
Program.ReadConfig("fixtures/config_with_empty_dictionaries.yaml"); | ||
|
||
[Fact] | ||
void creates_a_Config_with_the_expected_SourceDir() | ||
{ | ||
Assert.Equal("src", TopLevelConfig.Config.SourceDir); | ||
} | ||
[Fact] | ||
void creates_a_non_null_TopLevelConfig_Config() | ||
{ | ||
TopLevelConfig.Config.Should().NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
void creates_a_Config_with_the_expected_LayoutsDir() | ||
{ | ||
Assert.Equal("src/_layouts", TopLevelConfig.Config.LayoutsDir); | ||
} | ||
[Fact] | ||
void creates_a_Config_with_the_expected_SourceDir() | ||
{ | ||
TopLevelConfig.Config.SourceDir | ||
.Should().Be("src"); | ||
} | ||
|
||
[Fact] | ||
void creates_a_Config_with_the_expected_OutputDir() | ||
{ | ||
Assert.Equal("out", TopLevelConfig.Config.OutputDir); | ||
} | ||
[Fact] | ||
void creates_a_Config_with_the_expected_LayoutsDir() | ||
{ | ||
TopLevelConfig.Config.LayoutsDir | ||
.Should().Be("src/_layouts"); | ||
} | ||
|
||
[Fact] | ||
void creates_a_Config_with_the_expected_PostDir () | ||
{ | ||
Assert.Equal("src/_posts", TopLevelConfig.Config.PostsDir); | ||
} | ||
[Fact] | ||
void creates_a_Config_with_the_expected_OutputDir() | ||
{ | ||
TopLevelConfig.Config.OutputDir | ||
.Should().Be("out"); | ||
} | ||
|
||
[Fact] | ||
void creates_a_Config_with_the_expected_LineBreaks_setting () | ||
{ | ||
Assert.Equal(LineBreaks.Hard, TopLevelConfig.Config.LineBreaks); | ||
} | ||
[Fact] | ||
void creates_a_Config_with_the_expected_PostDir() | ||
{ | ||
TopLevelConfig.Config.PostsDir | ||
.Should().Be("src/_posts"); | ||
} | ||
|
||
[Fact] | ||
void creates_a_TopLevelConfig_with_a_non_null_Site() | ||
{ | ||
Assert.NotNull(TopLevelConfig.Site); | ||
[Fact] | ||
void creates_a_Config_with_the_expected_LineBreaks_setting() | ||
{ | ||
TopLevelConfig.Config.LineBreaks | ||
.Should().Be(LineBreaks.Hard); | ||
} | ||
|
||
[Fact] | ||
void creates_a_TopLevelConfig_with_a_non_null_Site() | ||
{ | ||
TopLevelConfig.Site | ||
.Should().NotBeNull(); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Content from include file |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Sitegen.Extensions | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static string TrimLines(this string str) | ||
{ | ||
return String.Join( | ||
"\n", | ||
str.Split('\n').Select(s => s.Trim())); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,11 @@ public class Config : IDeserialized | |
public string LayoutsDir { get; set; } | ||
public string OutputDir { get; set; } | ||
public string PostsDir { get; set; } | ||
public LineBreaks? LineBreaks { get; set; } | ||
|
||
// Enabling "soft line breaks as hard" is currently the default. Can be opted out by individual blog posts | ||
// as needed. | ||
public LineBreaks LineBreaks { get; set; } = Models.LineBreaks.Hard; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I inlined the default value here, making it possible to make this field non-nullable. 馃帀 |
||
public bool MultipleLanguages { get; set; } | ||
|
||
public void OnDeserialized() | ||
|
@@ -17,10 +21,6 @@ public void OnDeserialized() | |
LayoutsDir ??= Path.Join(SourceDir, "_layouts"); | ||
OutputDir ??= "out"; | ||
PostsDir ??= "src/_posts"; | ||
|
||
// Enabling "soft line breaks as hard" is currently the default. Can be opted out by individual blog posts | ||
// as needed. | ||
LineBreaks ??= Models.LineBreaks.Hard; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Unrelated change. This was missed in #54, causing the project to be treated as an F# project in some regards, triggering various weird semantics in JetBrains Rider. (Right-click to add new file would provide incorrect options, and project directories would not be properly displayed in Solution Explorer) Fixing the project type GUID here seems to fix the problem.)