Permalink
Browse files

Fix the support for different bundle types that share the same path.

  • Loading branch information...
andrewdavey committed Jan 3, 2012
1 parent 54cbc5b commit 137493a0f76099f74e8fa57498be2c6008df9b55
@@ -82,6 +82,12 @@
<Content Include="assets\templates\asset-2.htm">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
+ <Content Include="overlap\widget.css">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="overlap\widget.js">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cassette.Web\Cassette.Web.csproj">
@@ -8,6 +8,7 @@
using Cassette.HtmlTemplates;
using Cassette.IO;
using Cassette.Scripts;
+using Cassette.Stylesheets;
using Cassette.Web;
using Moq;
using Should;
@@ -111,16 +112,40 @@ public void HtmlTemplatesCanBeRenderedInlineInPage()
}
}
- CassetteApplication CreateApplication(Action<BundleCollection> configure)
+ [Fact]
+ public void GivenReferenceToBundlesWithSharedPath_WhenRenderEachType_ThenHtmlIsReturnedForEach()
+ {
+ Action<BundleCollection> config = bundles =>
+ {
+ bundles.Add<StylesheetBundle>("~/");
+ bundles.Add<ScriptBundle>("~/");
+ };
+
+ using (var app = CreateApplication(config, "overlap"))
+ {
+ app.OnPostMapRequestHandler();
+ var builder = app.GetReferenceBuilder();
+
+ builder.Reference("~");
+ var scriptHtml = builder.Render<ScriptBundle>(null);
+ var stylesheetHtml = builder.Render<StylesheetBundle>(null);
+
+ stylesheetHtml.ShouldContain("<link");
+ scriptHtml.ShouldContain("<script");
+ }
+ }
+
+ CassetteApplication CreateApplication(Action<BundleCollection> configure, string sourceDirectory = "assets")
{
+ IBundleContainer bundleContainer = null;
var settings = new CassetteSettings("")
{
CacheDirectory = new IsolatedStorageDirectory(storage),
- SourceDirectory = new FileSystemDirectory(Path.GetFullPath("assets"))
+ SourceDirectory = new FileSystemDirectory(Path.GetFullPath(sourceDirectory)),
+ UrlGenerator = new CassetteRouting(new VirtualDirectoryPrepender("/"), () => bundleContainer)
};
var bundles = new BundleCollection(settings);
configure(bundles);
- IBundleContainer bundleContainer = null;
var application = new CassetteApplication(
bundles,
settings,
@@ -0,0 +1 @@
+/* File Created: January 3, 2012 */
@@ -0,0 +1 @@
+
@@ -50,7 +50,7 @@ public void FindBundleContainingPathOfBundleReturnsTheBundle()
var container = new BundleContainer(new[] {
expectedBundle
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/test");
+ var actualBundle = container.FindBundlesContainingPath("~/test").First();
actualBundle.ShouldBeSameAs(expectedBundle);
}
@@ -60,8 +60,8 @@ public void FindBundleContainingPathWithWrongPathReturnsNull()
var container = new BundleContainer(new[] {
new TestableBundle("~/test")
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/WRONG");
- actualBundle.ShouldBeNull();
+ var actualBundle = container.FindBundlesContainingPath("~/WRONG");
+ actualBundle.ShouldBeEmpty();
}
[Fact]
@@ -76,7 +76,7 @@ public void FindBundleContainingPathOfAssetReturnsTheBundle()
var container = new BundleContainer(new[] {
expectedBundle
});
- var actualBundle = container.FindBundleContainingPath<Bundle>("~/test/test.js");
+ var actualBundle = container.FindBundlesContainingPath("~/test/test.js").First();
actualBundle.ShouldBeSameAs(expectedBundle);
}
@@ -22,8 +22,8 @@ public void WhenCreateWithBundles_ThenItReturnsContainerWithBundles()
var builder = CreateFactory(new Dictionary<Type, IBundleFactory<Bundle>>());
var container = builder.Create(bundles, CreateSettings());
- container.FindBundleContainingPath<Bundle>("~/test1").ShouldBeSameAs(bundle1);
- container.FindBundleContainingPath<Bundle>("~/test2").ShouldBeSameAs(bundle2);
+ container.FindBundlesContainingPath("~/test1").First().ShouldBeSameAs(bundle1);
+ container.FindBundlesContainingPath("~/test2").First().ShouldBeSameAs(bundle2);
}
[Fact]
@@ -54,7 +54,7 @@ public void WhenCreateWithBundleHavingExternalReference_ThenAnExternalBundleIsAl
var builder = CreateFactory(factories);
var container = builder.Create(new[] { bundle }, CreateSettings());
- container.FindBundleContainingPath<Bundle>("http://external.com/api.js").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://external.com/api.js").First().ShouldBeSameAs(externalBundle);
}
[Fact]
@@ -99,7 +99,7 @@ public void GivenAssetWithUrlReference_WhenCreate_ThenExternalBundleInContainer(
var settings = CreateSettings();
var container = containerFactory.Create(new[] { bundle }, settings);
- container.FindBundleContainingPath<Bundle>("http://test.com/").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://test.com/").First().ShouldBeSameAs(externalBundle);
}
[Fact]
@@ -123,7 +123,7 @@ public void GivenAssetWithUrlReferenceAndSameBundleLevelUrlReference_WhenCreate_
var settings = CreateSettings();
var container = containerFactory.Create(new[] { bundle }, settings);
- container.FindBundleContainingPath<Bundle>("http://test.com/").ShouldBeSameAs(externalBundle);
+ container.FindBundlesContainingPath("http://test.com/").First().ShouldBeSameAs(externalBundle);
}
[Fact]
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using Cassette.Persistence;
using Moq;
using Should;
@@ -20,7 +21,7 @@ public void GivenCacheIsUpToDate_WhenCreateWithBundle_ThenContainerCreatedWithTh
var factory = CreateFactory();
var container = factory.Create(bundles, CreateSettings());
- container.FindBundleContainingPath<Bundle>("~/test").ShouldBeSameAs(bundles[0]);
+ container.FindBundlesContainingPath("~/test").SequenceEqual(bundles).ShouldBeTrue();
}
[Fact]
@@ -15,7 +15,7 @@ public class ReferenceBuilder_Reference_Tests
{
public ReferenceBuilder_Reference_Tests()
{
- application = new Mock<ICassetteApplication>();
+ new Mock<ICassetteApplication>();
bundleFactories = new Dictionary<Type, IBundleFactory<Bundle>>();
bundleContainer = new Mock<IBundleContainer>();
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
@@ -26,15 +26,14 @@ public ReferenceBuilder_Reference_Tests()
ReferenceBuilder builder;
readonly Mock<IBundleContainer> bundleContainer;
- readonly Mock<ICassetteApplication> application;
readonly Dictionary<Type, IBundleFactory<Bundle>> bundleFactories;
[Fact]
public void WhenAddReferenceToBundleDirectory_ThenGetBundlesReturnTheBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -50,8 +49,8 @@ public void WhenAddReferenceToBundleDirectory_ThenGetBundlesReturnTheBundle()
public void WhenAddReferenceToSameBundleTwice_ThenGetBundlesReturnsOnlyOneBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -68,8 +67,8 @@ public void WhenAddReferenceToBundleDirectoryWithLocation_ThenGetBundlesThatLoca
{
var bundle = new ScriptBundle("~/test");
bundle.PageLocation = "body";
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle })
.Verifiable();
@@ -87,10 +86,10 @@ public void OnlyBundlesMatchingLocationAreReturnedByGetBundles()
var bundle1 = new ScriptBundle("~/test1");
var bundle2 = new ScriptBundle("~/test2");
bundle1.PageLocation = "body";
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test1"))
- .Returns(bundle1);
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test2"))
- .Returns(bundle2);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test1"))
+ .Returns(new[] { bundle1 });
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test2"))
+ .Returns(new[] { bundle2 });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle1 });
builder.Reference("test1", null);
@@ -104,7 +103,7 @@ public void OnlyBundlesMatchingLocationAreReturnedByGetBundles()
[Fact]
public void WhenAddReferenceToNonExistentBundle_ThenThrowException()
{
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~\\test")).Returns((ScriptBundle)null);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~\\test")).Returns(new ScriptBundle[0]);
Assert.Throws<ArgumentException>(delegate
{
@@ -118,8 +117,8 @@ public void GivenBundleAReferencesBundleB_WhenAddReferenceToBundleA_ThenGetBundl
var bundleA = new ScriptBundle("~/a");
var bundleB = new ScriptBundle("~/b");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/a"))
- .Returns(bundleA);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/a"))
+ .Returns(new[] { bundleA });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundleB, bundleA });
@@ -222,8 +221,8 @@ public void WhenAddReferenceToUnknownUrlWithBundleTypeAndUnexpectedExtension_The
public void WhenAddReferenceWithLocation_ThenGetBundlesForThatLocationReturnsTheBundle()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
bundleContainer.Setup(c => c.IncludeReferencesAndSortBundles(It.IsAny<IEnumerable<Bundle>>()))
.Returns(new[] { bundle });
builder.Reference("test", "body");
@@ -235,8 +234,8 @@ public void WhenAddReferenceWithLocation_ThenGetBundlesForThatLocationReturnsThe
public void GivenNullLocationAlreadyRendered_WhenAddReferenceToNullLocation_ThenExceptionThrown()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>();
@@ -249,8 +248,8 @@ public void GivenNullLocationAlreadyRendered_WhenAddReferenceToNullLocation_Then
public void GivenLocationAlreadyRendered_WhenAddReferenceToThatLocation_ThenExceptionThrown()
{
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>("location");
@@ -268,14 +267,26 @@ public void GivenLocationAlreadyRenderedButHtmlRewrittingEnabled_WhenAddReferenc
new CassetteSettings("") { IsHtmlRewritingEnabled = true }
);
var bundle = new ScriptBundle("~/test");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/test"))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
builder.Render<ScriptBundle>("test");
builder.Reference("~/test", "test");
builder.GetBundles("test").First().ShouldBeSameAs(bundle);
}
+
+ [Fact]
+ public void GivenTwoBundlesWithSamePathButDifferentType_WhenReferenceThePath_ThenBothBundlesAreReferenced()
+ {
+ var bundle1 = new ScriptBundle("~/test");
+ var bundle2 = new StylesheetBundle("~/test");
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new Bundle[] { bundle1, bundle2 });
+
+ builder.Reference("~/test");
+ builder.GetBundles(null).Count().ShouldEqual(2);
+ }
}
public class ReferenceBuilder_Render_Tests
@@ -306,8 +317,8 @@ public ReferenceBuilder_Render_Tests()
public void GivenAddReferenceToPath_WhenRender_ThenBundleRenderOutputReturned()
{
var bundle = new TestableBundle("~/stub");
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(new[] { bundle });
referenceBuilder.Reference("test");
@@ -320,8 +331,8 @@ public void GivenAddReferenceToPath_WhenRender_ThenBundleRenderOutputReturned()
public void GivenAddReferenceToPath_WhenRenderWithLocation_ThenBundleRenderOutputReturned()
{
var bundle = new TestableBundle("~/stub") { RenderResult = "output" };
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(bundle);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(new[] { bundle });
referenceBuilder.Reference("test");
var html = referenceBuilder.Render<TestableBundle>("body");
@@ -334,10 +345,10 @@ public void GivenAddReferenceToTwoPaths_WhenRender_ThenBundleRenderOutputsSepara
{
var bundle1 = new TestableBundle("~/stub1") { RenderResult = "output1" };
var bundle2 = new TestableBundle("~/stub2") { RenderResult = "output2" };
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/stub1"))
- .Returns(bundle1);
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>("~/stub2"))
- .Returns(bundle2);
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/stub1"))
+ .Returns(new[] { bundle1 });
+ bundleContainer.Setup(c => c.FindBundlesContainingPath("~/stub2"))
+ .Returns(new[] { bundle2 });
referenceBuilder.Reference("~/stub1");
referenceBuilder.Reference("~/stub2");
@@ -37,9 +37,9 @@ public AssetRequestHandler_Tests()
response.SetupGet(r => r.Cache).Returns(cache.Object);
request.SetupGet(r => r.Headers).Returns(requestHeaders);
- var bundleContainer = new Mock<IBundleContainer>();
- bundleContainer.Setup(c => c.FindBundleContainingPath<Bundle>(It.IsAny<string>()))
- .Returns(() => bundle);
+ bundleContainer = new Mock<IBundleContainer>();
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(() => new[] { bundle });
handler = new AssetRequestHandler(requestContext.Object, () => bundleContainer.Object);
}
@@ -48,6 +48,7 @@ public AssetRequestHandler_Tests()
readonly Mock<HttpResponseBase> response;
readonly Mock<HttpCachePolicyBase> cache;
readonly NameValueCollection requestHeaders;
+ readonly Mock<IBundleContainer> bundleContainer;
Bundle bundle;
MemoryStream outputStream;
@@ -58,8 +59,10 @@ public void IsReusableIsFalse()
}
[Fact]
- public void GivenBundleIsNull_WhenProcessRequest_ThenNotFoundResponse()
+ public void GivenBundleNotFound_WhenProcessRequest_ThenNotFoundResponse()
{
+ bundleContainer.Setup(c => c.FindBundlesContainingPath(It.IsAny<string>()))
+ .Returns(() => new Bundle[0]);
handler.ProcessRequest(null);
response.VerifySet(r => r.StatusCode = 404);
}
@@ -64,8 +64,8 @@ protected void SetupTestBundle()
.Returns(() => "asset-content".AsStream());
asset.SetupGet(a => a.Hash).Returns(new byte[] { 1, 2, 3 });
bundle.Assets.Add(asset.Object);
- container.Setup(c => c.FindBundleContainingPath<TestableBundle>("~/test"))
- .Returns(bundle);
+ container.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
}
void IDisposable.Dispose()
@@ -84,8 +84,8 @@ public GivenBundleExists_WhenProcessRequest()
.Returns(() => "asset-content".AsStream());
asset.SetupGet(a => a.Hash).Returns(new byte[] { 1, 2, 3 });
bundle.Assets.Add(asset.Object);
- container.Setup(c => c.FindBundleContainingPath<TestableBundle>("~/test"))
- .Returns(bundle);
+ container.Setup(c => c.FindBundlesContainingPath("~/test"))
+ .Returns(new[] { bundle });
var handler = CreateRequestHandler("test_010203");
handler.ProcessRequest();
Oops, something went wrong.

0 comments on commit 137493a

Please sign in to comment.