Skip to content

Commit

Permalink
Add tests for DateTime behaviour, fix typo (#84)
Browse files Browse the repository at this point in the history
Fixes #63.
  • Loading branch information
yaakov-h authored Nov 7, 2023
1 parent e756e31 commit 2776521
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
42 changes: 42 additions & 0 deletions ValveKeyValue/ValveKeyValue.Test/KVDateTimeTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using NUnit.Framework;

namespace ValveKeyValue.Test
{
class KVDateTimeTestCase
{
[Test]
public void InvalidCast()
{
var obj = new KVObject("test", "some value that could be a date");
Assert.That(
() => obj.Value.ToDateTime(default),
Throws.InstanceOf<InvalidCastException>());
}

[Test]
public void DeserializeDateTimeNotSupported()
{
var obj = new KVObject("test", new[]
{
new KVObject("Value", "some value that could be a date")
});
using var ms = new MemoryStream();
var serializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);

serializer.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);

Assert.That(
() => serializer.Deserialize<SerializedType>(ms),
Throws.InstanceOf<NotSupportedException>()
.With.Message.EqualTo("Converting to DateTime is not supported. (key = Value, type = String)"));
}

class SerializedType
{
public DateTime Value { get;set; }
}
}
}
5 changes: 1 addition & 4 deletions ValveKeyValue/ValveKeyValue/KVObjectValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public override TypeCode GetTypeCode()

public override char ToChar(IFormatProvider provider) => (char)Convert.ChangeType(value, typeof(char), provider);

public override DateTime ToDateTime(IFormatProvider provider)
{
throw new InvalidCastException("Casting to DateTime is not supported.");
}
public override DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Casting to DateTime is not supported.");

public override decimal ToDecimal(IFormatProvider provider) => (decimal)Convert.ChangeType(value, typeof(decimal), provider);

Expand Down
3 changes: 1 addition & 2 deletions ValveKeyValue/ValveKeyValue/ObjectCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static TObject MakeObject<TObject>(KVObject keyValueObject, IObjectReflec
else
{
// TODO: For nullable types this typeof is not that useful
throw new NotSupportedException($"Convering to {typeof(TObject).Name} is not supported. (key = {keyValueObject.Name}, type = {keyValueObject.Value.ValueType})");
throw new NotSupportedException($"Converting to {typeof(TObject).Name} is not supported. (key = {keyValueObject.Name}, type = {keyValueObject.Value.ValueType})");
}
}

Expand Down Expand Up @@ -414,7 +414,6 @@ static bool CanConvertValueTo(Type type)
type == typeof(bool) ||
type == typeof(byte) ||
type == typeof(char) ||
// type == typeof(DateTime) || // TODO: Casting to DateTime (from int32) is not supported
type == typeof(decimal) ||
type == typeof(double) ||
type == typeof(float) ||
Expand Down

0 comments on commit 2776521

Please sign in to comment.