|
| 1 | +// |
| 2 | +// MessageProviderIntegrationTests.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Craig Fowler <craig@csf-dev.com> |
| 6 | +// |
| 7 | +// Copyright (c) 2017 Craig Fowler |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using CSF.Reflection; |
| 29 | +using CSF.Validation.Manifest; |
| 30 | +using CSF.Validation.Messages; |
| 31 | +using CSF.Validation.StockRules; |
| 32 | +using CSF.Validation.Tests.Resources; |
| 33 | +using CSF.Validation.ValidationRuns; |
| 34 | +using Moq; |
| 35 | +using NUnit.Framework; |
| 36 | + |
| 37 | +namespace CSF.Validation.Tests.Messages |
| 38 | +{ |
| 39 | + [TestFixture] |
| 40 | + public class MessageProviderIntegrationTests |
| 41 | + { |
| 42 | + [Test] |
| 43 | + public void GetFailureMessage_returns_expected_formatted_message_with_constant_replacements() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + var sut = GetSut(); |
| 47 | + var failure = GetFailures()[0]; |
| 48 | + |
| 49 | + // Act |
| 50 | + var result = sut.GetFailureMessage(failure); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.AreEqual("Number must be between 5 and 20.", result); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void GetFailureMessage_returns_expected_formatted_message_with_dynamic_replacement() |
| 58 | + { |
| 59 | + // Arrange |
| 60 | + var sut = GetSut(); |
| 61 | + var failure = GetFailures()[1]; |
| 62 | + |
| 63 | + // Act |
| 64 | + var result = sut.GetFailureMessage(failure); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.AreEqual("The year 2001 is too early.", result); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void GetFailureMessage_returns_expected_formatted_message_with_no_formatter() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var sut = GetSut(); |
| 75 | + var failure = GetFailures()[2]; |
| 76 | + |
| 77 | + // Act |
| 78 | + var result = sut.GetFailureMessage(failure); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.AreEqual("Value must not be null.", result); |
| 82 | + } |
| 83 | + |
| 84 | + IMessageProvider GetSut() |
| 85 | + { |
| 86 | + var templateProvider = new FailureMessageTemplates(); |
| 87 | + var provider = new ByIdentityFormatterProvider(); |
| 88 | + |
| 89 | + object identity; |
| 90 | + PlaceholderMessageFormatter<StubValidatedObject> formatter; |
| 91 | + |
| 92 | + identity = new DefaultManifestIdentity(typeof(StubValidatedObject), |
| 93 | + typeof(NumericRangeValueRule), |
| 94 | + validatedMember: Reflect.Member<StubValidatedObject>(x => x.IntegerProperty)); |
| 95 | + formatter = new PlaceholderMessageFormatter<StubValidatedObject>(); |
| 96 | + formatter.AddPlaceholder("{min}", 5); |
| 97 | + formatter.AddPlaceholder("{max}", 20); |
| 98 | + provider.RegisteredFormatters.Add(identity, formatter); |
| 99 | + |
| 100 | + identity = new DefaultManifestIdentity(typeof(StubValidatedObject), |
| 101 | + typeof(DateTimeRangeValueRule), |
| 102 | + validatedMember: Reflect.Member<StubValidatedObject>(x => x.DateTimeProperty)); |
| 103 | + formatter = new PlaceholderMessageFormatter<StubValidatedObject>(); |
| 104 | + formatter.AddPlaceholder("{actual}", x => x.DateTimeProperty.Year); |
| 105 | + provider.RegisteredFormatters.Add(identity, formatter); |
| 106 | + |
| 107 | + return new MessageProvider(templateProvider, provider); |
| 108 | + } |
| 109 | + |
| 110 | + IRunnableRuleResult[] GetFailures() |
| 111 | + { |
| 112 | + var identityOne = new DefaultManifestIdentity(typeof(StubValidatedObject), |
| 113 | + typeof(NumericRangeValueRule), |
| 114 | + validatedMember: Reflect.Member<StubValidatedObject>(x => x.IntegerProperty)); |
| 115 | + var identityTwo = new DefaultManifestIdentity(typeof(StubValidatedObject), |
| 116 | + typeof(DateTimeRangeValueRule), |
| 117 | + validatedMember: Reflect.Member<StubValidatedObject>(x => x.DateTimeProperty)); |
| 118 | + var identityThree = new DefaultManifestIdentity(typeof(StubValidatedObject), |
| 119 | + typeof(NotNullValueRule), |
| 120 | + validatedMember: Reflect.Member<StubValidatedObject>(x => x.StringProperty)); |
| 121 | + |
| 122 | + var validated = new StubValidatedObject { DateTimeProperty = new DateTime(2001, 1, 1) }; |
| 123 | + |
| 124 | + return new [] { |
| 125 | + Mock.Of<IRunnableRuleResult>(x => x.ManifestIdentity == identityOne && x.Validated == validated), |
| 126 | + Mock.Of<IRunnableRuleResult>(x => x.ManifestIdentity == identityTwo && x.Validated == validated), |
| 127 | + Mock.Of<IRunnableRuleResult>(x => x.ManifestIdentity == identityThree && x.Validated == validated), |
| 128 | + }; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments