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