private static void TestVisibility(string source, string className, string propertyName, SyntaxKind expectedVisibility) { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(source); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Locating the field IFieldInfoProxy fieldDeclaration = classDefinition.LocateField("myField"); Assert.IsNotNull(fieldDeclaration); // Generating the AST var factory = new FieldDeclarationSyntaxFactory(fieldDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built"); var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax; var modifiers = fieldDeclarationSyntaxNode.Modifiers; Assert.IsTrue(Utils.CheckModifier(modifiers, expectedVisibility), "Method does not have correct visibility"); }
/// <summary> /// Initializes a new instance of the <see cref="Visibility"/> class. /// </summary> /// <param name="field">The <see cref="IFieldInfoProxy"/> to analyze.</param> public Visibility(IFieldInfoProxy field) { if (field == null) { throw new ArgumentNullException(nameof(field)); } this.field = field; }
/// <summary> /// Initializes a new instance of the <see cref="FieldDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="propertyInfo"></param> /// <param name="withBody"></param> public FieldDeclarationSyntaxFactory(IFieldInfoProxy fieldInfo) { if (fieldInfo == null) { throw new ArgumentNullException(nameof(fieldInfo)); } this.fieldInfo = fieldInfo; }
/// <summary> /// Initializes a new instance of the <see cref="FieldDeclarationSyntaxFactory"/> class. /// </summary> /// <param name="fieldInfo"></param> /// <param name="typeLookup"></param> public FieldDeclarationSyntaxFactory(IFieldInfoProxy fieldInfo, ITypeLookup typeLookup) : base(fieldInfo) { if (typeLookup == null) { throw new ArgumentNullException(nameof(typeLookup)); } this.typeLookup = typeLookup; }
public void NameCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public int myField; } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Locating the field IFieldInfoProxy fieldDeclaration = classDefinition.LocateField("myField"); Assert.IsNotNull(fieldDeclaration); // Generating the AST var factory = new FieldDeclarationSyntaxFactory(fieldDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built"); var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax; var variables = fieldDeclarationSyntaxNode.Declaration.Variables; Assert.IsNotNull(variables); Assert.IsTrue(variables.Any()); var variable = fieldDeclarationSyntaxNode.Declaration.Variables.First(); Assert.IsNotNull(variable); var name = variable.Identifier.Text; Assert.AreEqual("myField", name, "Field name not correctly acquired"); }
public void StaticModifierCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public static int myField1; public int myField2; } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); Action <string, bool> CheckStatic = (fieldName, expected) => { // Locating the field IFieldInfoProxy fieldDeclaration = classDefinition.LocateField(fieldName); Assert.IsNotNull(fieldDeclaration); // Generating the AST var factory = new FieldDeclarationSyntaxFactory(fieldDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built"); var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax; var modifiers = fieldDeclarationSyntaxNode.Modifiers; Assert.IsNotNull(modifiers); var staticModifier = modifiers.Where(modifier => modifier.Kind() == SyntaxKind.StaticKeyword); Assert.AreEqual(expected ? 1 : 0, staticModifier.Count(), expected ? "Expected one static modifier" : "No static modifier expected"); }; CheckStatic("myField1", true); CheckStatic("myField2", false); }
public void TypeCorrectlyAcquired() { // Assembling some code IAssemblyLoader assemblyLoader = new Utils.AsmlDasmlAssemblyLoader(@" namespace MyNamespace { public class MyClass { public int myField; } } "); // Loading the assembly IAssemblyProxy assembly = assemblyLoader.Load(); // Locating the class ITypeInfoProxy classDefinition = assembly.LocateType("MyClass"); Assert.IsNotNull(classDefinition); // Locating the field IFieldInfoProxy fieldDeclaration = classDefinition.LocateField("myField"); Assert.IsNotNull(fieldDeclaration); // Generating the AST var factory = new FieldDeclarationSyntaxFactory(fieldDeclaration); var syntaxNode = factory.Create(); Assert.IsNotNull(syntaxNode, "A node was expected to be built"); Assert.IsInstanceOfType(syntaxNode, typeof(FieldDeclarationSyntax), "Expected a field declaration node to be built"); var fieldDeclarationSyntaxNode = syntaxNode as FieldDeclarationSyntax; var type = fieldDeclarationSyntaxNode.Declaration.Type; Assert.IsNotNull(type); var typeIdentifier = type as QualifiedNameSyntax; Assert.IsNotNull(typeIdentifier, "Type expected to be qualified name"); Assert.AreEqual("System.Int32", typeIdentifier.ToString(), "Parameter name does not match"); }
protected virtual ISyntaxFactory CreateFieldDeclarationSyntaxFactory(IFieldInfoProxy fieldInfo) => new FieldDeclarationSyntaxFactory(fieldInfo);
protected override ISyntaxFactory CreateFieldDeclarationSyntaxFactory(IFieldInfoProxy fieldInfo) => new FieldDeclarationSyntaxFactory(fieldInfo, this.typeLookup);