1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Xml.Linq;
using Microsoft.TestCommon;
using Microsoft.TestCommon.Types;
namespace System.Web.Http.OData.Formatter
{
public class EdmPrimitiveHelpersTest
{
public static TheoryDataSet<object, object, Type> ConvertPrimitiveValue_NonStandardPrimitives_Data
{
get
{
return new TheoryDataSet<object, object, Type>
{
{ "1", (char)'1', typeof(char) },
{ "1", (char?)'1', typeof(char?) },
{ "123", (char[]) new char[] {'1', '2', '3' }, typeof(char[]) },
{ (int)1 , (ushort)1, typeof(ushort)},
{ (int?)1, (ushort?)1, typeof(ushort?) },
{ (long)1, (uint)1, typeof(uint) },
{ (long?)1, (uint?)1, typeof(uint?) },
{ (long)1 , (ulong)1, typeof(ulong)},
{ (long?)1 ,(ulong?)1, typeof(ulong?)},
//(Stream) new MemoryStream(new byte[] { 1 }), // TODO: Enable once we have support for streams
{ "<element xmlns=\"namespace\" />" ,(XElement) new XElement(XName.Get("element","namespace")), typeof(XElement)},
{ new byte[] {1}, new Binary(new byte[] {1}), typeof(Binary)},
// Enums
{ "Second", SimpleEnum.Second, typeof(SimpleEnum) },
{ "Second", SimpleEnum.Second, typeof(SimpleEnum?) },
{ "ThirdLong" , LongEnum.ThirdLong, typeof(LongEnum) },
{ "ThirdLong" , LongEnum.ThirdLong, typeof(LongEnum?) },
{ "One, Four" , FlagsEnum.One | FlagsEnum.Four, typeof(FlagsEnum) },
{ "One, Four" , FlagsEnum.One | FlagsEnum.Four, typeof(FlagsEnum?) }
};
}
}
[Theory]
[PropertyData("ConvertPrimitiveValue_NonStandardPrimitives_Data")]
public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType)
{
Assert.Equal(result.GetType(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType).GetType());
Assert.Equal(result.ToString(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType).ToString());
}
[Theory]
[InlineData("123")]
[InlineData("")]
public void ConvertPrimitiveValueToChar_Throws(string input)
{
Assert.Throws<ValidationException>(
() => EdmPrimitiveHelpers.ConvertPrimitiveValue(input, typeof(char)),
"The value must be a string with a length of 1.");
}
[Fact]
public void ConvertPrimitiveValueToNullableChar_Throws()
{
Assert.Throws<ValidationException>(
() => EdmPrimitiveHelpers.ConvertPrimitiveValue("123", typeof(char?)),
"The value must be a string with a maximum length of 1.");
}
[Fact]
public void ConvertPrimitiveValueToXElement_Throws_IfInputIsNotString()
{
Assert.Throws<ValidationException>(
() => EdmPrimitiveHelpers.ConvertPrimitiveValue(123, typeof(XElement)),
"The value must be a string.");
}
}
}
|