public void Only_Calls_GetRulesFromTypeCore_Once_Per_Type()
        {
            // Arrange
            int numCalls = 0;
            var expectedStringResult = new RuleSet(new Rule[] { }.ToLookup(x => "test1", x => x));
            var expectedDateTimeResult = new RuleSet(new Rule[] { }.ToLookup(x => "test2", x => x));
            var provider = new TestRulesProvider(t =>
            {
                numCalls++;
                if (t == typeof(string))
                    return expectedStringResult;
                if (t == typeof(DateTime))
                    return expectedDateTimeResult;
                throw new ArgumentException();
            });

            // Act / Assert
            var stringResult = provider.GetRulesFromType(typeof(string));
            Assert.Same(expectedStringResult, stringResult);
            Assert.Equal(1, numCalls);

            var dateTimeResult = provider.GetRulesFromType(typeof(DateTime));
            Assert.Same(expectedDateTimeResult, dateTimeResult);
            Assert.Equal(2, numCalls);

            var stringResult2 = provider.GetRulesFromType(typeof(string));
            Assert.Same(expectedStringResult, stringResult2);
            Assert.Equal(2, numCalls);

            var dateTimeResult2 = provider.GetRulesFromType(typeof(DateTime));
            Assert.Same(expectedDateTimeResult, dateTimeResult2);
            Assert.Equal(2, numCalls);
        }
예제 #2
0
        public void ClientSideValidationRules_Helper_Passes_ActiveRuleProvider_Output_To_Formatter()
        {
            // Arrange
            var html = new HtmlHelperMocks<object>().HtmlHelper;
            var arbitraryType = typeof (DateTime);
            var ruleProvider = new Moq.Mock<IRulesProvider>();
            var rules = new RuleSet(new[] { "someProperty" }.ToLookup(x => x, x => (Rule)new RequiredRule()));
            ruleProvider.Expect(x => x.GetRulesFromType(arbitraryType)).Returns(rules);
            ActiveRuleProviders.Providers.Clear();
            ActiveRuleProviders.Providers.Add(ruleProvider.Object);

            // Capture params passed to mockFormatter
            var mockFormatter = new Moq.Mock<IValidationConfigFormatter>(MockBehavior.Strict);
            RuleSet passedRules = null;
            Action<RuleSet> callback = x =>
            {
                passedRules = x;
            };
            mockFormatter.Expect(x => x.FormatRules(It.IsAny<RuleSet>()))
                .Callback(callback)
                .Returns("ok");
            ValidationInfo.Formatter = mockFormatter.Object;

            // Act
            var result = html.ClientSideValidationRules(arbitraryType);

            // Assert
            Assert.Equal("ok", result.ToString());
            Assert.Equal(1, passedRules.Keys.Count());
            Assert.Same(rules["someProperty"].Single(), passedRules["someProperty"].First());
        }
 private static IRulesProvider MakeMockRuleProvider(Type forModelType, params string[] rulePropertyNames)
 {
     var ruleset = new RuleSet(rulePropertyNames.ToLookup(x => x, x => (Rule)new RequiredRule()));
     var mockProvider = new Moq.Mock<IRulesProvider>();
     mockProvider.Expect(x => x.GetRulesFromType(forModelType)).Returns(ruleset);
     return mockProvider.Object;
 }
예제 #4
0
 private static string ClientSideValidationRules(RuleSet rules)
 {
     if (rules == null)
     {
         throw new ArgumentNullException("rules");
     }
     var formatter = new JsonValidationConfigFormatter();
     return formatter.FormatRules(rules);
 }
예제 #5
0
 public string FormatRules(RuleSet rules)
 {
     var allRules = from key in rules.Keys
                    from rule in rules[key]
                    select new { key, rule };
     var result = new StringBuilder();
     foreach (var pair in allRules)
         result.AppendFormat("{0}={1},", pair.key, pair.rule.RuleName);
     return result.ToString();
 }
예제 #6
0
        public void ClientSideValidationRules_Can_Take_Explicit_RuleSet_And_Passes_It_To_Formatter()
        {
            // Arrange
            var html = new HtmlHelperMocks<object>().HtmlHelper;
            var rules = new RuleSet(new[] { "someProperty" }.ToLookup(x => x, x => (Rule)new RequiredRule()));

            // Capture params passed to mockFormatter
            var mockFormatter = new Moq.Mock<IValidationConfigFormatter>(MockBehavior.Strict);
            mockFormatter.Expect(x => x.FormatRules(rules)).Returns("ok");

            ValidationInfo.Formatter = mockFormatter.Object;

            // Act
            var result = html.ClientSideValidationRules(rules);

            // Assert
            Assert.Equal("ok", result.ToString());
        }
        public string FormatRules(RuleSet rules)
        {
            var tb = new TagBuilder(TagName);
            var rulesBuilder = new StringBuilder();
            var allRules = from key in rules.Keys
                           from rule in rules[key]
                           select new { key, rule };

            if (allRules.Any())
                rulesBuilder.Append(NewLine);
            foreach (var item in allRules) {
                rulesBuilder.Append(Indent);
                rulesBuilder.Append(FormatSingleRule(item.rule, item.key));
                rulesBuilder.Append(NewLine);
            }

            tb.InnerHtml = rulesBuilder.ToString();
            return tb.ToString(allRules.Any() ? TagRenderMode.Normal : TagRenderMode.SelfClosing);
        }
        public void Returns_Rules_Given_By_Subclass()
        {
            // Arrange
            var expectedStringResult = new RuleSet(new Rule[] {}.ToLookup(x => "test1", x => x));
            var expectedDateTimeResult = new RuleSet(new Rule[] {}.ToLookup(x => "test2", x => x));
            var provider = new TestRulesProvider(t => {
                if(t == typeof(string))
                    return expectedStringResult;
                if (t == typeof(DateTime))
                    return expectedDateTimeResult;
                throw new ArgumentException();
            });

            // Act
            var stringResult = provider.GetRulesFromType(typeof (string));
            var dateTimeResult = provider.GetRulesFromType(typeof(DateTime));

            // Assert
            Assert.Same(expectedStringResult, stringResult);
            Assert.Same(expectedDateTimeResult, dateTimeResult);
        }
 public string FormatRules(RuleSet rules)
 {
     return Serializer.Serialize(rules);
 }
예제 #10
0
 /// <summary>
 /// Constructs a ValidationInfo that will render JavaScript code that attaches the rules to DOM elements
 /// </summary>
 public ValidationInfo(RuleSet rules, string attachToElementPrefix)
     : this(rules)
 {
     currentRenderMode = RenderMode.CompleteScriptBlock;
     elementPrefix = attachToElementPrefix;
 }
예제 #11
0
 /// <summary>
 /// Constructs a ValidationInfo that simply format and render the rules
 /// </summary>
 public ValidationInfo(RuleSet rules)
 {
     if (rules == null) throw new ArgumentNullException("rules");
     this.rules = rules;
     currentRenderMode = RenderMode.ConfigOnly;
 }
예제 #12
0
 private void MergeAddedRulesIntoRuleSet()
 {
     if(addedRules.Count > 0) {
         var addedRuleSet = new RuleSet(addedRules.ToLookup(x => x.Key, x => x.Value));
         rules = new RuleSet(new[] { rules, addedRuleSet });
         addedRules.Clear();
     }
 }