public Generator(GeneratorOptions options, ILogger logger = null) { Requires.NotNull(options, nameof(options)); _generationContext = new GenerationContext(); FileContentGenerated += OnFileContentGenerated; Options = options; Logger = logger; var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = options }; var internalStorage = new InternalStorage(); _fileSystem = new FileSystem(); _metadataReaderFactory = new MetadataReaderFactory(); _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); _typeDependencyService = new TypeDependencyService(_typeService, _metadataReaderFactory); _templateService = new TemplateService(internalStorage, generatorOptionsProvider); _tsContentGenerator = new TsContentGenerator(_typeDependencyService, _typeService, _templateService, new TsContentParser(_fileSystem), _metadataReaderFactory, generatorOptionsProvider, logger); }
public void GetImportsText_TypeGiven_ImportsTextGenerated(Type type, string outputDir, TypeNameConverterCollection fileNameConverters, TypeNameConverterCollection typeNameConverters, IEnumerable <object> typeDependencies, string expectedOutput) { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { FileNameConverters = fileNameConverters, TypeNameConverters = typeNameConverters } }; _typeDependencyService.GetTypeDependencies(Arg.Any <Type>()).Returns(typeDependencies); _templateService.FillImportTemplate(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Returns(i => $"{i.ArgAt<string>(0)} | {i.ArgAt<string>(1)} | {i.ArgAt<string>(2)};"); var tsContentGenerator = new TsContentGenerator(_typeDependencyService, _typeService, _templateService, _tsContentParser, _metadataReaderFactory, generatorOptionsProvider, null); //act string actualOutput = tsContentGenerator.GetImportsText(type, outputDir); //assert Assert.Equal(expectedOutput, actualOutput); }
static Program() { _consoleArgsReader = new ConsoleArgsReader(); _logger = new Logger(); _fileSystem = new FileSystem(); _configProvider = new ConfigProvider(_fileSystem, _logger, new JsonSerializer()); _generatorOptionsProvider = new GeneratorOptionsProvider(_fileSystem, _logger); _projectFileManager = new ProjectFileManager(_fileSystem); }
public void GetImportsText_TypeNull_ExceptionThrown() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; var tsContentGenerator = new TsContentGenerator(_typeDependencyService, _typeService, _templateService, _tsContentParser, _metadataReaderFactory, generatorOptionsProvider, null); //act,assert Assert.Throws <ArgumentNullException>(() => tsContentGenerator.GetImportsText(null, "asdf")); }
public TypeServiceTest() { // this needs to be changed to use mocked MetadataReader _metadataReaderFactory = Substitute.For <IMetadataReaderFactory>(); _metadataReaderFactory.GetInstance().Returns(new AttributeMetadataReader()); var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); }
public void GetImplementsText_TypeNameConvertersNull_ExceptionThrown() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { TypeNameConverters = null } }; var tsContentGenerator = new TsContentGenerator(_typeDependencyService, _typeService, _templateService, _tsContentParser, _metadataReaderFactory, generatorOptionsProvider, null); //act,assert Assert.Throws <ArgumentNullException>(() => tsContentGenerator.GetImplementsText(typeof(string))); }
public void GetExtendsText_NameGiven_ExtendsTextReturned() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.GetExtendsText("MyName"); //assert Assert.Equal(" extends MyName", actual); }
public void GetMemberValueText_MemberGiven_CorrectValueReturned(MemberInfo memberInfo, bool convertTypesToString, string expected) { //arrange ITypeService typeService = GetTypeService(convertTypesToString); var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; var tsContentGenerator = new TsContentGenerator(_typeDependencyService, typeService, _templateService, _tsContentParser, _metadataReaderFactory, generatorOptionsProvider, null); //act string actual = tsContentGenerator.GetMemberValueText(memberInfo); //assert Assert.Equal(expected, actual); }
public void FillClassPropertyTemplate_ValuesGiven_TemplateFilledWithValues() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.ClassProperty.tpl") .Returns("$tg{modifiers} | $tg{name} | $tg{type} | $tg{defaultValue}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.FillClassPropertyTemplate("a", "B", "c", "D"); //assert Assert.Equal("a | B | : c | = D", actual); }
public void FillClassDefaultExportTemplate_ValuesGiven_TemplateFilledWithValues() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.ClassDefaultExport.tpl") .Returns("$tg{imports} | $tg{name} | $tg{exportName} | $tg{extends} | $tg{properties} | $tg{customHead} | $tg{customBody}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.FillClassDefaultExportTemplate("a", "B", "c", "D", "e", "F", "g"); //assert Assert.Equal("a | B | c | D | e | F | g", actual); }
public void GetTsTypeName_TypeGiven_TsTypeNameReturned(Type type, TypeNameConverterCollection converters, bool forTypeDeclaration, string expectedResult) { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { TypeNameConverters = converters } }; _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); //act string actualResult = _typeService.GetTsTypeName(type, forTypeDeclaration); //assert Assert.Equal(expectedResult, actualResult); }
public void UseDefaultExport_TypeGiven_DeterminedIfDefaultExport(Type type, bool optionsDefaultExport, bool expectedResult) { // arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { UseDefaultExport = optionsDefaultExport } }; _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); // act bool actualResult = _typeService.UseDefaultExport(type); // assert Assert.Equal(expectedResult, actualResult); }
public void FillEnumValueTemplate_StringValuesGiven_TemplateFilledWithValues() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.EnumValue.tpl") .Returns("$tg{name} | $tg{value}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.FillEnumValueTemplate("a", "stringValue"); //assert Assert.Equal(@"a | ""stringValue""", actual); }
public void FillImportTemplate_ValuesGiven_TemplateFilledWithValues(string typeAlias, string expectedResult) { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.Import.tpl") .Returns("$tg{name} | $tg{aliasText} | $tg{path}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.FillImportTemplate("a", typeAlias, "c"); //assert Assert.Equal(expectedResult, actual); }
public void FillIndexExportTemplate_ValuesGiven_TemplateFilledWithValues() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.IndexExport.tpl") .Returns("$tg{filename}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actual = templateService.FillIndexExportTemplate("a"); //assert Assert.Equal("a", actual); }
public void FillEnumDefaultExportTemplate_ValuesGiven_TemplateFilledWithValues() { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions() }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.EnumDefaultExport.tpl") .Returns("$tg{imports} | $tg{name} | $tg{values} | $tg{modifiers}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actualConst = templateService.FillEnumDefaultExportTemplate("a", "B", "c", true); string actualNotConst = templateService.FillEnumDefaultExportTemplate("a", "B", "c", false); //assert Assert.Equal("a | B | c | const", actualConst); Assert.Equal("a | B | c | ", actualNotConst); }
public void GetTsTypeName_MemberGiven_TsTypeNameReturned(MemberInfo memberInfo, TypeNameConverterCollection converters, StrictNullTypeUnionFlags csNullableTranslation, string expectedResult) { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { TypeNameConverters = converters, CsNullableTranslation = csNullableTranslation } }; _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); //act string actualResult = _typeService.GetTsTypeName(memberInfo); //assert Assert.Equal(expectedResult, actualResult); }
public void GetTsTypeName_CustomTypeGiven_CustomTsTypeNameReturned(Type type, TypeNameConverterCollection converters, bool forTypeDeclaration, Type customMappingKey, string customMappingValue, string expectedResult) { //arrange var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = new GeneratorOptions { CustomTypeMappings = { { customMappingKey.FullName, customMappingValue } }, TypeNameConverters = converters } }; _typeService = new TypeService(_metadataReaderFactory, generatorOptionsProvider); //act string actualResult = _typeService.GetTsTypeName(type, forTypeDeclaration); //assert Assert.Equal(expectedResult, actualResult); }
public void FillClassDefaultExportTemplate_SpecialCharsPresent_SpecialCharsReplaced() { //arrange var generatorOptions = new GeneratorOptions { TabLength = 3 }; var generatorOptionsProvider = new GeneratorOptionsProvider { GeneratorOptions = generatorOptions }; _internalStorage.GetEmbeddedResource("TypeGen.Core.Templates.ClassDefaultExport.tpl") .Returns("$tg{tab} | $tg{quot}"); var templateService = new TemplateService(_internalStorage, generatorOptionsProvider); //act string actualDoubleQuote = templateService.FillClassDefaultExportTemplate("", "", "", "", "", "", ""); generatorOptions.SingleQuotes = true; string actualSingleQuote = templateService.FillClassDefaultExportTemplate("", "", "", "", "", "", ""); //assert Assert.Equal(" | \"", actualDoubleQuote); Assert.Equal(" | '", actualSingleQuote); }