コード例 #1
0
        public void CanDealWithNestedNamespaces()
        {
            var builder = new TsModelBuilder();
            builder.Add<Person>();
            builder.Add<Drink>();

            var generator = new TsGenerator();
            generator.SetModuleNameFormatter(_ => "custom");//everything should go into the 'custom' module
            var model = builder.Build();
            var result = generator.Generate(model);

            var expectedOutput = @"
            declare module custom {
            interface Person {
            WhiteRussian: custom.Drink;
            AllDrinks: custom.Drink[];
            }
            }
            declare module custom {
            interface Drink {
            }
            }
            ";

            Assert.Equal(expectedOutput, result);
        }
コード例 #2
0
        public void WhenClassContainsPropertyOfSystemType_InvalidCastExceptionIsntThrown()
        {
            var builder = new TsModelBuilder();
            builder.Add<Issue15Example>();

            var generator = new TsGenerator();
            var model = builder.Build();

            Assert.DoesNotThrow(() => generator.Generate(model));
        }
コード例 #3
0
        public void WhenClassContainsNullableEnumProperty_ExceptionIsNotThrown()
        {
            var builder = new TsModelBuilder();
            builder.Add<Issue24Example>();

            var generator = new TsGenerator();
            var model = builder.Build();

            Assert.DoesNotThrow(() => generator.Generate(model));
        }
コード例 #4
0
        public void WhenPropertyWithDbGeometryTypeIsAnnotedWithTsIgnoreAttribute_GeneratorDoesntCrash()
        {
            var builder = new TsModelBuilder();
            builder.Add<DbGeometryTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();

            Assert.DoesNotThrow(() => generator.Generate(model));
        }
コード例 #5
0
        public void WhenClassIsReferencedAndOutputIsSetToConstants_ClassIsntInOutput()
        {
            var builder = new TsModelBuilder();
            builder.Add<Item>();
            var model = builder.Build();
            var target = new TsGenerator();
            var script = target.Generate(model, TsGeneratorOutput.Constants);

            Assert.DoesNotContain("interface Item", script);
        }
コード例 #6
0
        public void WhenAssemblyContainsEnumWithoutClass_EnumIsGeneratedInTheOutput()
        {
            var builder = new TsModelBuilder();
            builder.Add(typeof(TypeLite.Tests.AssemblyWithEnum.TestEnum).Assembly);

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model, TsGeneratorOutput.Enums);

            Assert.Contains("TestEnum", result);
        }
        public void WhenClassReferencesItself_ClassIsGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<GenericWithSelfReference>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model);

            Assert.Contains("Children: TypeLite.Tests.RegressionTests.GenericWithSelfReference[]", result);
        }
コード例 #8
0
        public void WhenDateTimePropertyIsInModel_DateWithUppercaseDIsGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<ModelWithDateTime>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model);

            Assert.Contains("Property: Date", result);
        }
コード例 #9
0
        public void WhenModuleDoesntContainsAnyEnums_ItIsntGeneratedWithEnumsOption()
        {
            var builder = new TsModelBuilder();
            builder.Add<MyTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model, TsGeneratorOutput.Enums);

            Assert.DoesNotContain("MyTestModule", result);
        }
コード例 #10
0
        public void WhenClassIsIgnoredByAttribute_InterfaceForClassIsntGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<IgnoreTestBase>();
            var model = builder.Build();

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.DoesNotContain("IgnoreTestBase", script);
        }
コード例 #11
0
        public void PropertyIsMarkedOptional_OptionalPropertyIsGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.Contains("CountryID?: number", script);
        }
コード例 #12
0
        public void WhenEnumIsGenerated_ItHasExportKeyword()
        {
            var builder = new TsModelBuilder();
            builder.Add<MyTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model, TsGeneratorOutput.Enums);

            Assert.Contains("export", result);
        }
コード例 #13
0
        public void WhenModuleIsGeneratedWithEnumsOnlyOption_ModuleDoesntHaveDeclareKeyword()
        {
            var builder = new TsModelBuilder();
            builder.Add<MyTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model, TsGeneratorOutput.Enums);

            Assert.DoesNotContain("declare", result);
        }
コード例 #14
0
        public void WhenClassIsIgnored_InterfaceForClassIsntGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();
            model.Classes.Where(o => o.Name == "Address").Single().IsIgnored = true;

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.DoesNotContain("Address", script);
        }
コード例 #15
0
        public void WhenModuleIsGeneratedWithConstantsAndProperties_ThrowsInvalidOperationException()
        {
            var builder = new TsModelBuilder();
            builder.Add<MyTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            Assert.Throws<InvalidOperationException>(() =>
            {
                generator.Generate(model, TsGeneratorOutput.Constants | TsGeneratorOutput.Properties);
            });
        }
        public void WhenEnumTypeAppearsOnlyInCollectionTypeProperty_EnumIsGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<AuthenticationResult>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model);

            Assert.True(model.Enums.Count == 1);
            Assert.True(model.Enums.Single().Type == typeof(AuthenticationError));
        }
コード例 #17
0
        public void WhenClassHasNullableStructure_StructureIsAddedToModel()
        {
            var builder = new TsModelBuilder();
            builder.Add<NullableStructureContainer>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model);

            Assert.Contains("NullableStructure: TypeLite.Tests.RegressionTests.Structure1;", result);
            Assert.Contains("NullableStructureCollection: TypeLite.Tests.RegressionTests.Structure2[];", result);
            Assert.Contains("NullableInt: number;", result);
        }
コード例 #18
0
        public void WhenClassHasBase_MultipleBasesArentGenerated()
        {
            var builder = new TsModelBuilder();

            builder.Add <Employee>();
            builder.Add <User>();
            var model = builder.Build();

            var target = new TsGenerator();
            var script = target.Generate(model);

            var count = Regex.Matches(script, Regex.Escape("interface Person")).Count;

            Assert.True(count == 1, script);
        }
コード例 #19
0
        public void OrdersOutputByNamespace()
        {
            var builder = new TsModelBuilder();

            builder.Add <DifferentNamespaces_Class1>();
            builder.Add <DifferentNamespaces_Class2>();
            builder.Add <DifferentNamespaces_Class3>();

            var generator = new TsGenerator();
            var model     = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("TypeLite.Tests.TestModels.Namespace1") < result.IndexOf("TypeLite.Tests.TestModels.Namespace2"), "Didn't order namespaces");
        }
コード例 #20
0
        public void WhenSortOrderIsNotSpecified_Namespace1IsFirst()
        {
            var builder = new TsModelBuilder();

            builder.Add <DifferentNamespaces_Class3>();
            builder.Add <DifferentNamespaces_Class2>();
            builder.Add <DifferentNamespaces_Class1>();

            var model = builder.Build();

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.True(script.IndexOf("TypeLitePlus.Tests.NetCore.TestModels.Namespace2") > script.IndexOf("TypeLitePlus.Tests.NetCore.TestModels.Namespace1"));
        }
コード例 #21
0
        public void RespectsFieldNameFormatterOverrides()
        {
            var builder = new TsModelBuilder();

            builder.Add <DifferentNamespaces_Class2>();

            var generator = new TsGenerator();

            // Reverse the order of the properties
            generator.SetIdentifierFormatter(x => x.Name == "Property1" ? "propz" : "propa");
            var model = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("propa") < result.IndexOf("propz"), "Didn't order properties when formatters involved");
        }
        public void WhenClosedGenericTypeAppearsAsPropertyTypeMultipleTimes_OnlyOneInterfaceGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<GenericPropertiesBug>();

            var generator = new TsGenerator();
            var model = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("interface KeyValuePair") > -1, "KeyValuePair interface missing");
            Assert.True(result.IndexOf("interface KeyValuePair") == result.LastIndexOf("interface KeyValuePair"), "KeyValuePair interface generated too many times");

            Assert.True(result.Contains("Test1: System.Collections.Generic.KeyValuePair"));
            Assert.True(result.Contains("Test2: System.Collections.Generic.KeyValuePair"));
        }
コード例 #23
0
        public void RespectsTypeNameFormatterOverrides()
        {
            var builder = new TsModelBuilder();

            builder.Add <DifferentNamespaces_Class1>();
            builder.Add <DifferentNamespaces_Class2>();

            var generator = new TsGenerator();

            // Reverse the order of the classes
            generator.RegisterTypeFormatter((t, f) => ((TsClass)t).Name == "DifferentNamespaces_Class1" ? "classz" : "classa");
            var model = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("classa") < result.IndexOf("classz"), "Didn't order classes when formatters involved");
        }
        public void WhenClosedGenericTypeAppearsAsPropertyTypeMultipleTimes_OnlyOneInterfaceGenerated()
        {
            var builder = new TsModelBuilder();

            builder.Add <GenericPropertiesBug>();

            var generator = new TsGenerator();
            var model     = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("interface KeyValuePair") > -1, "KeyValuePair interface missing");
            Assert.True(result.IndexOf("interface KeyValuePair") == result.LastIndexOf("interface KeyValuePair"), "KeyValuePair interface generated too many times");

            Assert.True(result.Contains("Test1: System.Collections.Generic.KeyValuePair"));
            Assert.True(result.Contains("Test2: System.Collections.Generic.KeyValuePair"));
        }
コード例 #25
0
        public void RespectsModuleNameFormatterOverrides()
        {
            var builder = new TsModelBuilder();

            builder.Add <DifferentNamespaces_Class2>();
            builder.Add <DifferentNamespaces_Class3>();

            var generator = new TsGenerator();

            // Reverse the order of the modules
            generator.SetModuleNameFormatter(m => m.Name == "TypeLitePlus.Tests.NetCore.TestModels.Namespace1" ? "modz" : "moda");
            var model = builder.Build();

            var result = generator.Generate(model);

            Assert.True(result.IndexOf("moda") < result.IndexOf("modz"), "Didn't order namespaces when formatters involved");
        }
コード例 #26
0
        private static string CompileModels(List <Type> types)
        {
            var builder = new TsModelBuilder();

            types.ForEach(a => builder.Add(a));

            var model = builder.Build();

            var generator = new TsGenerator();

            generator.IndentationString = "    ";
            generator.SetTypeVisibilityFormatter((tsClass, typeName) => true);
            generator.SetModuleNameFormatter((module) => "");
            generator.SetIdentifierFormatter((identifier) => Char.ToLower(identifier.Name[0]) + identifier.Name.Substring(1));

            string typeScript = generator.Generate(model);

            return(typeScript);
        }
コード例 #27
0
        public void CanUseCustomFormatter()
        {
            var builder = new TsModelBuilder();
            builder.Add<Drink>();

            var generator = new TsGenerator();
            generator.SetModuleNameFormatter(_ => "custom");//everything should go into the 'custom' module
            var model = builder.Build();
            var result = generator.Generate(model);

            var expectedOutput = @"
            declare module custom {
            interface Drink {
            }
            }
            ";

            Assert.Equal(expectedOutput, result);
        }
コード例 #28
0
        public void WhenArrayOfArrayEncountered_ArrayOfArrayTypeScriptTypeGenerated()
        {
            var builder = new TsModelBuilder();
            builder.Add<TestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model);

            Assert.Contains("MyStringProperty: string;", result);
            Assert.Contains("MyArray: string[];", result);
            Assert.Contains("MyJaggedArray: string[][];", result);
            Assert.Contains("MyVeryJaggedArray: string[][][];", result);
            Assert.Contains("MyIEnumerableOfString: string[];", result);
            Assert.Contains("MyListOfString: string[];", result);

            Assert.Contains("MyListOfStringArrays: string[][];", result);
            Assert.Contains("MyListOfIEnumerableOfString: string[][];", result);
            Assert.Contains("MyListOfListOfStringArray: string[][][];", result);
        }
コード例 #29
0
        public void CanUseCustomFormatter()
        {
            var builder = new TsModelBuilder();

            builder.Add <Drink>();

            var generator = new TsGenerator();

            generator.SetModuleNameFormatter(_ => "custom");//everything should go into the 'custom' module
            var model  = builder.Build();
            var result = generator.Generate(model);

            var expectedOutput = @"
declare module custom {
	interface Drink {
	}
}
";

            Assert.Equal(expectedOutput, result);
        }
コード例 #30
0
        public void WhenArrayOfArrayEncountered_ArrayOfArrayTypeScriptTypeGenerated()
        {
            var builder = new TsModelBuilder();

            builder.Add <TestClass>();

            var generator = new TsGenerator();
            var model     = builder.Build();
            var result    = generator.Generate(model);

            Assert.Contains("MyStringProperty: string;", result);
            Assert.Contains("MyArray: string[];", result);
            Assert.Contains("MyJaggedArray: string[][];", result);
            Assert.Contains("MyVeryJaggedArray: string[][][];", result);
            Assert.Contains("MyIEnumerableOfString: string[];", result);
            Assert.Contains("MyListOfString: string[];", result);

            Assert.Contains("MyListOfStringArrays: string[][];", result);
            Assert.Contains("MyListOfIEnumerableOfString: string[][];", result);
            Assert.Contains("MyListOfListOfStringArray: string[][][];", result);
        }
コード例 #31
0
 static void Main(string[] args)
 {
     DllResolver.HandleUnresolvedDlls(args[0]);
     TsGenerator.Generate(args[0], args[1], args[2]);
 }
コード例 #32
0
        public void WhenConvertorIsRegistered_ConvertedTypeNameIsUsedForFields()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();

            var target = new TsGenerator();
            target.RegisterTypeConvertor<string>(type => "KnockoutObservable<string>");
            var script = target.Generate(model, TsGeneratorOutput.Fields);

            Assert.Contains("PostalCode: KnockoutObservable<string>", script);
        }
コード例 #33
0
        public void WhenModelContainsReference_ReferenceIsAddedToOutput()
        {
            var model = new TsModel();
            model.References.Add("knockout.d.ts");

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.Contains("/// <reference path=\"knockout.d.ts\" />", script);
        }
コード例 #34
0
        public void WhenEnumIsReferencedAndOutputIsSetToFields_ConstantIsntInOutput()
        {
            var builder = new TsModelBuilder();
            builder.Add<Item>();
            var model = builder.Build();
            var target = new TsGenerator();
            var script = target.Generate(model, TsGeneratorOutput.Fields);

            Assert.DoesNotContain("MaxItems", script);
        }
コード例 #35
0
        public void WhenEnumIsReferencedAndOutputIsSetToProperties_EnumIsntInOutput()
        {
            var builder = new TsModelBuilder();
            builder.Add<Item>();
            var model = builder.Build();
            var target = new TsGenerator();
            var script = target.Generate(model, TsGeneratorOutput.Properties);

            Assert.DoesNotContain("enum ItemType", script);
        }
コード例 #36
0
        public void WhenFieldIsIgnored_FieldIsExcludedFromInterface()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();
            model.Classes.Where(o => o.Name == "Address").Single().Fields.Where(f => f.Name == "PostalCode").Single().IsIgnored = true;

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.False(script.Contains("PostalCode"));
        }
コード例 #37
0
        public void WhenInterfaceIsAdded_InterfaceIsInOutput()
        {
            var builder = new TsModelBuilder();
            builder.Add<IShippingService>();
            var model = builder.Build();
            var target = new TsGenerator();
            var script = target.Generate(model, TsGeneratorOutput.Properties);

            Assert.Contains("IShippingService", script);
            Assert.Contains("Price", script);
        }
コード例 #38
0
        public void WhenModuleIsGeneratedWithPropertiesOnlyOption_ModuleHasDeclareKeyword()
        {
            var builder = new TsModelBuilder();
            builder.Add<MyTestClass>();

            var generator = new TsGenerator();
            var model = builder.Build();
            var result = generator.Generate(model, TsGeneratorOutput.Properties);

            Assert.Contains("declare", result);
        }
コード例 #39
0
        public void WhenGenerate_OutputIsFormated()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();

            var target = new TsGenerator();
            var script = target.Generate(model);

            using (var reader = new StringReader(script)) {
                var line = string.Empty;
                while((line = reader.ReadLine()) != null) {
                    if (line.Contains("interface Address {")) {
                        Assert.True(line.StartsWith("\t"));
                    }
                    if (line.Contains("ID: Guid")) {
                        Assert.True(line.StartsWith("\t\t"));
                    }
                }
            }
        }
コード例 #40
0
        public void WhenConvertorIsRegisteredForGuid_NoStringInterfaceIsDefined()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();

            var target = new TsGenerator();
            target.RegisterTypeConvertor<Guid>(type => "string");
            var script = target.Generate(model);

            Assert.DoesNotContain("interface string {", script);
        }
コード例 #41
0
        public void WhenPropertyIsIgnored_PropertyIsExcludedFromInterface()
        {
            var builder = new TsModelBuilder();
            builder.Add<Address>();
            var model = builder.Build();
            model.Classes.Where(o => o.Name == "Address").Single().Properties.Where(p => p.Name == "Street").Single().IsIgnored = true;

            var target = new TsGenerator();
            var script = target.Generate(model);

            Assert.False(script.Contains("Street"));
        }