public void GetAllEntries_ProperConditions_ReturnsAllEntries(string filePath, int numOfLines) { var fileValidator = new FileValidator(new ValidationData()); var setService = new SetService(fileValidator); var fileSource = new EmbeddedSource(filePath, Assembly.GetExecutingAssembly()); var wordsSetOperator = new WordsSetOperator(setService, fileSource); if (!wordsSetOperator.LoadSet()) { throw new Exception("Set is null."); } int index = 0; foreach (Entry?entry in wordsSetOperator.GetEntries(false, true)) { if (entry == null) { break; } index++; } Assert.Equal(numOfLines, index); }
public static ConfigService <T> Get(string name, Assembly assembly) { var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{name}.json"); var configSource = new FileSource(configFilePath); var configSchemaSource = new EmbeddedSource($"{name}.schema.json", assembly); var configService = new ConfigService <T>(configSource, configSchemaSource); return(configService); }
public void LoadSet_NotExistedFile_ReturnsFalseResult(string filePath) { // Arrange var fileValidator = new FileValidator(new ValidationData()); var setService = new SetService(fileValidator); var fileSource = new EmbeddedSource(filePath, Assembly.GetExecutingAssembly()); // Act void action() => setService.Load(fileSource); // Assert ResourceNotFoundException ex = Assert.Throws <ResourceNotFoundException>(action); }
public void Init_NotExistedEmbeddedResource_ThrowsFileNotFoundException(string resourcePath) { // Act void action() { var embeddedSource = new EmbeddedSource(resourcePath, Assembly.GetExecutingAssembly()); embeddedSource.GetContents(); } // Assert ResourceNotFoundException ex = Assert.Throws <ResourceNotFoundException>(action); Assert.Equal(String.Format("Resource ending with {0} not found.", resourcePath), ex.Message); }
public void Get_CorrectConfiguration_ReturnsSettingsObject( string configPath, string schemaPath, Settings expectedSettings) { // Arrange var configSource = new EmbeddedSource(configPath, Assembly.GetExecutingAssembly()); var configSchemaSource = new EmbeddedSource(schemaPath, Assembly.GetExecutingAssembly()); var appSettings = new ConfigService <Settings>(configSource, configSchemaSource); // Act Settings settings = appSettings.Get(); // Assert var options = new JsonSerializerOptions { WriteIndented = true, }; string expectedSettingsJson = JsonSerializer.Serialize(expectedSettings, options); string settingsJson = JsonSerializer.Serialize(settings, options); Assert.Equal(expectedSettingsJson, settingsJson); }
public void Init_WrongConfiguration_ThrowsWrongConfigException( string configPath, string schemaPath, string expectedErrorMsg, Dictionary <string, string> expectedExceptionDetails) { // Arrange var configSource = new EmbeddedSource(configPath, Assembly.GetExecutingAssembly()); var configSchemaSource = new EmbeddedSource(schemaPath, Assembly.GetExecutingAssembly()); // Act void action() => _ = new ConfigService <Settings>(configSource, configSchemaSource); // Assert WrongConfigException ex = Assert.Throws <WrongConfigException>(action); Assert.Equal(expectedErrorMsg, ex.Message); Assert.Equal(expectedExceptionDetails.Count, ex.Data.Count); foreach (KeyValuePair <string, string> el in expectedExceptionDetails) { Assert.True(ex.Data.Contains(el.Key)); Assert.Equal(el.Value, ex.Data[el.Key]); } }
public static async Task RunAsync(string[] args, IServiceProvider servicesProvider) { var cliMapSource = new EmbeddedSource("cli-map.json", Assembly.GetExecutingAssembly()); var cliMapSchemaSource = new EmbeddedSource("cli-map.schema.json", Assembly.GetExecutingAssembly()); var cliMapService = new ConfigService <CLIMap>(cliMapSource, cliMapSchemaSource); List <Command>?commands = cliMapService.Config?.Commands; if (commands == null) { throw new ArgsException("CLI map has empty commands array."); } var commandsToExecute = new List <Command>(); foreach (string arg in args) { Command?command = commands.Where(x => x.Name == arg || x.Shortcut == arg).FirstOrDefault(); if (commandsToExecute.Count == 0 && command == null) { throw new ArgsException($"Argument {arg} isn't handled."); } if (command == null) { commandsToExecute[^ 1].Parameters.Add(arg);
public void RandomizeEntries_ProperConditions_ReturnsEntriesInRandomOrder(string filePath) { var fileValidator = new FileValidator(new ValidationData()); var setService = new SetService(fileValidator); var fileSource = new EmbeddedSource(filePath, Assembly.GetExecutingAssembly()); var wordsSetOperator = new WordsSetOperator(setService, fileSource); if (!wordsSetOperator.LoadSet()) { throw new Exception("Set is null."); } string str1 = ""; foreach (Entry?entry1 in wordsSetOperator.GetEntries(false, true)) { if (entry1 == null) { break; } str1 += string.Join(',', entry1.Words) + ',' + string.Join(',', entry1.Translations); } string str2 = ""; foreach (Entry?entry2 in wordsSetOperator.GetEntries(false, true)) { if (entry2 == null) { break; } str1 += string.Join(',', entry2.Words) + ',' + string.Join(',', entry2.Translations); } Assert.NotEqual(str1, str2); }
public static ProjectFile ToProjectFile(ProjectModel project) { ProjectFile file = new ProjectFile(); file.Stub.Type = project.Stub.Type; file.Stub.IconPath = ConvertAbsoluteToRelativePath(project.Stub.IconPath.ToNullIfEmpty()); file.Stub.Padding = project.Stub.Padding; file.Startup.Melt = project.Startup.Melt; file.VersionInfo.FileDescription = project.VersionInfo.FileDescription.ToNullIfEmptyOrWhiteSpace(); file.VersionInfo.ProductName = project.VersionInfo.ProductName.ToNullIfEmptyOrWhiteSpace(); file.VersionInfo.FileVersion = project.VersionInfo.FileVersion1 != 0 || project.VersionInfo.FileVersion2 != 0 || project.VersionInfo.FileVersion3 != 0 || project.VersionInfo.FileVersion4 != 0 ? project.VersionInfo.FileVersion1 + "." + project.VersionInfo.FileVersion2 + "." + project.VersionInfo.FileVersion3 + "." + project.VersionInfo.FileVersion4 : null; file.VersionInfo.ProductVersion = project.VersionInfo.ProductVersion.ToNullIfEmptyOrWhiteSpace(); file.VersionInfo.Copyright = project.VersionInfo.Copyright.ToNullIfEmptyOrWhiteSpace(); file.VersionInfo.OriginalFilename = project.VersionInfo.OriginalFilename.ToNullIfEmptyOrWhiteSpace(); if (project.Manifest.UseTemplate) { file.Manifest.Template = project.Manifest.Template; } else if (project.Manifest.UseFile) { file.Manifest.Path = ConvertAbsoluteToRelativePath(project.Manifest.Path.ToNullIfEmpty()); } foreach (ProjectItemModel item in project.Items) { ProjectSource source; if (item is ProjectMessageBoxItemModel) { source = null; } else { switch (item.Source) { case ProjectItemSource.Embedded: file.AddSource(source = new EmbeddedSource { Path = ConvertAbsoluteToRelativePath(item.SourceEmbeddedPath.ToNullIfEmpty()), Compress = item.SourceEmbeddedCompress, EofData = item.SourceEmbeddedEofData }); break; case ProjectItemSource.Download: file.AddSource(source = new DownloadSource { Url = item.SourceDownloadUrl }); break; default: throw new InvalidEnumArgumentException(); } source.Id = item.SourceId; } if (item is ProjectRunPEItemModel) { file.AddAction(new RunPEAction { Source = source }); } else if (item is ProjectInvokeItemModel) { file.AddAction(new InvokeAction { Source = source }); } else if (item is ProjectDropItemModel dropItem) { file.AddAction(new DropAction { Source = source, Location = dropItem.Location, FileName = dropItem.FileName, FileAttributeHidden = dropItem.FileAttributeHidden, FileAttributeSystem = dropItem.FileAttributeSystem, ExecuteVerb = dropItem.ExecuteVerb }); } else if (item is ProjectMessageBoxItemModel messageBoxItem) { file.AddAction(new MessageBoxAction { Title = messageBoxItem.Title, Text = messageBoxItem.Text, Icon = messageBoxItem.Icon, Buttons = messageBoxItem.Buttons, OnOk = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Ok) ? messageBoxItem.OnOk : ActionEvent.None, OnCancel = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Cancel) ? messageBoxItem.OnCancel : ActionEvent.None, OnYes = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Yes) ? messageBoxItem.OnYes : ActionEvent.None, OnNo = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.No) ? messageBoxItem.OnNo : ActionEvent.None, OnAbort = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Abort) ? messageBoxItem.OnAbort : ActionEvent.None, OnRetry = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Retry) ? messageBoxItem.OnRetry : ActionEvent.None, OnIgnore = MessageBoxAction.HasEvent(messageBoxItem.Buttons, MessageBoxEvent.Ignore) ? messageBoxItem.OnIgnore : ActionEvent.None }); } else { throw new InvalidOperationException(); } } return(file); string ConvertAbsoluteToRelativePath(string absolutePath) { return(project.ProjectPath == null ? absolutePath : RelativePath.AbsoluteToRelativePath(Path.GetDirectoryName(project.ProjectPath), absolutePath)); } }