public void TestEvaluateConditionsMatch() { var userAttributes = new UserAttributes { { "device_type", "iPhone" }, { "location", "San Francisco" }, { "browser", "Chrome" } }; Assert.IsTrue(ConditionEvaluator.Evaluate(Conditions, userAttributes)); }
public CheckPromotionConditionResult CheckConditions(Promotion promotion, PriceCalculationContext context) { var result = new CheckPromotionConditionResult(); if (!promotion.Conditions.Any()) { result.Success = true; foreach (var item in context.Items) { result.MatchedItems.Add(item); } return(result); } var operators = _ruleEngine.ComparisonOperators.Select(o => o.Name).ToList(); foreach (var item in context.Items) { var contextModel = new PromotionConditionContextModel { Item = item, Customer = context.Customer }; if (_ruleEngine.Evaluate(promotion.Conditions, contextModel)) { result.Success = true; result.MatchedItems.Add(item); } } return(result); }
public void TestLogic() { var sut = new ConditionEvaluator(); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("false && true")).ShouldBe(false); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("true && true")).ShouldBe(true); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("true && false")).ShouldBe(false); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("false && false")).ShouldBe(false); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("false || true")).ShouldBe(true); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("true || true")).ShouldBe(true); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("true || false")).ShouldBe(true); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("false || false")).ShouldBe(false); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("!false")).ShouldBe(true); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("!true")).ShouldBe(false); }
public void TestStackOverflow() { var sut = new ConditionEvaluator(); var product = new Product { Name = "Huel", Category = new Category { CategoryId = 5 } }; product.Category.ProductReference = product; var code = new StringBuilder("resource"); for (var i = 0; i < 100; i++) { code.Append(".Category.ProductReference"); } code.Append(@".Name == ""Huel"""); ActualValueDelegate <bool> del = () => sut.Evaluate(product, Les2LanguageService.Value.ParseSingle(code.ToString())); Assert.That(del, Throws.TypeOf <StackOverflowException>()); }
async Task BuildLineItem(IList <BillLineItem> container, BillRequest request, BillRequestLineItem item) { // No stage 2 payments if (request.Type == BillType.Stage2 && (item.Scheme == Scheme.Endorsement || item.SubScheme == SubScheme.Canteen || item.SubScheme == SubScheme.ShortTerm)) { return; } var codes = await GetCertFeeCodes(request.ReferenceDate, item.Scheme, item.SubScheme); var code = codes.Count() == 1 ? codes.First() : codes.FirstOrDefault(e => ConditionEvaluator.Evaluate(item.Area, e.Conditions?.ToArray())); if (code == null) { return; } if (request.Type == BillType.Stage1) { await Stage1CertFee(container, code, item); } else if (request.Type == BillType.Stage2) { await Stage2CertFee(request.RequestType, container, code, item); } }
public virtual void ApplyRule(RewriteContext context) { // 1. Figure out which section of the string to match for the initial rule. var initMatchRes = InitialMatch.Evaluate(context.HttpContext.Request.Path, context); if (!initMatchRes.Success) { context.Logger.ModRewriteNotMatchedRule(); return; } BackReferenceCollection?condBackReferences = null; if (Conditions != null) { var condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchRes.BackReferences); if (!condResult.Success) { context.Logger.ModRewriteNotMatchedRule(); return; } condBackReferences = condResult.BackReferences; } // At this point, we know our rule passed, first apply pre conditions, // which can modify things like the cookie or env, and then apply the action context.Logger.ModRewriteMatchedRule(); foreach (var action in Actions) { action.ApplyAction(context, initMatchRes?.BackReferences, condBackReferences); } }
public void TestJavaScriptStyleInsanity() { // arrange var sut = new ConditionEvaluator(); // act (ish) ActualValueDelegate <bool> del = () => sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("!5")); // assert Assert.That(del, Throws.TypeOf <InvalidOperationException>()); }
public void should_throw_for_unrecognized_params() { var ruleEngine = new ConditionEvaluator(); Assert.Throws <UnrecognizedParameterException>(() => { var condition = "Age > 10 OR NotExistParam == 5"; ruleEngine.Evaluate(condition, new Person { Age = 10 }); }); }
public override IEnumerable <ConfiguredActivity> Execute(object dataContext) { var evaluator = new ConditionEvaluator(); var success = evaluator.Evaluate(Conditions, dataContext); if (success) { return(Further(Then, dataContext)); } else { return(Further(Else, dataContext)); } }
public void TestMultiplePropertyReferences() { var sut = new ConditionEvaluator(); var product = new Product { Name = "Huel", Category = new Category { CategoryId = 5 } }; sut.Evaluate(product, Les2LanguageService.Value.ParseSingle("resource.Category.CategoryId == 5 || resource.Category.CategoryId == 6 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7 || resource.Category.CategoryId == 7")) .ShouldBe(true); }
public void ConditionExpressionEvaluate() { Assert.Equal(1, ConditionEvaluator.Evaluate(true, 1, 2)); Assert.Equal(1, ConditionEvaluator.Evaluate(1, 1, 2)); Assert.Equal(1, ConditionEvaluator.Evaluate(2.3f, 1, 2)); Assert.Equal(1, ConditionEvaluator.Evaluate("aa", 1, 2)); Assert.Equal(1, ConditionEvaluator.Evaluate(new object(), 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(false, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(0, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(-9, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(-0f, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(-9.0f, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate(null, 1, 2)); Assert.Equal(2, ConditionEvaluator.Evaluate("", 1, 2)); }
public void TestEnum() { var sut = new ConditionEvaluator(); var product = new Product { Name = "Huel", Category = new Category { CategoryId = 5, Type = CategoryType.Product } }; sut.Evaluate(product, Les2LanguageService.Value.ParseSingle("resource.Category.Type == 'Product'")) .ShouldBe(true); }
public void ApplyRule(RewriteContext context) { // Due to the path string always having a leading slash, // remove it from the path before regex comparison var path = context.HttpContext.Request.Path; MatchResults initMatchResults; if (path == PathString.Empty) { initMatchResults = InitialMatch.Evaluate(path.ToString(), context); } else { initMatchResults = InitialMatch.Evaluate(path.ToString().Substring(1), context); } if (!initMatchResults.Success) { context.Logger.UrlRewriteNotMatchedRule(Name); return; } MatchResults?condResult = null; if (Conditions != null) { condResult = ConditionEvaluator.Evaluate(Conditions, context, initMatchResults.BackReferences); if (!condResult.Success) { context.Logger.UrlRewriteNotMatchedRule(Name); return; } } context.Logger.UrlRewriteMatchedRule(Name); // at this point we know the rule passed, evaluate the replacement. Action.ApplyAction(context, initMatchResults?.BackReferences, condResult?.BackReferences); }
public void TestSimple() { var sut = new ConditionEvaluator(); sut.Evaluate(null, Les2LanguageService.Value.ParseSingle("0 < 1")).ShouldBe(true); }
private bool CheckCondition(string condition, object contextModel) { var ruleEngine = new ConditionEvaluator(); return(ruleEngine.Evaluate(condition, contextModel)); }