1 // OpenTween - Client of Twitter
2 // Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
24 using System.Collections.Generic;
25 using System.Globalization;
28 using System.Threading;
29 using System.Threading.Tasks;
34 public class ExtensionsTest
37 [InlineData("ja", "ja-JP", true)]
38 [InlineData("ja", "ja", true)]
39 [InlineData("ja-JP", "ja-JP", true)]
40 [InlineData("ja-JP", "ja", false)]
42 // 参照: https://msdn.microsoft.com/ja-jp/library/dd997383(v=vs.100).aspx#%E6%96%B0%E3%81%97%E3%81%84%E7%89%B9%E5%AE%9A%E3%82%AB%E3%83%AB%E3%83%81%E3%83%A3
43 [InlineData("zh-Hant", "zh-TW", true)]
44 [InlineData("zh-Hant", "zh-CHT", true)]
45 [InlineData("zh-Hant", "zh-Hant", true)]
46 [InlineData("zh-Hant", "zh", false)]
47 public void Contains_Test(string thisCultureStr, string thatCultureStr, bool expected)
49 var thisCulture = new CultureInfo(thisCultureStr);
50 var thatCulture = new CultureInfo(thatCultureStr);
51 Assert.Equal(expected, thisCulture.Contains(thatCulture));
55 public void Contains_InvariantCultureTest()
57 // InvariantCulture は全てのカルチャを内包する
58 Assert.True(CultureInfo.InvariantCulture.Contains(new CultureInfo("ja")));
59 Assert.True(CultureInfo.InvariantCulture.Contains(CultureInfo.InvariantCulture));
63 [InlineData("aaa", new string[0], -1)]
64 [InlineData("aaa", new[] { "aaa" }, 0)]
65 [InlineData("bbb", new[] { "aaa" }, -1)]
66 [InlineData("bbb", new[] { "aaa", "bbb" }, 1)]
67 public void FindIndex_Test(string item, string[] array, int expected)
69 // このテストでは items が List<T> または T[] のインスタンスと認識されないようにする
70 var items = new LinkedList<string>(array).AsEnumerable();
71 Assert.Equal(expected, items.FindIndex(x => x == item));
75 public void FindIndex_ListTest()
77 var items = new List<string> { "aaa", "bbb" }.AsEnumerable();
78 Assert.Equal(1, items.FindIndex(x => x == "bbb"));
82 public void FindIndex_ArrayTest()
84 var items = new[] { "aaa", "bbb" }.AsEnumerable();
85 Assert.Equal(1, items.FindIndex(x => x == "bbb"));
89 [InlineData("abc", new int[] { 'a', 'b', 'c' })]
90 [InlineData("🍣", new int[] { 0x1f363 })] // サロゲートペア
91 public void ToCodepoints_Test(string s, int[] expected)
92 => Assert.Equal(expected, s.ToCodepoints());
95 // char.ConvertToUtf32 をそのまま使用するとエラーになるパターン
96 [InlineData(new[] { '\ud83c' }, new[] { 0xd83c })] // 壊れたサロゲートペア (LowSurrogate が無い)
97 [InlineData(new[] { '\udf63' }, new[] { 0xdf63 })] // 壊れたサロゲートペア (HighSurrogate が無い)
98 public void ToCodepoints_BrokenSurrogateTest(char[] s, int[] expected)
100 // InlineDataAttribute で壊れたサロゲートペアの string を扱えないため char[] を使う
101 Assert.Equal(expected, new string(s).ToCodepoints());
105 public void ToCodepoints_ErrorTest()
106 => Assert.Throws<ArgumentNullException>(() => ((string)null!).ToCodepoints());
109 [InlineData("", 0, 0, 0)]
110 [InlineData("sushi 🍣", 0, 8, 7)]
111 [InlineData("sushi 🍣", 0, 5, 5)]
112 [InlineData("sushi 🍣", 6, 8, 1)]
113 [InlineData("sushi 🍣", 6, 7, 1)] // サロゲートペアの境界を跨ぐ範囲 (LowSurrogate が無い)
114 [InlineData("sushi 🍣", 7, 8, 1)] // サロゲートペアの境界を跨ぐ範囲 (HighSurrogate が無い)
115 public void GetCodepointCount_Test(string str, int start, int end, int expected)
116 => Assert.Equal(expected, str.GetCodepointCount(start, end));
119 public void GetCodepointCount_ErrorTest()
121 Assert.Throws<ArgumentNullException>(() => ((string)null!).GetCodepointCount(0, 0));
122 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(-1, 3));
123 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(0, 4));
124 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(4, 5));
125 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(2, 1));
129 public async Task ForEachAsync_Test()
131 var mock = new Mock<IObservable<int>>();
132 mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
133 .Callback<IObserver<int>>(x =>
140 .Returns(Mock.Of<IDisposable>());
142 var results = new List<int>();
144 await mock.Object.ForEachAsync(x => results.Add(x));
146 Assert.Equal(new[] { 1, 2, 3 }, results);
150 public async Task ForEachAsync_EmptyTest()
152 var mock = new Mock<IObservable<int>>();
153 mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
154 .Callback<IObserver<int>>(x => x.OnCompleted())
155 .Returns(Mock.Of<IDisposable>());
157 var results = new List<int>();
159 await mock.Object.ForEachAsync(x => results.Add(x));
161 Assert.Empty(results);
165 public async Task ForEachAsync_CancelledTest()
167 var mockUnsubscriber = new Mock<IDisposable>();
169 var mockObservable = new Mock<IObservable<int>>();
170 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
171 .Callback<IObserver<int>>(x =>
178 .Returns(mockUnsubscriber.Object);
180 var cts = new CancellationTokenSource();
182 await mockObservable.Object.ForEachAsync(x => cts.Cancel(), cts.Token);
184 mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
188 public async Task ForEachAsync_ErrorOccursedAtObservableTest()
190 var mockObservable = new Mock<IObservable<int>>();
191 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
192 .Callback<IObserver<int>>(x =>
195 x.OnError(new Exception());
197 .Returns(Mock.Of<IDisposable>());
199 var results = new List<int>();
201 await Assert.ThrowsAsync<Exception>(async () =>
203 await mockObservable.Object.ForEachAsync(x => results.Add(x));
205 Assert.Equal(new[] { 1 }, results);
209 public async Task ForEachAsync_ErrorOccursedAtSubscriberTest()
211 var mockUnsubscriber = new Mock<IDisposable>();
213 var mockObservable = new Mock<IObservable<int>>();
214 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
215 .Callback<IObserver<int>>(x =>
220 .Returns(mockUnsubscriber.Object);
222 await Assert.ThrowsAsync<Exception>(async () =>
224 await mockObservable.Object.ForEachAsync(x => throw new Exception());
227 mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());