開発に使用するリポジトリ
Révision | c1232a8172a47a88704f43e12a49982c34c96e17 (tree) |
---|---|
l'heure | 2012-11-29 10:38:27 |
Auteur | Kimura Youichi <kim.upsilon@bucy...> |
Commiter | Kimura Youichi |
テストケース追加
@@ -3,13 +3,154 @@ using System.Collections.Generic; | ||
3 | 3 | using System.Linq; |
4 | 4 | using System.Text; |
5 | 5 | using NUnit.Framework; |
6 | +using NSubstitute; | |
6 | 7 | using OpenTween; |
8 | +using System.Runtime.InteropServices; | |
9 | +using System.Reflection; | |
10 | +using System.Windows.Forms; | |
11 | +using System.Runtime.Serialization; | |
7 | 12 | |
8 | 13 | namespace OpenTween |
9 | 14 | { |
10 | 15 | [TestFixture] |
11 | 16 | public class MyCommonTest |
12 | 17 | { |
18 | + [TestCase("http://ja.wikipedia.org/wiki/Wikipedia", Result = "http://ja.wikipedia.org/wiki/Wikipedia")] | |
19 | + [TestCase("http://ja.wikipedia.org/wiki/メインページ", | |
20 | + Result = "http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8")] | |
21 | + [TestCase("http://fr.wikipedia.org/wiki/Café", Result = "http://fr.wikipedia.org/wiki/Caf%E9")] | |
22 | + [TestCase("http://ja.wikipedia.org/wiki/勇気100%", Result = "http://ja.wikipedia.org/wiki/%E5%8B%87%E6%B0%97100%25")] | |
23 | + [TestCase("http://ja.wikipedia.org/wiki/Bio_100%", Result = "http://ja.wikipedia.org/wiki/Bio_100%25")] | |
24 | + public string urlEncodeMultibyteCharTest(string uri) | |
25 | + { | |
26 | + return MyCommon.urlEncodeMultibyteChar(uri); | |
27 | + } | |
28 | + | |
29 | + [TestCase("http://日本語.idn.icann.org/", Result = "http://xn--wgv71a119e.idn.icann.org/")] | |
30 | + [TestCase("http://例え.テスト/", Result = "http://xn--r8jz45g.xn--zckzah/")] | |
31 | + public string IDNDecodeTest(string uri) | |
32 | + { | |
33 | + return MyCommon.IDNDecode(uri); | |
34 | + } | |
35 | + | |
36 | + [TestCase(new int[] { 1, 2, 3, 4 }, 0, 3, Result = new int[] { 2, 3, 4, 1 })] // 左ローテイト? | |
37 | + [TestCase(new int[] { 1, 2, 3, 4 }, 3, 0, Result = new int[] { 4, 1, 2, 3 })] // 右ローテイト? | |
38 | + [TestCase(new int[] { 1, 2, 3, 4, 5 }, 1, 3, Result = new int[] { 1, 3, 4, 2, 5 })] | |
39 | + [TestCase(new int[] { 1, 2, 3, 4, 5 }, 3, 1, Result = new int[] { 1, 4, 2, 3, 5 })] | |
40 | + public int[] MoveArrayItemTest(int[] values, int idx_fr, int idx_to) | |
41 | + { | |
42 | + // MoveArrayItem は values を直接変更するため複製を用意する | |
43 | + var copy = new int[values.Length]; | |
44 | + Array.Copy(values, copy, values.Length); | |
45 | + | |
46 | + MyCommon.MoveArrayItem(copy, idx_fr, idx_to); | |
47 | + return copy; | |
48 | + } | |
49 | + | |
50 | + [Test] | |
51 | + public void EncryptStringTest() | |
52 | + { | |
53 | + var str = "hogehoge"; | |
54 | + | |
55 | + var crypto = MyCommon.EncryptString(str); | |
56 | + Assert.That(crypto, Is.Not.EqualTo(str)); | |
57 | + | |
58 | + var decrypt = MyCommon.DecryptString(crypto); | |
59 | + Assert.That(decrypt, Is.EqualTo(str)); | |
60 | + } | |
61 | + | |
62 | + [TestCase(new byte[] { 0x01, 0x02 }, 3, Result = new byte[] { 0x01, 0x02, 0x00 })] | |
63 | + [TestCase(new byte[] { 0x01, 0x02 }, 2, Result = new byte[] { 0x01, 0x02 })] | |
64 | + [TestCase(new byte[] { 0x01, 0x02 }, 1, Result = new byte[] { 0x03 })] | |
65 | + public byte[] ResizeBytesArrayTest(byte[] bytes, int size) | |
66 | + { | |
67 | + return MyCommon.ResizeBytesArray(bytes, size); | |
68 | + } | |
69 | + | |
70 | + [TestCase("Resources/re.gif", Result = true)] | |
71 | + [TestCase("Resources/re1.gif", Result = false)] | |
72 | + [TestCase("Resources/re1.png", Result = false)] | |
73 | + public bool IsAnimatedGifTest(string filename) | |
74 | + { | |
75 | + return MyCommon.IsAnimatedGif(filename); | |
76 | + } | |
77 | + | |
78 | + static object[] DateTimeParse_TestCase = | |
79 | + { | |
80 | + new object[] { | |
81 | + "Sun Nov 25 06:10:00 +00:00 2012", | |
82 | + new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc) | |
83 | + }, | |
84 | + new object[] { | |
85 | + "Sun, 25 Nov 2012 06:10:00 +00:00", | |
86 | + new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc) | |
87 | + }, | |
88 | + }; | |
89 | + [TestCaseSource("DateTimeParse_TestCase")] | |
90 | + public void DateTimeParseTest(string date, DateTime except) | |
91 | + { | |
92 | + Assert.That(MyCommon.DateTimeParse(date).ToUniversalTime(), Is.EqualTo(except)); | |
93 | + } | |
94 | + | |
95 | + [DataContract] | |
96 | + public struct JsonData | |
97 | + { | |
98 | + [DataMember(Name = "id")] public string Id { get; set; } | |
99 | + [DataMember(Name = "body")] public string Body { get; set; } | |
100 | + } | |
101 | + static object[] CreateDataFromJson_TestCase = | |
102 | + { | |
103 | + new object[] { | |
104 | + @"{""id"":""1"", ""body"":""hogehoge""}", | |
105 | + new JsonData { Id = "1", Body = "hogehoge" }, | |
106 | + }, | |
107 | + }; | |
108 | + [TestCaseSource("CreateDataFromJson_TestCase")] | |
109 | + public void CreateDataFromJsonTest<T>(string json, T expect) | |
110 | + { | |
111 | + Assert.That(MyCommon.CreateDataFromJson<T>(json), Is.EqualTo(expect)); | |
112 | + } | |
113 | + | |
114 | + [TestCase("hoge123@example.com", Result = true)] | |
115 | + [TestCase("hogehoge", Result = false)] | |
116 | + [TestCase("foo.bar@example.com", Result = true)] | |
117 | + [TestCase("foo..bar@example.com", Result = false)] | |
118 | + [TestCase("foobar.@example.com", Result = false)] | |
119 | + [TestCase("foo+bar@example.com", Result = true)] | |
120 | + public bool IsValidEmailTest(string email) | |
121 | + { | |
122 | + return MyCommon.IsValidEmail(email); | |
123 | + } | |
124 | + | |
125 | + [TestCase(Keys.Shift, Keys.Shift, Result = true)] | |
126 | + [TestCase(Keys.Shift, Keys.Control, Result = false)] | |
127 | + [TestCase(Keys.Control | Keys.Alt, Keys.Control, Result = true)] | |
128 | + [TestCase(Keys.Control | Keys.Alt, Keys.Alt, Result = true)] | |
129 | + [TestCase(Keys.Control | Keys.Alt, Keys.Control, Keys.Alt, Result = true)] | |
130 | + [TestCase(Keys.Control | Keys.Alt, Keys.Shift, Result = false)] | |
131 | + public bool IsKeyDownTest(Keys modifierKeys, params Keys[] checkKeys) | |
132 | + { | |
133 | + return MyCommon._IsKeyDown(modifierKeys, checkKeys); | |
134 | + } | |
135 | + | |
136 | + [Test] | |
137 | + public void GetAssemblyNameTest() | |
138 | + { | |
139 | + var mockAssembly = Substitute.For<_Assembly>(); | |
140 | + mockAssembly.GetName().Returns(new AssemblyName("OpenTween")); | |
141 | + MyCommon.EntryAssembly = mockAssembly; | |
142 | + | |
143 | + Assert.That(MyCommon.GetAssemblyName(), Is.EqualTo("OpenTween")); | |
144 | + } | |
145 | + | |
146 | + [TestCase("", "")] | |
147 | + [TestCase("%AppName%", "OpenTween")] | |
148 | + [TestCase("%AppName% %AppName%", "OpenTween OpenTween")] | |
149 | + public void ReplaceAppNameTest(string str, string except) | |
150 | + { | |
151 | + Assert.That(MyCommon.ReplaceAppName(str, "OpenTween"), Is.EqualTo(except)); | |
152 | + } | |
153 | + | |
13 | 154 | [TestCase("1.0.0.0", "1.0.0")] |
14 | 155 | [TestCase("1.0.0.1", "1.0.1-beta1")] |
15 | 156 | [TestCase("1.0.0.9", "1.0.1-beta9")] |
@@ -21,5 +162,29 @@ namespace OpenTween | ||
21 | 162 | { |
22 | 163 | Assert.That(OpenTween.MyCommon.GetReadableVersion(fileVersion), Is.EqualTo(expect)); |
23 | 164 | } |
165 | + | |
166 | + static object[] GetStatusUrlTest1_TestCase = | |
167 | + { | |
168 | + new object[] { | |
169 | + new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = 0L, RetweetedBy = null }, | |
170 | + "https://twitter.com/Favstar_LM/status/249493863826350080", | |
171 | + }, | |
172 | + new object[] { | |
173 | + new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"}, | |
174 | + "https://twitter.com/haru067/status/200245741443235840", | |
175 | + }, | |
176 | + }; | |
177 | + [TestCaseSource("GetStatusUrlTest1_TestCase")] | |
178 | + public void GetStatusUrlTest1(PostClass post, string except) | |
179 | + { | |
180 | + Assert.That(MyCommon.GetStatusUrl(post), Is.EqualTo(except)); | |
181 | + } | |
182 | + | |
183 | + [TestCase("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")] | |
184 | + [TestCase("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")] | |
185 | + public void GetStatusUrlTest2(string screenName, long statusId, string except) | |
186 | + { | |
187 | + Assert.That(MyCommon.GetStatusUrl(screenName, statusId), Is.EqualTo(except)); | |
188 | + } | |
24 | 189 | } |
25 | 190 | } |
@@ -41,6 +41,8 @@ | ||
41 | 41 | </Reference> |
42 | 42 | <Reference Include="System" /> |
43 | 43 | <Reference Include="System.Core" /> |
44 | + <Reference Include="System.Runtime.Serialization" /> | |
45 | + <Reference Include="System.Windows.Forms" /> | |
44 | 46 | <Reference Include="System.Xml.Linq" /> |
45 | 47 | <Reference Include="System.Data.DataSetExtensions" /> |
46 | 48 | <Reference Include="Microsoft.CSharp" /> |
@@ -60,7 +62,17 @@ | ||
60 | 62 | <ItemGroup> |
61 | 63 | <Content Include="dlls\NSubstitute.dll" /> |
62 | 64 | <Content Include="dlls\nunit.framework.dll" /> |
65 | + <Content Include="Resources\re.gif"> | |
66 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
67 | + </Content> | |
68 | + <Content Include="Resources\re1.gif"> | |
69 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
70 | + </Content> | |
71 | + <Content Include="Resources\re1.png"> | |
72 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
73 | + </Content> | |
63 | 74 | </ItemGroup> |
75 | + <ItemGroup /> | |
64 | 76 | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
65 | 77 | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
66 | 78 | Other similar extension points exist, see Microsoft.Common.targets. |
@@ -43,6 +43,7 @@ using System.Reflection; | ||
43 | 43 | using System.Diagnostics; |
44 | 44 | using System.Text.RegularExpressions; |
45 | 45 | using System.Net.NetworkInformation; |
46 | +using System.Runtime.InteropServices; | |
46 | 47 | |
47 | 48 | namespace OpenTween |
48 | 49 | { |
@@ -762,7 +763,7 @@ namespace OpenTween | ||
762 | 763 | } |
763 | 764 | } |
764 | 765 | |
765 | - static bool IsValidEmail(string strIn) | |
766 | + public static bool IsValidEmail(string strIn) | |
766 | 767 | { |
767 | 768 | // Return true if strIn is in valid e-mail format. |
768 | 769 | return Regex.IsMatch(strIn, |
@@ -777,9 +778,14 @@ namespace OpenTween | ||
777 | 778 | /// <returns><paramref name="keys"/> で指定された修飾キーがすべて押されている状態であれば true。それ以外であれば false。</returns> |
778 | 779 | public static bool IsKeyDown(params Keys[] keys) |
779 | 780 | { |
780 | - foreach (Keys key in keys) | |
781 | + return MyCommon._IsKeyDown(Control.ModifierKeys, keys); | |
782 | + } | |
783 | + | |
784 | + internal static bool _IsKeyDown(Keys modifierKeys, Keys[] targetKeys) | |
785 | + { | |
786 | + foreach (Keys key in targetKeys) | |
781 | 787 | { |
782 | - if ((Control.ModifierKeys & key) != key) | |
788 | + if ((modifierKeys & key) != key) | |
783 | 789 | { |
784 | 790 | return false; |
785 | 791 | } |
@@ -796,9 +802,11 @@ namespace OpenTween | ||
796 | 802 | /// <returns>アプリケーションのアセンブリ名</returns> |
797 | 803 | public static string GetAssemblyName() |
798 | 804 | { |
799 | - return Assembly.GetEntryAssembly().GetName().Name; | |
805 | + return MyCommon.EntryAssembly.GetName().Name; | |
800 | 806 | } |
801 | 807 | |
808 | + internal static _Assembly EntryAssembly = Assembly.GetEntryAssembly(); | |
809 | + | |
802 | 810 | /// <summary> |
803 | 811 | /// 文字列中に含まれる %AppName% をアプリケーション名に置換する |
804 | 812 | /// </summary> |
@@ -806,7 +814,18 @@ namespace OpenTween | ||
806 | 814 | /// <returns>置換後の文字列</returns> |
807 | 815 | public static string ReplaceAppName(string orig) |
808 | 816 | { |
809 | - return orig.Replace("%AppName%", Application.ProductName); | |
817 | + return MyCommon.ReplaceAppName(orig, Application.ProductName); | |
818 | + } | |
819 | + | |
820 | + /// <summary> | |
821 | + /// 文字列中に含まれる %AppName% をアプリケーション名に置換する | |
822 | + /// </summary> | |
823 | + /// <param name="orig">対象となる文字列</param> | |
824 | + /// <param name="appname">アプリケーション名</param> | |
825 | + /// <returns>置換後の文字列</returns> | |
826 | + public static string ReplaceAppName(string orig, string appname) | |
827 | + { | |
828 | + return orig.Replace("%AppName%", appname); | |
810 | 829 | } |
811 | 830 | |
812 | 831 | /// <summary> |