Skip to content

Commit 70aff86

Browse files
committed
Make Branch mockable
1 parent 8ec4f12 commit 70aff86

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

LibGit2Sharp/Branch.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public class Branch : ReferenceWrapper<Commit>
1212
{
1313
private readonly Lazy<Branch> trackedBranch;
1414

15+
/// <summary>
16+
/// Needed for mocking purposes.
17+
/// </summary>
18+
protected Branch()
19+
{ }
20+
1521
/// <summary>
1622
/// Initializes a new instance of the <see cref = "Branch" /> class.
1723
/// </summary>
@@ -47,7 +53,7 @@ private Branch(Repository repo, Reference reference, Func<Reference, string> can
4753
/// </summary>
4854
/// <param name = "relativePath">The relative path to the <see cref = "TreeEntry" /> from the <see cref = "Tip" /> working directory.</param>
4955
/// <returns><c>null</c> if nothing has been found, the <see cref = "TreeEntry" /> otherwise.</returns>
50-
public TreeEntry this[string relativePath]
56+
public virtual TreeEntry this[string relativePath]
5157
{
5258
get
5359
{
@@ -74,31 +80,31 @@ public virtual bool IsRemote
7480
/// <summary>
7581
/// Gets the remote branch which is connected to this local one.
7682
/// </summary>
77-
public Branch TrackedBranch
83+
public virtual Branch TrackedBranch
7884
{
7985
get { return trackedBranch.Value; }
8086
}
8187

8288
/// <summary>
8389
/// Determines if this local branch is connected to a remote one.
8490
/// </summary>
85-
public bool IsTracking
91+
public virtual bool IsTracking
8692
{
8793
get { return TrackedBranch != null; }
8894
}
8995

9096
/// <summary>
9197
/// Gets the number of commits, starting from the <see cref="Tip"/>, that have been performed on this local branch and aren't known from the remote one.
9298
/// </summary>
93-
public int AheadBy
99+
public virtual int AheadBy
94100
{
95101
get { return IsTracking ? repo.Commits.QueryBy(new Filter { Since = Tip, Until = TrackedBranch }).Count() : 0; }
96102
}
97103

98104
/// <summary>
99105
/// Gets the number of commits that exist in the remote branch, on top of <see cref="Tip"/>, and aren't known from the local one.
100106
/// </summary>
101-
public int BehindBy
107+
public virtual int BehindBy
102108
{
103109
get { return IsTracking ? repo.Commits.QueryBy(new Filter { Since = TrackedBranch, Until = Tip }).Count() : 0; }
104110
}
@@ -109,23 +115,23 @@ public int BehindBy
109115
/// <value>
110116
/// <c>true</c> if this instance is the current branch; otherwise, <c>false</c>.
111117
/// </value>
112-
public bool IsCurrentRepositoryHead
118+
public virtual bool IsCurrentRepositoryHead
113119
{
114120
get { return repo.Head == this; }
115121
}
116122

117123
/// <summary>
118124
/// Gets the <see cref="Commit"/> that this branch points to.
119125
/// </summary>
120-
public Commit Tip
126+
public virtual Commit Tip
121127
{
122128
get { return TargetObject; }
123129
}
124130

125131
/// <summary>
126132
/// Gets the commits on this branch. (Starts walking from the References's target).
127133
/// </summary>
128-
public ICommitLog Commits
134+
public virtual ICommitLog Commits
129135
{
130136
get { return repo.Commits.QueryBy(new Filter { Since = this }); }
131137
}

LibGit2Sharp/ReferenceWrapper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TO
1919
private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper =
2020
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(new Func<ReferenceWrapper<TObject>, object>[] { x => x.CanonicalName, x => x.TargetObject });
2121

22+
/// <summary>
23+
/// Needed for mocking purposes.
24+
/// </summary>
25+
protected ReferenceWrapper()
26+
{ }
27+
2228
/// <param name="repo">The repository.</param>
2329
/// <param name="reference">The reference.</param>
2430
/// <param name="canonicalNameSelector">A function to construct the reference's canonical name.</param>
@@ -36,12 +42,12 @@ protected internal ReferenceWrapper(Repository repo, Reference reference, Func<R
3642
/// <summary>
3743
/// Gets the full name of this reference.
3844
/// </summary>
39-
public string CanonicalName { get; protected set; }
45+
public virtual string CanonicalName { get; protected set; }
4046

4147
/// <summary>
4248
/// Gets the name of this reference.
4349
/// </summary>
44-
public string Name
50+
public virtual string Name
4551
{
4652
get { return Shorten(CanonicalName); }
4753
}

0 commit comments

Comments
 (0)