OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween.Tests / Models / PostFilterRuleVersion113DeserializeTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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.IO;
25 using System.Linq;
26 using System.Text;
27 using System.Xml.Serialization;
28 using Xunit;
29 using Xunit.Extensions;
30
31 namespace OpenTween.Models
32 {
33     /// <summary>
34     /// FiltersClass -> PostFilterRule クラスへの変更で v1.1.2 までの
35     /// 設定ファイルと互換性が保たれているかどうかを確認するテスト
36     /// </summary>
37     public class PostFilterRuleVersion113DeserializeTest
38     {
39         // OpenTween v1.1.2 時点の FiltersClass クラス
40         public sealed class FiltersClass
41         {
42             public string? NameFilter { get; set; }
43             public string? ExNameFilter { get; set; }
44             public string[] BodyFilterArray { get; set; } = Array.Empty<string>();
45             public string[] ExBodyFilterArray { get; set; } = Array.Empty<string>();
46             public bool SearchBoth { get; set; }
47             public bool ExSearchBoth { get; set; }
48             public bool MoveFrom { get; set; }
49             public bool SetMark { get; set; }
50             public bool SearchUrl { get; set; }
51             public bool ExSearchUrl { get; set; }
52             public bool CaseSensitive { get; set; }
53             public bool ExCaseSensitive { get; set; }
54             public bool UseLambda { get; set; }
55             public bool ExUseLambda { get; set; }
56             public bool UseRegex { get; set; }
57             public bool ExUseRegex { get; set; }
58             public bool IsRt { get; set; }
59             public bool IsExRt { get; set; }
60             public string? Source { get; set; }
61             public string? ExSource { get; set; }
62         }
63
64         [Fact]
65         public void DeserializeTest()
66         {
67             var oldVersion = new FiltersClass
68             {
69                 NameFilter = "name",
70                 ExNameFilter = "exname",
71                 BodyFilterArray = new[] { "body1", "body2" },
72                 ExBodyFilterArray = new[] { "exbody1", "exbody2" },
73                 SearchBoth = true,
74                 ExSearchBoth = true,
75                 MoveFrom = true,
76                 SetMark = true,
77                 SearchUrl = true,
78                 ExSearchUrl = true,
79                 CaseSensitive = true,
80                 ExCaseSensitive = true,
81                 UseLambda = true,
82                 ExUseLambda = true,
83                 UseRegex = true,
84                 ExUseRegex = true,
85                 IsRt = true,
86                 IsExRt = true,
87                 Source = "source",
88                 ExSource = "exsource",
89             };
90
91             PostFilterRule newVersion;
92
93             using (var stream = new MemoryStream())
94             {
95                 var oldSerializer = new XmlSerializer(typeof(FiltersClass));
96                 oldSerializer.Serialize(stream, oldVersion);
97
98                 stream.Seek(0, SeekOrigin.Begin);
99
100                 var newSerializer = new XmlSerializer(typeof(PostFilterRule));
101                 newVersion = (PostFilterRule)newSerializer.Deserialize(stream);
102             }
103
104             Assert.Equal(oldVersion.NameFilter, newVersion.FilterName);
105             Assert.Equal(oldVersion.ExNameFilter, newVersion.ExFilterName);
106             Assert.Equal(oldVersion.BodyFilterArray, newVersion.FilterBody);
107             Assert.Equal(oldVersion.ExBodyFilterArray, newVersion.ExFilterBody);
108             Assert.Equal(oldVersion.SearchBoth, newVersion.UseNameField);
109             Assert.Equal(oldVersion.ExSearchBoth, newVersion.ExUseNameField);
110             Assert.Equal(oldVersion.MoveFrom, newVersion.MoveMatches);
111             Assert.Equal(oldVersion.SetMark, newVersion.MarkMatches);
112             Assert.Equal(oldVersion.SearchUrl, newVersion.FilterByUrl);
113             Assert.Equal(oldVersion.ExSearchUrl, newVersion.ExFilterByUrl);
114             Assert.Equal(oldVersion.CaseSensitive, newVersion.CaseSensitive);
115             Assert.Equal(oldVersion.ExCaseSensitive, newVersion.ExCaseSensitive);
116             Assert.Equal(oldVersion.UseLambda, newVersion.UseLambda);
117             Assert.Equal(oldVersion.ExUseLambda, newVersion.ExUseLambda);
118             Assert.Equal(oldVersion.UseRegex, newVersion.UseRegex);
119             Assert.Equal(oldVersion.ExUseRegex, newVersion.ExUseRegex);
120             Assert.Equal(oldVersion.IsRt, newVersion.FilterRt);
121             Assert.Equal(oldVersion.IsExRt, newVersion.ExFilterRt);
122             Assert.Equal(oldVersion.Source, newVersion.FilterSource);
123             Assert.Equal(oldVersion.ExSource, newVersion.ExFilterSource);
124         }
125     }
126 }