OSDN Git Service

pbs.twimg.com の画像URLのフォーマット変更に対応
[opentween/open-tween.git] / OpenTween.Tests / ExtensionsTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2017 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using Moq;
23 using System;
24 using System.Collections.Generic;
25 using System.Globalization;
26 using System.Linq;
27 using System.Text;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using Xunit;
31
32 namespace OpenTween
33 {
34     public class ExtensionsTest
35     {
36         [Theory]
37         [InlineData("ja", "ja-JP", true)]
38         [InlineData("ja", "ja", true)]
39         [InlineData("ja-JP", "ja-JP", true)]
40         [InlineData("ja-JP", "ja", false)]
41         // 4 階層以上の親を持つカルチャ
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)
48         {
49             var thisCulture = new CultureInfo(thisCultureStr);
50             var thatCulture = new CultureInfo(thatCultureStr);
51             Assert.Equal(expected, thisCulture.Contains(thatCulture));
52         }
53
54         [Fact]
55         public void Contains_InvariantCultureTest()
56         {
57             // InvariantCulture は全てのカルチャを内包する
58             Assert.True(CultureInfo.InvariantCulture.Contains(new CultureInfo("ja")));
59             Assert.True(CultureInfo.InvariantCulture.Contains(CultureInfo.InvariantCulture));
60         }
61
62         [Theory]
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)
68         {
69             // このテストでは items が List<T> または T[] のインスタンスと認識されないようにする
70             var items = new LinkedList<string>(array).AsEnumerable();
71             Assert.Equal(expected, items.FindIndex(x => x == item));
72         }
73
74         [Fact]
75         public void FindIndex_ListTest()
76         {
77             var items = new List<string> { "aaa", "bbb" }.AsEnumerable();
78             Assert.Equal(1, items.FindIndex(x => x == "bbb"));
79         }
80
81         [Fact]
82         public void FindIndex_ArrayTest()
83         {
84             var items = new[] { "aaa", "bbb" }.AsEnumerable();
85             Assert.Equal(1, items.FindIndex(x => x == "bbb"));
86         }
87
88         [Theory]
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());
93
94         [Theory]
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)
99         {
100             // InlineDataAttribute で壊れたサロゲートペアの string を扱えないため char[] を使う
101             Assert.Equal(expected, new string(s).ToCodepoints());
102         }
103
104         [Fact]
105         public void ToCodepoints_ErrorTest()
106             => Assert.Throws<ArgumentNullException>(() => ((string)null!).ToCodepoints());
107
108         [Theory]
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));
117
118         [Fact]
119         public void GetCodepointCount_ErrorTest()
120         {
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));
126         }
127
128         [Fact]
129         public async Task ForEachAsync_Test()
130         {
131             var mock = new Mock<IObservable<int>>();
132             mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
133                 .Callback<IObserver<int>>(x =>
134                 {
135                     x.OnNext(1);
136                     x.OnNext(2);
137                     x.OnNext(3);
138                     x.OnCompleted();
139                 })
140                 .Returns(Mock.Of<IDisposable>());
141
142             var results = new List<int>();
143
144             await mock.Object.ForEachAsync(x => results.Add(x));
145
146             Assert.Equal(new[] { 1, 2, 3 }, results);
147         }
148
149         [Fact]
150         public async Task ForEachAsync_EmptyTest()
151         {
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>());
156
157             var results = new List<int>();
158
159             await mock.Object.ForEachAsync(x => results.Add(x));
160
161             Assert.Empty(results);
162         }
163
164         [Fact]
165         public async Task ForEachAsync_CancelledTest()
166         {
167             var mockUnsubscriber = new Mock<IDisposable>();
168
169             var mockObservable = new Mock<IObservable<int>>();
170             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
171                 .Callback<IObserver<int>>(x =>
172                 {
173                     x.OnNext(1);
174                     x.OnNext(2);
175                     x.OnNext(3);
176                     x.OnCompleted();
177                 })
178                 .Returns(mockUnsubscriber.Object);
179
180             var cts = new CancellationTokenSource();
181
182             await mockObservable.Object.ForEachAsync(x => cts.Cancel(), cts.Token);
183
184             mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
185         }
186
187         [Fact]
188         public async Task ForEachAsync_ErrorOccursedAtObservableTest()
189         {
190             var mockObservable = new Mock<IObservable<int>>();
191             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
192                 .Callback<IObserver<int>>(x =>
193                 {
194                     x.OnNext(1);
195                     x.OnError(new Exception());
196                 })
197                 .Returns(Mock.Of<IDisposable>());
198
199             var results = new List<int>();
200
201             await Assert.ThrowsAsync<Exception>(async () =>
202             {
203                 await mockObservable.Object.ForEachAsync(x => results.Add(x));
204             });
205             Assert.Equal(new[] { 1 }, results);
206         }
207
208         [Fact]
209         public async Task ForEachAsync_ErrorOccursedAtSubscriberTest()
210         {
211             var mockUnsubscriber = new Mock<IDisposable>();
212
213             var mockObservable = new Mock<IObservable<int>>();
214             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
215                 .Callback<IObserver<int>>(x =>
216                 {
217                     x.OnNext(1);
218                     x.OnCompleted();
219                 })
220                 .Returns(mockUnsubscriber.Object);
221
222             await Assert.ThrowsAsync<Exception>(async () =>
223             {
224                 await mockObservable.Object.ForEachAsync(x => throw new Exception());
225             });
226
227             mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
228         }
229     }
230 }