Skip to content

Commit 6670e93

Browse files
committed
BrowserCookieImplementationsのテストを作成して、リファクタリング
1 parent 4ca2dc7 commit 6670e93

File tree

8 files changed

+236
-36
lines changed

8 files changed

+236
-36
lines changed

Diff for: BrowserCookieImplementations/Firefox.cs

+54-36
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,44 @@ public static FirefoxProfile GetDefaultProfile(string moz_path, string iniFileNa
170170
}
171171
return null;
172172
}
173+
public static List<FirefoxProfile> GetProfiles(IEnumerable<string> lines, string moz_path)
174+
{
175+
var list = new List<FirefoxProfile>();
176+
FirefoxProfile profile = null;
177+
foreach (var line in lines)
178+
{
179+
if (line.StartsWith("[Profile"))
180+
{
181+
profile = new FirefoxProfile();
182+
list.Add(profile);
183+
continue;
184+
}
185+
if (profile != null)
186+
{
187+
var pair = SplitByEqual(line);
188+
switch (pair.Key)
189+
{
190+
case "Name":
191+
profile.Name = pair.Value;
192+
break;
193+
case "IsRelative":
194+
profile.IsRelative = (pair.Value == "1") ? true : false;
195+
break;
196+
case "Path":
197+
profile.path = pair.Value.Replace("/", "\\");
198+
if (profile.IsRelative)
199+
profile.path = moz_path + "\\" + profile.path;
200+
break;
201+
case "Default":
202+
profile.IsDefault = (pair.Value == "1") ? true : false;
203+
break;
204+
default:
205+
break;
206+
}
207+
}
208+
}
209+
return list;
210+
}
173211
/// <summary>
174212
///
175213
/// </summary>
@@ -187,54 +225,34 @@ public static List<FirefoxProfile> GetProfiles(string moz_path, string iniFileNa
187225
}
188226
using (var sr = new System.IO.StreamReader(path, enc))
189227
{
190-
FirefoxProfile profile = null;
191-
while (!sr.EndOfStream)
192-
{
193-
var line = sr.ReadLine();
194-
if (line.StartsWith("[Profile"))
195-
{
196-
profile = new FirefoxProfile();
197-
list.Add(profile);
198-
}
199-
if (profile != null)
200-
{
201-
var pair = SplitByEqual(line);
202-
switch (pair.Key)
203-
{
204-
case "Name":
205-
profile.Name = pair.Value;
206-
break;
207-
case "IsRelative":
208-
profile.IsRelative = (pair.Value == "1") ? true : false;
209-
break;
210-
case "Path":
211-
profile.path = pair.Value.Replace("/", "\\");
212-
if (profile.IsRelative)
213-
profile.path = moz_path + "\\" + profile.path;
214-
break;
215-
case "Default":
216-
profile.IsDefault = (pair.Value == "1") ? true : false;
217-
break;
218-
default:
219-
break;
220-
}
221-
}
222-
}
228+
list.AddRange(GetProfiles(ReadLines(sr), moz_path));
223229
}
224230
return list;
225231
}
232+
public static IEnumerable<string> ReadLines(System.IO.StreamReader sr)
233+
{
234+
while (!sr.EndOfStream)
235+
{
236+
yield return sr.ReadLine();
237+
}
238+
}
226239
/// <summary>
227240
/// 文字列を'='で2つに分割する。
228241
/// </summary>
229242
/// <param name="line"></param>
230243
/// <returns></returns>
231-
private static KeyValuePair<string, string> SplitByEqual(string line)
244+
internal static KeyValuePair<string, string> SplitByEqual(string line)
232245
{
233246
var arr = line.Split('=').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
234-
if (arr.Length == 2)
235-
return new KeyValuePair<string, string>(arr[0], arr[1]);
247+
if (arr.Length >= 2)
248+
{
249+
var pos = line.IndexOf('=');
250+
return new KeyValuePair<string, string>(arr[0],line.Substring(pos+1));
251+
}
236252
else
253+
{
237254
return new KeyValuePair<string, string>();
255+
}
238256
}
239257
}
240258
}

Diff for: BrowserCookieImplementations/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion("1.0.0.0")]
3636
[assembly: AssemblyFileVersion("1.0.0.0")]
37+
[assembly:InternalsVisibleTo("BrowserCookieImplementationsTests")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>BrowserCookieImplementationsTests</RootNamespace>
11+
<AssemblyName>BrowserCookieImplementationsTests</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="FirefoxProfileTests.cs" />
45+
<Compile Include="Tools.cs" />
46+
<Content Include="SampleData\profiles.ini.txt">
47+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
48+
</Content>
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<PackageReference Include="NUnit">
53+
<Version>3.11.0</Version>
54+
</PackageReference>
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\BrowserCookieImplementations\BrowserCookieImplementations.csproj">
58+
<Project>{dfe01d83-bdbf-4b5c-95d5-9f1034597ede}</Project>
59+
<Name>BrowserCookieImplementations</Name>
60+
</ProjectReference>
61+
</ItemGroup>
62+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
63+
</Project>
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using NUnit.Framework;
2+
using ryu_s.BrowserCookie.Firefox;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BrowserCookieImplementationsTests
10+
{
11+
[TestFixture]
12+
public class FirefoxProfileTests
13+
{
14+
[Test]
15+
public void GetProfilesTest()
16+
{
17+
var data = DataLoader.GetSampleData("profiles.ini.txt");
18+
var lines = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
19+
var moz_path = "path";
20+
var ps = FirefoxProfile.GetProfiles(lines, moz_path);
21+
Assert.AreEqual(2, ps.Count);
22+
Assert.AreEqual("default", ps[0].Name);
23+
Assert.IsTrue(ps[0].IsRelative);
24+
Assert.IsFalse(ps[0].IsDefault);
25+
Assert.AreEqual("path\\Profiles\\f3ezfk6m.default", ps[0].path);
26+
Assert.AreEqual("dev-edition-default", ps[1].Name);
27+
Assert.IsTrue(ps[1].IsRelative);
28+
Assert.IsTrue(ps[1].IsDefault);
29+
Assert.AreEqual("path\\Profiles\\42522y5w.dev-edition-default-1516971409783", ps[1].path);
30+
}
31+
[Test]
32+
public void SplitByEqualTest()
33+
{
34+
var a = FirefoxProfile.SplitByEqual("a=b=c");
35+
Assert.AreEqual("a", a.Key);
36+
Assert.AreEqual("b=c", a.Value);
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("BrowserCookieImplementationsTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("BrowserCookieImplementationsTests")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("ef214eb0-699d-4cf9-9ca5-2fc49b1bedb4")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[General]
2+
StartWithLastProfile=1
3+
4+
[Profile0]
5+
Name=default
6+
IsRelative=1
7+
Path=Profiles/f3ezfk6m.default
8+
9+
[Profile1]
10+
Name=dev-edition-default
11+
IsRelative=1
12+
Path=Profiles/42522y5w.dev-edition-default-1516971409783
13+
Default=1
14+

Diff for: BrowserCookieImplementationsTests/Tools.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BrowserCookieImplementationsTests
8+
{
9+
internal static class DataLoader
10+
{
11+
public static string GetSampleData(string filename)
12+
{
13+
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"SampleData\" + filename);
14+
string sample = "";
15+
16+
using (var sr = new System.IO.StreamReader(path))
17+
{
18+
sample = sr.ReadToEnd();
19+
}
20+
return sample;
21+
}
22+
}
23+
}

Diff for: MultiCommentViewer.sln

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestSitePlugin", "TestSiteP
9595
EndProject
9696
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestSiteIF", "TestSiteIF\TestSiteIF.csproj", "{FF176BCD-7F7E-4C42-9E8D-17912097B3C7}"
9797
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserCookieImplementationsTests", "BrowserCookieImplementationsTests\BrowserCookieImplementationsTests.csproj", "{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}"
99+
EndProject
98100
Global
99101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
100102
Debug|Any CPU = Debug|Any CPU
@@ -269,6 +271,10 @@ Global
269271
{FF176BCD-7F7E-4C42-9E8D-17912097B3C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
270272
{FF176BCD-7F7E-4C42-9E8D-17912097B3C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
271273
{FF176BCD-7F7E-4C42-9E8D-17912097B3C7}.Release|Any CPU.Build.0 = Release|Any CPU
274+
{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
275+
{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
276+
{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
277+
{EF214EB0-699D-4CF9-9CA5-2FC49B1BEDB4}.Release|Any CPU.Build.0 = Release|Any CPU
272278
EndGlobalSection
273279
GlobalSection(SolutionProperties) = preSolution
274280
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)