BeginScope() публичный статический Метод

Creates a new stringly scope, which can be used to register types that will be used as strings, so that safe code imports can be determined and type names can be shortened to the safest form.
The scope takes into account all type names in use, and determines what namespaces can be safely removed from certain type names without introducing ambiguities.
public static BeginScope ( ) : IStringlyScope
Результат IStringlyScope
Пример #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>)));
        }