8000 Configuration.Testability++ · apfunk/libgit2sharp@b8dc660 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b8dc660

Browse files
committed
Configuration.Testability++
1 parent e3108d4 commit b8dc660

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<Compile Include="CleanFixture.cs" />
8181
<Compile Include="CurrentOperationFixture.cs" />
8282
<Compile Include="MetaFixture.cs" />
83-
<Compile Include="MockedRepositoryFixture.cs" />
83+
<Compile Include="MockingFixture.cs" />
8484
<Compile Include="ConfigurationFixture.cs" />
8585
<Compile Include="AttributesFixture.cs" />
8686
<Compile Include="CommitAncestorFixture.cs" />

LibGit2Sharp.Tests/MockedRepositoryFixture.cs renamed to LibGit2Sharp.Tests/MockingFixture.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace LibGit2Sharp.Tests
99
{
10-
// This fixture shows how one can mock the IRepository when writing an application against LibGit2Sharp.
11-
// The application we want to test is simulated by the CommitCounter class (see below), which takes an IRepository,
12-
// and whose role is to compute and return the number of commits in the given repository.
13-
public class MockedRepositoryFixture : BaseFixture
10+
// This fixture shows how one can mock various LibGit2Sharp APIs.
11+
public class MockingFixture : BaseFixture
1412
{
13+
// The application we want to test is simulated by the CommitCounter class (see below), which takes an IRepository,
14+
// and whose role is to compute and return the number of commits in the given repository.
15+
1516
// In this test, we pass to CommitCounter a concrete instance of the Repository. It means we will end up calling the concrete Repository
1617
// during the test run.
1718
[Fact]
@@ -67,5 +68,42 @@ public int NumberOfCommits
6768
get { return repo.Commits.Count(); }
6869
}
6970
}
71+
72+
[Fact]
73+
public void CanFakeConfigurationBuildSignature()
74+
{
75+
var name = "name";
76+
var email = "email";
77+
var now = DateTimeOffset.UtcNow;
78+
79+
var fakeConfig = new Mock<Configuration>();
80+
fakeConfig.Setup(c => c.BuildSignature(now))
81+
.Returns<DateTimeOffset>(t => new Signature(name, email, t));
82+
83+
var sig = fakeConfig.Object.BuildSignature(now);
84+
Assert.Equal(name, sig.Name);
85+
Assert.Equal(email, sig.Email);
86+
Assert.Equal(now, sig.When);
87+
}
88+
89+
[Fact]
90+
public void CanFakeEnumerationOfConfiguration()
91+
{
92+
var fakeConfig = new Mock<Configuration>();
93+
fakeConfig.Setup(c => c.GetEnumerator()).Returns(FakeEntries);
94+
95+
Assert.Equal(2, fakeConfig.Object.Count());
96+
}
97+
98+
private static IEnumerator<ConfigurationEntry<string>> FakeEntries()
99+
{
100+
yield return FakeConfigurationEntry("foo", "bar", ConfigurationLevel.Local);
101+
yield return FakeConfigurationEntry("baz", "quux", ConfigurationLevel.Global);
102+
}
103+
104+
private static ConfigurationEntry<string> FakeConfigurationEntry(string key, string value, ConfigurationLevel level)
105+
{
106+
return new Mock<ConfigurationEntry<string>>(key, value, level).Object;
107+
}
70108
}
71109
}

LibGit2Sharp/Configuration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Act
284284
{ typeof(string), GetUpdater<string>(Proxy.git_config_set_string) },
285285
};
286286

287-
IEnumerator<ConfigurationEntry<string>> IEnumerable<ConfigurationEntry<String>>.GetEnumerator()
287+
/// <summary>
288+
/// Returns an enumerator that iterates through the configuration entries.
289+
/// </summary>
290+
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the configuration entries.</returns>
291+
public virtual IEnumerator<ConfigurationEntry<string>> GetEnumerator()
288292
{
289293
return BuildConfigEntries().GetEnumerator();
290294
}

LibGit2Sharp/ConfigurationEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected ConfigurationEntry()
3737
/// <param name="key">The option name</param>
3838
/// <param name="value">The option value</param>
3939
/// <param name="level">The origin store</param>
40-
internal ConfigurationEntry(string key, T value, ConfigurationLevel level)
40+
protected internal ConfigurationEntry(string key, T value, ConfigurationLevel level)
4141
{
4242
Key = key;
4343
Value = value;

0 commit comments

Comments
 (0)
0