private static void RegisterDynamicVariableEvaluatorForMonthTimeSpan(DynamicVariableEvaluator evaluator) { var regex = new Regex("^(\\d+)(m|month)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); var item = new RegexAutoCompleteItem("NNNmonth", "Represents time span (e.g. 1month)", (v, o) => Regex.IsMatch(v, "^\\d+$"), (v, o) => v + "month"); evaluator.RegisterVariable(regex, o => TimeSpan.FromDays(Int32.Parse(o.Groups[1].Value, CultureInfo.CurrentCulture) * 30), item); }
public void RegisterVariableShouldThrowExceptionWhenProcedureIsNull() { var regex = new Regex(""); var item = new RegexAutoCompleteItem("", "", (v, o) => true, (v, o) => v); Assert.Throws <ArgumentNullException>(() => testee.RegisterVariable(regex, null, item)); }
private static void RegisterDynamicVariableEvaluatorForYearTimeSpan(DynamicVariableEvaluator evaluator) { var regex = new Regex("^(\\d+)(y|year)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); var item = new RegexAutoCompleteItem("NNNyear", "Represents time span (e.g. 1year)", (v, o) => Regex.IsMatch(v, "^\\d+$"), (v, o) => string.Format("{0}year", v)); evaluator.RegisterVariable(regex, o => TimeSpan.FromDays(Int32.Parse(o.Groups[1].Value, CultureInfo.CurrentCulture) * 365), item); }
public void RegisterVariableShouldThrowExcepionWhenRegexIsNull() { Regex regex = null; var item = new RegexAutoCompleteItem("", "", (v, o) => true, (v, o) => v); Assert.Throws <ArgumentNullException>(() => testee.RegisterVariable(regex, o => o, item)); }
/// <summary> /// Registers a new mapping between a pattern of regular expression and a value generator. /// </summary> /// <param name="pattern">The pattern of regular expression to find out a variable.</param> /// <param name="procedure">The value generator.</param> /// <param name="item">The autocomplete item.</param> public void RegisterVariable(string pattern, Func <Match, object> procedure, RegexAutoCompleteItem item) { if (pattern == null) { throw new ArgumentNullException("pattern"); } RegisterVariable(new Regex(pattern, RegexOptions.Compiled), procedure, item); }
public string ConstructorShouldWorkCorrectly(string displayName, string description, string pattern, string complete) { var isMatchProcedure = (pattern != null) ? (v, o) => v == pattern : (Func <string, MatchingOption, bool>)null; var completeProcedure = (complete != null) ? (v, o) => complete : (Func <string, MatchingOption, string>)null; var testee = new RegexAutoCompleteItem(displayName, description, isMatchProcedure, completeProcedure); return(String.Format("{0}:{1}", testee.DisplayName, testee.Description)); }
public void EvaluateShouldReturnNullWhenSpecifiedVariableIsNotMatchedAnyPattnens(string value) { var item = new RegexAutoCompleteItem("", "", (v, o) => true, (v, o) => v); testee.RegisterVariable("^x.*$", o => 10, item); testee.RegisterVariable("^y.*$", o => "foo", item); testee.RegisterVariable("^z.*$", o => true, item); Assert.IsNull(testee.Evaluate(null, value)); }
public void RegisterVariableShouldWorkCorrectly(string pattern, object value) { var item = new RegexAutoCompleteItem("", "", (v, o) => true, (v, o) => v); testee.RegisterVariable(pattern, o => value, item); Assert.AreEqual(1, evaluators.Count); Assert.AreEqual(1, testee.AutocompleteItems.Count()); Assert.AreSame(item, testee.AutocompleteItems.ElementAt(0)); }
public object EvaluateShouldWorkCorrectly(string value) { var item = new RegexAutoCompleteItem("", "", (v, o) => true, (v, o) => v); testee.RegisterVariable("^x.*$", o => 10, item); testee.RegisterVariable("^y.*$", o => "foo", item); testee.RegisterVariable("^z.*$", o => true, item); var operand = testee.Evaluate(null, value); return(Expression.Lambda(operand.Expression).Compile().DynamicInvoke()); }
public void CompleteShouldCallCompleteProcedure() { var counter = 0; Func <string, MatchingOption, string> proc = (v, o) => { counter++; return(v); }; var testee = new RegexAutoCompleteItem("value", "description", (v, o) => true, proc); testee.Complete("", new MatchingOption()); Assert.AreEqual(1, counter); }
public void IsMatchShouldCallIsMatchProcedure() { var counter = 0; Func <string, MatchingOption, bool> proc = (v, o) => { counter++; return(true); }; var testee = new RegexAutoCompleteItem("value", "description", proc, (v, o) => v); testee.IsMatch("", new MatchingOption()); Assert.AreEqual(1, counter); }
/// <summary> /// Registers a new mapping between a regular expression and a value generator. /// </summary> /// <param name="regex">The regular expression to find out a variable.</param> /// <param name="procedure">The value generator.</param> /// <param name="item">The autocomplete item.</param> public void RegisterVariable(Regex regex, Func <Match, object> procedure, RegexAutoCompleteItem item) { if (regex == null) { throw new ArgumentNullException("regex"); } if (procedure == null) { throw new ArgumentNullException("procedure"); } if (item == null) { throw new ArgumentNullException("item"); } Logger.Verbose("Registered the specified pattern '{0}'.", regex.ToString()); evaluators[regex] = procedure; Register(item); }