From f49f81d50686f1caa39980255d3733f6b8e9c8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=B4=EF=BC=B2?= <31824852+TetsuOtter@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:08:24 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TRViS.IO.Tests/Loaders/LoaderJson.Tests.cs | 184 +++++++++---------- TRViS.IO.Tests/Loaders/LoaderSQL.Tests.cs | 23 +-- TRViS.IO.Tests/Loaders/LoaderSQL.v0.Tests.cs | 23 +-- 3 files changed, 109 insertions(+), 121 deletions(-) diff --git a/TRViS.IO.Tests/Loaders/LoaderJson.Tests.cs b/TRViS.IO.Tests/Loaders/LoaderJson.Tests.cs index db98ce26..4e4b661c 100644 --- a/TRViS.IO.Tests/Loaders/LoaderJson.Tests.cs +++ b/TRViS.IO.Tests/Loaders/LoaderJson.Tests.cs @@ -21,57 +21,50 @@ public void GetWorkGroupListTest() { IReadOnlyList actual = loader!.GetWorkGroupList(); - Assert.That(actual, Is.EquivalentTo(new WorkGroup[] + Assert.That(actual, Has.Count.EqualTo(2)); + Assert.Multiple(() => { - new WorkGroup() - { - Name = "WorkGroup01", - DBVersion = 1, - Id = 0, - }, - new WorkGroup() - { - Name = "WorkGroup02", - DBVersion = null, - Id = 1, - }, - })); + Assert.That(Guid.TryParse(actual[0].Id, out _), Is.True); + Assert.That(actual[0].Name, Is.EqualTo("WorkGroup01")); + Assert.That(actual[0].DBVersion, Is.EqualTo(1)); + + Assert.That(Guid.TryParse(actual[1].Id, out _), Is.True); + Assert.That(actual[1].Name, Is.EqualTo("WorkGroup02")); + Assert.That(actual[1].DBVersion, Is.Null); + }); } [Test] public void GetWorkListTest0() { - IReadOnlyList actual = loader!.GetWorkList(0); + IReadOnlyList workGroupList = loader!.GetWorkGroupList(); + string workGroupId = workGroupList[0].Id; + IReadOnlyList actual = loader!.GetWorkList(workGroupId); - Assert.That(actual, Is.EquivalentTo(new Work[] + Assert.That(actual, Has.Count.EqualTo(2)); + Assert.Multiple(() => { - new Work() - { - WorkGroupId = 0, - Id = 0, - Name = "WG01-Work01", - AffectDate = "20230318", - - AffixContent = null, - AffixContentType = -1, - Remarks = "仕業に対する注意事項を記載する", - HasETrainTimetable = false, - ETrainTimetableContent = null, - ETrainTimetableContentType = -1, - }, - new Work() - { - WorkGroupId = 0, - Id = 1, - Name = "WG01-Work02", - }, - })); + Assert.That(Guid.TryParse(actual[0].Id, out _), Is.True); + Assert.That(actual[0].WorkGroupId, Is.EqualTo(workGroupId)); + Assert.That(actual[0].Name, Is.EqualTo("WG01-Work01")); + Assert.That(actual[0].AffectDate, Is.EqualTo("20230318")); + Assert.That(actual[0].Remarks, Is.EqualTo("仕業に対する注意事項を記載する")); + Assert.That(actual[0].HasETrainTimetable, Is.False); + Assert.That(actual[0].ETrainTimetableContent, Is.Null); + Assert.That(actual[0].ETrainTimetableContentType, Is.EqualTo(-1)); + + Assert.That(Guid.TryParse(actual[1].Id, out _), Is.True); + Assert.That(actual[1].WorkGroupId, Is.EqualTo(workGroupId)); + Assert.That(actual[1].Name, Is.EqualTo("WG01-Work02")); + }); } [Test] public void GetWorkListTest1() { - IReadOnlyList actual = loader!.GetWorkList(1); + IReadOnlyList workGroupList = loader!.GetWorkGroupList(); + string workGroupId = workGroupList[1].Id; + IReadOnlyList actual = loader!.GetWorkList(workGroupId); Assert.That(actual, Is.EquivalentTo(Array.Empty())); } @@ -79,73 +72,66 @@ public void GetWorkListTest1() [Test] public void GetTrainListTest0() { - IReadOnlyList actual = loader!.GetTrainDataList(0); - - Assert.That(actual, Is.EquivalentTo(new TrainData[] + IReadOnlyList workGroupList = loader!.GetWorkGroupList(); + string workGroupId = workGroupList[0].Id; + IReadOnlyList workList = loader!.GetWorkList(workGroupId); + string workId = workList[0].Id; + IReadOnlyList actual = loader!.GetTrainDataList(workId); + + Assert.That(actual, Has.Count.EqualTo(2)); + Assert.Multiple(() => { - new TrainData() - { - AfterArrive = "着後作業", - AfterArrive_OnStationTrackCol = "「着後」のうち、着発番線と同じ行に記載されている内容", - AfterRemarks = "`(乗継)`など 最後の駅の下に記載されている内容", - BeforeDeparture = "発前作業", - BeforeDeparture_OnStationTrackCol = "「発前」のうち、着発番線と同じ行に記載されている内容", - BeginRemarks = "`(乗継)`など 最初の駅の上に記載されている内容", - CarCount = 10, - ColorId = -1, // Not Implemented - DayCount = 0, - Destination = "終着駅", - Direction = 1, - Id = 0, - IsRideOnMoving = false, - MaxSpeed = "最高速度", - NominalTractiveCapacity = "けん引定数", - Remarks = "列車に対する注意事項を記載する", - SpeedType = "速度種別", - TrainInfo = "列車情報 (列車名など)", - TrainNumber = "WG01-W01-Train01", - WorkId = 0, - WorkType = 0, - }, - - new TrainData() - { - Direction = -1, - Id = 1, - TrainNumber = "WG01-W01-Train02", - WorkId = 0, - - ColorId = -1, // Not Implemented - }, - })); + Assert.That(Guid.TryParse(actual[0].Id, out _), Is.True); + Assert.That(actual[0].WorkId, Is.EqualTo(workId)); + Assert.That(actual[0].TrainNumber, Is.EqualTo("WG01-W01-Train01")); + Assert.That(actual[0].Direction, Is.EqualTo(1)); + Assert.That(actual[0].ColorId, Is.EqualTo(-1)); + Assert.That(actual[0].TrainInfo, Is.EqualTo("列車情報 (列車名など)")); + Assert.That(actual[0].Destination, Is.EqualTo("終着駅")); + Assert.That(actual[0].CarCount, Is.EqualTo(10)); + Assert.That(actual[0].SpeedType, Is.EqualTo("速度種別")); + Assert.That(actual[0].MaxSpeed, Is.EqualTo("最高速度")); + Assert.That(actual[0].NominalTractiveCapacity, Is.EqualTo("けん引定数")); + Assert.That(actual[0].BeforeDeparture, Is.EqualTo("発前作業")); + Assert.That(actual[0].BeforeDeparture_OnStationTrackCol, Is.EqualTo("「発前」のうち、着発番線と同じ行に記載されている内容")); + Assert.That(actual[0].AfterArrive, Is.EqualTo("着後作業")); + Assert.That(actual[0].AfterArrive_OnStationTrackCol, Is.EqualTo("「着後」のうち、着発番線と同じ行に記載されている内容")); + Assert.That(actual[0].BeginRemarks, Is.EqualTo("`(乗継)`など 最初の駅の上に記載されている内容")); + Assert.That(actual[0].AfterRemarks, Is.EqualTo("`(乗継)`など 最後の駅の下に記載されている内容")); + Assert.That(actual[0].Remarks, Is.EqualTo("列車に対する注意事項を記載する")); + Assert.That(actual[0].IsRideOnMoving, Is.False); + + Assert.That(Guid.TryParse(actual[1].Id, out _), Is.True); + Assert.That(actual[1].WorkId, Is.EqualTo(workId)); + Assert.That(actual[1].TrainNumber, Is.EqualTo("WG01-W01-Train02")); + Assert.That(actual[1].Direction, Is.EqualTo(-1)); + Assert.That(actual[1].ColorId, Is.EqualTo(-1)); + }); } [Test] public void GetTrainListTest1() { - IReadOnlyList actual = loader!.GetTrainDataList(1); - - Assert.That(actual, Is.EquivalentTo(new TrainData[] + IReadOnlyList workGroupList = loader!.GetWorkGroupList(); + string workGroupId = workGroupList[0].Id; + IReadOnlyList workList = loader!.GetWorkList(workGroupId); + string workId = workList[1].Id; + IReadOnlyList actual = loader!.GetTrainDataList(workId); + + Assert.That(actual, Has.Count.EqualTo(2)); + Assert.Multiple(() => { - new TrainData() - { - Direction = 1, - Id = 2, - TrainNumber = "WG01-W02-Train01", - WorkId = 1, - - ColorId = -1, // Not Implemented - }, - - new TrainData() - { - Direction = -1, - Id = 3, - TrainNumber = "WG01-W02-Train02", - WorkId = 1, - - ColorId = -1, // Not Implemented - }, - })); + Assert.That(Guid.TryParse(actual[0].Id, out _), Is.True); + Assert.That(actual[0].WorkId, Is.EqualTo(workId)); + Assert.That(actual[0].TrainNumber, Is.EqualTo("WG01-W02-Train01")); + Assert.That(actual[0].Direction, Is.EqualTo(1)); + Assert.That(actual[0].ColorId, Is.EqualTo(-1)); + + Assert.That(Guid.TryParse(actual[1].Id, out _), Is.True); + Assert.That(actual[1].WorkId, Is.EqualTo(workId)); + Assert.That(actual[1].TrainNumber, Is.EqualTo("WG01-W02-Train02")); + Assert.That(actual[1].Direction, Is.EqualTo(-1)); + Assert.That(actual[1].ColorId, Is.EqualTo(-1)); + }); } } diff --git a/TRViS.IO.Tests/Loaders/LoaderSQL.Tests.cs b/TRViS.IO.Tests/Loaders/LoaderSQL.Tests.cs index 94e229b7..402b42ca 100644 --- a/TRViS.IO.Tests/Loaders/LoaderSQL.Tests.cs +++ b/TRViS.IO.Tests/Loaders/LoaderSQL.Tests.cs @@ -71,9 +71,9 @@ public void GetTrainData() using LoaderSQL loader = new(DB_FILE_PATH); TimetableRow[] emptyArr = Array.Empty(); - var all = loader.GetTrainData(1); + var all = loader.GetTrainData("1"); - TrainData? actual = loader.GetTrainData(1); + TrainData? actual = loader.GetTrainData("1"); Assert.That(actual, Is.Not.Null); Assert.That(actual.Rows, Is.Not.Null); @@ -81,6 +81,7 @@ public void GetTrainData() { Assert.That(actual, Is.EqualTo( new TrainData( + "1", "Work01", new(2022, 9, 15), "T9910X", @@ -108,8 +109,8 @@ public void GetTrainData() Assert.That(actual.Rows, Is.EquivalentTo(new TimetableRow[] { - new(new(1, null, null, null), 12, 34, "Station1", false, false, false, false, null, new(12, 34, 56, null), "1-1", null, null, "abc", false, null, null), - new(new(2, 135.5, 35.5, 200), 12, null, "Station2", false, false, false, true, new(null, null, null, "停車"), null, null, null, null, null, false, null, null) + new("1", new(1, null, null, null), 12, 34, "Station1", false, false, false, false, null, new(12, 34, 56, null), "1-1", null, null, "abc", false, null, null), + new("2", new(2, 135.5, 35.5, 200), 12, null, "Station2", false, false, false, true, new(null, null, null, "停車"), null, null, null, null, null, false, null, null) })); }); } @@ -123,7 +124,7 @@ public void GetWorkGroupListTest() Assert.That(actual, Has.Member(new Models.DB.WorkGroup() { - Id = 1, + Id = "1", Name = "Group01", DBVersion = 1, })); @@ -134,14 +135,14 @@ public void GetWorkListTest() { using LoaderSQL loader = new(DB_FILE_PATH); - var actual = loader.GetWorkList(1); + var actual = loader.GetWorkList("1"); for (int i = 1; i <= 3; i++) { Assert.That(actual, Has.Member(new Models.DB.Work() { - Id = i, - WorkGroupId = 1, + Id = i.ToString(), + WorkGroupId = "1", Name = $"Work0{i}", AffectDate = "2022-09-15", @@ -160,12 +161,12 @@ public void GetTrainDataListTest() { using LoaderSQL loader = new(DB_FILE_PATH); - var actual = loader.GetTrainDataList(1); + var actual = loader.GetTrainDataList("1"); Assert.That(actual, Has.Member(new Models.DB.TrainData() { - Id = 1, - WorkId = 1, + Id = "1", + WorkId = "1", TrainNumber = "T9910X", MaxSpeed = "95", SpeedType = "高速特定", diff --git a/TRViS.IO.Tests/Loaders/LoaderSQL.v0.Tests.cs b/TRViS.IO.Tests/Loaders/LoaderSQL.v0.Tests.cs index 5f2c1e21..ccc66094 100644 --- a/TRViS.IO.Tests/Loaders/LoaderSQL.v0.Tests.cs +++ b/TRViS.IO.Tests/Loaders/LoaderSQL.v0.Tests.cs @@ -71,13 +71,14 @@ public void GetTrainData() using LoaderSQL loader = new(DB_FILE_PATH); TimetableRow[] emptyArr = Array.Empty(); - var all = loader.GetTrainData(1); + var all = loader.GetTrainData("1"); - TrainData? actual = loader.GetTrainData(1); + TrainData? actual = loader.GetTrainData("1"); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo( new TrainData( + "1", "Work01", new(2022, 9, 15), "T9910X", @@ -98,8 +99,8 @@ public void GetTrainData() Assert.That(actual.Rows, Is.EquivalentTo(new TimetableRow[] { - new(new(1), 12, 34, "Station1", false, false, false, false, null, new(12, 34, 56, null), "1-1", null, null, "abc"), - new(new(2), 12, null, "Station2", false, false, false, true, new(null, null, null, "停車"), null, null, null, null, null) + new("1", new(1), 12, 34, "Station1", false, false, false, false, null, new(12, 34, 56, null), "1-1", null, null, "abc"), + new("2", new(2), 12, null, "Station2", false, false, false, true, new(null, null, null, "停車"), null, null, null, null, null) })); } @@ -112,7 +113,7 @@ public void GetWorkGroupListTest() Assert.That(actual, Has.Member(new Models.DB.WorkGroup() { - Id = 1, + Id = "1", Name = "Group01" })); } @@ -122,14 +123,14 @@ public void GetWorkListTest() { using LoaderSQL loader = new(DB_FILE_PATH); - var actual = loader.GetWorkList(1); + var actual = loader.GetWorkList("1"); for (int i = 1; i <= 3; i++) { Assert.That(actual, Has.Member(new Models.DB.Work() { - Id = i, - WorkGroupId = 1, + Id = i.ToString(), + WorkGroupId = "1", Name = $"Work0{i}", AffectDate = "2022-09-15" })); @@ -141,12 +142,12 @@ public void GetTrainDataListTest() { using LoaderSQL loader = new(DB_FILE_PATH); - var actual = loader.GetTrainDataList(1); + var actual = loader.GetTrainDataList("1"); Assert.That(actual, Has.Member(new Models.DB.TrainData() { - Id = 1, - WorkId = 1, + Id = "1", + WorkId = "1", TrainNumber = "T9910X", MaxSpeed = "95", SpeedType = "高速特定", From 3ad55701c9f4f613d7f2e8da65b27e9420fb3510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=B4=EF=BC=B2?= <31824852+TetsuOtter@users.noreply.github.com> Date: Mon, 19 Aug 2024 23:09:22 +0900 Subject: [PATCH 2/3] =?UTF-8?q?ID=E3=81=8C=E5=AD=98=E5=9C=A8=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84JSON=E3=82=92=E8=AA=AD=E3=81=BF=E8=BE=BC?= =?UTF-8?q?=E3=82=93=E3=81=A0=E9=9A=9B=E3=81=AB=E3=82=AF=E3=83=A9=E3=83=83?= =?UTF-8?q?=E3=82=B7=E3=83=A5=E3=81=99=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TRViS.IO/Loaders/LoaderJson.cs | 46 ++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/TRViS.IO/Loaders/LoaderJson.cs b/TRViS.IO/Loaders/LoaderJson.cs index f9f19ffa..a03527a8 100644 --- a/TRViS.IO/Loaders/LoaderJson.cs +++ b/TRViS.IO/Loaders/LoaderJson.cs @@ -44,6 +44,16 @@ static string GenerateUniqueId(IReadOnlyDictionary dict) } throw new InvalidOperationException("Failed to generate a unique ID"); } + static string GenerateUniqueId(IReadOnlyList idList) + { + for (int i = 0; i < 100; i++) + { + string tmpId = Guid.NewGuid().ToString(); + if (!idList.Contains(tmpId)) + return tmpId; + } + throw new InvalidOperationException("Failed to generate a unique ID"); + } static readonly JsonSerializerOptions opts = new() { @@ -55,11 +65,37 @@ private LoaderJson(WorkGroupData[] workGroups) { if (workGroups is null) throw new ArgumentNullException(nameof(workGroups)); + + string[] workGroupIdArray = new string[workGroups.Length]; + List workIdList = []; + List trainIdList = []; + for (int i = 0; i < workGroups.Length; i++) + { + string? id = workGroups[i].Id; + workGroupIdArray[i] = string.IsNullOrEmpty(id) ? GenerateUniqueId(workGroupIdArray) : id; + + workIdList.EnsureCapacity(workIdList.Count + workGroups[i].Works.Length); + for (int j = 0; j < workGroups[i].Works.Length; j++) + { + string? workId = workGroups[i].Works[j].Id; + workIdList.Add(string.IsNullOrEmpty(workId) ? GenerateUniqueId(workIdList) : workId); + + for (int k = 0; k < workGroups[i].Works[j].Trains.Length; k++) + { + string? trainId = workGroups[i].Works[j].Trains[k].Id; + trainIdList.Add(string.IsNullOrEmpty(trainId) ? GenerateUniqueId(trainIdList) : trainId); + + // TimetableRowIdは内部で使用しないため、ここでの生成は不要 + } + } + } + int workIdIndex = 0; + int trainIdIndex = 0; for (int workGroupIndex = 0; workGroupIndex < workGroups.Length; workGroupIndex++) { WorkGroupData workGroup = workGroups[workGroupIndex]; - string workGroupId = workGroup.Id ?? GenerateUniqueId(WorkGroups); + string workGroupId = workGroupIdArray[workGroupIndex]; WorkGroups[workGroupId] = new() { Id = workGroupId, @@ -72,7 +108,7 @@ private LoaderJson(WorkGroupData[] workGroups) for (int workIndex = 0; workIndex < workList.Length; workIndex++) { WorkData workData = workList[workIndex]; - string workId = workData.Id ?? GenerateUniqueId(WorkData); + string workId = workIdList[workIdIndex++]; WorkData[workId] = new() { WorkGroupId = workGroupId, @@ -94,7 +130,7 @@ private LoaderJson(WorkGroupData[] workGroups) for (int trainIndex = 0; trainIndex < trainList.Length; trainIndex++) { JsonModels.TrainData trainData = trainList[trainIndex]; - string trainId = trainData.Id ?? GenerateUniqueId(TrainData); + string trainId = trainIdList[trainIdIndex++]; TrainData[trainId] = ( new() { @@ -108,7 +144,7 @@ private LoaderJson(WorkGroupData[] workGroups) ColorId = -1, // Not Implemented DayCount = trainData.DayCount, Destination = trainData.Destination, - Id = trainData.Id!, + Id = trainId, Direction = trainData.Direction, IsRideOnMoving = trainData.IsRideOnMoving, MaxSpeed = trainData.MaxSpeed, @@ -120,7 +156,7 @@ private LoaderJson(WorkGroupData[] workGroups) WorkId = workId, WorkType = trainData.WorkType, // TODO: JSONでのNextTrainIdのサポート - NextTrainId = trainIndex != trainList.Length - 1 ? trainList[trainIndex + 1].Id : null + NextTrainId = trainIndex != trainList.Length - 1 ? trainIdList[trainIdIndex] : null }, trainData.TimetableRows.Select((v, i) => new TimetableRow( Id: v.Id ?? i.ToString(), From 67b6228c5695cf5e1a0307f8219d82f75d3cf9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=B4=EF=BC=B2?= <31824852+TetsuOtter@users.noreply.github.com> Date: Mon, 19 Aug 2024 23:12:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[#136]=20=E7=9D=80=E7=99=BA=E7=95=AA?= =?UTF-8?q?=E7=B7=9A=E3=81=AE=E6=96=87=E5=AD=97=E3=81=8C=E5=B0=8F=E3=81=95?= =?UTF-8?q?=E3=81=8F=E3=81=AA=E3=82=8A=E3=81=99=E3=81=8E=E3=82=8B=E5=A0=B4?= =?UTF-8?q?=E5=90=88=E3=81=8C=E3=81=82=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ` ` 等のエスケープの考慮漏れ - 念のため `\r` も取り除くよう変更 --- TRViS/DTAC/DTACElementStyles.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TRViS/DTAC/DTACElementStyles.cs b/TRViS/DTAC/DTACElementStyles.cs index c17a96f8..0fd13237 100644 --- a/TRViS/DTAC/DTACElementStyles.cs +++ b/TRViS/DTAC/DTACElementStyles.cs @@ -375,6 +375,8 @@ public static Style TimetableDefaultNumberLabelStyleResource private static partial Regex HtmlTagRegex(); [GeneratedRegex("]*/?>")] private static partial Regex HtmlBrTagRegex(); + [GeneratedRegex("&[^;]+;")] + private static partial Regex XmlEscapedStrRegex(); public static double GetTimetableTrackLabelFontSize(string trackName, double currentFontSize) { bool isTrackNameHtml = trackName.StartsWith('<'); @@ -382,8 +384,9 @@ public static double GetTimetableTrackLabelFontSize(string trackName, double cur { trackName = HtmlBrTagRegex().Replace(trackName, "\n"); trackName = HtmlTagRegex().Replace(trackName, ""); + trackName = XmlEscapedStrRegex().Replace(trackName, ""); } - int maxLineLength = trackName.Split("\n", StringSplitOptions.RemoveEmptyEntries).Select(v => v.Length).Max(); + int maxLineLength = trackName.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries).Select(v => v.Length).Max(); if (maxLineLength <= 2) return currentFontSize; else