Skip to content
Newer
Older
100644 119 lines (92 sloc) 3.38 KB
f7738b0 @remi Successfully extracted my IFile/IDirectory code and specs and get the…
authored
1 using System;
2 using System.IO;
3 using NUnit.Framework;
df03c0a @remi Renamed to IO.Interfaces (because putting the IFile interface in an I…
authored
4 using IO.Interfaces;
f7738b0 @remi Successfully extracted my IFile/IDirectory code and specs and get the…
authored
5
df03c0a @remi Renamed to IO.Interfaces (because putting the IFile interface in an I…
authored
6 namespace IO.Interfaces.Specs {
f7738b0 @remi Successfully extracted my IFile/IDirectory code and specs and get the…
authored
7
8 [TestFixture]
9 public class IFileSpec : Spec {
10
11 // sample IFile implementation
12 public class FileClass : IFile {
13 public FileClass(string path) { Path = path; }
14 public string Path { get; set; }
15 }
16
17 string dir_1 = PathToTemp("my", "dir");
18 string dir_2 = PathToTemp("my", "other", "dir");
19
20 string[] AllFiles { get { return Directory.GetFiles(PathToTemp(), "*", SearchOption.AllDirectories); } }
21
22 IFile Dir1File(string name) { return new FileClass(Path.Combine(dir_1, name)); }
23 IFile Dir2File(string name) { return new FileClass(Path.Combine(dir_2, name)); }
24
25 [SetUp]
26 public void Before() {
27 base.BeforeEach();
28 Directory.CreateDirectory(dir_1);
29 Directory.CreateDirectory(dir_2);
30 }
31
32 [Test]
33 public void FileName() {
34 Dir1File("foo").FileName().ShouldEqual("foo");
35 Dir1File("foo.txt").FileName().ShouldEqual("foo.txt");
36 }
37
38 [Test]
39 public void DirectoryName() {
40 Dir1File("foo").DirectoryName().ShouldEqual(dir_1);
41 Dir2File("foo").DirectoryName().ShouldEqual(dir_2);
42
43 // shortcut
44 Dir1File("foo").DirName().ShouldEqual(dir_1);
45 Dir2File("foo").DirName().ShouldEqual(dir_2);
46 }
47
48 [Test]
49 public void Touch() {
50 AllFiles.Should(Be.Empty);
51
52 Dir1File("foo").Touch();
53
54 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "foo") });
55
56 Dir2File("README.txt").Touch();
57
58 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "foo"), Path.Combine(dir_2, "README.txt") });
59 }
60
61 [Test]
62 public void Exists() {
63 var file = Dir1File("foo");
64 file.Exists().Should(Be.False);
65 file.Touch();
66 file.Exists().Should(Be.True);
67 }
68
69 [Test]
70 public void Copy() {
71 var file = Dir1File("foo").Touch();
72 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "foo") });
73 using (var writer = new StreamWriter(file.Path)) writer.WriteLine("Foo Content");
74
75 // copy to new name
76 file.Copy(dir_1, "bar.txt");
77 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "bar.txt"), Path.Combine(dir_1, "foo") });
78
79 // copy to directory (no new name)
80 file.Copy(dir_2);
81 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "bar.txt"), Path.Combine(dir_1, "foo"), Path.Combine(dir_2, "foo") });
82
83 // copy again (should overwrite) ... test overwrite (File.Copy(x, y, true))
84 using (var writer = new StreamWriter(file.Path)) writer.WriteLine("Different");
85 File.ReadAllText(file.Path).ShouldEqual("Different\n");
86 File.ReadAllText(Path.Combine(dir_2, "foo")).ShouldEqual("Foo Content\n");
87 file.Copy(dir_2);
88 File.ReadAllText(Path.Combine(dir_2, "foo")).ShouldEqual("Different\n");
89 }
90
91 [Test]
92 public void Move() {
93 var file = Dir1File("foo").Touch();
94 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "foo") });
95
96 // move to new name
97 file.Move(dir_1, "bar.txt");
98 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "bar.txt") });
99
100 // move to directory (no new name)
101 file.Move(dir_2);
102 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_2, "bar.txt") });
103 }
104
105 [Test]
106 public void Delete() {
107 var file1 = Dir1File("foo").Touch();
108 var file2 = Dir2File("bar").Touch();
109 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_1, "foo"), Path.Combine(dir_2, "bar") });
110
111 file1.Delete();
112 AllFiles.ShouldEqual(new string[]{ Path.Combine(dir_2, "bar") });
113
114 file2.Delete();
115 AllFiles.ShouldEqual(new string[]{ });
116 }
117 }
118 }
Something went wrong with that request. Please try again.