diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-11-03 17:22:00 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-11-04 08:12:52 -0800 |
commit | 462e19abd9034c11a12cad30e9899740f2bef8ff (patch) | |
tree | e08667f1d69249f8daa6c348a919bd0fd5434415 /src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs | |
parent | 6a79be0eca3929d8fb4e797799dac8d6f7875475 (diff) | |
download | DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.zip DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.gz DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.bz2 |
Changed namepace and project names in preparation for merge with DotNetOpenId.
Diffstat (limited to 'src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs')
-rw-r--r-- | src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs b/src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs new file mode 100644 index 0000000..c3c69c2 --- /dev/null +++ b/src/DotNetOpenAuth.Test/Messaging/Reflection/MessagePartTests.cs @@ -0,0 +1,101 @@ +//----------------------------------------------------------------------- +// <copyright file="MessagePartTests.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.Messaging.Reflection { + using System; + using System.Linq; + using System.Reflection; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.Messaging.Reflection; + using DotNetOpenAuth.Test.Mocks; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class MessagePartTests : MessagingTestBase { + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void OptionalNonNullableStruct() { + this.ParameterizedMessageTypeTest(typeof(MessageWithNonNullableOptionalStruct)); + } + + [TestMethod] + public void RequiredNonNullableStruct() { + this.ParameterizedMessageTypeTest(typeof(MessageWithNonNullableRequiredStruct)); + } + + [TestMethod] + public void OptionalNullableStruct() { + this.ParameterizedMessageTypeTest(typeof(MessageWithNullableOptionalStruct)); + } + + [TestMethod] + public void RequiredNullableStruct() { + this.ParameterizedMessageTypeTest(typeof(MessageWithNullableRequiredStruct)); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void CtorNullMember() { + new MessagePart(null, new MessagePartAttribute()); + } + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void CtorNullAttribute() { + PropertyInfo field = typeof(MessageWithNullableOptionalStruct).GetProperty("OptionalInt", BindingFlags.NonPublic | BindingFlags.Instance); + new MessagePart(field, null); + } + + [TestMethod] + public void SetValue() { + var message = new MessageWithNonNullableRequiredStruct(); + MessagePart part = this.ParameterizedMessageTypeTest(message.GetType()); + part.SetValue(message, "5"); + Assert.AreEqual(5, message.OptionalInt); + } + + [TestMethod] + public void GetValue() { + var message = new MessageWithNonNullableRequiredStruct(); + message.OptionalInt = 8; + MessagePart part = this.ParameterizedMessageTypeTest(message.GetType()); + Assert.AreEqual("8", part.GetValue(message)); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void NonFieldOrPropertyMember() { + MemberInfo method = typeof(MessageWithNullableOptionalStruct).GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance); + new MessagePart(method, new MessagePartAttribute()); + } + + private MessagePart ParameterizedMessageTypeTest(Type messageType) { + PropertyInfo field = messageType.GetProperty("OptionalInt", BindingFlags.NonPublic | BindingFlags.Instance); + MessagePartAttribute attribute = field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single(); + return new MessagePart(field, attribute); + } + + private class MessageWithNonNullableOptionalStruct : TestMessage { + // Optional structs like int must be nullable for Optional to make sense. + [MessagePart(IsRequired = false)] + internal int OptionalInt { get; set; } + } + + private class MessageWithNonNullableRequiredStruct : TestMessage { + // This should work because a required field will always have a value so it + // need not be nullable. + [MessagePart(IsRequired = true)] + internal int OptionalInt { get; set; } + } + + private class MessageWithNullableOptionalStruct : TestMessage { + // Optional structs like int must be nullable for Optional to make sense. + [MessagePart(IsRequired = false)] + internal int? OptionalInt { get; set; } + } + + private class MessageWithNullableRequiredStruct : TestMessage { + [MessagePart(IsRequired = true)] + private int? OptionalInt { get; set; } + } + } +} |