BeginScope() public static method

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
return 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>)));
        }