Utilities for rendering .NET types as C# type names, with support for generics, nested types, type name simplification via "using" scopes, etc.
예제 #1
0
        public void WhenAddingOpenGeneric_ThenSimplifiesConcreteGeneric()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(Lazy <>));

            Assert.Equal("Lazy<System.String>", scope.GetTypeName(typeof(Lazy <string>)));
        }
예제 #2
0
        public void WhenSimplifyingAssemblyQualifiedName_ThenAddsUsingAndSimplifiesTypeName()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("Foo.Bar, Foo");

            Assert.Equal("Bar", scope.GetTypeName("Foo.Bar, Foo"));
            Assert.True(scope.SafeImports.Contains("Foo"));
        }
예제 #3
0
        public void WhenSimplifyingOpenGenericTypeWithTwoParameters_ThenRendersValidCSharp()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IDictionary <,>));

            Assert.Equal("IDictionary<,>", scope.GetTypeName(typeof(IDictionary <,>)));
            Assert.True(scope.SafeImports.Contains(typeof(IDictionary <,>).Namespace));
        }
예제 #4
0
        public void WhenSimplifyingOpenGenericType_ThenRendersValidCSharp()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IEnumerable <>));

            Assert.Equal("IEnumerable<>", scope.GetTypeName(typeof(IEnumerable <>)));
            Assert.True(scope.SafeImports.Contains(typeof(IEnumerable <>).Namespace));
        }
예제 #5
0
        public void WhenAddingAssembly_ThenSafeUsingsDoNotContainGenerics()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IEnumerable <string>));
            scope.AddType(typeof(IEnumerable));

            Assert.False(scope.SafeImports.Any(s => s.IndexOf('[') != -1));
        }
예제 #6
0
        public void WhenAddingTypesToScopeWithNoNamespace_ThenSucceeds()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("Foo");
            scope.AddType("Bar");

            Assert.Equal("Foo", scope.GetTypeName("Foo"));
            Assert.Equal("Bar", scope.GetTypeName("Bar"));
        }
예제 #7
0
        public void When_Resolving_Open_Generic_Parameter_Then_Adds_Name()
        {
            var scope = Stringly.BeginScope();

            var genericParam = typeof(IEnumerable <>).GetGenericTypeDefinition().GetGenericArguments()[0];

            scope.AddType(genericParam);

            Assert.Equal("T", scope.GetTypeName(genericParam));
        }
예제 #8
0
        public void When_Adding_Open_Generic_Type_Then_Resolves_Without_Param_Name()
        {
            var scope = Stringly.BeginScope();

            var genericType = typeof(Func <>);

            scope.AddType(genericType);

            Assert.Equal("Func<>", scope.GetTypeName(genericType));
        }
예제 #9
0
        public void WhenSimplifyingTypeMap_ThenUniqueTypeNamesAreSimplified()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("Foo.A");
            scope.AddType("Bar.B");

            Assert.Equal("A", scope.GetTypeName("Foo.A"));
            Assert.Equal("B", scope.GetTypeName("Bar.B"));
        }
예제 #10
0
        public void WhenSimplifyingGenericType_ThenAddsUsingsAndSimplifiesGenericParameterType()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IEnumerable <NonNestedType>));

            Assert.Equal("IEnumerable<NonNestedType>", scope.GetTypeName(typeof(IEnumerable <NonNestedType>)));
            Assert.True(scope.SafeImports.Contains(typeof(IEnumerable <>).Namespace));
            Assert.True(scope.SafeImports.Contains(typeof(NonNestedType).Namespace));
        }
예제 #11
0
        public void WhenGettingTypeNameOfTypeNotAddedWithAddedGeneric_ThenReturnsSimplifiedGenericParameter()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(string));
            scope.AddType(typeof(bool));

            Assert.Equal("System.Lazy<String>", scope.GetTypeName(typeof(Lazy <string>)));
            Assert.Equal("System.Lazy<System.Collections.Generic.KeyValuePair<String, Boolean>>", scope.GetTypeName(typeof(Lazy <KeyValuePair <string, bool> >)));
        }
예제 #12
0
        public void WhenSimplifyingGenericTypeWithNestedTypeParameter_ThenRemovesPlusFromNestedTypeName()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IEnumerable <NestedType>));

            Assert.Equal("IEnumerable<StringlySpec.NestedType>", scope.GetTypeName(typeof(IEnumerable <NestedType>)));
            Assert.True(scope.SafeImports.Contains(typeof(IEnumerable <>).Namespace));
            Assert.True(scope.SafeImports.Contains(typeof(NestedType).Namespace));
            Assert.False(scope.SafeImports.Contains(typeof(StringlySpec).FullName), "The nested type parent should not be mistaken for a namespace.");
        }
예제 #13
0
        public void WhenSimplifyingTwoGenerics_ThenSimplifiesAllParameters()
        {
            var scope = Stringly.BeginScope();
            var type  = typeof(IList <KeyValuePair <string, StringConverter> >);

            scope.AddType(type);
            scope.AddType(typeof(System.ComponentModel.StringConverter));

            Assert.Equal("IList<KeyValuePair<String, NetFx.StringlyTyped.StringConverter>>", scope.GetTypeName(type));
            Assert.True(scope.SafeImports.Contains(typeof(IEnumerable <>).Namespace));
        }
예제 #14
0
        public void WhenSimplifyingGenericTypeWithCollidingParameter_ThenKeepsParameterFullName()
        {
            var scope = Stringly.BeginScope();

            scope.AddType(typeof(IEnumerable <StringConverter>));
            scope.AddType(typeof(System.ComponentModel.StringConverter));

            Assert.Equal("IEnumerable<NetFx.StringlyTyped.StringConverter>", scope.GetTypeName(typeof(IEnumerable <StringConverter>)));
            Assert.True(scope.SafeImports.Contains(typeof(IEnumerable <>).Namespace));
            Assert.False(scope.SafeImports.Contains(typeof(StringConverter).Namespace));
        }
예제 #15
0
        public void WhenSimplifyingAllCoreLib_ThenAddsUsingForGenericsAndNonGenericComparable()
        {
            var scope = Stringly.BeginScope();

            scope.AddTypes(typeof(string).Assembly);
            scope.AddTypes(new[] { typeof(IComparable), typeof(IComparable <string>) });

            Assert.Equal("IComparable<String>", scope.GetTypeName(typeof(IComparable <string>)));
            Assert.Equal("IComparable", scope.GetTypeName(typeof(IComparable)));
            Assert.True(scope.SafeImports.Contains(typeof(IComparable <>).Namespace));
        }
예제 #16
0
        public void WhenSimplifyingTypeMap_ThenOnlySimplifiesNonCollidingTypeNames()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("Foo.A");
            scope.AddType("Foo.B");
            scope.AddType("Bar.A");

            Assert.Equal("Foo.A", scope.GetTypeName("Foo.A"));
            Assert.Equal("B", scope.GetTypeName("Foo.B"));
            Assert.Equal("Bar.A", scope.GetTypeName("Bar.A"));
        }
예제 #17
0
        public void When_Adding_CSharp_Generic_Then_Simplifies_Generic_Parameters()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("System.Device.Location.GeoCoordinate");
            scope.AddType("System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>");
            scope.AddType("System.EventHandler<System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>>");
            scope.AddType("System.Device.Location.GeoCoordinate");
            scope.AddType("System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>");

            Assert.Equal("GeoPositionChangedEventArgs<GeoCoordinate>",
                         scope.GetTypeName("System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>"));
        }
예제 #18
0
        public void WhenGettingSafeUsings_ThenOnlyGetsNamespacesFromSimplifiedTypeNames()
        {
            var scope = Stringly.BeginScope();

            scope.AddType("Foo.A");
            scope.AddType("Foo.B");
            scope.AddType("Bar.A");
            scope.AddType("Baz.C");

            Assert.True(scope.SafeImports.Contains("Foo"));
            Assert.True(scope.SafeImports.Contains("Baz"));
            Assert.False(scope.SafeImports.Contains("Bar"));
        }
예제 #19
0
        public void When_Adding_Empty_TypeName_Then_Throws()
        {
            var scope = Stringly.BeginScope();

            Assert.Throws <ArgumentException>(() => scope.AddType(""));
        }
예제 #20
0
        public void WhenGettingTypeNameOfTypeNotAdded_ThenReturnsValidCodeName()
        {
            var scope = Stringly.BeginScope();

            Assert.Equal("System.Lazy<System.String>", scope.GetTypeName(typeof(Lazy <string>)));
        }