PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI.Infrastructure.Tests/Converters/StringToNullableNumberConverterFixture.cs

#
C# | 110 lines | 64 code | 27 blank | 19 comment | 0 complexity | 0654157239da245887326b4185274113 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Globalization;
 19using System.Text;
 20using System.Collections.Generic;
 21using System.Linq;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23using StockTraderRI.Infrastructure.Converters;
 24
 25namespace StockTraderRI.Infrastructure.Tests.Converters
 26{
 27    /// <summary>
 28    /// Summary description for StringToNullableIntConverterFixture
 29    /// </summary>
 30    [TestClass]
 31    public class StringToNullableNumberConverterFixture
 32    {
 33
 34        [TestMethod]
 35        public void ShouldConvertValidIntFromString()
 36        {
 37            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
 38
 39            string source = "123";
 40
 41            object result = converter.ConvertBack(source, typeof(int?), null, null);
 42
 43            Assert.IsInstanceOfType(result, typeof(int));
 44            Assert.AreEqual<int>(123, (int)result);
 45        }
 46
 47        [TestMethod]
 48        public void ShouldReturnOriginalValueForNonNullableIntFromString()
 49        {
 50            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
 51
 52            string source = "123";
 53
 54            object result = converter.ConvertBack(source, typeof(int), null, null);
 55
 56            Assert.AreSame(source, result);
 57        }
 58
 59        [TestMethod]
 60        public void ShouldReturnNullForInvalidInteger()
 61        {
 62            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
 63
 64            string source = "xxx";
 65
 66            object result = converter.ConvertBack(source, typeof(int?), null, null);
 67
 68            Assert.IsNull(result);
 69        }
 70
 71        [TestMethod]
 72        public void ShouldConvertValidDecimalFromString()
 73        {
 74            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
 75
 76            string source = 10.05.ToString(CultureInfo.CurrentCulture);
 77            
 78            object result = converter.ConvertBack(source, typeof(decimal?), null, null);
 79
 80            Assert.IsInstanceOfType(result, typeof(decimal));
 81            Assert.AreEqual<decimal>(10.05M, (decimal)result);
 82        }
 83
 84        [TestMethod]
 85        public void ShouldReturnOriginalValueForNonNullableDecimalTarget()
 86        {
 87            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
 88
 89            string source = 10.05.ToString(CultureInfo.CurrentCulture);
 90
 91            object result = converter.ConvertBack(source, typeof(decimal), null, null);
 92
 93            Assert.AreSame(source, result);
 94        }
 95
 96        [TestMethod]
 97        public void ShouldReturnNullForInvalidDecimal()
 98        {
 99            StringToNullableNumberConverter converter = new StringToNullableNumberConverter();
100
101            string source = "xxx";
102
103            object result = converter.ConvertBack(source, typeof(decimal?), null, null);
104
105            Assert.IsNull(result);
106        }
107	
108
109    }
110}