Skip to content

Commit

Permalink
is behind
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed May 2, 2024
1 parent 40648c9 commit 8147e1a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/RepoM.Api/Git/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public string[] ReadAllBranches()
(LocalStaged ?? 0) > 0 ||
(LocalRemoved ?? 0) > 0;

public bool IsBehind => (BehindBy ?? 0) > 0;

public int? AheadBy { get; set; }

public int? BehindBy { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static void RegisterServices(IFileSystem fileSystem)
Container.Collection.Append<INamedQueryParser, DefaultQueryParser>(Lifestyle.Singleton);

Container.Collection.Append<IQueryMatcher, IsPinnedMatcher>(Lifestyle.Singleton);
Container.Collection.Append<IQueryMatcher, IsBehindMatcher>(Lifestyle.Singleton);
Container.Collection.Append<IQueryMatcher, IsBareRepositoryMatcher>(Lifestyle.Singleton);
Container.Collection.Append<IQueryMatcher, TagMatcher>(Lifestyle.Singleton);
Container.Collection.Append<IQueryMatcher, NameMatcher>(Lifestyle.Singleton);
Expand Down
31 changes: 31 additions & 0 deletions src/RepoM.App/RepositoryFiltering/QueryMatchers/IsBehindMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace RepoM.App.RepositoryFiltering.QueryMatchers;

using System;
using JetBrains.Annotations;
using RepoM.Core.Plugin.Repository;
using RepoM.Core.Plugin.RepositoryFiltering;
using RepoM.Core.Plugin.RepositoryFiltering.Clause.Terms;

[UsedImplicitly]
public class IsBehindMatcher : IQueryMatcher
{
public bool? IsMatch(in IRepository repository, in TermBase term)
{
if (term is not SimpleTerm st)
{
return null;
}

if (!"is".Equals(st.Term, StringComparison.CurrentCulture))
{
return null;
}

if (!"behind".Equals(st.Value, StringComparison.CurrentCulture))
{
return null;
}

return repository.IsBehind;
}
}
2 changes: 2 additions & 0 deletions src/RepoM.Core.Plugin/Repository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface IRepository


public bool HasLocalChanges { get; }

public bool IsBehind { get; }
}
2 changes: 2 additions & 0 deletions tests/RepoM.ActionMenu.Core.Tests/DummyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public string[] ReadAllBranches()
}

public bool HasLocalChanges => false;

public bool IsBehind => false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace RepoM.App.Tests.RepositoryFiltering.QueryMatchers;

using FakeItEasy;
using FluentAssertions;
using RepoM.App.RepositoryFiltering.QueryMatchers;
using RepoM.Core.Plugin.Repository;
using RepoM.Core.Plugin.RepositoryFiltering.Clause.Terms;
using Xunit;

public class IsBehindMatcherTests
{
private readonly IRepository _repository = A.Fake<IRepository>();
private readonly IsBehindMatcher _sut = new();

[Fact]
public void IsMatch_ShouldReturnNull_WhenTermIsNotSimpleTerm()
{
// arrange
TermBase term = A.Fake<TermBase>();

// act
var result = _sut.IsMatch(in _repository, in term);

// assert
result.Should().BeNull();
A.CallTo(_repository).MustNotHaveHappened();
}

[Theory]
[InlineData("","")]
[InlineData("bla","")]
[InlineData("is","")]
[InlineData("", "bla")]
[InlineData("x", "behind")]
[InlineData("", "behind")]
[InlineData("is", "abehind")]
[InlineData("is", "Behind")] // invalid casing
[InlineData("Is", "behind")] // invalid casing
public void IsMatch_ShouldReturnNull_WhenTermAndValueAreNotIsBehind(string term, string value)
{
// arrange
TermBase simpleTerm = new SimpleTerm(term, value);

// act
var result = _sut.IsMatch(in _repository, in simpleTerm);

// assert
result.Should().BeNull();
A.CallTo(_repository).MustNotHaveHappened();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsMatch_ShouldReturnRepositoryIsBehindValue_WhenTermAndValueMatches(bool repositoryIsBehind)
{
// arrange
A.CallTo(() => _repository.IsBehind).Returns(repositoryIsBehind);
TermBase simpleTerm = new SimpleTerm("is", "behind");

// act
var result = _sut.IsMatch(in _repository, in simpleTerm);

// assert
result.Should().Be(repositoryIsBehind);
A.CallTo(() => _repository.IsBehind).MustHaveHappenedOnceExactly();
}
}

0 comments on commit 8147e1a

Please sign in to comment.