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("abc", new int[] { 'a', 'b', 'c' })]
64 [InlineData("🍣", new int[] { 0x1f363 })] // サロゲートペア
65 public void ToCodepoints_Test(string s, int[] expected)
66 => Assert.Equal(expected, s.ToCodepoints());
69 // char.ConvertToUtf32 をそのまま使用するとエラーになるパターン
70 [InlineData(new[] { '\ud83c' }, new[] { 0xd83c })] // 壊れたサロゲートペア (LowSurrogate が無い)
71 [InlineData(new[] { '\udf63' }, new[] { 0xdf63 })] // 壊れたサロゲートペア (HighSurrogate が無い)
72 public void ToCodepoints_BrokenSurrogateTest(char[] s, int[] expected)
74 // InlineDataAttribute で壊れたサロゲートペアの string を扱えないため char[] を使う
75 Assert.Equal(expected, new string(s).ToCodepoints());
79 public void ToCodepoints_ErrorTest()
80 => Assert.Throws<ArgumentNullException>(() => ((string)null).ToCodepoints());
83 [InlineData("", 0, 0, 0)]
84 [InlineData("sushi 🍣", 0, 8, 7)]
85 [InlineData("sushi 🍣", 0, 5, 5)]
86 [InlineData("sushi 🍣", 6, 8, 1)]
87 [InlineData("sushi 🍣", 6, 7, 1)] // サロゲートペアの境界を跨ぐ範囲 (LowSurrogate が無い)
88 [InlineData("sushi 🍣", 7, 8, 1)] // サロゲートペアの境界を跨ぐ範囲 (HighSurrogate が無い)
89 public void GetCodepointCount_Test(string str, int start, int end, int expected)
90 => Assert.Equal(expected, str.GetCodepointCount(start, end));
93 public void GetCodepointCount_ErrorTest()
95 Assert.Throws<ArgumentNullException>(() => ((string)null).GetCodepointCount(0, 0));
96 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(-1, 3));
97 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(0, 4));
98 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(4, 5));
99 Assert.Throws<ArgumentOutOfRangeException>(() => "abc".GetCodepointCount(2, 1));
103 public async Task ForEachAsync_Test()
105 var mock = new Mock<IObservable<int>>();
106 mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
107 .Callback<IObserver<int>>(x =>
114 .Returns(Mock.Of<IDisposable>());
116 var results = new List<int>();
118 await mock.Object.ForEachAsync(x => results.Add(x));
120 Assert.Equal(new[] { 1, 2, 3 }, results);
124 public async Task ForEachAsync_EmptyTest()
126 var mock = new Mock<IObservable<int>>();
127 mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
128 .Callback<IObserver<int>>(x => x.OnCompleted())
129 .Returns(Mock.Of<IDisposable>());
131 var results = new List<int>();
133 await mock.Object.ForEachAsync(x => results.Add(x));
135 Assert.Empty(results);
139 public async Task ForEachAsync_CancelledTest()
141 var mockUnsubscriber = new Mock<IDisposable>();
143 var mockObservable = new Mock<IObservable<int>>();
144 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
145 .Callback<IObserver<int>>(x =>
152 .Returns(mockUnsubscriber.Object);
154 var cts = new CancellationTokenSource();
156 await mockObservable.Object.ForEachAsync(x => cts.Cancel(), cts.Token);
158 mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
162 public async Task ForEachAsync_ErrorOccursedAtObservableTest()
164 var mockObservable = new Mock<IObservable<int>>();
165 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
166 .Callback<IObserver<int>>(x =>
169 x.OnError(new Exception());
171 .Returns(Mock.Of<IDisposable>());
173 var results = new List<int>();
175 await Assert.ThrowsAsync<Exception>(async () =>
177 await mockObservable.Object.ForEachAsync(x => results.Add(x));
179 Assert.Equal(new[] { 1 }, results);
183 public async Task ForEachAsync_ErrorOccursedAtSubscriberTest()
185 var mockUnsubscriber = new Mock<IDisposable>();
187 var mockObservable = new Mock<IObservable<int>>();
188 mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
189 .Callback<IObserver<int>>(x =>
194 .Returns(mockUnsubscriber.Object);
196 await Assert.ThrowsAsync<Exception>(async () =>
198 await mockObservable.Object.ForEachAsync(x => throw new Exception());
201 mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());