public void SetParameter_should_apply_new_parameter_names_and_types() { var method = MethodName.Parse("TryParse(text:)"); var newMethod = method.SetParameter(0, "text", TypeName.FromType(typeof(string))); Assert.Equal("TryParse(System.String text)", newMethod.ToString()); }
public void Assembly_exists_for_type_specification() { TypeName tn = TypeName.FromType(typeof(void).MakePointerType()); var mscorlib = AssemblyName.FromAssemblyName(typeof(object).GetTypeInfo().Assembly.GetName()); Assert.Equal(mscorlib, tn.Assembly); }
public void AddParameter_should_apply_new_parameter_names_and_types() { var method = MethodName.Parse("TryParse"); var newMethod = method.AddParameter("text", TypeName.FromType(typeof(string))) .AddParameter("value", TypeName.Parse("Byte&")); Assert.Equal("TryParse(System.String text, Byte& value)", newMethod.ToString()); }
public void MakeGenericType_should_produce_correct_closed_generics_and_FullName() { var ti = typeof(C <,> .D <,>).GetTypeInfo(); TypeName nestedType = TypeName.FromType(typeof(C <,> .D <,>)); TypeName closed = nestedType.MakeGenericType(TypeName.Int32, TypeName.String, TypeName.Int32, TypeName.Int16); Assert.Equal("Carbonfrost.UnitTests.DotNet.TypeNameTests+C`2+D<Int32, String, Int32, Int16>", closed.FullName); }
public void FromType_convert_nested_type() { TypeName name = TypeName.FromType(typeof(E.Nested)); TypeName dname = TypeName.FromType(typeof(E)); Assert.True(name.IsNested); Assert.Equal(dname, name.DeclaringType); }
public void FromType_generic_instance_type() { TypeName name = TypeName.FromType(typeof(IList <Tuple <string, string[]> >)); Assert.True(name.IsGenericType); Assert.False(name.IsGenericTypeDefinition); Assert.Equal("System.Collections.Generic.IList<System.Tuple<System.String, System.String[]>>", name.FullName); }
public void SetParameter_should_apply_modifiers() { var method = MethodName.Parse("TryParse(text:)"); var newMethod = method.SetParameter(0, "text", TypeName.FromType(typeof(string)), new [] { TypeName.Parse("Const") }, new [] { TypeName.Parse("Volatile") }); Assert.Equal("TryParse(modreq(Const) modopt(Volatile) System.String text)", newMethod.ToString()); }
public void MakeByReferenceType_from_byref_type() { TypeName tn = TypeName.FromType(typeof(int)).MakeByReferenceType(); Assert.Throws <InvalidOperationException>(() => { tn.MakeNullableType(); }); }
public void FromType_convert_pointer_type() { TypeName name = TypeName.FromType(typeof(void).MakePointerType()); Assert.True(name.IsPointer); Assert.Equal(TypeName.Void, ((TypeSpecificationName)name).ElementType); Assert.Equal("Void*", name.Name); Assert.Equal("System.Void*", name.FullName); }
public void MakeGenericType_nominal() { TypeName tn = TypeName.FromType(typeof(Converter <,>)); var gen = tn.MakeGenericType(TypeName.Int16, TypeName.Int32); Assert.Equal(tn.Assembly, gen.Assembly); Assert.Equal(tn, gen.ElementType); Assert.Equal("Converter`2", gen.Name); }
public void SetParameter_should_apply_on_generics() { var method = MethodName.Parse("Create``2(``0,``1)"); Assert.Equal("Create", method.Name); var newMethod = method.SetParameter(0, "text", TypeName.FromType(typeof(string))); Assert.Equal("Create", newMethod.Name); Assert.Equal("Create``2(System.String text, ``1)", newMethod.ToString()); }
public void get_property_name_indexer() { var type = TypeName.Parse("System.String"); var prop = type.GetProperty("Chars", TypeName.FromType(typeof(char)), new [] { TypeName.FromType(typeof(int)) }); Assert.Equal("System.String.Chars[System.Int32]", prop.FullName); Assert.Equal("System.Char", prop.PropertyType.FullName); }
public static MethodSignature CreateMethodSignature(TypeName memberCompositeName, MemberMetaInfo member, string methodName) { Debug.Assert(member.ReturnType != null); return(MethodSignatureBuilder.CreateMethod(memberCompositeName, methodName) .AddGenerics(T_STATE, GenericConstrainKind.Struct, new TypeName("Decuplr.Serialization.Binary", "IBinaryWriteState<TState>")) .AddArgument((RefKind.In, member.ReturnType.Symbol, "member")) .AddArgument((RefKind.In, member.ContainingFullType.Symbol, "source")) .AddArgument((TypeName.FromGenericArgument(T_STATE), "state")) .AddArgument((TypeName.FromType(typeof(Span <byte>)), "data")) .WithReturn(TypeName.FromType <int>())); }
public void FromType_generic_type_definition() { TypeName name = TypeName.FromType(typeof(Tuple <, , ,>)); Assert.True(name.IsGenericType); Assert.True(name.IsGenericTypeDefinition); Assert.Equal("System.Tuple`4", name.FullName); Assert.Equal(4, name.GenericParameterCount); Assert.Equal(typeof(object).GetTypeInfo().Assembly.FullName, name.Assembly.FullName); Assert.Equal(new [] { "T1", "T2", "T3", "T4" }, name.GenericParameters.Select(t => t.Name)); }
public void FromType_convert_array_type() { TypeName name = TypeName.FromType(typeof(void).MakeArrayType(2)); Assert.True(name.IsArray); ArrayTypeName array = (ArrayTypeName)name; Assert.Equal(TypeName.Void, array.ElementType); Assert.Equal(2, array.Dimensions.Count); Assert.Equal("Void[,]", array.Name); }
public void FromType_generic_type_definition_nested() { TypeName name = TypeName.FromType(typeof(Dictionary <,> .ValueCollection)); Assert.True(name.IsGenericType); Assert.True(name.IsGenericTypeDefinition); Assert.Equal("System.Collections.Generic.Dictionary`2+ValueCollection", name.FullName); Assert.Equal(2, name.GenericParameterCount); Assert.Equal(typeof(object).GetTypeInfo().Assembly.FullName, name.Assembly.FullName); Assert.Equal(new [] { "TKey", "TValue" }, name.GenericParameters.Select(t => t.Name)); Assert.Equal(name, name.GenericParameters[0].DeclaringType); }
public void WithDeclaringType_should_update_it() { TypeName parentType = TypeName.FromType(typeof(int)); TypeName name = TypeName.FromType(typeof(string)); name = name.WithDeclaringType(parentType); Assert.Equal("String", name.Name); Assert.Equal("System.Int32+String", name.FullName); Assert.Equal("System", name.Namespace); Assert.Equal("System.Int32", name.DeclaringType.FullName); Assert.Same(parentType.Assembly, name.DeclaringType.Assembly); Assert.Same(parentType.Assembly, name.Assembly); }
public void Format_type_display_name_generic_nested() { var type = TypeName.FromType(typeof(Dictionary <,> .KeyCollection)); MetadataNameFormat format = new MetadataNameFormat(); format.IncludeTypeParameters = true; string expected = "System.Collections.Generic.Dictionary<TKey, TValue>+KeyCollection"; Assert.Equal(expected, format.Format(type)); Assert.Equal(expected, type.ToString(null, format)); }
/// <returns>Next state arg name</returns> private string LoadNextWriteState <TChainedMethod>(CodeNodeBuilder node, TChainedMethod chainedMethod) where TChainedMethod : MemberChainedMethods <TChainedMethod> { const string nextState = "nextState"; var tstateTypeName = TypeName.FromGenericArgument(SerializeWriterChainedMethods.T_STATE); var state = chainedMethod[tstateTypeName]; var tsource = chainedMethod[TypeName.FromType(_member.ContainingFullType.Symbol)]; node.If($"!{state}.Write({tsource}, out var {nextState})", node => { node.Return(); }); return(nextState); }
public void Format_use_generic_parameter_compact_format() { TypeName t = TypeName.FromType(typeof(IDictionary <Delegate, int>)); MetadataNameFormat format = new MetadataNameFormat(); format.DefaultFormatString[SymbolType.Parameter] = "Cv"; format.IncludeTypeParameters = true; string expected = "System.Collections.Generic.IDictionary<Delegate, Int32>"; Assert.Equal(expected, format.Format(t)); Assert.Equal(expected, t.ToString(null, format)); }
public void Format_use_generic_type_generic_parameter_names() { TypeName m = TypeName.FromType(typeof(Tuple <, , ,>)); MetadataNameFormat format = new MetadataNameFormat(); format.IncludeTypeParameters = true; format.IncludeTypeConstraints = false; format.IncludeVariance = false; string expected = "System.Tuple<T1, T2, T3, T4>"; Assert.Equal(expected, format.Format(m)); Assert.Equal(expected, m.ToString(null, format)); }
public void FromType_should_get_closed_inherited_generic_arguments() { var s = new C <int, long> .D <string, string>(); // proof, for reference: var args = s.GetType().GetTypeInfo().GetGenericArguments(); Assume.Equal(new [] { typeof(int), typeof(long), typeof(string), typeof(string) }, args); var t = (GenericInstanceTypeName)TypeName.FromType(s.GetType()); Assert.HasCount(4, t.GenericArguments); Assert.Equal(new [] { "Int32", "Int64", "String", "String" }, t.GenericArguments.Select(u => u.Name).ToArray()); }
public void FromType_generic_type_definition_nested_generic() { var ti = typeof(C <,> .D <,>).GetTypeInfo(); TypeName name = TypeName.FromType(typeof(C <,> .D <,>)); // proof, for reference: Assume.HasCount(4, ti.GetGenericArguments()); Assume.Equal("D`2", ti.Name); Assume.Equal("Carbonfrost.UnitTests.DotNet.TypeNameTests+C`2+D`2", ti.FullName); Assert.True(name.IsGenericType); Assert.True(name.IsGenericTypeDefinition); Assert.Equal("Carbonfrost.UnitTests.DotNet.TypeNameTests+C`2+D`2", name.FullName); Assert.Equal(4, name.GenericParameterCount); // inherited generics }
protected static IEnumerable <MethodArg> GetRelyingArgs(IEnumerable <MemberMetaInfo> relyingMembers) => relyingMembers.Select((member, i) => new MethodArg(RefKind.In, TypeName.FromType(member.ReturnType.Symbol), $"args{i}"));
public void FromType_should_apply_to_types_without_namespaces() { var type = TypeName.FromType(typeof(NoNamespaceType)); Assert.Equal("", type.Namespace); }
public void MakeGenericType_mismatch_arg_count() { TypeName tn = TypeName.FromType(typeof(Converter <,>)); tn.MakeGenericType(TypeName.Int16); }