Skip to content

Commit

Permalink
Merge branch 'master' of github.com:stefanforsberg/WhiteMagic
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanforsberg committed Aug 26, 2013
2 parents 0fb459f + e48e49c commit 7977909
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions src/WhiteMagic.Tests/ContentRepository/when_getting_a_page.cs
Original file line number Diff line number Diff line change
@@ -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<StartPage>(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<TypeMismatchException>(() =>ContentRepository.Get<StandardPage>(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<PageNotFoundException>(() => ContentRepository.Get<StandardPage>(new ContentReference(1000)));
}
}
}
15 changes: 13 additions & 2 deletions src/WhiteMagic.Tests/InMemoryContentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public T Get<T>(Guid contentGuid, ILanguageSelector selector) where T : IContent

public T Get<T>(ContentReference contentLink) where T : IContentData
{
return EnsureTOrNull<T>(GetPage(contentLink.ToPageReference()));
return EnsureTOrThrow<T>(GetPage(contentLink.ToPageReference()));
}

public T Get<T>(ContentReference contentLink, ILanguageSelector selector) where T : IContentData
{
return EnsureTOrNull<T>(GetPage(contentLink.ToPageReference(), selector));
return EnsureTOrThrow<T>(GetPage(contentLink.ToPageReference(), selector));
}

public IEnumerable<T> GetChildren<T>(ContentReference contentLink) where T : IContentData
Expand Down Expand Up @@ -419,10 +419,21 @@ private void InitBlocks(object page)
}

//Helper method to cast from PageData to T (if possible else null)

private T EnsureTOrNull<T>(PageData page)
{
return new[] { page }.OfType<T>().FirstOrDefault();
}

private T EnsureTOrThrow<T>(PageData page)
{
if (page is T)
{
return EnsureTOrNull<T>(page);
};

throw new TypeMismatchException();
}
}

}
1 change: 1 addition & 0 deletions src/WhiteMagic.Tests/WhiteMagic.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<ItemGroup>
<Compile Include="ContentExtensions.cs" />
<Compile Include="ContentRepositoryExtensions.cs" />
<Compile Include="ContentRepository\when_getting_a_page.cs" />
<Compile Include="ContentRepository\when_getting_children_using_startindex_and_maxrows.cs" />
<Compile Include="InMemoryContentRepository.cs" />
<Compile Include="InMemoryPermanentLinkMapper.cs" />
Expand Down

0 comments on commit 7977909

Please sign in to comment.