public void T01_Should_parse_simple_name(string x) { var a = GenericTypeName.FromString(x); Check(a, "int"); Assert.Null(a.DeclaredIn); }
internal static Type ResolveTypeName(ITypeName typeName) { Type reflectionType = typeName.GetReflectionType(); if (reflectionType != null) { return(reflectionType); } GenericTypeName name = typeName as GenericTypeName; if (name != null) { Type genericType = name.GetGenericType(ResolveTypeName(name.TypeName)); Type[] typeArguments = (from arg in name.GenericArguments select ResolveTypeName(arg)).ToArray <Type>(); try { if ((genericType != null) && genericType.ContainsGenericParameters) { genericType.MakeGenericType(typeArguments); } } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); throw InterpreterError.NewInterpreterException(typeName, typeof(RuntimeException), null, "TypeNotFoundWithMessage", ParserStrings.TypeNotFoundWithMessage, new object[] { typeName.FullName, exception.Message }); } } ArrayTypeName name2 = typeName as ArrayTypeName; if (name2 != null) { ResolveTypeName(name2.ElementType); } throw InterpreterError.NewInterpreterException(typeName, typeof(RuntimeException), null, "TypeNotFound", ParserStrings.TypeNotFound, new object[] { typeName.FullName }); }
public void T05_Should_parse_nested_name(string x) { var a = GenericTypeName.FromString(x); Check(a, "Master.Nested"); Assert.Equal("Master.Nested", a.GetGenericName()); }
public void TryGetGenericWhenNullable(string name, string arg) { Assert.AreEqual(true, GenericTypeName.TryParse(name, out var generic)); Assert.AreEqual("System.Nullable`1", generic.MetadataName); var typeArgument = generic.TypeArguments.Single(); Assert.AreEqual(arg, typeArgument.MetadataName); Assert.AreEqual(null, typeArgument.TypeArguments); }
public void T04_Should_parse_generic_dictionary_name_with_full_name(string x) { var a = GenericTypeName.FromString(x); Check(a, "System.Collections.Generic.Dictionary", 2); Check(a.Args[0], "int"); Check(a.Args[1], "string"); Assert.Equal("System.Collections.Generic.Dictionary`2", a.GetGenericName()); }
public void T03_Should_parse_generic_dictionary_name(string x) { var a = GenericTypeName.FromString(x); Check(a, "Dictionary", 2); Check(a.Args[0], "int"); Check(a.Args[1], "string"); Assert.Equal("Dictionary`2", a.GetGenericName()); }
public void T02_Should_parse_generic_list_name(string x) { var a = GenericTypeName.FromString(x); Check(a, "List", 1); Check(a.Args[0], "int"); Assert.Null(a.DeclaredIn); Assert.Equal("List`1", a.GetGenericName()); }
public void T06_Should_parse_nested_generic_name(string x) { var a = GenericTypeName.FromString(x); CheckNested(a, "KeyCollection"); Check(a.DeclaredIn, "System.Collections.Generic.Dictionary", 2); Check(a.DeclaredIn.Args[0], "int"); Check(a.DeclaredIn.Args[1], "string"); Assert.Equal("System.Collections.Generic.Dictionary`2+KeyCollection", a.GetGenericName()); }
public void TryGetGenericWhenKeyValuePair(string name, string arg0, string arg1) { Assert.AreEqual(true, GenericTypeName.TryParse(name, out var generic)); Assert.AreEqual("System.Collections.Generic.KeyValuePair`2", generic.MetadataName); var typeArguments = generic.TypeArguments; Assert.AreEqual(2, typeArguments.Length); Assert.AreEqual(arg0, typeArguments[0].MetadataName); Assert.AreEqual(null, typeArguments[0].TypeArguments); Assert.AreEqual(arg1, typeArguments[1].MetadataName); Assert.AreEqual(null, typeArguments[1].TypeArguments); }
public void TryGetGenericWhenNullableKeyValuePair(string name, string arg0, string arg1) { Assert.AreEqual(true, GenericTypeName.TryParse(name, out var generic)); Assert.AreEqual("System.Nullable`1", generic.MetadataName); var typeArgument = generic.TypeArguments.Single(); Assert.AreEqual("System.Collections.Generic.KeyValuePair`2", typeArgument.MetadataName); var genericArguments = typeArgument.TypeArguments; Assert.AreEqual(2, genericArguments.Count); Assert.AreEqual(arg0, genericArguments[0].MetadataName); Assert.AreEqual(null, genericArguments[0].TypeArguments); Assert.AreEqual(arg1, genericArguments[1].MetadataName); Assert.AreEqual(null, genericArguments[1].TypeArguments); }
public void GenericTypeTest() { Expression expression = parseExpression("a -> Banana<t,p,Banana<t,d,g>>"); VariableDeclarationExpression a = assertTypeAndCast <VariableDeclarationExpression>(expression); Assert.AreEqual("a", a.name); GenericTypeName b = assertTypeAndCast <GenericTypeName>(a.typeName); Assert.AreEqual(3, b.genericTypes.Count); Assert.AreEqual("t", b.genericTypes[0].name); Assert.AreEqual("p", b.genericTypes[1].name); Assert.AreEqual("Banana", b.genericTypes[2].name); GenericTypeName c = assertTypeAndCast <GenericTypeName>(b.genericTypes[2]); Assert.AreEqual(3, c.genericTypes.Count); Assert.AreEqual("t", c.genericTypes[0].name); Assert.AreEqual("d", c.genericTypes[1].name); Assert.AreEqual("g", c.genericTypes[2].name); }
internal static TypeName FindTypeNameToComplete(ITypeName type, IScriptPosition cursor) { TypeName name = type as TypeName; if (name != null) { if ((cursor.Offset > type.Extent.StartOffset) && (cursor.Offset <= type.Extent.EndOffset)) { return(name); } return(null); } GenericTypeName name2 = type as GenericTypeName; if (name2 != null) { name = FindTypeNameToComplete(name2.TypeName, cursor); if (name != null) { return(name); } foreach (ITypeName name3 in name2.GenericArguments) { name = FindTypeNameToComplete(name3, cursor); if (name != null) { return(name); } } return(null); } ArrayTypeName name4 = type as ArrayTypeName; if (name4 == null) { return(null); } return((TypeName)(FindTypeNameToComplete(name4.ElementType, cursor) ?? null)); }
public void AnonymousFunctionDefinitionAndAssignment() { Expression expression = parseExpression("anon -> Function<int,Banana, Platypus> = (a -> int, b -> Banana) -> Platypus { a + 5; }"); VariableDeclarationAssignmentExpression z = assertTypeAndCast <VariableDeclarationAssignmentExpression>(expression); Assert.AreEqual("anon", z.declaration.name); Assert.AreEqual("Function", z.declaration.typeName.name); GenericTypeName y = assertTypeAndCast <GenericTypeName>(z.declaration.typeName); Assert.AreEqual(3, y.genericTypes.Count); Assert.AreEqual("int", y.genericTypes[0].name); Assert.AreEqual("Banana", y.genericTypes[1].name); Assert.AreEqual("Platypus", y.genericTypes[2].name); AnonymousFunctionExpression a = assertTypeAndCast <AnonymousFunctionExpression>(z.value); Assert.AreEqual("Platypus", a.returnType.name); Assert.IsNotInstanceOfType(a.returnType, typeof(GenericTypeName)); Assert.AreEqual(2, a.arguments.members.Count); Assert.AreEqual(1, a.body.innerExpressions.Count); Assert.AreEqual("a", a.arguments.members[0].name); Assert.AreEqual("int", a.arguments.members[0].typeName.name); Assert.IsNotInstanceOfType(a.arguments.members[0].typeName, typeof(GenericTypeName)); Assert.AreEqual("b", a.arguments.members[1].name); Assert.AreEqual("Banana", a.arguments.members[1].typeName.name); Assert.IsNotInstanceOfType(a.arguments.members[1].typeName, typeof(GenericTypeName)); AdditionExpression d = assertTypeAndCast <AdditionExpression>(a.body.innerExpressions[0]); VariableReferenceExpression e = assertTypeAndCast <VariableReferenceExpression>(d.left); IntegralLiteralExpression f = assertTypeAndCast <IntegralLiteralExpression>(d.right); Assert.AreEqual("a", e.name); Assert.AreEqual(5, f.value); }
private static void Check(GenericTypeName a, string name, int genericArguments = 0) { CheckNested(a, name, genericArguments); Assert.Null(a.DeclaredIn); }
public static void TryGetGenericWhenFalse(string name) { Assert.AreEqual(null, GenericTypeName.TryParse(name)); }
public void TryGetGenericWhenFalse(string name) { Assert.AreEqual(false, GenericTypeName.TryParse(name, out _)); }
private static ITypeName ExpandTypeName(IReadOnlyDictionary <string, string> typeAccelerators, GenericTypeName typeName) { var genericArgs = new ITypeName[typeName.GenericArguments.Count]; for (int i = 0; i < genericArgs.Length; i++) { genericArgs[i] = ExpandTypeName(typeAccelerators, typeName.GenericArguments[i]); } ITypeName expandedTypeName = ExpandTypeName(typeAccelerators, (TypeName)typeName.TypeName, genericArgCount: genericArgs.Length); bool canUseOldTypeName = expandedTypeName == typeName.TypeName; for (int i = 0; i < genericArgs.Length; i++) { if (!canUseOldTypeName) { break; } canUseOldTypeName &= genericArgs[i] == typeName.GenericArguments[i]; } if (canUseOldTypeName) { return(typeName); } return(new GenericTypeName(s_emptyExtent, expandedTypeName, genericArgs)); }
public void ClassTest() { Expression expression = parseExpression(@"class Test { jelly -> int; public belly -> Banana<int,int>; p -> String = ""haha""; foopy(a -> int, b -> Banana) -> String { a = 5; } }"); ClassDefinitionExpression a = assertTypeAndCast <ClassDefinitionExpression>(expression); Assert.AreEqual("Test", a.name); Assert.AreEqual(4, a.members.Count); Assert.AreEqual(Visibility.None, a.visibility); VariableDeclarationExpression b = assertTypeAndCast <VariableDeclarationExpression>(a.members[0]); VariableDeclarationExpression c = assertTypeAndCast <VariableDeclarationExpression>(a.members[1]); VariableDeclarationAssignmentExpression d = assertTypeAndCast <VariableDeclarationAssignmentExpression>(a.members[2]); FunctionExpression e = assertTypeAndCast <FunctionExpression>(a.members[3]); Assert.AreEqual("jelly", b.name); Assert.AreEqual("int", b.typeName.name); Assert.IsNotInstanceOfType(b.typeName, typeof(GenericTypeName)); Assert.AreEqual(Visibility.None, b.visibility); Assert.AreEqual("belly", c.name); Assert.AreEqual("Banana", c.typeName.name); Assert.AreEqual(Visibility.Public, c.visibility); GenericTypeName f = assertTypeAndCast <GenericTypeName>(c.typeName); Assert.AreEqual(2, f.genericTypes.Count); Assert.AreEqual("int", f.genericTypes[0].name); Assert.IsNotInstanceOfType(f.genericTypes[0], typeof(GenericTypeName)); Assert.AreEqual("int", f.genericTypes[1].name); Assert.IsNotInstanceOfType(f.genericTypes[1], typeof(GenericTypeName)); Assert.AreEqual("p", d.declaration.name); Assert.AreEqual("String", d.declaration.typeName.name); Assert.IsNotInstanceOfType(d.declaration.typeName, typeof(GenericTypeName)); StringLiteralExpression g = assertTypeAndCast <StringLiteralExpression>(d.value); Assert.AreEqual("haha", g.value); Assert.AreEqual("foopy", e.name); Assert.AreEqual(Visibility.None, e.visibility); Assert.AreEqual("String", e.returnType.name); Assert.IsNotInstanceOfType(e.returnType, typeof(GenericTypeName)); Assert.AreEqual(2, e.arguments.members.Count); Assert.AreEqual(1, e.body.innerExpressions.Count); Assert.AreEqual("a", e.arguments.members[0].name); Assert.AreEqual("int", e.arguments.members[0].typeName.name); Assert.IsNotInstanceOfType(e.arguments.members[0].typeName, typeof(GenericTypeName)); Assert.AreEqual("b", e.arguments.members[1].name); Assert.AreEqual("Banana", e.arguments.members[1].typeName.name); Assert.IsNotInstanceOfType(e.arguments.members[1].typeName, typeof(GenericTypeName)); VariableAssignmentExpression h = assertTypeAndCast <VariableAssignmentExpression>(e.body.innerExpressions[0]); Assert.AreEqual("a", h.name); IntegralLiteralExpression i = assertTypeAndCast <IntegralLiteralExpression>(h.value); Assert.AreEqual(5, i.value); }
public GenericClassDefinitionExpression(GenericTypeName name, List <Expression> members, Visibility visibility = Visibility.None) : base(name.name, members, visibility) { genericTypes = name.genericTypes; }
private static void CheckNested(GenericTypeName a, string name, int genericArguments = 0) { Assert.NotNull(a); Assert.Equal(name, a.Name); Assert.Equal(genericArguments, a.Args.Length); }