Skip to content

Commit c4782af

Browse files
committed
I think I got saving down
It's not really all that hard, if you think about it
1 parent b6b9d6a commit c4782af

File tree

5 files changed

+104
-18
lines changed

5 files changed

+104
-18
lines changed

Jump.Location.Specs/FileStoreProviderSpec.cs

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,104 @@
66

77
namespace Jump.Location.Specs
88
{
9-
public class FileStoreProviderSpec : IDisposable
9+
public class FileStoreProviderSpec
1010
{
11-
private readonly string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
12-
13-
public void Dispose()
11+
public class DescribeRevive : IDisposable
1412
{
15-
try { File.Delete(path); }
16-
catch { }
13+
private readonly string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
14+
15+
public void Dispose()
16+
{
17+
try
18+
{
19+
File.Delete(path);
20+
}
21+
catch
22+
{
23+
}
24+
}
25+
26+
[Fact]
27+
public void It_can_Revive_a_single_record_of_correctly_formatted_text()
28+
{
29+
var lines = new[] {"FS::C:\\blah\t8.5"};
30+
File.WriteAllLines(path, lines);
31+
var provider = new FileStoreProvider(path);
32+
33+
var db = provider.Revive();
34+
db.Records.Count().ShouldEqual(1);
35+
db.Records.First().Weight.ShouldEqual(8.5M);
36+
}
37+
38+
[Fact]
39+
public void It_can_Revive_multiple_records_of_correctly_formatted_text()
40+
{
41+
var lines = new[]
42+
{
43+
"FS::C:\\blah\t8.5",
44+
"FS::C:\\blah\t8.5",
45+
"FS::C:\\blah\t8.5",
46+
};
47+
File.WriteAllLines(path, lines);
48+
var provider = new FileStoreProvider(path);
49+
50+
var db = provider.Revive();
51+
db.Records.Count().ShouldEqual(3);
52+
}
53+
54+
[Fact]
55+
public void It_skips_blank_and_empty_lines()
56+
{
57+
var lines = new[]
58+
{
59+
"",
60+
"FS::C:\\blah\t8.5",
61+
" ",
62+
};
63+
File.WriteAllLines(path, lines);
64+
var provider = new FileStoreProvider(path);
65+
66+
var db = provider.Revive();
67+
db.Records.Count().ShouldEqual(1);
68+
}
69+
70+
[Fact]
71+
public void It_throws_InvalidOperationException_when_row_doesnt_have_2_columns()
72+
{
73+
var lines = new[] {"FS::C:\\blah 8.5"};
74+
File.WriteAllLines(path, lines);
75+
var provider = new FileStoreProvider(path);
76+
77+
Assert.Throws<InvalidOperationException>(() => provider.Revive());
78+
}
1779
}
1880

19-
[Fact]
20-
public void It_can_Revive_a_single_record_of_correctly_formatted_text()
81+
public class DescribeSave : IDisposable
2182
{
22-
var lines = new[] { "FS::C:\\blah\t8.5" };
23-
File.WriteAllLines(path, lines);
24-
var provider = new FileStoreProvider(path);
83+
private readonly string path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
84+
85+
public void Dispose()
86+
{
87+
try
88+
{
89+
File.Delete(path);
90+
}
91+
catch
92+
{
93+
}
94+
}
95+
96+
[Fact]
97+
public void It_can_save_a_single_record()
98+
{
99+
var db = new Database();
100+
db.Add(new Record("FS::C:\\data", 42M));
101+
var provider = new FileStoreProvider(path);
102+
provider.Save(db);
25103

26-
var db = provider.Revive();
27-
db.Records.Count().ShouldEqual(1);
104+
var contents = File.ReadAllText(path);
105+
contents.ShouldEqual("FS::C:\\data\t42\r\n");
106+
}
28107
}
29108
}
30109
}

Jump.Location.Specs/Jump.Location.Specs.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Jump.Location.Specs</RootNamespace>
1111
<AssemblyName>Jump.Location.Specs</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1515
<RestorePackages>true</RestorePackages>
16+
<TargetFrameworkProfile />
1617
</PropertyGroup>
1718
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1819
<DebugSymbols>true</DebugSymbols>

Jump.Location/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
<configuration>
33
<startup>
44

5-
<supportedRuntime version="v2.0.50727"/></startup>
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
66
</configuration>

Jump.Location/FileStoreProvider.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45

@@ -21,14 +22,17 @@ public FileStoreProvider(string path)
2122

2223
public void Save(IDatabase database)
2324
{
24-
25+
var lines = database.Records.Select(record =>
26+
string.Format("{0}\t{1}", record.FullName, record.Weight));
27+
File.WriteAllLines(path, lines);
2528
}
2629

2730
public IDatabase Revive()
2831
{
2932
var db = new Database();
3033
var allLines = File.ReadAllLines(path);
31-
foreach (var columns in allLines.Select(line => line.Split('\t')))
34+
var nonBlankLines = allLines.Where(line => !string.IsNullOrWhiteSpace(line));
35+
foreach (var columns in nonBlankLines.Select(line => line.Split('\t')))
3236
{
3337
if (columns == null || columns.Length != 2)
3438
throw new InvalidOperationException("Row of file didn't have 2 columns separated by a tab");

Jump.Location/Jump.Location.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Jump.Location</RootNamespace>
1111
<AssemblyName>Jump.Location</AssemblyName>
12-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
</PropertyGroup>
@@ -22,6 +22,7 @@
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
<Prefer32Bit>false</Prefer32Bit>
2526
</PropertyGroup>
2627
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2728
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,6 +32,7 @@
3132
<DefineConstants>TRACE</DefineConstants>
3233
<ErrorReport>prompt</ErrorReport>
3334
<WarningLevel>4</WarningLevel>
35+
<Prefer32Bit>false</Prefer32Bit>
3436
</PropertyGroup>
3537
<PropertyGroup>
3638
<StartupObject />

0 commit comments

Comments
 (0)