|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | +using OpenUtau.Core; |
| 7 | +using OpenUtau.Core.Ustx; |
| 8 | +using Xunit; |
| 9 | +using static OpenUtau.Classic.PluginRunner; |
| 10 | + |
| 11 | +namespace OpenUtau.Classic { |
| 12 | + |
| 13 | + |
| 14 | + public class PluginRunnerTest { |
| 15 | + |
| 16 | + class ExecuteTestData : IEnumerable<object[]> { |
| 17 | + private readonly List<object[]> testData = new(); |
| 18 | + |
| 19 | + public ExecuteTestData() { |
| 20 | + testData.Add(new object[] { BasicUProject(), IncludeNullResponse(), IncludeNullAssertion(), EmptyErrorMEthod() }); |
| 21 | + } |
| 22 | + |
| 23 | + public IEnumerator<object[]> GetEnumerator() => testData.GetEnumerator(); |
| 24 | + |
| 25 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 26 | + |
| 27 | + public static ExecuteArgument BasicUProject() { |
| 28 | + var project = new UProject(); |
| 29 | + project.tracks.Add(new UTrack { |
| 30 | + TrackNo = 0, |
| 31 | + }); |
| 32 | + var part = new UVoicePart() { |
| 33 | + trackNo = 0, |
| 34 | + position = 0, |
| 35 | + }; |
| 36 | + project.parts.Add(part); |
| 37 | + |
| 38 | + var before = UNote.Create(); |
| 39 | + before.lyric = "a"; |
| 40 | + before.duration = 10; |
| 41 | + |
| 42 | + var first = UNote.Create(); |
| 43 | + first.lyric = "ka"; |
| 44 | + first.duration = 20; |
| 45 | + |
| 46 | + var second = UNote.Create(); |
| 47 | + second.lyric = "r"; |
| 48 | + second.duration = 30; |
| 49 | + |
| 50 | + var third = UNote.Create(); |
| 51 | + third.lyric = "ta"; |
| 52 | + third.duration = 40; |
| 53 | + |
| 54 | + var last = UNote.Create(); |
| 55 | + last.lyric = "na"; |
| 56 | + last.duration = 50; |
| 57 | + |
| 58 | + var after = UNote.Create(); |
| 59 | + after.lyric = "ha"; |
| 60 | + after.duration = 60; |
| 61 | + |
| 62 | + part.notes.Add(before); |
| 63 | + part.notes.Add(first); |
| 64 | + part.notes.Add(second); |
| 65 | + part.notes.Add(third); |
| 66 | + part.notes.Add(last); |
| 67 | + part.notes.Add(after); |
| 68 | + |
| 69 | + return new ExecuteArgument(project, part, first, last); |
| 70 | + } |
| 71 | + |
| 72 | + private static Action<StreamWriter> IncludeNullResponse() { |
| 73 | + return (writer) => { |
| 74 | + // duration and lyric |
| 75 | + writer.WriteLine("[#0000]"); |
| 76 | + writer.WriteLine("Length=480"); |
| 77 | + writer.WriteLine("Lyric=A"); |
| 78 | + writer.WriteLine("[#0001]"); |
| 79 | + writer.WriteLine("Length=480"); |
| 80 | + writer.WriteLine("Lyric=R"); |
| 81 | + // duration is null (change) |
| 82 | + writer.WriteLine("[#0002]"); |
| 83 | + writer.WriteLine("Lyric=zo"); |
| 84 | + // duration is zero (delete) |
| 85 | + writer.WriteLine("[#0003]"); |
| 86 | + writer.WriteLine("Length="); |
| 87 | + // insert |
| 88 | + writer.WriteLine("[#INSERT]"); |
| 89 | + writer.WriteLine("Length=240"); |
| 90 | + writer.WriteLine("Lyric=me"); |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + private static Action<ReplaceNoteEventArgs> IncludeNullAssertion() { |
| 95 | + return (args) => { |
| 96 | + Assert.Equal(4, args.ToRemove.Count); |
| 97 | + Assert.Equal(3, args.ToAdd.Count); |
| 98 | + Assert.Equal(480, args.ToAdd[0].duration); |
| 99 | + Assert.Equal("A", args.ToAdd[0].lyric); |
| 100 | + Assert.Equal(40, args.ToAdd[1].duration); |
| 101 | + Assert.Equal("zo", args.ToAdd[1].lyric); |
| 102 | + Assert.Equal(240, args.ToAdd[2].duration); |
| 103 | + Assert.Equal("me", args.ToAdd[2].lyric); |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + private static Action<PluginErrorEventArgs> EmptyErrorMEthod() { |
| 108 | + return (args) => { |
| 109 | + // do nothing |
| 110 | + }; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [ClassData(typeof(ExecuteTestData))] |
| 116 | + public void ExecuteTest(ExecuteArgument given, Action<StreamWriter> when, Action<ReplaceNoteEventArgs> then, Action<PluginErrorEventArgs> error) { |
| 117 | + // When |
| 118 | + var action = new Action<PluginRunner>((runner) => { |
| 119 | + runner.Execute(given.Project, given.Part, given.First, given.Last, new PluginStub(when)); |
| 120 | + }); |
| 121 | + |
| 122 | + // Then (Assert in ClassData) |
| 123 | + action(new PluginRunner(PathManager.Inst, then, error)); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void ExecuteErrorTest() { |
| 128 | + // Given |
| 129 | + var given = ExecuteTestData.BasicUProject(); |
| 130 | + |
| 131 | + // When |
| 132 | + var action = new Action<PluginRunner>((runner) => { |
| 133 | + runner.Execute(given.Project, given.Part, given.First, given.Last, new PluginStub((writer) => { |
| 134 | + // return empty text (invoke error) |
| 135 | + })); |
| 136 | + }); |
| 137 | + |
| 138 | + // Then |
| 139 | + var then = new Action<ReplaceNoteEventArgs>(( args) => { |
| 140 | + Assert.Fail(""); |
| 141 | + }); |
| 142 | + var error = new Action<PluginErrorEventArgs> ((args) => { |
| 143 | + Assert.True(true); |
| 144 | + }); |
| 145 | + action(new PluginRunner(PathManager.Inst, then,error)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + class PluginStub : IPlugin { |
| 150 | + public PluginStub(Action<StreamWriter> action) { |
| 151 | + this.action = action; |
| 152 | + } |
| 153 | + private readonly Action<StreamWriter> action; |
| 154 | + |
| 155 | + public string Encoding => "shift_jis"; |
| 156 | + |
| 157 | + public void Run(string tempFile) { |
| 158 | + File.Delete(tempFile); |
| 159 | + System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
| 160 | + using (var writer = new StreamWriter(tempFile, false, System.Text.Encoding.GetEncoding(Encoding))) { |
| 161 | + action.Invoke(writer); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public class ExecuteArgument { |
| 167 | + public readonly UProject Project; |
| 168 | + public readonly UVoicePart Part; |
| 169 | + public readonly UNote First; |
| 170 | + public readonly UNote Last; |
| 171 | + |
| 172 | + public ExecuteArgument(UProject project, UVoicePart part, UNote first, UNote last) { |
| 173 | + Project = project; |
| 174 | + Part = part; |
| 175 | + First = first; |
| 176 | + Last = last; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments