private TestClassUnit(Type type) { m_testGroupClass = type; m_attr = GetTestGroupAttribute(type); m_ignoreAttr = GetTestIgnoreAttribute(type); m_testCaseUnits = TestMethodUnit.GetTestCaseUnits(type); name = type.FullName; foreach (MethodInfo methodInfo in type.GetMethods()) { foreach (Attribute attr in methodInfo.GetCustomAttributes(true)) { if ((attr as AssemblyInitializeAttribute) != null) { AssemblyInitMethod = methodInfo; } if ((attr as AssemblyCleanupAttribute) != null) { AssemblyCleanupMethod = methodInfo; } if ((attr as ClassInitializeAttribute) != null) { ClassInitMethod = methodInfo; } if ((attr as ClassCleanupAttribute) != null) { ClassCleanupMethod = methodInfo; } if ((attr as TestInitializeAttribute) != null) { TestInitMethod = methodInfo; } if ((attr as TestCleanupAttribute) != null) { TestCleanupMethod = methodInfo; } } } // default is all enabled enable = true; if (m_testCaseUnits.Length == 0) { enable = false; } //if the Ignore attribute is specified, disable the test group if (m_ignoreAttr != null) { this.Enable = false; } }
public void IgnoreAttributeIgnoresTestUntilDateTimeSpecified() { var ignoreAttribute = new IgnoreAttribute("BECAUSE"); ignoreAttribute.Until = "4242-01-01 12:00:00Z"; ignoreAttribute.ApplyToTest(test); Assert.That(test.RunState, Is.EqualTo(RunState.Ignored)); }
public void IgnoreAttributeWithUntilAddsIgnoreUntilDateProperty() { var ignoreAttribute = new IgnoreAttribute("BECAUSE"); ignoreAttribute.Until = "4242-01-01"; ignoreAttribute.ApplyToTest(test); Assert.That(test.Properties.Get(PropertyNames.IgnoreUntilDate), Is.EqualTo("4242-01-01 00:00:00Z")); }
public void IgnoreAttributeUntilSetsTheReason(string date) { var ignoreAttribute = new IgnoreAttribute("BECAUSE"); ignoreAttribute.Until = date; ignoreAttribute.ApplyToTest(test); Assert.That(test.Properties.Get(PropertyNames.SkipReason), Is.EqualTo("Ignoring until 4242-01-01 00:00:00Z. BECAUSE")); }
public void IgnoreAttributeMarksTestAsRunnableAfterUntilDatePasses() { var ignoreAttribute = new IgnoreAttribute("BECAUSE"); ignoreAttribute.Until = "1492-01-01"; ignoreAttribute.ApplyToTest(test); Assert.That(test.RunState, Is.EqualTo(RunState.Runnable)); }
/// <summary> /// Gets the ignore attribute if present. /// </summary> /// <returns>The ignore attribute if present.</returns> public IgnoreAttribute GetIgnoreAttribute() { if (m_isIgnoreAttributeWasSet) { return(m_ignoreAttribute); } m_isIgnoreAttributeWasSet = true; return(m_ignoreAttribute = PropertyInfo.GetCustomAttribute(typeof(IgnoreAttribute)) as IgnoreAttribute); }
public void TestIgnoreAttribute() { IgnoreAttribute ignore = new IgnoreAttribute(); Assert.IsNull(ignore.Reason); ignore = new IgnoreAttribute("Message"); Assert.AreEqual("Message", ignore.Reason); ignore.Reason = "Message 2"; Assert.AreEqual("Message 2", ignore.Reason); }
public static IList <Option> Create(object obj) { var options = new List <Option>(); foreach (var property in PropertiesOf(obj)) { if (!IgnoreAttribute.IsDefinedOn(property)) { options.Add(new Option(property)); } } return(options); }
static private IgnoreAttribute GetTestIgnoreAttribute(Type type) { IgnoreAttribute ignoreAttr = null; foreach (Attribute attr in type.GetCustomAttributes(true)) { ignoreAttr = attr as IgnoreAttribute; if (null != ignoreAttr) { return(ignoreAttr); } } return(null); }
internal void AutoMapColumnsAction <T>(params Expression <Func <T, object> >[] ignorePropertyExpressions) { VerifyAutoMapAlreadyCalled(); var properties = ReflectionHelper.GetProperties(_data.Item.GetType()); var ignorePropertyNames = new HashSet <string>(); if (ignorePropertyExpressions != null) { foreach (var ignorePropertyExpression in ignorePropertyExpressions) { var ignorePropertyName = new PropertyExpressionParser <T>(_data.Item, ignorePropertyExpression).Name; ignorePropertyNames.Add(ignorePropertyName); } } foreach (var property in properties) { var ignoreProperty = ignorePropertyNames.SingleOrDefault(x => x.Equals(property.Value.Name, StringComparison.CurrentCultureIgnoreCase)); if (ignoreProperty != null) { continue; } //valid DataColumn attribute to skip no operate column. var ats = property.Value.Attributes; var cts = property.Value.GetCustomAttributes(true); bool isBreak = false; foreach (var attr in cts) { IgnoreAttribute ign = attr as IgnoreAttribute; if (ign != null) { isBreak = true; break; } } if (isBreak) { continue; } var propertyType = ReflectionHelper.GetPropertyType(property.Value); var propertyValue = ReflectionHelper.GetPropertyValue(_data.Item, property.Value); ColumnAction(property.Value.Name, propertyValue, propertyType, DataTypes.Object, 0); } }
public void DynamicMethod_SkipVisibilityCheck_ReadUserCodePrivateField() { var parameter = Ast.Parameter(typeof(IgnoreAttribute), "attribute"); var lambda = Ast.Lambda <Func <IgnoreAttribute, string> > ( Ast.Field( parameter, typeof(IgnoreAttribute).GetField("reason", BindingFlags.NonPublic | BindingFlags.Instance)), parameter); var reader = lambda.Compile(); var attribute = new IgnoreAttribute("foo"); Assert.AreEqual("foo", reader(attribute)); }
public TestSuite(Type type) { TestObject = Reflect.Construct(type, null); this.name = type.Name; this.fullName = type.FullName; object[] attrs = type.GetCustomAttributes(typeof(PropertyAttribute), true); foreach (PropertyAttribute attr in attrs) { foreach (DictionaryEntry entry in attr.Properties) { this.Properties[entry.Key] = entry.Value; } } IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(type, typeof(IgnoreAttribute)); if (ignore != null) { this.runState = RunState.Ignored; this.ignoreReason = ignore.Reason; } if (!InvalidTestSuite(type)) { foreach (MethodInfo method in type.GetMethods()) { if (TestCase.IsTestMethod(method)) { this.AddTest(new TestCase(method, TestObject)); } else if (IsTestFixtureSetup(method)) { TestFixtureSetUpMethod = method; } else if (IsTestFixtureTearDownAttribute(method)) { TestFixtureTearDownMethod = method; } } } }
/// <summary> /// 初始化策略 /// </summary> protected void InitStrategys() { if (Strategys == null) { Strategys = MetaDataManager.Type.Find(item => item.GetInterfaces().Contains(typeof(TStrategy)) || item.IsSubclassOf(typeof(TStrategy))) .Where(item => !item.IsAbstract) .SelectMany(item => { var descs = item.GetStrategys(); if (descs == null || IgnoreAttribute.IsDefined(item)) { return(new List <EditableKeyValuePair <string, Type> >()); } return(descs.Select(desc => new EditableKeyValuePair <string, Type>(desc, item))); }) .DistinctBy(item => item.Key) .Where(item => !string.IsNullOrEmpty(item.Key)) .ToDictionary(item => item.Key, item => item.Value); } }
private void Initialize(string name, object fixture) { this.name = name; this.fixture = fixture; this.fullName = this.fixture.GetType().FullName + "." + name; this.method = Reflect.GetMethod(this.fixture.GetType(), name); if (this.method == null) { this.runState = RunState.NotRunnable; } else { IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(this.method, typeof(IgnoreAttribute)); if (ignore != null) { this.runState = RunState.Ignored; this.ignoreReason = ignore.Reason; } } }
protected virtual void Init() { HasKey(item => item.Id); Property(item => item.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); typeof(TEntity).GetProperties().ForEach(item => { if (!IgnoreAttribute.IsDefined(item)) { return; } var param = Expression.Parameter(typeof(TEntity), "item"); var prorerty = Expression.Property(param, item.Name); //创建表达式 item=>item.property dynamic propertExpression = Expression.Lambda(prorerty, param); Ignore(propertExpression); }); ToTable(typeof(TEntity).Name); }
public TestSuite(Type type) { this.name = type.Name; this.fullName = type.FullName; object[] attrs = type.GetCustomAttributes(typeof(PropertyAttribute), true); foreach (PropertyAttribute attr in attrs) { foreach (var entry in attr.Properties) { this.Properties[entry.Key] = entry.Value; } } IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(type, typeof(IgnoreAttribute)); if (ignore != null) { this.runState = RunState.Ignored; this.ignoreReason = ignore.Reason; } if (!InvalidTestSuite(type)) { foreach (MethodInfo method in type.GetMethods()) { if (TestCase.IsTestMethod(method)) { this.AddTest(new TestCase(method)); } //{ // ITest test = TestCase.HasValidSignature(method) // ? (ITest)new TestCase(method) // : (ITest)new InvalidTestCase(method.Name, // "Test methods must have signature void MethodName()"); // this.AddTest(test); //} } } }
private TestMethodUnit(MethodInfo testCase) { m_testCase = testCase; name = testCase.Name; m_tagAttrs = new List <string>(); foreach (Attribute attr in testCase.GetCustomAttributes(true)) { if (null != (attr as TestMethodAttribute)) { m_attr = (TestMethodAttribute)attr; } if (null != (attr as TimeoutAttribute)) { m_timeoutAttr = (TimeoutAttribute)attr; } if (null != (attr as TestCategoryAttribute)) { m_tagAttrs.Add(((TestCategoryAttribute)attr).TestCategories[0]); } if (null != (attr as IgnoreAttribute)) { m_ignoreAttr = (IgnoreAttribute)attr; } } //default is all enabled if not ignored if (Ignore) { enable = false; } else { enable = true; } }
private void Initialize(MethodInfo method, object fixture) { this.name = method.Name; this.method = method; this.fullName = method.ReflectedType.FullName + "." + name; this.fixture = fixture; if (fixture == null) { this.fixture = Reflect.Construct(method.ReflectedType, null); } if (!HasValidSignature(method)) { this.runState = RunState.NotRunnable; this.ignoreReason = "Test methods must have signature void MethodName()"; } else { IgnoreAttribute ignore = (IgnoreAttribute)Reflect.GetAttribute(this.method, typeof(IgnoreAttribute)); if (ignore != null) { this.runState = RunState.Ignored; this.ignoreReason = ignore.Reason; } } foreach (MethodInfo m in method.ReflectedType.GetMethods()) { if (Reflect.HasAttribute(m, typeof(SetUpAttribute))) { this.setup = m; } if (Reflect.HasAttribute(m, typeof(TearDownAttribute))) { this.teardown = m; } } }
public static Dictionary <string, IPropertyConverter <T> > ActivatePropertyConverters <T>(IServiceProvider services) { var ownerType = Typeof <T> .Raw; var properties = ownerType.GetProperties(); var propertyConverters = new Dictionary <string, IPropertyConverter <T> >( properties.Length, StringUtils.IgnoreCaseComparer); var converters = (IConvertersCollection)services.GetService(typeof(IConvertersCollection)); foreach (var property in properties) { if (IgnoreAttribute.IsDefined(property)) { continue; } IPropertyConverter <T> propertyConverter; if (ConverterAttribute.IsDefined(property)) { var converterType = property .GetCustomAttribute <ConverterAttribute>() .GetConverterType(ownerType, property); var injections = new LocalList <object>(property, ownerType); propertyConverter = (IPropertyConverter <T>)services.Activate(converterType, injections); } else { propertyConverter = new PropertyConverter <T>(property, converters.Get(property.PropertyType)); } propertyConverters.Add(property.Name, propertyConverter); } return(propertyConverters); }
public override void Reflect(RunInvokerTree tree, RunInvokerVertex parent, Type t) { object fixture = null; try { // Check if fixture is ignored or explicit IgnoreAttribute ignore = null; if (TypeHelper.HasCustomAttribute(t, typeof(IgnoreAttribute))) { ignore = TypeHelper.GetFirstCustomAttribute(t, typeof(IgnoreAttribute)) as IgnoreAttribute; } ExplicitAttribute expl = null; if (TypeHelper.HasCustomAttribute(t, typeof(ExplicitAttribute))) { expl = TypeHelper.GetFirstCustomAttribute(t, typeof(ExplicitAttribute)) as ExplicitAttribute; } foreach (MethodInfo method in TypeHelper.GetAttributedMethods(t, typeof(CombinatorialTestAttribute))) { if (fixture == null) { fixture = TypeHelper.CreateInstance(t); } this.ReflectTestMethod(tree, parent, fixture, method, ignore, expl); } } finally { IDisposable disposable = fixture as IDisposable; if (disposable != null) { disposable.Dispose(); } } }
public void IgnoreAttributeWithInvalidDateThrowsException() { var ignoreAttribute = new IgnoreAttribute("BECAUSE"); Assert.Throws <FormatException>(() => ignoreAttribute.Until = "Thursday the twenty fifth of December"); }
private TestClassUnit(Type type) { m_testGroupClass = type; m_attr = GetTestGroupAttribute(type); m_ignoreAttr = GetTestIgnoreAttribute(type); m_testCaseUnits = TestMethodUnit.GetTestCaseUnits(type); name = type.FullName; foreach (MethodInfo methodInfo in type.GetMethods()) { foreach (Attribute attr in methodInfo.GetCustomAttributes(true)) { if ((attr as AssemblyInitializeAttribute) != null) { AssemblyInitMethod = methodInfo; } if ((attr as AssemblyCleanupAttribute) != null) { AssemblyCleanupMethod = methodInfo; } if ((attr as ClassInitializeAttribute) != null) { ClassInitMethod = methodInfo; } if ((attr as ClassCleanupAttribute) != null) { ClassCleanupMethod = methodInfo; } if ((attr as TestInitializeAttribute) != null) { TestInitMethod = methodInfo; } if ((attr as TestCleanupAttribute) != null) { TestCleanupMethod = methodInfo; } } } // default is all enabled enable = true; if (m_testCaseUnits.Length == 0) { enable = false; } //if the Ignore attribute is specified, disable the test group if (m_ignoreAttr != null) { this.Enable = false; } }
public void DynamicMethod_SkipVisibilityCheck_ReadUserCodePrivateField () { var parameter = Ast.Parameter (typeof (IgnoreAttribute), "attribute"); var lambda = Ast.Lambda<Func<IgnoreAttribute, string>> ( Ast.Field ( parameter, typeof (IgnoreAttribute).GetField ("reason", BindingFlags.NonPublic | BindingFlags.Instance)), parameter); var reader = lambda.Compile (); var attribute = new IgnoreAttribute ("foo"); Assert.AreEqual ("foo", reader (attribute)); }
/// <summary> /// Applies attribute configurations to the map. /// </summary> /// <param name="memberMap">The member map.</param> protected virtual void ApplyAttributes(MemberMap memberMap) { var member = memberMap.Data.Member; //if( member.GetCustomAttribute( typeof( IndexAttribute ) ) is IndexAttribute indexAttribute ) IndexAttribute indexAttribute = member.GetCustomAttributes(typeof(IndexAttribute), false).FirstOrDefault() as IndexAttribute; if (indexAttribute != null) { memberMap.Data.Index = indexAttribute.Index; memberMap.Data.IndexEnd = indexAttribute.IndexEnd; memberMap.Data.IsIndexSet = true; } //if( member.GetCustomAttribute( typeof( NameAttribute ) ) is NameAttribute nameAttribute ) NameAttribute nameAttribute = member.GetCustomAttributes(typeof(NameAttribute), false).FirstOrDefault() as NameAttribute; if (nameAttribute != null) { memberMap.Data.Names.Clear(); memberMap.Data.Names.AddRange(nameAttribute.Names); memberMap.Data.IsNameSet = true; } //if( member.GetCustomAttribute( typeof( NameIndexAttribute ) ) is NameIndexAttribute nameIndexAttribute ) NameIndexAttribute nameIndexAttribute = member.GetCustomAttributes(typeof(NameIndexAttribute), false).FirstOrDefault() as NameIndexAttribute; if (nameIndexAttribute != null) { memberMap.Data.NameIndex = nameIndexAttribute.NameIndex; } //if( member.GetCustomAttribute( typeof( IgnoreAttribute ) ) is IgnoreAttribute ignoreAttribute ) IgnoreAttribute ignoreAttribute = member.GetCustomAttributes(typeof(IgnoreAttribute), false).FirstOrDefault() as IgnoreAttribute; if (ignoreAttribute != null) { memberMap.Data.Ignore = true; } //if( member.GetCustomAttribute( typeof( DefaultAttribute ) ) is DefaultAttribute defaultAttribute ) DefaultAttribute defaultAttribute = member.GetCustomAttributes(typeof(DefaultAttribute), false).FirstOrDefault() as DefaultAttribute; if (defaultAttribute != null) { memberMap.Data.Default = defaultAttribute.Default; memberMap.Data.IsDefaultSet = true; } //if( member.GetCustomAttribute( typeof( ConstantAttribute ) ) is ConstantAttribute constantAttribute ) ConstantAttribute constantAttribute = member.GetCustomAttributes(typeof(ConstantAttribute), false).FirstOrDefault() as ConstantAttribute; if (constantAttribute != null) { memberMap.Data.Constant = constantAttribute.Constant; memberMap.Data.IsConstantSet = true; } //if( member.GetCustomAttribute( typeof( TypeConverterAttribute ) ) is TypeConverterAttribute typeConverterAttribute ) TypeConverterAttribute typeConverterAttribute = member.GetCustomAttributes(typeof(TypeConverterAttribute), false).FirstOrDefault() as TypeConverterAttribute; if (typeConverterAttribute != null) { memberMap.Data.TypeConverter = typeConverterAttribute.TypeConverter; } //if( member.GetCustomAttribute( typeof( CultureInfoAttribute ) ) is CultureInfoAttribute cultureInfoAttribute ) CultureInfoAttribute cultureInfoAttribute = member.GetCustomAttributes(typeof(CultureInfoAttribute), false).FirstOrDefault() as CultureInfoAttribute; if (cultureInfoAttribute != null) { memberMap.Data.TypeConverterOptions.CultureInfo = cultureInfoAttribute.CultureInfo; } //if( member.GetCustomAttribute( typeof( DateTimeStylesAttribute ) ) is DateTimeStylesAttribute dateTimeStylesAttribute ) DateTimeStylesAttribute dateTimeStylesAttribute = member.GetCustomAttributes(typeof(DateTimeStylesAttribute), false).FirstOrDefault() as DateTimeStylesAttribute; if (dateTimeStylesAttribute != null) { memberMap.Data.TypeConverterOptions.DateTimeStyle = dateTimeStylesAttribute.DateTimeStyles; } //if( member.GetCustomAttribute( typeof( NumberStylesAttribute ) ) is NumberStylesAttribute numberStylesAttribute ) NumberStylesAttribute numberStylesAttribute = member.GetCustomAttributes(typeof(NumberStylesAttribute), false).FirstOrDefault() as NumberStylesAttribute; if (numberStylesAttribute != null) { memberMap.Data.TypeConverterOptions.NumberStyle = numberStylesAttribute.NumberStyles; } FormatAttribute formatAttribute = member.GetCustomAttributes(typeof(FormatAttribute), false).FirstOrDefault() as FormatAttribute; if (formatAttribute != null) { memberMap.Data.TypeConverterOptions.Formats = formatAttribute.Formats; } BooleanTrueValuesAttribute booleanTrueValuesAttribute = member.GetCustomAttributes(typeof(BooleanTrueValuesAttribute), false).FirstOrDefault() as BooleanTrueValuesAttribute; if (booleanTrueValuesAttribute != null) { memberMap.Data.TypeConverterOptions.BooleanTrueValues.Clear(); memberMap.Data.TypeConverterOptions.BooleanTrueValues.AddRange(booleanTrueValuesAttribute.TrueValues); } BooleanFalseValuesAttribute booleanFalseValuesAttribute = member.GetCustomAttributes(typeof(BooleanFalseValuesAttribute), false).FirstOrDefault() as BooleanFalseValuesAttribute; if (booleanFalseValuesAttribute != null) { memberMap.Data.TypeConverterOptions.BooleanFalseValues.Clear(); memberMap.Data.TypeConverterOptions.BooleanFalseValues.AddRange(booleanFalseValuesAttribute.FalseValues); } NullValuesAttribute nullValuesAttribute = member.GetCustomAttributes(typeof(NullValuesAttribute), false).FirstOrDefault() as NullValuesAttribute; if (nullValuesAttribute != null) { memberMap.Data.TypeConverterOptions.NullValues.Clear(); memberMap.Data.TypeConverterOptions.NullValues.AddRange(nullValuesAttribute.NullValues); } }
public static string GetIgnoreReason(MemberInfo member) { IgnoreAttribute attribute = GetIgnoreAttribute(member); return(attribute == null ? "no reason" : attribute.Reason); }
private void ReflectTestMethod( RunInvokerTree tree, RunInvokerVertex parent, object fixture, MethodInfo method, IgnoreAttribute ignore, ExplicitAttribute expl) { // Check if fixture/method is ignored/explicit if (ignore == null && TypeHelper.HasCustomAttribute(method, typeof(IgnoreAttribute))) { ignore = TypeHelper.GetFirstCustomAttribute(method, typeof(IgnoreAttribute)) as IgnoreAttribute; } if (expl == null && TypeHelper.HasCustomAttribute(method, typeof(ExplicitAttribute))) { expl = TypeHelper.GetFirstCustomAttribute(method, typeof(ExplicitAttribute)) as ExplicitAttribute; } if (ignore != null) { // Do not generate unnecessary test cases IgnoredLoadingRunInvoker invoker = new IgnoredLoadingRunInvoker(this, method, ignore.Description); tree.AddChild(parent, invoker); } else if (expl != null) { // Do not generate unnecessary test cases IgnoredLoadingRunInvoker invoker = new IgnoredLoadingRunInvoker(this, method, expl.Description); tree.AddChild(parent, invoker); } else { CombinatorialTestAttribute testAttribute = TypeHelper.GetFirstCustomAttribute(method, typeof(CombinatorialTestAttribute)) as CombinatorialTestAttribute; ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length == 0) { Exception ex = new Exception("No parameters"); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } // create the models DomainCollection domains = new DomainCollection(); Type[] parameterTypes = new Type[parameters.Length]; int index = 0; foreach (ParameterInfo parameter in parameters) { parameterTypes[index] = parameter.ParameterType; DomainCollection pdomains = new DomainCollection(); foreach (UsingBaseAttribute usingAttribute in parameter.GetCustomAttributes(typeof(UsingBaseAttribute), true)) { try { usingAttribute.GetDomains(pdomains, parameter, fixture); } catch (Exception ex) { Exception pex = new Exception("Failed while loading domains from parameter " + parameter.Name, ex); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, pex, method); tree.AddChild(parent, invoker); } } if (pdomains.Count == 0) { Exception ex = new Exception("Could not find domain for argument " + parameter.Name); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } domains.Add(Domains.ToDomain(pdomains)); index++; } // get the validator method if any MethodInfo validator = null; if (testAttribute.TupleValidatorMethod != null) { validator = fixture.GetType().GetMethod(testAttribute.TupleValidatorMethod, parameterTypes); if (validator == null) { Exception ex = new Exception("Could not find validator method " + testAttribute.TupleValidatorMethod); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } } // we make a cartesian product of all those foreach (ITuple tuple in Products.Cartesian(domains)) { // create data domains DomainCollection tdomains = new DomainCollection(); for (int i = 0; i < tuple.Count; ++i) { IDomain dm = (IDomain)tuple[i]; tdomains.Add(dm); } // computing the pairwize product foreach (ITuple ptuple in testAttribute.GetProduct(tdomains)) { if (validator != null) { bool isValid = (bool)validator.Invoke(fixture, ptuple.ToObjectArray()); if (!isValid) { continue; } } TupleRunInvoker invoker = new TupleRunInvoker(this, method, tuple, ptuple); IRunInvoker dinvoker = DecoratorPatternAttribute.DecoreInvoker(method, invoker); tree.AddChild(parent, dinvoker); } } } }
private void ReflectTestMethod( RunInvokerTree tree, RunInvokerVertex parent, object fixture, MethodInfo method, IgnoreAttribute ignore) { // Check if fixture or method is ignored if (ignore == null && TypeHelper.HasCustomAttribute(method, typeof(IgnoreAttribute))) { ignore = TypeHelper.GetFirstCustomAttribute(method, typeof(IgnoreAttribute)) as IgnoreAttribute; } if (ignore != null) { // Do not generate unnecessary test cases IgnoredLoadingRunInvoker invoker = new IgnoredLoadingRunInvoker(this, method, ignore.Description); tree.AddChild(parent, invoker); } else { CombinatorialTestAttribute testAttribute = TypeHelper.GetFirstCustomAttribute(method, typeof(CombinatorialTestAttribute)) as CombinatorialTestAttribute; ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length == 0) { Exception ex = new Exception("No parameters"); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } // create the models DomainCollection domains = new DomainCollection(); Type[] parameterTypes = new Type[parameters.Length]; int index = 0; foreach (ParameterInfo parameter in parameters) { parameterTypes[index] = parameter.ParameterType; DomainCollection pdomains = new DomainCollection(); foreach (UsingBaseAttribute usingAttribute in parameter.GetCustomAttributes(typeof(UsingBaseAttribute), true)) { try { usingAttribute.GetDomains(pdomains, parameter, fixture); } catch (Exception ex) { Exception pex = new Exception("Failed while loading domains from parameter " + parameter.Name, ex); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, pex, method); tree.AddChild(parent, invoker); } } if (pdomains.Count == 0) { Exception ex = new Exception("Could not find domain for argument " + parameter.Name); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } domains.Add(Domains.ToDomain(pdomains)); index++; } // get the validator method if any MethodInfo validator = null; if (testAttribute.TupleValidatorMethod != null) { validator = fixture.GetType().GetMethod(testAttribute.TupleValidatorMethod, parameterTypes); if (validator == null) { Exception ex = new Exception("Could not find validator method " + testAttribute.TupleValidatorMethod); MethodFailedLoadingRunInvoker invoker = new MethodFailedLoadingRunInvoker(this, ex, method); tree.AddChild(parent, invoker); return; } } // we make a cartesian product of all those foreach (ITuple tuple in Products.Cartesian(domains)) { // create data domains DomainCollection tdomains = new DomainCollection(); for (int i = 0; i < tuple.Count; ++i) { IDomain dm = (IDomain)tuple[i]; tdomains.Add(dm); } // computing the pairwize product foreach (ITuple ptuple in testAttribute.GetProduct(tdomains)) { if (validator != null) { bool isValid = (bool)validator.Invoke(fixture, ptuple.ToObjectArray()); if (!isValid) continue; } TupleRunInvoker invoker = new TupleRunInvoker(this, method, tuple, ptuple); IRunInvoker dinvoker = DecoratorPatternAttribute.DecoreInvoker(method, invoker); tree.AddChild(parent, dinvoker); } } } }
public static TableMapper ToMapper <T>() where T : BaseEntity { Type type = typeof(T); TableAttribute tableAttribute = type.GetCustomAttribute <TableAttribute>(); string tableName; if (tableAttribute != null && !string.IsNullOrWhiteSpace(tableAttribute.Name)) { tableName = tableAttribute.Name.Trim(); } else { tableName = GetName(type.Name); } TableMapper tableMapper = new TableMapper() { TableName = tableName, Type = type, TypeName = type.Name }; List <PropertyInfo> pList = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList(); foreach (PropertyInfo property in pList) { // 检查 IgnoreAttribute IgnoreAttribute ignore = property.GetCustomAttribute <IgnoreAttribute>(); if (ignore != null) { continue; } // 检查 ColumnAttribute bool isKey; bool isAuto = false; KeyAttribute key = property.GetCustomAttribute <KeyAttribute>(); if (key != null) { isKey = true; isAuto = key.IsAuto; } else if (string.Equals(property.Name, "id", StringComparison.OrdinalIgnoreCase)) { // 当属性名为ID,但又未指定Key特性时,按以下规则判断,如果是整数,则为自动增量,否则不是自动增量。 string propertyTypeName = property.PropertyType.FullName; if (propertyTypeName == "System.Int32" || propertyTypeName == "System.UInt32" || propertyTypeName == "System.Int64" || propertyTypeName == "System.UInt64") { isAuto = true; } else { isAuto = false; } isKey = true; } else { isKey = false; isAuto = false; } ColumnAttribute columnAttribute = property.GetCustomAttribute <ColumnAttribute>(); IndexAttribute indexAttribute = property.GetCustomAttribute <IndexAttribute>(); string columnName = ""; BaseValueConvert valueConvert = null; if (columnAttribute != null) { // 获取列名 if (!string.IsNullOrWhiteSpace(columnAttribute.Name)) { columnName = columnAttribute.Name.Trim(); } else { columnName = GetName(property.Name); } // 获取 ValueConvert if (columnAttribute.Convert != null) { Type convertType = columnAttribute.Convert; if (convertType.IsAbstract) { throw new Exception($"{property.Name} value convert cannot be an abstract class."); } if (!convertType.IsSubclassOf(typeof(BaseValueConvert))) { string name = typeof(BaseValueConvert).Name; throw new Exception($"{property.Name} value convert must inherit {name}."); } valueConvert = Activator.CreateInstance(convertType) as BaseValueConvert; } } else { columnName = GetName(property.Name); } // 创建映射实例 ColumnMapper columnMapper = new ColumnMapper() { ColumnName = columnName, IsPrimarykey = isKey, PropertyInfo = property, PropertyName = property.Name, ValueConvert = valueConvert, IsAuto = isAuto, IsEnum = property.PropertyType.IsEnum, IsGuidString = property.PropertyType == typeof(string) && columnName.ToLower().Contains("id"), IsCanNull = IsNullable(property.PropertyType), IsIndex = indexAttribute != null, DbType = (columnAttribute != null && !string.IsNullOrWhiteSpace(columnAttribute.DbType)) ? columnAttribute.DbType : "", DbDefaultValue = (columnAttribute != null && !string.IsNullOrWhiteSpace(columnAttribute.DbDefaultValue)) ? columnAttribute.DbDefaultValue : "" }; tableMapper.Add(columnMapper); } return(tableMapper); }