PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Questionnaires.Tests/Model/OpenQuestionFixture.cs

#
C# | 93 lines | 63 code | 14 blank | 16 comment | 0 complexity | 077536285f6250c9af7f7600eddf5452 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.ComponentModel;
18using System.ComponentModel.DataAnnotations;
19using System.Linq;
20using Microsoft.VisualStudio.TestTools.UnitTesting;
21using MVVM.Questionnaires.Model;
22
23namespace MVVM.Questionnaires.Tests.Model
24{
25    [TestClass]
26    public class OpenQuestionFixture
27    {
28        [TestMethod]
29        public void WhenQuestionIsNew_ThenItIsNotComplete()
30        {
31            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion();
32            Assert.IsFalse(question.IsComplete);
33        }
34
35        [TestMethod]
36        public void WhenQuestionIsNew_ThenTemplateValuesAreCopied()
37        {
38            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
39            Assert.AreEqual(10, question.MaxLength);
40        }
41
42        [TestMethod]
43        public void WhenQuestionHasInvalidValue_ThenItIsNotComplete()
44        {
45            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
46            question.Response = "aaaaaaaaaaaaaaaaaaaaaaaaa";
47
48            Assert.IsFalse(question.IsComplete);
49        }
50
51        [TestMethod]
52        public void WhenQuestionHasEmptyValue_ThenItIsNotComplete()
53        {
54            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
55            question.Response = "";
56
57            Assert.IsFalse(question.IsComplete);
58        }
59
60        [TestMethod]
61        public void WhenQuestionHasValidValue_ThenItIsComplete()
62        {
63            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
64            question.Response = "aaaa";
65
66            Assert.IsTrue(question.IsComplete);
67        }
68
69        [TestMethod]
70        public void WhenResponseLengthExceedsMaxLength_ThenIndicatesErrorOnResponse()
71        {
72            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
73            var notifyErrorInfo = (INotifyDataErrorInfo)question;
74
75            question.Response = "ThisIsLongerThanTenCharacters";
76
77            Assert.IsTrue(notifyErrorInfo.GetErrors("Response").Cast<ValidationResult>().Any());
78        }
79
80        [TestMethod]
81        public void WhenSettingResponseToNull_ThenIndicatesErrorOnResponse()
82        {
83            var question = new OpenQuestionTemplate() { MaxLength = 10 }.CreateNewQuestion() as OpenQuestion;
84            var notifyErrorInfo = (INotifyDataErrorInfo)question;
85
86            question.Response = "valid";
87            Assert.IsFalse(notifyErrorInfo.GetErrors("Response").Cast<ValidationResult>().Any());
88
89            question.Response = null;
90            Assert.IsTrue(notifyErrorInfo.GetErrors("Response").Cast<ValidationResult>().Any());
91        }
92    }
93}