public static IRule Create(IOutputBuilder outputBuilder) { var by15RuleBuilder = new DivisibleBy15RuleBuilder().RedirectResultTo(outputBuilder); var by3RuleBuilder = new DivisibleBy3RuleBuilder().RedirectResultTo(outputBuilder); var by5RuleBuilder = new DivisibleBy5RuleBuilder().RedirectResultTo(outputBuilder); var integerRuleBuilder = new IntegerRuleBuilder().RedirectResultTo(outputBuilder); var endOfChain = new NullRule(); integerRuleBuilder.ContinueWith(endOfChain); by5RuleBuilder.BreakIfApplied().ContinueWith(integerRuleBuilder.Build()); by3RuleBuilder.BreakIfApplied().ContinueWith(by5RuleBuilder.Build()); by15RuleBuilder.BreakIfApplied().ContinueWith(by3RuleBuilder.Build()); return(by15RuleBuilder.Build()); }
public void null_rule_returns_unit_price() { const int UNIT_PRICE = 50; var rule = new NullRule("A", UNIT_PRICE); var basket = new Basket(); CollectionAssert.IsEmpty(rule.ApplyRule(basket)); basket.Add("A"); Assert.AreEqual(UNIT_PRICE, rule.ApplyRule(basket).Single()); CollectionAssert.IsEmpty(rule.ApplyRule(basket)); }
public IRule GetRule(RuleInfo ruleInfo) { IRule rule = new NullRule(ruleInfo); if (RelationOperatorRule(ruleInfo)) { rule = new RelationalRule(ruleInfo); } else if (ruleInfo.ValueType == ValueTypes.DateTime && ruleInfo.Operator == Operator.IsFuture) { rule = new DateTimeRule(ruleInfo); } else if (IsBandRule(ruleInfo) && ruleInfo.ValueType != ValueTypes.String) { rule = new BandRule(ruleInfo); } return(rule); }
public static ICARule GetRuleByName(string name) { if (_rulesCache.TryGetValue(name, out ICARule rule)) { return(rule); } else { if (LifeLikeRule.LifeLikeRuleString(name)) { rule = new LifeLikeRule(name, name); } else if (name.Trim().ToLower().Equals("life")) { rule = new LifeRule(); } else if (name.Trim().ToLower().Equals("highlife")) { rule = new HighLifeRule(); } else if (name.Trim().ToLower().Equals("seeds")) { rule = new SeedsRule(); } else if (name.Trim().ToLower().Equals("null")) { rule = new NullRule(); } else { rule = GetRuleFromFile(name, $"RuleFiles/{name}.table"); } _rulesCache[name] = rule; return(rule); } }