From 823f297ae6e92f987ffa791f9e72f17afdbb913d Mon Sep 17 00:00:00 2001 From: Stefan Forsberg Date: Wed, 20 Mar 2013 10:25:44 +0100 Subject: [PATCH 1/2] Added merge files to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 0a211aa..fe9840b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. +*.orig + # User-specific files *.suo *.user From e48e49cc6879b1dd0be30b9c5cc7224b69591ed0 Mon Sep 17 00:00:00 2001 From: Stefan Forsberg Date: Mon, 26 Aug 2013 16:51:06 +0200 Subject: [PATCH 2/2] Added tests for getting page with wrong type and by invalid content reference --- .../ContentRepository/when_getting_a_page.cs | 36 +++++++++++++++++++ .../InMemoryContentRepository.cs | 15 ++++++-- src/WhiteMagic.Tests/WhiteMagic.Tests.csproj | 1 + 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/WhiteMagic.Tests/ContentRepository/when_getting_a_page.cs diff --git a/src/WhiteMagic.Tests/ContentRepository/when_getting_a_page.cs b/src/WhiteMagic.Tests/ContentRepository/when_getting_a_page.cs new file mode 100644 index 0000000..8bb3f63 --- /dev/null +++ b/src/WhiteMagic.Tests/ContentRepository/when_getting_a_page.cs @@ -0,0 +1,36 @@ +using EPiServer.Core; +using NUnit.Framework; +using WhiteMagic.Tests.Pages; + +namespace WhiteMagic.Tests.ContentRepository +{ + public class when_getting_a_page : TestBase + { + protected PageReference StartPageReference; + + public override void Given() + { + base.Given(); + StartPageReference = ContentRepository + .Publish(ContentReference.RootPage, p => p.MainBody = "Hejhej!"); + } + } + + public class when_getting_a_page_with_wrong_type_constraint : when_getting_a_page + { + [Test] + public void it_should_throw_a_type_mismatch_exception() + { + Assert.Throws(() =>ContentRepository.Get(StartPageReference)); + } + } + + public class when_getting_a_page_by_content_reference_that_does_not_exist : when_getting_a_page + { + [Test] + public void it_should_throw_a_page_not_found_exception() + { + Assert.Throws(() => ContentRepository.Get(new ContentReference(1000))); + } + } +} diff --git a/src/WhiteMagic.Tests/InMemoryContentRepository.cs b/src/WhiteMagic.Tests/InMemoryContentRepository.cs index 8fdd2a6..f03013d 100644 --- a/src/WhiteMagic.Tests/InMemoryContentRepository.cs +++ b/src/WhiteMagic.Tests/InMemoryContentRepository.cs @@ -91,12 +91,12 @@ public T Get(Guid contentGuid, ILanguageSelector selector) where T : IContent public T Get(ContentReference contentLink) where T : IContentData { - return EnsureTOrNull(GetPage(contentLink.ToPageReference())); + return EnsureTOrThrow(GetPage(contentLink.ToPageReference())); } public T Get(ContentReference contentLink, ILanguageSelector selector) where T : IContentData { - return EnsureTOrNull(GetPage(contentLink.ToPageReference(), selector)); + return EnsureTOrThrow(GetPage(contentLink.ToPageReference(), selector)); } public IEnumerable GetChildren(ContentReference contentLink) where T : IContentData @@ -419,10 +419,21 @@ private void InitBlocks(object page) } //Helper method to cast from PageData to T (if possible else null) + private T EnsureTOrNull(PageData page) { return new[] { page }.OfType().FirstOrDefault(); } + + private T EnsureTOrThrow(PageData page) + { + if (page is T) + { + return EnsureTOrNull(page); + }; + + throw new TypeMismatchException(); + } } } \ No newline at end of file diff --git a/src/WhiteMagic.Tests/WhiteMagic.Tests.csproj b/src/WhiteMagic.Tests/WhiteMagic.Tests.csproj index 983f31c..c1a0a7b 100644 --- a/src/WhiteMagic.Tests/WhiteMagic.Tests.csproj +++ b/src/WhiteMagic.Tests/WhiteMagic.Tests.csproj @@ -153,6 +153,7 @@ +