OSDN Git Service

GetTextLengthRemainWeightedの内部で使用するインデックスをコードポイント単位となるように変更
[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("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());
67
68         [Theory]
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)
73         {
74             // InlineDataAttribute で壊れたサロゲートペアの string を扱えないため char[] を使う
75             Assert.Equal(expected, new string(s).ToCodepoints());
76         }
77
78         [Fact]
79         public void ToCodepoints_ErrorTest()
80             => Assert.Throws<ArgumentNullException>(() => ((string)null).ToCodepoints());
81
82         [Theory]
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));
91
92         [Fact]
93         public void GetCodepointCount_ErrorTest()
94         {
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));
100         }
101
102         [Fact]
103         public async Task ForEachAsync_Test()
104         {
105             var mock = new Mock<IObservable<int>>();
106             mock.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
107                 .Callback<IObserver<int>>(x =>
108                 {
109                     x.OnNext(1);
110                     x.OnNext(2);
111                     x.OnNext(3);
112                     x.OnCompleted();
113                 })
114                 .Returns(Mock.Of<IDisposable>());
115
116             var results = new List<int>();
117
118             await mock.Object.ForEachAsync(x => results.Add(x));
119
120             Assert.Equal(new[] { 1, 2, 3 }, results);
121         }
122
123         [Fact]
124         public async Task ForEachAsync_EmptyTest()
125         {
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>());
130
131             var results = new List<int>();
132
133             await mock.Object.ForEachAsync(x => results.Add(x));
134
135             Assert.Empty(results);
136         }
137
138         [Fact]
139         public async Task ForEachAsync_CancelledTest()
140         {
141             var mockUnsubscriber = new Mock<IDisposable>();
142
143             var mockObservable = new Mock<IObservable<int>>();
144             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
145                 .Callback<IObserver<int>>(x =>
146                 {
147                     x.OnNext(1);
148                     x.OnNext(2);
149                     x.OnNext(3);
150                     x.OnCompleted();
151                 })
152                 .Returns(mockUnsubscriber.Object);
153
154             var cts = new CancellationTokenSource();
155
156             await mockObservable.Object.ForEachAsync(x => cts.Cancel(), cts.Token);
157
158             mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
159         }
160
161         [Fact]
162         public async Task ForEachAsync_ErrorOccursedAtObservableTest()
163         {
164             var mockObservable = new Mock<IObservable<int>>();
165             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
166                 .Callback<IObserver<int>>(x =>
167                 {
168                     x.OnNext(1);
169                     x.OnError(new Exception());
170                 })
171                 .Returns(Mock.Of<IDisposable>());
172
173             var results = new List<int>();
174
175             await Assert.ThrowsAsync<Exception>(async () =>
176             {
177                 await mockObservable.Object.ForEachAsync(x => results.Add(x));
178             });
179             Assert.Equal(new[] { 1 }, results);
180         }
181
182         [Fact]
183         public async Task ForEachAsync_ErrorOccursedAtSubscriberTest()
184         {
185             var mockUnsubscriber = new Mock<IDisposable>();
186
187             var mockObservable = new Mock<IObservable<int>>();
188             mockObservable.Setup(x => x.Subscribe(It.IsNotNull<IObserver<int>>()))
189                 .Callback<IObserver<int>>(x =>
190                 {
191                     x.OnNext(1);
192                     x.OnCompleted();
193                 })
194                 .Returns(mockUnsubscriber.Object);
195
196             await Assert.ThrowsAsync<Exception>(async () =>
197             {
198                 await mockObservable.Object.ForEachAsync(x => throw new Exception());
199             });
200
201             mockUnsubscriber.Verify(x => x.Dispose(), Times.AtLeastOnce());
202         }
203     }
204 }