private void TestCore(string resultName, string expectedName, string typeName, string value, string stepText) { string compareText = GetCompareText(stepText); CompareOp compareOp = ToCompareOp(compareText); //Console.WriteLine($"***** Testing: result name: <{resultName}>, expected name: <{expectedName}>, type name: <{typeName}>, compare: <{compareOp}>, value: <{value}>"); ITypeProcessor expected = null; if (!IsSingleCompareOp(compareOp)) { if (!_manager.GetProcessorForExistingName(expectedName, out expected)) { expected = _manager.GetProcessorFor(typeName, value); } } if (expected == null)// && string.IsNullOrWhiteSpace(resultName)) { expected = new ObjectProcessor(_scenario); expected.PostCurrentValue(); } ITypeProcessor actual = _manager.GetProcessorForExistingName(resultName); DoCompare(actual, expected, compareOp); }
public ITypeProcessor GetCollectionProcessorFor(string collectionKind, string dataTypeName, string value) { //Console.WriteLine($"*** GetCollectionProcessorFor(<{collectionKind}>, <{dataTypeName}>, <{(value == null ? "NULL" : value)}>)"); if (string.IsNullOrEmpty(collectionKind)) { return(GetCollectionProcessorFor(dataTypeName, value)); } if (string.IsNullOrEmpty(dataTypeName)) { return(GetCollectionProcessorForValue(collectionKind, value)); } IValueProcessor elementProcessor = GetProcessorForDataType(dataTypeName) as IValueProcessor; if (elementProcessor == null) { ThrowMissingTypeName(dataTypeName); } ITypeProcessor processor = GetCollectionProcessorFor(collectionKind, elementProcessor); if (value != null) { processor.SetValue(value); } return(processor); }
public override void CompareTo(ITypeProcessor other, CompareOp compareOp) { ICollection otherValue; //Console.WriteLine($"***** Comparing {this} {compareOp} {other}"); switch (compareOp) { case CompareOp.Null: Assert.IsNull(Value); break; case CompareOp.NotNull: Assert.IsNotNull(Value); break; case CompareOp.Equal: otherValue = GetCollectionToCompareTo(other); CollectionAssert.AreEqual(otherValue, CollectionValue, Comparer, $"{this} is not equal to {other}"); break; case CompareOp.NotEqual: otherValue = GetCollectionToCompareTo(other); CollectionAssert.AreNotEqual(otherValue, CollectionValue, Comparer, $"{this} is equal to {other}"); break; case CompareOp.Equivalent: otherValue = GetCollectionToCompareTo(other); CollectionAssert.AreEquivalent(otherValue, CollectionValue, $"{this} is not equivalent to {other}"); break; case CompareOp.NotEquivalent: otherValue = GetCollectionToCompareTo(other); CollectionAssert.AreNotEquivalent(otherValue, CollectionValue, $"{this} is equivalent to {other}"); break; case CompareOp.Contain: otherValue = GetCollectionToCompareTo(other); CollectionAssert.IsSubsetOf(otherValue, CollectionValue, $"{this} does not contain {other}"); break; case CompareOp.NotContain: otherValue = GetCollectionToCompareTo(other); CollectionAssert.IsNotSubsetOf(otherValue, CollectionValue, $"{this} contains {other}"); break; case CompareOp.Match: case CompareOp.NotMatch: case CompareOp.Greater: case CompareOp.GreaterOrEqual: case CompareOp.NotGreater: case CompareOp.NotGreaterOrEqual: case CompareOp.Less: case CompareOp.LessOrEqual: case CompareOp.NotLess: case CompareOp.NotLessOrEqual: default: ThrowCompareOpNotSupported(compareOp); break; } }
/// <summary> /// 处理每种类型,在其内部遍历所有加载程序集里面的对象类型 /// </summary> /// <param name="typeProcessor">对象类型加工处理器</param> public void ProcessType(ITypeProcessor typeProcessor) { if (typeProcessor == null) { return; } this.ProcessType(new[] { typeProcessor }); }
public IStartupService RegisterProcessor(ITypeProcessor processor) { Check.MustNotNull(processor, "processor"); this.MustNotStarted(); _processors.Add(processor); return this; }
protected static void CheckProcessorExists(Type dataType, ITypeProcessor processor) { if (processor != null) { return; } throw new Exception($"No processor registered to handle: {dataType.Name}."); }
protected ICollection GetCollectionToCompareTo(ITypeProcessor other) { if (other is ICollectionProcessor otherCollection) { return(otherCollection.CollectionValue); } throw new ArgumentException( $"Cannot compare {this} to {other}. Can only compare to another {nameof(ICollectionProcessor)}", nameof(other)); }
protected T GetValue(ITypeProcessor processor) { //if (processor is IValueProcessor processorT //) //{ // return (T)processorT.Value; //} //else { return((T)Convert.ChangeType(processor.Value, DataType)); } }
private ITypeProcessor GetProcessorFor(object instance, PropertyInfo prop, FieldInfo field) { Type propFieldType = prop?.PropertyType ?? field?.FieldType; ITypeProcessor processor = _manager.GetProcessorFor(propFieldType); CheckProcessorExists(propFieldType, processor); object value = prop != null?prop.GetValue(instance) : field.GetValue(instance); processor.SetValue(value); return(processor); }
private void GivenCore(string name, string typeName, string value) { //Console.WriteLine($"***** Given: name: {name}, type name: {typeName}, value: {value}"); bool isNullValue = value == "null"; ITypeProcessor processor = isNullValue ? _manager.GetProcessorForDataType(typeName) ?? new ObjectProcessor(_scenario) : _manager.GetProcessorFor(typeName, value); processor.PostCurrentValue(name); //Console.WriteLine($"***** Processor: {(processor != null ? processor.ToString() : "NULL")}"); }
public static void RunTypeProcessor(this IContainer container, ITypeProcessor typeProcessor) { var assemblies = Assemblies.AllFromApplicationBaseDirectory(a => !a.FullName.StartsWith("System")); foreach (var assembly in assemblies) { var types = assembly.GetTypes(); foreach (var type in types) { typeProcessor.Process(type, container); } } }
private void GivenCore(string name, string typeName, string value, string collectionKind) { //Console.WriteLine($"***** Given: name: <{name}>, type name: <{typeName}>, value: <{value}>, collection: <{collectionKind}>"); bool isNullValue = value == null || value == "null"; if (isNullValue) { value = null; } ITypeProcessor processor = _manager.GetCollectionProcessorFor(collectionKind, typeName, value); processor.PostCurrentValue(name); //Console.WriteLine($"***** Processor: {(processor != null ? processor.ToString() : "NULL")}"); }
public ITypeProcessor GetProcessorFor(string dataTypeName, string value) { if (string.IsNullOrEmpty(dataTypeName)) { return(GetProcessorForValue(value)); } ITypeProcessor processor = GetProcessorForDataType(dataTypeName); if (processor == null) { ThrowMissingTypeName(dataTypeName); } processor?.Parse(value); return(processor); }
private void PropertySetImpl(string propName, string keyOrTypeName, string keyOrValue, bool isValue) { //Console.WriteLine($"***** PropertySet: propName: <{propName}>, keyOrTypeName: <{keyOrTypeName}>, keyOrValue: <{keyOrValue}>"); GetPropertyOrFieldData(propName, keyOrTypeName, out object instance, out PropertyInfo prop, out FieldInfo field); var setValue = prop != null ? (Action <object, object>)prop.SetValue : field.SetValue; if (!isValue) { setValue(instance, _scenario.GetEx <object>(keyOrValue)); return; } ITypeProcessor processor = _manager.GetProcessorFor(prop.PropertyType); CheckProcessorExists(prop.PropertyType, processor); processor.SetValue(keyOrValue); setValue(instance, processor.Value); }
private void PropertyTestImpl( string propNameOrValueKey, string keyOrTypeName, string keyOrValue, string stepText, bool isValue, string propName2 = null, string keyOrTypeName2 = null) { string compareText = GetCompareText(stepText); CompareOp compareOp = ToCompareOp(compareText); //Console.WriteLine($"***** PropertyTest: propName: <{propNameOrValueKey}>, keyOrTypeName: <{keyOrTypeName}>, keyOrValue: <{keyOrValue}>, compare: <{compareOp}>, propName2: <{propName2}>, keyOrTypeName2: <{keyOrTypeName2}>"); GetPropertyOrFieldData(propNameOrValueKey, keyOrTypeName, out object instance, out PropertyInfo prop, out FieldInfo field); GetPropertyOrFieldData(propName2, keyOrTypeName2, out object instance2, out PropertyInfo prop2, out FieldInfo field2); ITypeProcessor actual = GetProcessorFor(propNameOrValueKey, instance, prop, field); ITypeProcessor expected = null; if (!IsSingleCompareOp(compareOp)) { if (isValue) { expected = actual.Clone(); expected.SetValue(keyOrValue); } else { expected = GetProcessorFor(keyOrValue, instance2, prop2, field2); } } if (expected == null) { expected = new ObjectProcessor(_scenario); expected.PostCurrentValue(); } DoCompare(actual, expected, compareOp); }
protected static void DoCompare(ITypeProcessor actual, ITypeProcessor expected, CompareOp compareOp) { if (actual == null) { if (expected != null) { actual = expected.Clone(); actual.ReadFromContext(); } } Console.WriteLine($"***** Actual: {(actual != null ? actual.ToString() : "NULL")}"); Console.WriteLine($"***** Expected: {(expected != null ? expected.ToString() : "NULL")}"); if (actual == null || (expected == null && !IsSingleCompareOp(compareOp))) { throw new Exception($"Unable to parse test step."); } actual.CompareTo(expected, compareOp); }
public ITypeProcessor GetCollectionProcessorFor(string dataTypeName, string value) { if (string.IsNullOrEmpty(dataTypeName)) { return(GetCollectionProcessorForValue(null, value)); } IValueProcessor elementProcessor = GetProcessorForDataType(dataTypeName) as IValueProcessor; if (elementProcessor == null) { ThrowMissingTypeName(dataTypeName); } ITypeProcessor processor = GetCollectionProcessorFor(null, elementProcessor); if (value != null) { processor.SetValue(value); } return(processor); }
private void GivenTypeOrKeyCore(string typeNameOrKey, string value) { //Console.WriteLine($"***** Given: type or key: {typeNameOrKey}, value: {value}"); bool isNullValue = value == "null"; ITypeProcessor processor = null; if (isNullValue) { processor = _manager.GetProcessorForDataType(typeNameOrKey) ?? new ObjectProcessor(_scenario); } else { if (_manager.GetProcessorForDataType(typeNameOrKey, out processor)) { processor.PostValue(value); return; } _manager.GetProcessorForValue(value, out processor); } processor.PostCurrentValue(typeNameOrKey); //Console.WriteLine($"***** Processor: {(processor != null ? processor.ToString() : "NULL")}"); }
private void TestCollectionCore(string collectionKind, string resultName, string typeName, string value, string stepText) { string compareText = GetCompareText(stepText); CompareOp compareOp = ToCompareOp(compareText); //Console.WriteLine($"***** Testing Collection: collection kind: <{collectionKind}>, result name: <{resultName}>, type name: <{typeName}>, compare: <{compareOp}>, value: <{value}>"); ITypeProcessor actual = _manager.GetCollectionProcessorForExistingName(resultName); ITypeProcessor expected = null; if (!IsSingleCompareOp(compareOp)) { expected = _manager.GetCollectionProcessorFor(collectionKind, typeName, value); } else if (compareOp == CompareOp.Null || compareOp == CompareOp.NotNull) { if (actual == null) { actual = _manager.GetCollectionProcessorFor(collectionKind, typeName, null); actual?.ReadFromContext(); } } DoCompare(actual, expected, compareOp); }
public abstract void CompareTo(ITypeProcessor other, CompareOp compareOp);
public PropertyMap <TEntity, TProperty> Processor(ITypeProcessor processor) { this.m_typeProcessorFactory = () => processor; return(this); }
public override void CompareTo(ITypeProcessor other, CompareOp compareOp) { IComparer comparer = Context.GetComparer(DataType); T otherValue; T value = (T)Value; string thisString; string otherString; switch (compareOp) { case CompareOp.Null: Assert.IsNull(Value); break; case CompareOp.NotNull: Assert.IsNotNull(Value); break; case CompareOp.Match: thisString = Value.ToString(); otherString = other.Value.ToString(); Assert.IsTrue(Regex.IsMatch(thisString, otherString), $"{thisString} does not match {otherString}"); break; case CompareOp.NotMatch: thisString = Value.ToString(); otherString = other.Value.ToString(); Assert.IsFalse(Regex.IsMatch(thisString, otherString), $"{thisString} matches {otherString}"); break; case CompareOp.Equal: case CompareOp.Equivalent: otherValue = GetValue(other); if (comparer == null) { Assert.AreEqual(otherValue, value); } else { Assert.IsTrue(Comparer.Compare(value, otherValue) == 0, $"Expected {value} = {otherValue}"); } break; case CompareOp.NotEqual: case CompareOp.NotEquivalent: otherValue = GetValue(other); if (comparer == null) { Assert.AreNotEqual(otherValue, value); } else { Assert.IsTrue(Comparer.Compare(value, otherValue) != 0, $"Expected {value} <> {otherValue}"); } break; case CompareOp.Greater: case CompareOp.NotLessOrEqual: otherValue = GetValue(other); Assert.IsTrue(Comparer.Compare(value, otherValue) > 0, $"Expected {value} > {otherValue}"); break; case CompareOp.NotGreater: case CompareOp.LessOrEqual: otherValue = GetValue(other); Assert.IsTrue(Comparer.Compare(value, otherValue) <= 0, $"Expected {value} <= {otherValue}"); break; case CompareOp.NotGreaterOrEqual: case CompareOp.Less: otherValue = GetValue(other); Assert.IsTrue(Comparer.Compare(value, otherValue) < 0, $"Expected {value} < {otherValue}"); break; case CompareOp.GreaterOrEqual: case CompareOp.NotLess: otherValue = GetValue(other); Assert.IsTrue(Comparer.Compare(value, otherValue) >= 0, $"Expected {value} >= {otherValue}"); break; case CompareOp.Contain: case CompareOp.NotContain: case CompareOp.Unknown: default: ThrowCompareOpNotSupported(compareOp); break; } }
public bool GetProcessorForExistingName(string resultName, out ITypeProcessor processor) { processor = GetProcessorForExistingName(resultName); return(processor != null); }
public bool GetProcessorForDataType(string dataTypeName, out ITypeProcessor processor) { processor = GetProcessorForDataType(dataTypeName); return(processor != null); }
public bool GetProcessorForValue(string value, out ITypeProcessor processor) { processor = GetProcessorForValue(value); return(processor != null); }
public bool GetProcessorForDataType(Type dataType, out ITypeProcessor processor) { processor = GetProcessorFor(dataType); return(processor != null); }