public void Validation_Early_Fail() { const string IllegalValue = "green"; // No { }, so we can determine validity immediately ValidationWithAutoResolveAttribute attr = new ValidationWithAutoResolveAttribute { Value = IllegalValue }; Dictionary <string, object> values = new Dictionary <string, object>() { { "name", "ignored" }, // ignored since no {name} token in the attr }; var ctx = GetCtx(values); try { new AttributeCloner <ValidationWithAutoResolveAttribute>(attr, GetBindingContract("name")); Assert.False(true, "Validation should have failed"); } catch (InvalidOperationException e) { // Since this is [AutoResolve], include the illegal value in the message. Assert.True(e.Message.Contains(IllegalValue)); } }
public void Validation_Late() { // with { } , can't determine if it's valid until after resolution. ValidationWithAutoResolveAttribute attr = new ValidationWithAutoResolveAttribute { Value = "a{name}" }; // Can't fail yet. var cloner = new AttributeCloner <ValidationWithAutoResolveAttribute>(attr, GetBindingContract("name")); // Valid { Dictionary <string, object> values = new Dictionary <string, object>() { { "name", "aa" }, // Ok }; var ctx = GetCtx(values); var attr2 = cloner.ResolveFromBindingData(ctx); Assert.Equal("aaa", attr2.Value); } // Invalid { Dictionary <string, object> values = new Dictionary <string, object>() { { "name", "b" }, // regex failure }; var ctx = GetCtx(values); Assert.Throws <InvalidOperationException>(() => cloner.ResolveFromBindingData(ctx)); } }
public void Validation_Early_Succeed() { ValidationWithAutoResolveAttribute attr = new ValidationWithAutoResolveAttribute { Value = "aaa" }; Dictionary <string, object> values = new Dictionary <string, object>() { { "name", "green" }, }; var ctx = GetCtx(values); var cloner = new AttributeCloner <ValidationWithAutoResolveAttribute>(attr, GetBindingContract("name")); var attr2 = cloner.ResolveFromBindingData(ctx); Assert.Equal("aaa", attr2.Value); }