Skip to content

Handlebars HTML template plugin. #358

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4691BC2D-A3BF-4B7A-94B3-2388B57BA19D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Cassette.Handlebars.UnitTests</RootNamespace>
<AssemblyName>Cassette.Handlebars.UnitTests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Jurassic">
<HintPath>..\packages\Jurassic.2.1.1\lib\Jurassic.dll</HintPath>
</Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="Should">
<HintPath>..\packages\Should.1.1.12.0\lib\Should.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.9.1\lib\net20\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Handlebars\HandlebarsCompiler.cs" />
<Compile Include="Handlebars\HandlebarsFileSearchModifier.cs" />
<Compile Include="Handlebars\HandlebarsPipeline.cs" />
<Compile Include="Handlebars\HandlebarsServices.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cassette.Handlebars\Cassette.Handlebars.csproj">
<Project>{235663D0-24CC-442E-AEDF-0C0DD7227166}</Project>
<Name>Cassette.Handlebars</Name>
</ProjectReference>
<ProjectReference Include="..\Cassette.UnitTests\Cassette.UnitTests.csproj">
<Project>{F200A67A-D7B5-441B-AB36-69AA5C87577E}</Project>
<Name>Cassette.UnitTests</Name>
</ProjectReference>
<ProjectReference Include="..\Cassette\Cassette.csproj">
<Project>{A5CCF9D3-5D49-4BFC-B9A6-9EC9E0E29C50}</Project>
<Name>Cassette</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="..\Cassette.Handlebars\Resources\handlebars.js">
<Link>handlebars.js</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
28 changes: 28 additions & 0 deletions src/Cassette.Handlebars.UnitTests/Handlebars/HandlebarsCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Jurassic;
using Should;
using Xunit;

namespace Cassette.HtmlTemplates
{
public class HandlebarsCompiler_Tests
{
[Fact]
public void CanCompileHandlebarsTemplate()
{
var compiler = new HandlebarsCompiler();
var result = compiler.Compile("Hello {{world}}", new CompileContext());

var engine = new ScriptEngine();
engine.ExecuteFile("handlebars.js");
var source = @"
(function(){
var template = new Handlebars.Template();
template.r = " + result.Output + @";
return template.render({world:'Andrew'});
}());";
var templateRender = engine.Evaluate<string>(source);

templateRender.ShouldEqual("Hello Andrew");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Should;
using Xunit;

namespace Cassette.HtmlTemplates
{
public class HandlebarsFileSearchModifier_Tests
{
[Fact]
public void ModifyAddsMustacheToPattern()
{
AssertPatternContains("*.handlebars");
}

[Fact]
public void ModifyAddsJstToPattern()
{
AssertPatternContains("*.hbr");
}

[Fact]
public void ModifyAddsTmplToPattern()
{
AssertPatternContains("*.hbt");
}

void AssertPatternContains(string filePattern)
{
var modifier = new HandlebarsFileSearchModifier();
var fileSearch = new FileSearch();
modifier.Modify(fileSearch);
filePattern.ShouldContain(filePattern);
}
}
}
49 changes: 49 additions & 0 deletions src/Cassette.Handlebars.UnitTests/Handlebars/HandlebarsPipeline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Cassette.TinyIoC;
using Cassette.Utilities;
using Jurassic;
using Should;
using Xunit;

namespace Cassette.HtmlTemplates
{
public class HandlebarsPipeline_Tests
{
readonly HandlebarsPipeline pipeline;

public HandlebarsPipeline_Tests()
{
var container = new TinyIoCContainer();
container.Register<IUrlModifier>(new VirtualDirectoryPrepender("/"));
container.Register<IUrlGenerator>((c, n) => new UrlGenerator(c.Resolve<IUrlModifier>(), new FakeFileSystem(), ""));
container.Register<IHtmlTemplateIdStrategy>(new HtmlTemplateIdBuilder());
pipeline = container.Resolve<HandlebarsPipeline>();
}

[Fact]
public void WhenProcessHtmlTemplateBundleWithHandlebarsAssets_ThenGeneratedScriptTemplatesWillRenderOutput()
{
var bundle = new HtmlTemplateBundle("~");
bundle.Assets.Add(new StubAsset("~/a.htm", "first {{first}}"));
bundle.Assets.Add(new StubAsset("~/b.htm", "second {{second}}"));

pipeline.Process(bundle);

var scriptEngine = LoadHtmlTemplateScriptsIntoEngine(bundle);

var renderOutputA = scriptEngine.Evaluate<string>("JST.a({ first: 'test' });");
renderOutputA.ShouldEqual("first test");

var renderOutputB = scriptEngine.Evaluate<string>("JST.b({ second: 'test' });");
renderOutputB.ShouldEqual("second test");
}

static ScriptEngine LoadHtmlTemplateScriptsIntoEngine(HtmlTemplateBundle bundle)
{
var output = bundle.OpenStream().ReadToEnd();
var scriptEngine = new ScriptEngine();
scriptEngine.ExecuteFile("handlebars.js");
scriptEngine.Execute(output);
return scriptEngine;
}
}
}
47 changes: 47 additions & 0 deletions src/Cassette.Handlebars.UnitTests/Handlebars/HandlebarsServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Cassette.BundleProcessing;
using Cassette.TinyIoC;
using Moq;
using Should;
using Xunit;

namespace Cassette.HtmlTemplates
{
public class HandlebarsServices_Tests
{
readonly TinyIoCContainer container;
readonly HandlebarsServices services;

public HandlebarsServices_Tests()
{
container = new TinyIoCContainer();
container.Register(Mock.Of<IUrlGenerator>());
container.Register<IHtmlTemplateIdStrategy>(new HtmlTemplateIdBuilder());

services = new HandlebarsServices();
}

[Fact]
public void RegistersHandlebarsPipeline()
{
services.Configure(container);

var pipeline = container.Resolve<IBundlePipeline<HtmlTemplateBundle>>();
pipeline.ShouldBeType<HandlebarsPipeline>();
}

[Fact]
public void AppliesHandlebarsSettingsConfigurations()
{
var configuration1 = new Mock<IConfiguration<HandlebarsSettings>>();
var configuration2 = new Mock<IConfiguration<HandlebarsSettings>>();
container.Register(configuration1.Object, "a");
container.Register(configuration2.Object, "b");

services.Configure(container);

var settings = container.Resolve<HandlebarsSettings>();
configuration1.Verify(c => c.Configure(settings));
configuration2.Verify(c => c.Configure(settings));
}
}
}
36 changes: 36 additions & 0 deletions src/Cassette.Handlebars.UnitTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Cassette.Handlebars.UnitTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Cassette.Handlebars.UnitTests")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c850ee2c-80e5-485f-b615-832b23ea4bf7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
7 changes: 7 additions & 0 deletions src/Cassette.Handlebars.UnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Jurassic" version="2.1.1" targetFramework="net40" />
<package id="Moq" version="4.0.10827" targetFramework="net40" />
<package id="Should" version="1.1.12.0" targetFramework="net40" />
<package id="xunit" version="1.9.1" targetFramework="net40" />
</packages>
Loading