OSDN Git Service

コード分析に使用する規則セットを定義
[opentween/open-tween.git] / OpenTween.Tests / PostClassTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 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.Reflection;
26 using System.Text;
27 using Xunit;
28 using Xunit.Extensions;
29
30 namespace OpenTween
31 {
32     public class PostClassTest
33     {
34         class TestPostClass : PostClass
35         {
36             public TestPostClass(
37                 string Nickname = null,
38                 string textFromApi = null,
39                 string text = null,
40                 string ImageUrl = null,
41                 string screenName = null,
42                 DateTime createdAt = new DateTime(),
43                 long statusId = 0L,
44                 bool IsFav = false,
45                 bool IsRead = false,
46                 bool IsReply = false,
47                 bool IsExcludeReply = false,
48                 bool IsProtect = false,
49                 bool IsOwl = false,
50                 bool IsMark = false,
51                 string InReplyToUser = null,
52                 long? InReplyToStatusId = null,
53                 string Source = null,
54                 string SourceHtml = null,
55                 List<string> ReplyToList = null,
56                 bool IsMe = false,
57                 bool IsDm = false,
58                 long userId = 0L,
59                 bool FilterHit = false,
60                 string RetweetedBy = null,
61                 long? RetweetedId = null,
62                 StatusGeo Geo = null) :
63                 base(Nickname, textFromApi, text, ImageUrl, screenName, createdAt, statusId, IsFav, IsRead,
64                 IsReply, IsExcludeReply, IsProtect, IsOwl, IsMark, InReplyToUser, InReplyToStatusId, Source,
65                 SourceHtml, ReplyToList, IsMe, IsDm, userId, FilterHit, RetweetedBy, RetweetedId, Geo)
66             {
67             }
68
69             protected override PostClass GetRetweetSource(long statusId)
70             {
71                 return PostClassTest.TestCases.ContainsKey(statusId) ?
72                     PostClassTest.TestCases[statusId] :
73                     null;
74             }
75         }
76
77         private static Dictionary<long, PostClass> TestCases;
78
79         public PostClassTest()
80         {
81             PostClassTest.TestCases = new Dictionary<long, PostClass>
82             {
83                 {1L, new TestPostClass(statusId: 1L)},
84                 {2L, new TestPostClass(statusId: 2L, IsFav: true)},
85                 {3L, new TestPostClass(statusId: 3L, IsFav: false, RetweetedId: 2L)},
86             };
87         }
88
89         [Fact]
90         public void CloneTest()
91         {
92             var post = new PostClass();
93             var clonePost = post.Clone();
94
95             TestUtils.CheckDeepCloning(post, clonePost);
96         }
97
98         [Theory]
99         [InlineData(null,  null)]
100         [InlineData("", "")]
101         [InlineData("aaa\nbbb", "aaa bbb")]
102         public void TextSingleLineTest(string text, string expected)
103         {
104             var post = new TestPostClass(textFromApi: text);
105
106             Assert.Equal(expected, post.TextSingleLine);
107         }
108
109         [Theory]
110         [InlineData(1L, false)]
111         [InlineData(2L, true)]
112         [InlineData(3L, true)]
113         public void GetIsFavTest(long statusId, bool expected)
114         {
115             Assert.Equal(expected, PostClassTest.TestCases[statusId].IsFav);
116         }
117
118         [Theory]
119         [InlineData(2L, true)]
120         [InlineData(2L, false)]
121         [InlineData(3L, true)]
122         [InlineData(3L, false)]
123         public void SetIsFavTest(long statusId, bool isFav)
124         {
125             var post = PostClassTest.TestCases[statusId];
126
127             post.IsFav = isFav;
128             Assert.Equal(isFav, post.IsFav);
129
130             if (post.RetweetedId != null)
131                 Assert.Equal(isFav, PostClassTest.TestCases[post.RetweetedId.Value].IsFav);
132         }
133
134         [Theory]
135         [InlineData(false, false, false, false, -0x01)]
136         [InlineData( true, false, false, false, 0x00)]
137         [InlineData(false,  true, false, false, 0x01)]
138         [InlineData( true,  true, false, false, 0x02)]
139         [InlineData(false, false,  true, false, 0x03)]
140         [InlineData( true, false,  true, false, 0x04)]
141         [InlineData(false,  true,  true, false, 0x05)]
142         [InlineData( true,  true,  true, false, 0x06)]
143         [InlineData(false, false, false,  true, 0x07)]
144         [InlineData( true, false, false,  true, 0x08)]
145         [InlineData(false,  true, false,  true, 0x09)]
146         [InlineData( true,  true, false,  true, 0x0A)]
147         [InlineData(false, false,  true,  true, 0x0B)]
148         [InlineData( true, false,  true,  true, 0x0C)]
149         [InlineData(false,  true,  true,  true, 0x0D)]
150         [InlineData( true,  true,  true,  true, 0x0E)]
151         public void StateIndexTest(bool protect, bool mark, bool reply, bool geo, int expected)
152         {
153             var post = new TestPostClass
154             {
155                 IsProtect = protect,
156                 IsMark = mark,
157                 InReplyToStatusId = reply ? (long?)100L : null,
158                 PostGeo = geo ? new PostClass.StatusGeo { Lat = -47.15, Lng = -126.716667 } : new PostClass.StatusGeo(),
159             };
160
161             Assert.Equal(expected, post.StateIndex);
162         }
163
164         [Fact]
165         public void DeleteTest()
166         {
167             var post = new TestPostClass
168             {
169                 InReplyToStatusId = 10L,
170                 InReplyToUser = "hogehoge",
171                 InReplyToUserId = 100L,
172                 IsReply = true,
173                 ReplyToList = new List<string> {"hogehoge"},
174             };
175
176             post.IsDeleted = true;
177
178             Assert.Null(post.InReplyToStatusId);
179             Assert.Equal("", post.InReplyToUser);
180             Assert.Null(post.InReplyToUserId);
181             Assert.False(post.IsReply);
182             Assert.Empty(post.ReplyToList);
183             Assert.Equal(-1, post.StateIndex);
184         }
185     }
186 }