public void SetUp() { var generationContext = new GenerationContext(); _frameworkSet = new FrameworkSet(new NUnit3TestFramework(), new NSubstituteMockingFramework(generationContext), generationContext, "{0}Tests"); _testClass = new ComparableGenerationStrategy(_frameworkSet); }
public ExpressionSyntax Invoke(ClassModel owner, bool suppressAwait, IFrameworkSet frameworkSet, params CSharpSyntaxNode[] arguments) { if (owner == null) { throw new ArgumentNullException(nameof(owner)); } if (frameworkSet == null) { throw new ArgumentNullException(nameof(frameworkSet)); } if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } var ownerTargetInstance = Node.Modifiers.Any(x => x.IsKind(SyntaxKind.StaticKeyword)) ? owner.TypeSyntax : owner.TargetInstance; if (Node.ExplicitInterfaceSpecifier != null) { ownerTargetInstance = SyntaxFactory.ParenthesizedExpression(SyntaxFactory.CastExpression(Node.ExplicitInterfaceSpecifier.Name, ownerTargetInstance)); var typeSymbol = owner.SemanticModel.GetTypeInfo(Node.ExplicitInterfaceSpecifier.Name).Type; frameworkSet.Context.AddEmittedType(typeSymbol); } var expressionSyntax = Generate.MethodCall(ownerTargetInstance, Node, Name, frameworkSet, arguments); if (IsAsync && !suppressAwait) { return(SyntaxFactory.AwaitExpression(expressionSyntax)); } return(expressionSyntax); }
public static string GetTargetTypeName(this IFrameworkSet frameworkSet, ClassModel classModel, bool withGenericDisambiguation) { if (frameworkSet == null) { throw new ArgumentNullException(nameof(frameworkSet)); } if (classModel == null) { throw new ArgumentNullException(nameof(classModel)); } try { var sourceName = classModel.ClassName; if (withGenericDisambiguation && classModel.TypeSymbol?.TypeParameters.Length > 0) { sourceName += "_" + classModel.TypeSymbol.TypeParameters.Length; } return(string.Format(CultureInfo.CurrentCulture, frameworkSet.TestTypeNaming, sourceName)); } catch (FormatException) { throw new InvalidOperationException(Strings.TargetNameTransform_GetTargetTypeName_Cannot_not_derive_target_type_name__please_check_the_test_type_naming_setting_); } }
public ExpressionSyntax Invoke(ClassModel owner, bool suppressAwait, IFrameworkSet frameworkSet, params CSharpSyntaxNode[] arguments) { if (owner == null) { throw new ArgumentNullException(nameof(owner)); } if (frameworkSet == null) { throw new ArgumentNullException(nameof(frameworkSet)); } if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } if (arguments.Length == 1 && arguments[0] is ExpressionSyntax expression && UnaryExpressionKinds.TryGetValue(OperatorText, out var kind)) { return(SyntaxFactory.PrefixUnaryExpression(kind, expression)); } if (arguments.Length == 2 && arguments[0] is ExpressionSyntax left && arguments[1] is ExpressionSyntax right && BinaryExpressionKinds.TryGetValue(OperatorText, out kind)) { return(SyntaxFactory.BinaryExpression(kind, left, right)); } return(null); }
public ClassDecorationStrategyFactory(IFrameworkSet frameworkSet) { var frameworkSet1 = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); _strategies = new List <IClassDecorationStrategy> { new RequiresStaGenerationStrategy(frameworkSet1), }; }
public ClassGenerationStrategyFactory(IFrameworkSet frameworkSet) { var frameworkSet1 = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); _strategies = new List <IClassGenerationStrategy> { new StandardClassGenerationStrategy(frameworkSet1), new AbstractClassGenerationStrategy(frameworkSet1), new StaticClassGenerationStrategy(frameworkSet1), }; }
public ExpressionSyntax GetObjectCreationExpression(IFrameworkSet frameworkSet) { if (frameworkSet == null) { throw new ArgumentNullException(nameof(frameworkSet)); } var targetConstructor = Constructors.OrderByDescending(x => x.Parameters.Count).FirstOrDefault(); var objectCreation = SyntaxFactory.ObjectCreationExpression(TypeSyntax); if (targetConstructor != null && targetConstructor.Parameters.Count > 0) { return(objectCreation.WithArgumentList(Generate.Arguments(targetConstructor.Parameters.Select(x => SyntaxFactory.IdentifierName(GetConstructorParameterFieldName(x)))))); } if (targetConstructor != null || !Declaration.ChildNodes().OfType <ConstructorDeclarationSyntax>().Any()) { return(objectCreation.WithArgumentList(SyntaxFactory.ArgumentList())); } return(AssignmentValueHelper.GetDefaultAssignmentValue(TypeSymbol, SemanticModel, frameworkSet)); }
protected InterfaceGenerationStrategyBase(IFrameworkSet frameworkSet) { FrameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new NotifyPropertyChangedGenerationStrategy(_frameworkSet); }
public ExpressionSyntax CreateValueExpression(ITypeSymbol symbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { if (symbol is null) { throw new ArgumentNullException(nameof(symbol)); } if (model is null) { throw new ArgumentNullException(nameof(model)); } if (frameworkSet is null) { throw new ArgumentNullException(nameof(frameworkSet)); } return(_factory(symbol, model, visitedTypes, frameworkSet)); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new ReadWritePropertyGenerationStrategy(_frameworkSet); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new EnumerableGenerationStrategy(_frameworkSet); }
public IndexerGenerationStrategyFactory(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public static ExpressionSyntax GenerateFor(ITypeSymbol symbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { return(GenerateFor(symbol.ToFullName(), symbol, model, visitedTypes, frameworkSet)); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new ClassLevelGenerationStrategyFactory(_frameworkSet); }
public static ExpressionSyntax ImplicitlyTyped(ITypeSymbol typeSymbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { if (typeSymbol is INamedTypeSymbol namedTypeSymbol && namedTypeSymbol.TypeArguments.Length > 0) { var targetType = namedTypeSymbol.TypeArguments[0]; if (visitedTypes.Contains(targetType.ToFullName())) { return(SyntaxFactory.ArrayCreationExpression(SyntaxFactory.ArrayType(targetType.ToTypeSyntax(frameworkSet.Context)))); } return(SyntaxFactory.ImplicitArrayCreationExpression( SyntaxFactory.InitializerExpression( SyntaxKind.ArrayInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( new SyntaxNodeOrToken[] { AssignmentValueHelper.GetDefaultAssignmentValue(targetType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), SyntaxFactory.Token(SyntaxKind.CommaToken), AssignmentValueHelper.GetDefaultAssignmentValue(targetType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), SyntaxFactory.Token(SyntaxKind.CommaToken), AssignmentValueHelper.GetDefaultAssignmentValue(targetType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), })))); } return(AssignmentValueHelper.GetDefaultAssignmentValue(typeSymbol, model, frameworkSet)); }
public static ExpressionSyntax ImplicitlyTypedArray(ITypeSymbol typeSymbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { if (typeSymbol is IArrayTypeSymbol arrayTypeSymbol) { if (visitedTypes.Contains(arrayTypeSymbol.ElementType.ToFullName())) { return(SyntaxFactory.ArrayCreationExpression(SyntaxFactory.ArrayType(arrayTypeSymbol.ElementType.ToTypeSyntax(frameworkSet.Context)))); } return(SyntaxFactory.ImplicitArrayCreationExpression( SyntaxFactory.InitializerExpression( SyntaxKind.ArrayInitializerExpression, SyntaxFactory.SeparatedList <ExpressionSyntax>( new SyntaxNodeOrToken[] { AssignmentValueHelper.GetDefaultAssignmentValue(arrayTypeSymbol.ElementType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), SyntaxFactory.Token(SyntaxKind.CommaToken), AssignmentValueHelper.GetDefaultAssignmentValue(arrayTypeSymbol.ElementType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), SyntaxFactory.Token(SyntaxKind.CommaToken), AssignmentValueHelper.GetDefaultAssignmentValue(arrayTypeSymbol.ElementType, model, new HashSet <string>(visitedTypes, StringComparer.OrdinalIgnoreCase), frameworkSet), })))); } var random = ValueGenerationStrategyFactory.Random; return(SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Array"), SyntaxFactory.IdentifierName("CreateInstance"))) .WithArgumentList( Generate.Arguments(SyntaxFactory.TypeOfExpression(SyntaxFactory.IdentifierName("int")), Generate.Literal(random.Next(int.MaxValue)), Generate.Literal(random.Next(int.MaxValue)), Generate.Literal(random.Next(int.MaxValue))))); }
public static ExpressionSyntax Random(ITypeSymbol typeSymbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { if (typeSymbol == null) { throw new ArgumentNullException(nameof(typeSymbol)); } if (frameworkSet == null) { throw new ArgumentNullException(nameof(frameworkSet)); } var enumMembers = typeSymbol.GetMembers().OfType <IFieldSymbol>().Select(x => x.Name).ToList(); var identifier = enumMembers[ValueGenerationStrategyFactory.Random.Next(enumMembers.Count)]; return(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, typeSymbol.ToTypeSyntax(frameworkSet.Context), SyntaxFactory.IdentifierName(identifier))); }
public WriteOnlyPropertyGenerationStrategy(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public EnumerableGenerationStrategy(IFrameworkSet frameworkSet) : base(frameworkSet) { }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new CanCallOperatorGenerationStrategy(_frameworkSet); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new CanConstructSingleConstructorGenerationStrategy(_frameworkSet); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new StandardClassGenerationStrategy(_frameworkSet); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new NullParameterCheckMethodGenerationStrategy(_frameworkSet); }
public static ExpressionSyntax GenerateFor(string typeName, ITypeSymbol symbol, SemanticModel model, HashSet <string> visitedTypes, IFrameworkSet frameworkSet) { if (symbol == null) { throw new ArgumentNullException(nameof(symbol)); } var strategy = Strategies.FirstOrDefault(x => x.SupportedTypeNames.Any(t => string.Equals(t, typeName, StringComparison.OrdinalIgnoreCase))); if (strategy != null) { return(strategy.CreateValueExpression(symbol, model, visitedTypes, frameworkSet)); } var baseType = symbol.BaseType; while (baseType != null) { var name = baseType.ToFullName(); strategy = Strategies.FirstOrDefault(x => x.SupportedTypeNames.Any(t => string.Equals(t, name, StringComparison.OrdinalIgnoreCase))); if (strategy != null) { return(strategy.CreateValueExpression(symbol, model, visitedTypes, frameworkSet)); } baseType = baseType.BaseType; } return(null); }
public CanCallOperatorGenerationStrategy(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public NotifyPropertyChangedGenerationStrategy(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public AbstractClassGenerationStrategy(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public MultiConstructorInitializedPropertyGenerationStrategy(IFrameworkSet frameworkSet) { _frameworkSet = frameworkSet ?? throw new ArgumentNullException(nameof(frameworkSet)); }
public void SetUp() { _frameworkSet = Substitute.For <IFrameworkSet>(); _testClass = new ReadOnlyIndexerGenerationStrategy(_frameworkSet); }