refactor: Move GitViewBuilder into module 'lib_tests'

pull/1/head
sgoudham 2 years ago
parent c8c7bdd1bd
commit 8bb5abe048
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -4,7 +4,7 @@ use std::{
process::{Command, Output}, process::{Command, Output},
}; };
use crate::{error::AppError, GitView}; use crate::error::AppError;
#[cfg(test)] #[cfg(test)]
use mockall::automock; use mockall::automock;
@ -47,59 +47,6 @@ pub enum GitOutput {
Err(String), Err(String),
} }
#[derive(Default)]
pub(crate) struct GitViewBuilder<'a> {
remote: Option<&'a str>,
branch: Option<&'a str>,
commit: Option<&'a str>,
suffix: Option<&'a str>,
is_issue: bool,
is_print: bool,
}
impl<'a> GitViewBuilder<'a> {
pub(crate) fn with_remote(mut self, remote: &'a str) -> Self {
self.remote = Some(remote);
self
}
pub(crate) fn with_branch(mut self, branch: &'a str) -> Self {
self.branch = Some(branch);
self
}
pub(crate) fn with_commit(mut self, commit: &'a str) -> Self {
self.commit = Some(commit);
self
}
pub(crate) fn with_suffix(mut self, suffix: &'a str) -> Self {
self.suffix = Some(suffix);
self
}
pub(crate) fn is_issue(mut self, is_issue: bool) -> Self {
self.is_issue = is_issue;
self
}
pub(crate) fn is_print(mut self, is_print: bool) -> Self {
self.is_print = is_print;
self
}
pub(crate) fn build(self) -> GitView<'a> {
GitView::new(
self.branch,
self.remote,
self.commit,
self.suffix,
self.is_issue,
self.is_print,
)
}
}
#[cfg_attr(test, automock)] #[cfg_attr(test, automock)]
pub trait GitTrait { pub trait GitTrait {
fn is_valid_repository(&self) -> Result<GitOutput, AppError>; fn is_valid_repository(&self) -> Result<GitOutput, AppError>;

@ -4,7 +4,7 @@ mod git;
use std::borrow::Cow; use std::borrow::Cow;
use error::{AppError, ErrorType}; use error::{AppError, ErrorType};
use git::{Domain, GitOutput, GitTrait, GitViewBuilder, Local, Url}; use git::{Domain, GitOutput, GitTrait, Local, Url};
pub use git::Git; pub use git::Git;
@ -19,10 +19,6 @@ pub struct GitView<'a> {
} }
impl<'a> GitView<'a> { impl<'a> GitView<'a> {
fn builder() -> GitViewBuilder<'a> {
GitViewBuilder::default()
}
pub fn new( pub fn new(
branch: Option<&'a str>, branch: Option<&'a str>,
remote: Option<&'a str>, remote: Option<&'a str>,
@ -316,6 +312,61 @@ fn escape_ascii_chars(remote_ref: &str) -> Cow<'_, str> {
#[cfg(test)] #[cfg(test)]
mod lib_tests { mod lib_tests {
use crate::GitView;
impl<'a> GitView<'a> {
fn builder() -> GitViewBuilder<'a> {
GitViewBuilder::default()
}
}
#[derive(Default)]
pub(crate) struct GitViewBuilder<'a> {
remote: Option<&'a str>,
branch: Option<&'a str>,
commit: Option<&'a str>,
suffix: Option<&'a str>,
is_issue: bool,
is_print: bool,
}
impl<'a> GitViewBuilder<'a> {
pub(crate) fn with_remote(mut self, remote: &'a str) -> Self {
self.remote = Some(remote);
self
}
pub(crate) fn with_branch(mut self, branch: &'a str) -> Self {
self.branch = Some(branch);
self
}
pub(crate) fn with_commit(mut self, commit: &'a str) -> Self {
self.commit = Some(commit);
self
}
pub(crate) fn with_suffix(mut self, suffix: &'a str) -> Self {
self.suffix = Some(suffix);
self
}
pub(crate) fn with_issue(mut self, is_issue: bool) -> Self {
self.is_issue = is_issue;
self
}
pub(crate) fn build(self) -> GitView<'a> {
GitView::new(
self.branch,
self.remote,
self.commit,
self.suffix,
self.is_issue,
self.is_print,
)
}
}
mod is_valid_repository { mod is_valid_repository {
use crate::{ use crate::{
@ -791,7 +842,7 @@ mod lib_tests {
#[test] #[test]
fn is_user_issue() { fn is_user_issue() {
let handler = GitView::builder().is_issue(true).build(); let handler = GitView::builder().with_issue(true).build();
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view"); let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/issues/1234"; let expected_final_url = "https://github.com/sgoudham/git-view/issues/1234";
let mock = MockGitTrait::default(); let mock = MockGitTrait::default();
@ -804,7 +855,7 @@ mod lib_tests {
#[test] #[test]
fn is_normal_branch() { fn is_normal_branch() {
let handler = GitView::builder().is_issue(false).build(); let handler = GitView::builder().with_issue(false).build();
let url = Url::new("https", Domain::GitHub, "sgoudham/git-view"); let url = Url::new("https", Domain::GitHub, "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/tree/%23test%23"; let expected_final_url = "https://github.com/sgoudham/git-view/tree/%23test%23";
let mock = MockGitTrait::default(); let mock = MockGitTrait::default();

Loading…
Cancel
Save