protected bool TryGetSymbolicConstant <T>(ExpressionSyntax expression, out SymbolicConstant <T> value, bool acceptSymbolic = true) where T : IComparable <T> { var constant = Model.GetConstantValue(expression); if (!constant.HasValue) { var symbol = Model.GetSymbolInfo(expression).Symbol; if (_DateTimeConstants.TryGetValue(symbol, out var dt) || _DateTimeConstants.TryGetValue(symbol.OriginalDefinition, out dt)) { value = new SymbolicConstant <T>((T)(object)dt); return(true); } if (_DateTimeConstructions.Contains(symbol)) { return(TryCreateDateTime((ObjectCreationExpressionSyntax)expression, out value)); } if (acceptSymbolic) { if (symbol is IFieldSymbol field) { // Symbolic Range value = new SymbolicConstant <T>(field); return(true); } if (symbol is IPropertySymbol property && !property.IsIndexer) { // Symbolic Range value = new SymbolicConstant <T>(property); return(true); } } if (Equals((symbol as ILocalSymbol)?.Type ?? (symbol as IPropertySymbol)?.Type ?? (symbol as IMethodSymbol)?.ReturnType, _DateTimeType)) { ReportDiagnostic(SmartTestsDiagnostics.CreateNotADateCreation(expression)); } else { ReportDiagnostic(SmartTestsDiagnostics.CreateNotAConstantPropertyField(expression)); } value = default; return(false); } value = new SymbolicConstant <T>((T)Convert.ChangeType(constant.Value, typeof(T))); return(true); }
protected bool TryGetConstant(ExpressionSyntax expression, out bool value) { var constant = Model.GetConstantValue(expression); if (!constant.HasValue) { ReportDiagnostic(SmartTestsDiagnostics.CreateNotAConstantPropertyField(expression)); value = false; return(false); } value = (bool)Convert.ChangeType(constant.Value, typeof(bool)); return(true); }
protected bool TryGetConstant <T>(ExpressionSyntax expression, out T value) { var constant = Model.GetConstantValue(expression); if (!constant.HasValue) { var symbol = Model.GetSymbolInfo(expression).Symbol; if (_DateTimeConstants.TryGetValue(symbol, out var dt) || _DateTimeConstants.TryGetValue(symbol.OriginalDefinition, out dt)) { value = (T)(object)dt; return(true); } if (_DateTimeConstructions.Contains(symbol)) { return(TryCreateDateTime((ObjectCreationExpressionSyntax)expression, out value)); } if (Equals((symbol as ILocalSymbol)?.Type ?? (symbol as IPropertySymbol)?.Type ?? (symbol as IMethodSymbol)?.ReturnType, _DateTimeType)) { ReportDiagnostic(SmartTestsDiagnostics.CreateNotADateCreation(expression)); } else { ReportDiagnostic(SmartTestsDiagnostics.CreateNotAConstantPropertyField(expression)); } value = default; return(false); } value = (T)Convert.ChangeType(constant.Value, typeof(T)); return(true); }