mirror of https://github.com/Squidex/squidex.git
7 changed files with 212 additions and 29 deletions
@ -0,0 +1,107 @@ |
|||
// ==========================================================================
|
|||
// PropertiesTypeAccessorTest.cs
|
|||
// PinkParrot Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) PinkParrot Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
// ReSharper disable ValueParameterNotUsed
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using Xunit; |
|||
|
|||
namespace PinkParrot.Infrastructure.Reflection |
|||
{ |
|||
public class PropertiesTypeAccessorTest |
|||
{ |
|||
public class TestClass |
|||
{ |
|||
private int target; |
|||
|
|||
public int ReadWrite |
|||
{ |
|||
get { return target; } |
|||
set { target = value; } |
|||
} |
|||
|
|||
public int Read |
|||
{ |
|||
get { return target; } |
|||
} |
|||
|
|||
public int Write |
|||
{ |
|||
set { target = value; } |
|||
} |
|||
} |
|||
|
|||
private readonly TestClass target = new TestClass(); |
|||
private readonly PropertiesTypeAccessor accessor = PropertiesTypeAccessor.Create(typeof(TestClass)); |
|||
|
|||
[Fact] |
|||
public void Should_provide_properties() |
|||
{ |
|||
var properties = accessor.Properties.Select(x => x.Name).ToArray(); |
|||
|
|||
Assert.Equal(new[] { "ReadWrite", "Read", "Write" }, properties); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_set_read_write_property() |
|||
{ |
|||
accessor.SetValue(target, "ReadWrite", 123); |
|||
|
|||
Assert.Equal(123, target.Read); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_set_write_property() |
|||
{ |
|||
accessor.SetValue(target, "Write", 123); |
|||
|
|||
Assert.Equal(123, target.Read); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_setting_unknown_property() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => accessor.SetValue(target, "Unknown", 123)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_setting_readonly() |
|||
{ |
|||
Assert.Throws<NotSupportedException>(() => accessor.SetValue(target, "Read", 123)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_get_read_write_property() |
|||
{ |
|||
target.Write = 123; |
|||
|
|||
Assert.Equal(123, accessor.GetValue(target, "ReadWrite")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_get_read_property() |
|||
{ |
|||
target.Write = 123; |
|||
|
|||
Assert.Equal(123, accessor.GetValue(target, "Read")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_getting_unknown_property() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => accessor.GetValue(target, "Unknown")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_getting_readonly() |
|||
{ |
|||
Assert.Throws<NotSupportedException>(() => accessor.GetValue(target, "Write")); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace PinkParrot.Infrastructure |
|||
{ |
|||
public class TypeNameRegistryTests |
|||
{ |
|||
[TypeName("my")] |
|||
public sealed class MyType |
|||
{ |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_register_and_retrieve_types() |
|||
{ |
|||
TypeNameRegistry.Map(typeof(int), "number"); |
|||
|
|||
Assert.Equal("number", TypeNameRegistry.GetName<int>()); |
|||
Assert.Equal("number", TypeNameRegistry.GetName(typeof(int))); |
|||
|
|||
Assert.Equal(typeof(int), TypeNameRegistry.GetType("number")); |
|||
Assert.Equal(typeof(int), TypeNameRegistry.GetType("Number")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_register_from_assembly() |
|||
{ |
|||
TypeNameRegistry.Map(typeof(TypeNameRegistryTests).GetTypeInfo().Assembly); |
|||
|
|||
Assert.Equal("my", TypeNameRegistry.GetName<MyType>()); |
|||
Assert.Equal("my", TypeNameRegistry.GetName(typeof(MyType))); |
|||
|
|||
Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("my")); |
|||
Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("My")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_type_is_already_registered() |
|||
{ |
|||
TypeNameRegistry.Map(typeof(long), "long"); |
|||
|
|||
Assert.Throws<ArgumentException>(() => TypeNameRegistry.Map(typeof(long), "longer")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_name_is_already_registered() |
|||
{ |
|||
TypeNameRegistry.Map(typeof(short), "short"); |
|||
|
|||
Assert.Throws<ArgumentException>(() => TypeNameRegistry.Map(typeof(byte), "short")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_name_is_not_supported() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetType("unsupported")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_if_type_is_not_supported() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetName<Guid>()); |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
// ==========================================================================
|
|||
// HideAttribute.cs
|
|||
// PinkParrot Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) PinkParrot Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace PinkParrot.Infrastructure |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Property)] |
|||
public class HideAttribute : Attribute |
|||
{ |
|||
} |
|||
} |
|||
Loading…
Reference in new issue