public void Test_Interface_On_Subclass() { var type1 = typeof(Sub); var type2 = typeof(ITest); Assert.IsTrue(TypeIs.Is(type1, type2)); }
public void Test_Nullable_Reverse() { var type1 = typeof(int?); var type2 = typeof(int); Assert.IsTrue(TypeIs.Is(type1, type2)); }
public void Test_Types_Are_Equal() { var type1 = typeof(Base); var type2 = typeof(Base); Assert.IsTrue(TypeIs.Is(type1, type2)); }
public void Test_Interface() { var type1 = typeof(Base); var type2 = typeof(ITest); Assert.IsTrue(TypeIs.Is(type1, type2)); }
public void Test_Types_Are_Not_Equal() { var type1 = typeof(DateTime); var type2 = typeof(Base); Assert.IsFalse(TypeIs.Is(type1, type2)); }
public void SelectsTypeIs() { var spec = new TypeIs<Attribute>(typeof(FlagsAttribute)); Assert.That(spec.IsSatisfiedBy(new FlagsAttribute()), Is.True); Assert.That(spec.IsSatisfiedBy(new ExplicitAttribute()), Is.False); }
/// <summary> /// Is /// </summary> /// <param name="s"></param> /// <param name="type"></param> /// <param name="action"></param> /// <param name="ignoreCase"></param> /// <param name="format"></param> /// <param name="numberStyle"></param> /// <param name="dateTimeStyle"></param> /// <param name="provider"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> public static bool Is(this string s, Type type, IgnoreCase ignoreCase = IgnoreCase.FALSE, Action <object> action = null, string format = null, NumberStyles?numberStyle = null, DateTimeStyles?dateTimeStyle = null, IFormatProvider provider = null) { if (type is null) { throw new ArgumentNullException(nameof(type)); } __unsupportedTypeCheck(type, out var typeIsAssignableFromEncoding); return(TypeIs.__enumIs(s, type, action, ignoreCase) || TypeIs.__charIs(s, type, action) || TypeIs.__numericIs(s, type, action, numberStyle, provider) || TypeIs.__booleanIs(s, type, action) || TypeIs.__dateTimeIs(s, type, action, format, dateTimeStyle, provider) || TypeIs.__dateTimeOffsetIs(s, type, action, format, dateTimeStyle, provider) || TypeIs.__timeSpanIs(s, type, action, format, provider) || TypeIs.__guidIs(s, type, action, format) || TypeIs.__versionIs(s, type, action) || TypeIs.__ipAddressIs(s, type, action) || TypeIs.__encodingIs(s, action, typeIsAssignableFromEncoding)); void __unsupportedTypeCheck(Type t, out bool flag) { flag = typeof(Encoding).IsAssignableFrom(t); if (!t.IsValueType && !flag && t == typeof(Version) && t == typeof(IPAddress)) { throw new ArgumentException("Unsupported type"); } } }
public void Test_SubType() { var type1 = typeof(Sub); var type2 = typeof(Base); Assert.IsTrue(TypeIs.Is(type1, type2)); Assert.IsFalse(TypeIs.Is(type2, type1)); }
public void TypeIsCriteria() { var spec = new TypeIs<Attribute>(typeof(FlagsAttribute)); var criteria = spec.Criteria; Assert.That(criteria.Body, Is.TypeOf<TypeBinaryExpression>()); var binary = (TypeBinaryExpression)criteria.Body; Assert.That(binary.TypeOperand, Is.EqualTo(typeof(FlagsAttribute))); Assert.That(binary.Expression, Is.AssignableTo<ParameterExpression>()); ExpressionWriter.Write(criteria); }
public void IsOutputType(INamedType type) { /* Given */ /* When */ var isOutput = TypeIs.IsOutputType(type); var isOutputAsNonNull = TypeIs.IsOutputType( new NonNull(type)); var isOutputAsList = TypeIs.IsOutputType( new List(type)); /* Then */ Assert.True(isOutput); Assert.True(isOutputAsNonNull); Assert.True(isOutputAsList); }
public TypeTracker(ISchema schema) { EnterOperationDefinition = node => { var root = node.Operation switch { OperationType.Query => schema.Query, OperationType.Mutation => schema.Mutation, OperationType.Subscription => schema.Subscription, _ => throw new ArgumentOutOfRangeException() }; Types.Push(root); }; LeaveOperationDefinition = node => { Types.TryPop(out _); }; EnterSelectionSet = node => { ParentTypes.Push(CurrentType); }; LeaveSelectionSet = node => { ParentTypes.TryPop(out _); }; EnterFieldSelection = node => { if (ParentType is not null) { var fieldDefinition = schema.GetField(ParentType.Name, node.Name); FieldDefinitions.Push(fieldDefinition ?? null); if (fieldDefinition?.Type is not null) { var fieldTypeDefinition = Ast.UnwrapAndResolveType(schema, fieldDefinition.Type); if (fieldTypeDefinition is not null && TypeIs.IsOutputType(fieldTypeDefinition)) { Types.Push(fieldTypeDefinition); } else { Types.Push(null); } } else { Types.Push(null); } }
public InputObjectField( IType type, string description = null, object defaultValue = null, IEnumerable <DirectiveInstance> directives = null) { if (!TypeIs.IsInputType(type)) { throw new ArgumentOutOfRangeException( $" Type '{type}' is not valid input type"); } Type = type; Description = description ?? string.Empty; DefaultValue = defaultValue; _directives = new DirectiveList(directives); }
public static Block DoRestoreTypeIs(Block hir) { if (hir.Family().OfType <TypeAs>().None()) { return(hir); } else { return(hir.Transform((Operator op) => { var is_rel = op.OperatorType.IsRelational(); if (is_rel) { var bin = op.AssertCast <BinaryOperator>(); var opt = bin.OperatorType; var needs_xform = bin.Lhs is TypeAs ^ bin.Rhs is TypeAs; if (needs_xform) { var type_as = bin.Lhs as TypeAs ?? bin.Rhs as TypeAs; var other = bin.Lhs == type_as ? bin.Rhs : bin.Lhs; other.AssertCast <Const>().AssertThat(c => c.Value == null); var pos = opt == OperatorType.NotEqual || opt == OperatorType.GreaterThan; var neg = opt == OperatorType.Equal || opt == OperatorType.LessThanOrEqual; (pos || neg).AssertTrue(); var type_is = new TypeIs(type_as.Type, type_as.Target) as Expression; return pos ? type_is : Operator.Negate(type_is); } else { return op.DefaultTransform(); } } else { return op.DefaultTransform(); } }).AssertCast <Block>()); } }
public static Block DoRestoreTypeIs(Block hir) { if (hir.Family().OfType<TypeAs>().None()) return hir; else { return hir.Transform((Operator op) => { var is_rel = op.OperatorType.IsRelational(); if (is_rel) { var bin = op.AssertCast<BinaryOperator>(); var opt = bin.OperatorType; var needs_xform = bin.Lhs is TypeAs ^ bin.Rhs is TypeAs; if (needs_xform) { var type_as = bin.Lhs as TypeAs ?? bin.Rhs as TypeAs; var other = bin.Lhs == type_as ? bin.Rhs : bin.Lhs; other.AssertCast<Const>().AssertThat(c => c.Value == null); var pos = opt == OperatorType.NotEqual || opt == OperatorType.GreaterThan; var neg = opt == OperatorType.Equal || opt == OperatorType.LessThanOrEqual; (pos || neg).AssertTrue(); var type_is = new TypeIs(type_as.Type, type_as.Target) as Expression; return pos ? type_is : Operator.Negate(type_is); } else { return op.DefaultTransform(); } } else { return op.DefaultTransform(); } }).AssertCast<Block>(); } }
public virtual T Visit(TypeIs node) { return(Visit((TypeBinaryExpression)node)); }
protected internal override void TraverseTypeIs(TypeIs typeIs) { Types.Add(typeIs, typeof(bool)); }
protected internal override void TraverseTypeIs(TypeIs typeIs) { Dispatch(typeIs); }
public override bool Visit(TypeIs node) { Visit((TypeBinaryExpression)node); return(true); }
protected internal override Node TransformTypeIs(TypeIs typeIs) { return(Dispatch(typeIs)); }
protected internal override T ReduceTypeIs(TypeIs typeIs) { return(Dispatch(typeIs)); }
public static IReadOnlyDictionary <string, object?> CoerceVariableValues( ISchema schema, OperationDefinition operation, Dictionary <string, object> variableValues) { var coercedValues = new Dictionary <string, object?>(); var variableDefinitions = operation.VariableDefinitions; if (variableDefinitions == null) { return(coercedValues); } foreach (var variableDefinition in variableDefinitions) { var variableName = variableDefinition.Variable.Name; var variableType = variableDefinition.Type; // should be assert? if (!TypeIs.IsInputType(schema, variableType)) { throw new VariableException("Variable is not of input type", variableName, variableType); } var defaultValue = variableDefinition.DefaultValue?.Value; var hasValue = variableValues.ContainsKey(variableName); var value = hasValue ? variableValues[variableName] : null; if (!hasValue && defaultValue != null) { coercedValues[variableName] = Values.CoerceValue( schema, defaultValue, variableType); ; } if (variableType is NonNullType && (!hasValue || value == null)) { throw new ValueCoercionException( $"Variable {variableName} type is non-nullable but value is null or not set", value, variableType); } if (hasValue) { if (value == null) { coercedValues[variableName] = null; } else { coercedValues[variableName] = Values.CoerceValue( schema, value, variableType); } } } return(coercedValues); }
protected internal override Node TransformTypeIs(TypeIs typeIs) { return Dispatch(typeIs); }
/// <summary> /// Determine whether the given string can be of the given type. <br /> /// 判断给定的字符串是否能成为给定的类型。 /// </summary> /// <param name="text"></param> /// <param name="type"></param> /// <param name="matchedCallback"></param> /// <param name="context"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> public static bool Is(this string text, Type type, CastingContext context, Action <object> matchedCallback = null) { if (type is null) { throw new ArgumentNullException(nameof(type)); } if (context is null) { throw new ArgumentNullException(nameof(context)); } if (Types.IsNullableType(type)) { return(text is null || Is(text, TypeConv.GetNonNullableType(type), context, matchedCallback)); } if (!__unsupportedTypeCheck(type, out var typeIsAssignableFromEncoding, out var typeCanBeChecking, out var checkingHandler)) { return(false); } if (typeCanBeChecking) { return(__customChecking(checkingHandler)); } return(TypeIs.__enumIs(text, type, matchedCallback, context.IgnoreCase) .Or(() => TypeIs.__charIs(text, type, matchedCallback)) .Or(() => TypeIs.__numericIs(text, type, matchedCallback, context.NumberStyles, context.FormatProvider)) .Or(() => TypeIs.__booleanIs(text, type, matchedCallback)) .Or(() => TypeIs.__dateTimeIs(text, type, matchedCallback, context.Format, context.DateTimeStyles, context.FormatProvider)) .Or(() => TypeIs.__dateTimeOffsetIs(text, type, matchedCallback, context.Format, context.DateTimeStyles, context.FormatProvider)) .Or(() => TypeIs.__timeSpanIs(text, type, matchedCallback, context.Format, context.FormatProvider)) .Or(() => TypeIs.__guidIs(text, type, matchedCallback, context.Format)) .Or(() => TypeIs.__versionIs(text, type, matchedCallback)) .Or(() => TypeIs.__ipAddressIs(text, type, matchedCallback)) .Or(() => TypeIs.__encodingIs(text, matchedCallback, typeIsAssignableFromEncoding))); // ReSharper disable once InconsistentNaming bool __unsupportedTypeCheck(Type t, out bool encodingFlag, out bool checkingFlag, out Func <object, bool> checker) { encodingFlag = t == typeof(Encoding) || TypeReflections.IsTypeDerivedFrom(t, typeof(Encoding), TypeDerivedOptions.CanAbstract); checkingFlag = CustomConvertManager.TryGetChecker(TypeClass.StringClazz, t, out checker); return(t.IsValueType .Or(encodingFlag) .Or(checkingFlag) .Or(() => t == typeof(Version)) .Or(() => t == typeof(IPAddress))); } // ReSharper disable once InconsistentNaming bool __customChecking(Func <object, bool> handler) { var result = handler?.Invoke(text) ?? false; result.IfTrue(matchedCallback, text); return(result); } }
protected internal virtual void TraverseTypeIs(TypeIs typeIs) { typeIs.Unsupported(); }
protected internal override void TraverseTypeIs(TypeIs typeIs) { Traverse(typeIs.Target); _writer.Write(" is "); _writer.Write(typeIs.Type == null ? "?" : typeIs.Type.GetCSharpRef(ToCSharpOptions.Informative)); }
public void Test_Generic() { Assert.IsTrue(TypeIs.Is <Base>(typeof(Base))); }
protected internal virtual Node TransformTypeIs(TypeIs typeIs) { return(typeIs.AcceptTransformer(this, true)); }
protected internal virtual Node TransformTypeIs(TypeIs typeIs) { return typeIs.AcceptTransformer(this, true); }