Beispiel #1
0
            public void NullEncoding_ExceptionThrown()
            {
                var contents = Enumerable.Repeat("This is a test line.", 30000).ToList();
                var path     = Path.Combine(readAllLinesTestFolder, nameof(NullEncoding_ExceptionThrown));

                Directory.CreateDirectory(FileTestFolder);
                File.WriteAllLines(path, contents);

                Assert.ThrowsAsync <ArgumentNullException>(async() => await AsyncFile.ReadAllLinesAsync(path, null).ConfigureAwait(false));
            }
Beispiel #2
0
            public async Task ReadAllLinesWithEncoding(Encoding encoding)
            {
                var contents = Enumerable.Repeat("This is a test line.", 150).ToList();
                var path     = Path.Combine(readAllLinesTestFolder, nameof(ReadAllLinesWithEncoding));

                Directory.CreateDirectory(readAllLinesTestFolder);
                File.WriteAllLines(path, contents, encoding);

                var result = await AsyncFile.ReadAllLinesAsync(path, encoding).ConfigureAwait(false);

                CollectionAssert.AreEqual(contents, result);
            }
Beispiel #3
0
            public void CancellationToken_ExceptionThrown()
            {
                var contents = Enumerable.Repeat("This is a test line.", 30000).ToList();
                var path     = Path.Combine(readAllLinesTestFolder, nameof(CancellationToken_ExceptionThrown));

                Directory.CreateDirectory(readAllLinesTestFolder);
                File.WriteAllLines(path, contents);

                var tokenSource = new CancellationTokenSource();

                Assert.ThrowsAsync <TaskCanceledException>(async() =>
                {
                    tokenSource.Cancel();
                    await AsyncFile.ReadAllLinesAsync(path, tokenSource.Token).ConfigureAwait(false);
                });
            }
Beispiel #4
0
        protected override async void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            string[] file = null;

            if (e.Args.Length > 0 && File.Exists(e.Args[0]))
            {
                file = await AsyncFile.ReadAllLinesAsync(e.Args[0]);
            }
            else
            {
                var ofd = new OpenFileDialog {
                    Title = "Sélectionnez un fichier de traductions"
                };

                if (ofd.ShowDialog() == true && File.Exists(ofd.FileName))
                {
                    file = await AsyncFile.ReadAllLinesAsync(ofd.FileName);
                }
                else
                {
                    Shutdown(2);
                    return;
                }
            }

            if (file.Length < 4)
            {
                ShowError(true);
            }
            if (await file[0].AllAsync(c => c.Equals('=')) && await file[3].AllAsync(c => c.Equals('=')))
            {
                OSTName = file[1];
                Number  = file[2].StartsWith("⇒ ");
                Pattern = file[2].TrimStart("⇒ ", 1).Split(new[] { " ⇔ " }, StringSplitOptions.None);
                if (Pattern.Length <= 1 || await Pattern.AnyAsync(line => line.IsNullOrWhiteSpace()) || await(await Pattern.GroupByAsync(line => line)).AnyAsync(async group => await group.CountAsync() > 1))
                {
                    ShowError(true);
                }
            }
            else
            {
                ShowError(true);
            }

            if (file.Length < 6)
            {
                ShowError(false);
            }

            Translations = (await(await file.SubArray(5, file.Length - 5).SelectAsync(line =>
            {
                if (!Number ^ line.Contains(" ⇒ ") && line.Split(new[] { " ⇔ " }, StringSplitOptions.None).Length == Pattern.Length)
                {
                    if (Number)
                    {
                        int index = line.IndexOf(" ⇒ ");
                        string number = line.Substring(0, index);
                        string litt = line.Substring(index + 3);
                        return(new[] { number }.Concat(litt.Split(new[] { " ⇔ " }, StringSplitOptions.None)).ToArray());
                    }
                    else
                    {
                        return(line.Split(new[] { " ⇔ " }, StringSplitOptions.None));
                    }
                }
                else
                {
                    return(null);
                }
            })).WhereAsync(line => line != null)).ToArray();

            MainWindow = new MainWindow()
            {
                Title = $"{OSTName} - OSTRename"
            };
            MainWindow.Closed += (sender, args) => Shutdown();
            MainWindow.Show();
        }