• R/O
  • HTTP
  • SSH
  • HTTPS

open-tween: Commit

開発に使用するリポジトリ


Commit MetaInfo

Revision06b3944b574fdd23a04d956f3fff1700bd586844 (tree)
Time2019-11-14 04:32:25
AuthorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

RelatedPostsTabModelの読み込み中にタブを閉じるとKeyNotFoundExceptionが発生する不具合を修正

TweenMain.OpenRelatedTab 内の2回目の TabCollection.IndexOf でエラーになる

Change Summary

Incremental Difference

--- a/OpenTween.Tests/ExtensionsTest.cs
+++ b/OpenTween.Tests/ExtensionsTest.cs
@@ -60,6 +60,32 @@ namespace OpenTween
6060 }
6161
6262 [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]
6389 [InlineData("abc", new int[] { 'a', 'b', 'c' })]
6490 [InlineData("🍣", new int[] { 0x1f363 })] // サロゲートペア
6591 public void ToCodepoints_Test(string s, int[] expected)
--- /dev/null
+++ b/OpenTween.Tests/Models/TabCollectionTest.cs
@@ -0,0 +1,87 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2019 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 System;
23+using System.Collections.Generic;
24+using System.Linq;
25+using System.Text;
26+using System.Threading.Tasks;
27+using Xunit;
28+
29+namespace OpenTween.Models
30+{
31+ public class TabCollectionTest
32+ {
33+ [Fact]
34+ public void Getter_Test()
35+ {
36+ var tabs = new TabCollection();
37+ var tab = new PublicSearchTabModel("hoge");
38+ tabs.Add(tab);
39+
40+ Assert.Equal(tab, tabs["hoge"]);
41+ }
42+
43+ [Fact]
44+ public void IndexOf_TabName_Test()
45+ {
46+ var tabs = new TabCollection();
47+ var tab1 = new PublicSearchTabModel("aaaaa");
48+ var tab2 = new PublicSearchTabModel("bbbbb");
49+
50+ tabs.Add(tab1);
51+ tabs.Add(tab2);
52+
53+ Assert.Equal(1, tabs.IndexOf("bbbbb"));
54+ }
55+
56+ [Fact]
57+ public void IndexOf_TabName_NotExistsTest()
58+ {
59+ var tabs = new TabCollection();
60+
61+ Assert.Equal(-1, tabs.IndexOf("hoge"));
62+ }
63+
64+ [Fact]
65+ public void TryGetValue_TabName_Test()
66+ {
67+ var tabs = new TabCollection();
68+ var tab1 = new PublicSearchTabModel("aaaaa");
69+ var tab2 = new PublicSearchTabModel("bbbbb");
70+
71+ tabs.Add(tab1);
72+ tabs.Add(tab2);
73+
74+ Assert.True(tabs.TryGetValue("bbbbb", out var actualTab));
75+ Assert.Equal(tab2, actualTab);
76+ }
77+
78+ [Fact]
79+ public void TryGetValue_TabName_NotExistsTest()
80+ {
81+ var tabs = new TabCollection();
82+
83+ Assert.False(tabs.TryGetValue("hoge", out var actualTab));
84+ Assert.Null(actualTab);
85+ }
86+ }
87+}
--- a/OpenTween/Extensions.cs
+++ b/OpenTween/Extensions.cs
@@ -70,6 +70,27 @@ namespace OpenTween
7070 return false;
7171 }
7272
73+ public static int FindIndex<T>(this IEnumerable<T> enumerable, Predicate<T> finder)
74+ {
75+ if (enumerable is List<T> list)
76+ return list.FindIndex(finder);
77+
78+ if (enumerable is T[] array)
79+ return Array.FindIndex(array, finder);
80+
81+ var index = 0;
82+
83+ foreach (var item in enumerable)
84+ {
85+ if (finder(item))
86+ return index;
87+
88+ index++;
89+ }
90+
91+ return -1;
92+ }
93+
7394 public static IEnumerable<(T Value, int Index)> WithIndex<T>(this IEnumerable<T> enumerable)
7495 {
7596 var i = 0;
--- a/OpenTween/Models/TabCollection.cs
+++ b/OpenTween/Models/TabCollection.cs
@@ -23,16 +23,24 @@
2323
2424 using System.Collections.ObjectModel;
2525 using System.Diagnostics.CodeAnalysis;
26+using System.Linq;
2627
2728 namespace OpenTween.Models
2829 {
2930 public class TabCollection : KeyedCollection<string, TabModel>, IReadOnlyTabCollection
3031 {
3132 public int IndexOf(string tabName)
32- => this.IndexOf(this[tabName]);
33+ => this.Items.FindIndex(x => x.TabName == tabName);
3334
3435 public bool TryGetValue(string tabName, [NotNullWhen(true)] out TabModel? tab)
35- => this.Dictionary.TryGetValue(tabName, out tab);
36+ {
37+ if (this.Dictionary is { } dict)
38+ return dict.TryGetValue(tabName, out tab);
39+
40+ tab = this.Items.FirstOrDefault(x => x.TabName == tabName);
41+
42+ return tab != null;
43+ }
3644
3745 protected override string GetKeyForItem(TabModel tab)
3846 => tab.TabName;
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -1,6 +1,7 @@
11 更新履歴
22
33 ==== Ver 2.4.3-dev(2019/xx/xx)
4+ * FIX: 関連発言表示のタブを読み込み中に閉じるとエラーが発生する不具合を修正
45
56 ==== Ver 2.4.2(2019/10/23)
67 * FIX: タブ更新時にエラーが発生するとプロセスが異常終了する場合がある不具合を修正
Show on old repository browser