summaryrefslogtreecommitdiff
path: root/slips/src/test/Marina.Test/Unit/Presentation/PayloadKeyTest.cs
blob: b7a35407464d9db15fd6c1fb07ee4d81b2c28b0b (plain)
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
using System;
using System.Collections.Specialized;
using Marina.Presentation;
using MbUnit.Framework;

namespace Marina.Test.Unit.Presentation {
	[TestFixture]
	public class PayloadKeyTest {
		[Test]
		public void Should_be_able_to_convert_an_item_from_the_payload_into_an_item_of_the_correct_type( ) {
			string key = "DID";
			int expectedValue = 1;

			NameValueCollection payload = new NameValueCollection( );
			payload.Add( key, expectedValue.ToString( ) );

			int actualValue = CreateSUT< int >( key ).ParseFrom( payload );
			Assert.AreEqual( expectedValue, actualValue );
		}

		[Test]
		[ExpectedException( typeof( Exception ) )]
		public void Should_not_be_able_to_parse_if_the_item_in_the_payload_does_not_match_the_data_type_for_the_key( ) {
			NameValueCollection payload = new NameValueCollection( );
			payload.Add( "DID", "NotAnInt" );
			CreateSUT< int >( "DID" ).ParseFrom( payload );
		}

		[Test]
		public void Should_be_able_to_implicitly_cast_to_a_string( ) {
			string implictlyCasted = CreateSUT< int >( "DID" );
			Assert.AreEqual( "DID", implictlyCasted );
		}

		[Test]
		[ExpectedException( typeof( PayloadKeyNotFoundException ) )]
		public void Should_return_default_value_if_key_is_not_in_payload( ) {
			CreateSUT< string >( "NOTINPAYLOAD" ).ParseFrom( new NameValueCollection( ) );
		}

		private PayloadKey< T > CreateSUT< T >( string key ) {
			return new PayloadKey< T >( key );
		}
	}
}