public void WhenEntriesInNonSequentialOrder_ThenSort()
            {
                var entry1 = TestSubRipEntryFactory.Create1();
                var entry2 = TestSubRipEntryFactory.Create2();
                var entry3 = TestSubRipEntryFactory.Create3();
                var entry4 = TestSubRipEntryFactory.Create4();
                var entry5 = TestSubRipEntryFactory.Create5();

                var entries = new List <SubRipEntry>
                {
                    entry2, entry1, entry3, entry5, entry4
                };

                var sut = new SubRipFile(FileName, entries);

                sut.Sort();

                Assert.That(sut.Entries.First().OrderId, Is.EqualTo(entry1.OrderId));
                Assert.That(sut.Entries.First().Text, Is.EqualTo(entry1.Text));

                Assert.That(sut.Entries.Second().OrderId, Is.EqualTo(entry2.OrderId));
                Assert.That(sut.Entries.Second().Text, Is.EqualTo(entry2.Text));

                Assert.That(sut.Entries.Third().OrderId, Is.EqualTo(entry3.OrderId));
                Assert.That(sut.Entries.Third().Text, Is.EqualTo(entry3.Text));

                Assert.That(sut.Entries.Fourth().OrderId, Is.EqualTo(entry4.OrderId));
                Assert.That(sut.Entries.Fourth().Text, Is.EqualTo(entry4.Text));

                Assert.That(sut.Entries.Fifth().OrderId, Is.EqualTo(entry5.OrderId));
                Assert.That(sut.Entries.Fifth().Text, Is.EqualTo(entry5.Text));
            }
        public void LoadSrtFile(string fileLocation)
        {
            ClearGrid();
            OpenSubRipFile = SubRipFile.Load(fileLocation);

            LoadSrtVideoIntoGrid();
        }
            public void WhenNoEntries_ThenDoNothing()
            {
                var sut = new SubRipFile(FileName, new List <SubRipEntry>());

                sut.Sort();

                Assert.That(sut.Entries, Is.Empty);
            }
            public void WhenValidOrder_ThenReturnTrue()
            {
                var sut = new SubRipFile(FileName, new List <SubRipEntry>
                {
                    _entry1, _entry2, _entry3, _entry4, _entry5
                });

                var result = sut.IsOrderIdSequenceValid();

                Assert.That(result, Is.True);
            }
Esempio n. 5
0
 public ActionResult SubRipHistory(int id)
 {
     if (id > 0)
     {
         ArquiveHistory arquive = db.ArquiveHistory.Find(id);
         SubRipFile     srf     = new SubRipFile(arquive.ArquiveFile);
         Tuple <String, String, String> status = srf.OpenDialogSaveAs();
         ModelState.AddModelError(status.Item1, status.Item2);
     }
     return(View(db.ArquiveHistory.ToList()));
 }
            public void WhenTextContainsUnderLine_ThenRemove()
            {
                _entry1.Text = "This is some <u>text</u> and some other {u}text{/u}.";

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.RemoveTextFormatting();

                Assert.That(sut.Entries.Single().Text, Is.EqualTo("This is some text and some other text."));
            }
            public void WhenDoesNotContainReturnChars_ThenDoNothing()
            {
                _entry1.Text = "Some text";

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.RemoveTextReturnChars();

                Assert.That(sut.Entries.Single().Text, Is.EqualTo("Some text"));
            }
            public void WhenEntryLessThanMax_ThenDoNothing()
            {
                var ts = new SubRipTimeSpan(0, 0, 2, 700);

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.SetMaxDuration(ts);

                Assert.That(sut.Entries.Single().Duration.ToString(), Is.EqualTo("01:40:55,758 --> 01:40:58,426"));
            }
            public void WhenEntryOverMax_ThenSetDurationToMax()
            {
                var ts = new SubRipTimeSpan(0, 0, 2, 500);

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.SetMaxDuration(ts);

                Assert.That(sut.Entries.Single().Duration.ToString(), Is.EqualTo("01:40:55,758 --> 01:40:58,258"));
            }
            public void WhenTextContainsFontColor_ThenRemove(string text, string expected)
            {
                _entry1.Text = text;

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.RemoveTextFormatting();

                Assert.That(sut.Entries.Single().Text, Is.EqualTo(expected));
            }
            public void WhenContainsReturns_ThenRemoveReturns(string text, string expected)
            {
                _entry1.Text = text;

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.RemoveTextReturnChars();

                Assert.That(sut.Entries.Single().Text, Is.EqualTo(expected));
            }
        /// <summary>
        /// Perform extra validation on the provided SubRip file.
        /// </summary>
        /// <param name="subRipFile">SubRip file to perform the validation on.</param>
        public void Validate(SubRipFile subRipFile)
        {
            if (!subRipFile.IsOrderIdSequenceValid())
            {
                throw new SubRipException("SubRip file contains entries with Order ID in the wrong order/position. Order IDs should be sequential from one onwards.");
            }

            if (subRipFile.AnyEntryOverlap())
            {
                throw new SubRipException("SubRip file contains entries whose durations overlap with their neighbor.");
            }
        }
            public void WhenTextLengthIsEqualOrLessThanMax_ThenDoNothing(string text, int maxLineLength)
            {
                _entry1.Text = text;

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.SetTextLineMaxLength(maxLineLength);

                Assert.That(sut.Entries.Single().Text, Is.EqualTo(text));
            }
            public void WhenOneEntry_ThenDoNothing()
            {
                var subRipEntry = TestSubRipEntryFactory.Create1();

                var sut = new SubRipFile(FileName, new List <SubRipEntry>
                {
                    subRipEntry
                });

                sut.Sort();

                Assert.That(sut.Entries.First().Text, Is.EqualTo(subRipEntry.Text));
            }
            public void WhenTextLengthIsMoreThanMax_ThenReturnThreeLines()
            {
                // 1234567890123456789012345678901234567890
                _entry1.Text = "This is some text thats just too long. Other text.";

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1
                });

                sut.SetTextLineMaxLength(20);

                Assert.That(sut.Entries.Single().Text, Is.EqualTo("This is some text\r\nthats just too long.\r\n Other text."));
            }
            public void WhenTextContainsReturnChars_ThenDoNothing(string text)
            {
                var entry = TestSubRipEntryFactory.Create1();

                entry.Text = text;

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    entry
                });

                sut.SetTextLineMaxLength(20);

                Assert.That(sut.Entries.Single().Text, Is.EqualTo(text));
            }
            public void SetUp()
            {
                // "01:40:55,758 --> 01:40:58,426"
                var entry1 = TestSubRipEntryFactory.Create1();

                // "01:40:58,677 --> 01:41:02,013"
                var entry2 = TestSubRipEntryFactory.Create2();

                var entries = new List <SubRipEntry> {
                    entry1, entry2
                };

                _sut = new SubRipFile(FileName, entries);
            }
Esempio n. 18
0
        /// <summary>
        /// 指定のログファイルを読み込む
        /// </summary>
        /// <param name="logPath"></param>
        /// <returns>読み込み成功したらtrue</returns>
        public static bool LoadSRTFile(string logPath)
        {
            Console.WriteLine("srtファイル読み込み開始");

            //改行コードがCRLFでないと正しくパースできないので、事前にチェックして変換、上書きする
            if (ReplaceLF2CRLF(logPath) == false)
            {
                return(false);                                  //falseなら中断
            }
            //読み込み処理
            SubRipFile subs = SubRipFile.Load(logPath);

            foreach (SubRipEntry sub in subs.Entries)
            {
                //Console.WriteLine(sub.OrderId);         //通し番号
                //Console.WriteLine(sub.Duration.Start);  //INタイムコード
                //Console.WriteLine(sub.Duration.End);    //OUTタイムコード
                //Console.WriteLine(sub.Text);            //字幕テキスト
                //Console.WriteLine(sub.Duration.Start.ToString());

                Dogagan_Record rec            = new Dogagan_Record();
                char[]         delimiterChars = { ':', ',' };
                string[]       tc             = sub.Duration.Start.ToString().Split(delimiterChars);

                //タイムコードを変換&整合性チェック
                int  hour, min, sec;
                bool isIntTc0 = int.TryParse(tc[0], out hour);
                bool isIntTc1 = int.TryParse(tc[1], out min);
                bool isIntTc2 = int.TryParse(tc[2], out sec);
                if (!isIntTc0 || !isIntTc1 || !isIntTc2)
                {
                    MessageBox.Show("タイムコード形式が不正な行があり、ログ読み込みを中断します。\r" + sub.ToString());
                    return(false);
                }
                else
                {
                    rec.TimeStamp = hour * 3600 + min * 60 + sec;
                }
                rec.Transcript = sub.Text;
                AppModel.Records.Add(rec);
                rec.Renew();
            }

            AppModel.MainWindow.ListBox_Records.DataContext = AppModel.Records.Records;
            AppModel.MainWindow.MI_Replace.IsEnabled        = true;

            AppModel.CurrentLogFilePath = logPath;
            return(true);
        }
            public void WhenEntriesMix_ThenSetDuration()
            {
                var ts = new SubRipTimeSpan(0, 0, 2, 800);

                var sut = new SubRipFile(FileName, new List <SubRipEntry> {
                    _entry1, _entry2
                });

                sut.SetMaxDuration(ts);

                // Hasn't changed
                Assert.That(sut.Entries.First().Duration.ToString(), Is.EqualTo("01:40:55,758 --> 01:40:58,426"));

                // Has set to new ts
                Assert.That(sut.Entries.Second().Duration.ToString(), Is.EqualTo("01:40:58,677 --> 01:41:01,477"));
            }
Esempio n. 20
0
        public ActionResult SubRipForm(HttpPostedFileBase file, int offset = 0)
        {
            //Validate if the file was uploaded
            if (object.ReferenceEquals(null, file))
            {
                ModelState.AddModelError("FileError", "Selecione um arquivo");
                return(View());
            }
            //Validate if file extension is .srt
            if (!Path.GetExtension(file.FileName).Equals(".srt"))
            {
                ModelState.AddModelError("FileError", "Formato do arquivo deve ser .srt");
                return(View());
            }

            if (file is null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            //Validate if offset is not null
            if (offset == 0)
            {
                ModelState.AddModelError("OffsetError", "Valor não pode ser 0 ou nulo");
                return(View());
            }


            try
            {
                SubRipFile srf = new SubRipFile(SubRipFile.FileToByte(file));
                srf.Add(offset);

                Tuple <String, String, String> status = srf.OpenDialogSaveAs();
                ModelState.AddModelError(status.Item1, status.Item2);

                //Adiciona ao banco
                db.ArquiveHistory.Add(new ArquiveHistory(Path.GetFileName(status.Item3), DateTime.Now, SubRipFile.StringToByte(srf.Render()), offset));
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Error", ex.Message);
            }
            return(View());
        }
            public void WhenFileExists_ThenLoadFromFile()
            {
                var sut = SubRipFile.Load(TestFiles.CarlitosWay);

                Assert.That(sut.FileName, Is.EqualTo("Carlito's Way [1993] (English Forced).srt"));
                Assert.That(sut.Entries.Count, Is.EqualTo(11));

                Assert.That(sut.Entries.First().OrderId, Is.EqualTo(1));
                Assert.That(sut.Entries.First().Duration.ToString(), Is.EqualTo("01:40:55,758 --> 01:40:58,426"));
                Assert.That(sut.Entries.First().Text, Is.EqualTo("Listen to me carefully, Carlito."));

                Assert.That(sut.Entries.Second().OrderId, Is.EqualTo(2));
                Assert.That(sut.Entries.Second().Duration.ToString(), Is.EqualTo("01:40:58,677 --> 01:41:02,013"));
                Assert.That(sut.Entries.Second().Text, Is.EqualTo("Rudy says Pachanga is complaining\r\nabout being broke;"));

                Assert.That(sut.Entries.Last().OrderId, Is.EqualTo(11));
                Assert.That(sut.Entries.Last().Duration.ToString(), Is.EqualTo("01:41:37,633 --> 01:41:39,634"));
                Assert.That(sut.Entries.Last().Text, Is.EqualTo("See you tomorrow."));
            }
Esempio n. 22
0
        /// <summary>
        /// Checks whether all the entries order IDs are in sequential
        /// order from one onwards.
        /// </summary>
        /// <param name="source">SubRip file to check.</param>
        /// <returns>True if entries are in sequence; otherwise false.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
        public static bool IsOrderIdSequenceValid(this SubRipFile source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var expectedOrderId = SubRipFile.FirstOrderId;

            foreach (var subRipEntry in source.Entries)
            {
                if (subRipEntry.OrderId != expectedOrderId)
                {
                    return(false);
                }

                expectedOrderId++;
            }

            return(true);
        }
Esempio n. 23
0
        /// <summary>
        /// Determine if any entry's duration overlaps with it's neighbor.
        /// </summary>
        /// <param name="source">SubRip file to check.</param>
        /// <returns>True if an entry's duration does overlap with it's neighbor.</returns>
        public static bool AnyEntryOverlap(this SubRipFile source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            for (var i = 0; i < source.Entries.Count; i++)
            {
                var entry = source.Entries[i];

                var nextEntry = source.Entries.GetNext(i);

                if (nextEntry != null)
                {
                    if (entry.Duration.End >= nextEntry.Duration.Start)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 public void WhenPathIsNull_ThenThrowException()
 {
     Assert.Throws <ArgumentNullException>(() => _ = SubRipFile.Load(null));
 }
            public void WhenPerformExtraValidation_AndFileInvalid_ThenThrowException()
            {
                var ex = Assert.Throws <SubRipException>(() => _ = SubRipFile.Load(TestFiles.WrongOrder, true));

                Assert.That(ex.Message, Is.EqualTo("SubRip file contains entries with Order ID in the wrong order/position. Order IDs should be sequential from one onwards."));
            }
 public void WhenFileDoesNotExist_ThenThrowException()
 {
     Assert.Throws <FileNotFoundException>(() => SubRipFile.Load(TestFiles.DoesNotExist));
 }
            public void WhenFileContentsNullOrEmptyOrWhiteSpace_ThenSetEmptyEntries(string fileContents)
            {
                var sut = new SubRipFile(FileName, fileContents);

                Assert.That(sut.Entries, Is.Empty);
            }
 public void WhenEntriesIsNull_ThenThrowException()
 {
     Assert.Throws <ArgumentNullException>(() => _ = new SubRipFile(FileName, null as List <SubRipEntry>));
 }
            public void WhenNumberOfCharsIsLessThanOne_ThenThrowException()
            {
                var sut = new SubRipFile(FileName, new List <SubRipEntry>());

                Assert.Throws <ArgumentOutOfRangeException>(() => sut.SetTextLineMaxLength(0));
            }