Skip to content

Commit

Permalink
Add support for decimal and unsigned integer types in ReflectionValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi Caput committed Sep 12, 2016
1 parent 8827f01 commit 6e781b8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cottle.Test/Cottle.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
<Compile Include="src\Builtins\BuiltinOperatorsTester.cs" />
<Compile Include="src\Builtins\BuiltinTrimmersTester.cs" />
<Compile Include="src\DocumentTester.cs" />
<Compile Include="src\Values\ReflectionValueTester.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="src\Builtins" />
<Folder Include="src" />
<Folder Include="src\Values" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cottle\Cottle.csproj">
Expand Down
54 changes: 54 additions & 0 deletions Cottle.Test/src/Values/ReflectionValueTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Cottle.Values;
using NUnit.Framework;

namespace Cottle.Test.Values
{
public static class ReflectionValueTester
{
[Test]
public static void ReadMember ()
{
ReflectionValueTester.ReadMember (true, new BooleanValue (true));
ReflectionValueTester.ReadMember ((byte)4, new NumberValue ((byte)4));
ReflectionValueTester.ReadMember ((sbyte)-5, new NumberValue ((sbyte)-5));
ReflectionValueTester.ReadMember ((short)-9, new NumberValue ((short)-9));
ReflectionValueTester.ReadMember ((ushort)8, new NumberValue ((ushort)8));
ReflectionValueTester.ReadMember (42, new NumberValue (42));
ReflectionValueTester.ReadMember (42u, new NumberValue (42u));
ReflectionValueTester.ReadMember (17L, new NumberValue (17L));
ReflectionValueTester.ReadMember (24LU, new NumberValue (24L));
ReflectionValueTester.ReadMember (1f, new NumberValue (1f));
ReflectionValueTester.ReadMember (3d, new NumberValue (3d));
ReflectionValueTester.ReadMember (5m, new NumberValue (5m));
ReflectionValueTester.ReadMember ('x', new StringValue ("x"));
ReflectionValueTester.ReadMember ("abc", new StringValue ("abc"));
}

private static void ReadMember<T> (T reference, Value expected)
{
var container = new Container<T> ();
Value result;
var value = new ReflectionValue (container);

container.Field = reference;
container.Property = reference;

Assert.IsTrue (value.Fields.TryGet ("Field", out result), "reflection value must have a 'Field' key");
Assert.AreEqual (expected, result, "reflection value should be able to read field of type {0}", typeof (T));
Assert.IsTrue (value.Fields.TryGet ("Property", out result), "reflection value must have a 'Property' key");
Assert.AreEqual (expected, result, "reflection value should be able to read property of type {0}", typeof (T));
}

private class Container<T>
{
public T Field;

public T Property
{
get;
set;
}
}
}
}
5 changes: 2 additions & 3 deletions Cottle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Les informations générales relatives à un assembly dépendent de
Expand Down Expand Up @@ -32,5 +31,5 @@
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.2.1")]
[assembly: AssemblyFileVersion("1.3.2.1")]
[assembly: AssemblyVersion("1.3.2.2")]
[assembly: AssemblyFileVersion("1.3.2.2")]
9 changes: 7 additions & 2 deletions Cottle/src/Values/ReflectionValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ public sealed class ReflectionValue : ResolveValue
{typeof (byte), (s) => (byte)s},
{typeof (char), (s) => (char)s},
{typeof (double), (s) => (double)s},
{typeof (decimal), (s) => (decimal)s},
{typeof (float), (s) => (float)s},
{typeof (int), (s) => (int)s},
{typeof (long), (s) => (long)s},
{typeof (sbyte), (s) => (sbyte)s},
{typeof (short), (s) => (short)s},
{typeof (string), (s) => (string)s}
{typeof (string), (s) => (string)s},
{typeof (uint), (s) => (uint)s},
{typeof (ulong), (s) => (long)(ulong)s},
{typeof (ushort), (s) => (ushort)s}
};

private static readonly Dictionary<Type, List<MemberReader>> readers = new Dictionary<Type, List<MemberReader>> ();
Expand Down Expand Up @@ -70,7 +75,7 @@ protected override Value Resolve ()

// Return undefined value for other primitive types
#if CORECLR
if (type.GetTypeInfo().IsPrimitive)
if (type.GetTypeInfo ().IsPrimitive)
#else
if (type.IsPrimitive)
#endif
Expand Down

0 comments on commit 6e781b8

Please sign in to comment.