use std::{
ffi::OsStr,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
};
use super::{ExclusiveOption, SubCommand, Unselected};
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;
#[cfg(not(feature = "lt2016_1"))]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ShowPrevious;
#[cfg_attr(
feature = "lt2016_1",
doc = "The set form: `p4 [g-opts] counter [-f] countername value`."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "The set form: `p4 [g-opts] counter [-f -v] countername value`."
)]
#[cfg_attr(
feature = "lt2016_1",
doc = "The `B` type parameter only ever takes [`Unselected`]; `-v` is not available in this version."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "The `B` type parameter tracks whether `-v` has been supplied: [`Unselected`] does not display the previous value; [`ShowPrevious`] does."
)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetMode<B = Unselected> {
force: bool,
show_previous: B,
}
impl ExclusiveOption for SetMode<Unselected> {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
}
}
#[cfg(not(feature = "lt2016_1"))]
impl ExclusiveOption for SetMode<ShowPrevious> {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
command.arg("-v");
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DeleteMode {
force: bool,
}
impl ExclusiveOption for DeleteMode {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
command.arg("-d");
}
}
#[cfg_attr(
feature = "lt2016_1",
doc = "The increment form: `p4 [g-opts] counter [-f] -i countername`."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "The increment form: `p4 [g-opts] counter [-f -v] -i countername`."
)]
#[cfg_attr(
feature = "lt2016_1",
doc = "The `B` type parameter only ever takes [`Unselected`]; `-v` is not available in this version."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "The `B` type parameter tracks whether `-v` has been supplied: [`Unselected`] does not display the previous value; [`ShowPrevious`] does."
)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct IncrementMode<B = Unselected> {
force: bool,
show_previous: B,
}
impl ExclusiveOption for IncrementMode<Unselected> {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
command.arg("-i");
}
}
#[cfg(not(feature = "lt2016_1"))]
impl ExclusiveOption for IncrementMode<ShowPrevious> {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
command.arg("-v");
command.arg("-i");
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MultiMode {
force: bool,
}
impl ExclusiveOption for MultiMode {
fn inject_args(&self, command: &mut Command) {
if self.force {
command.arg("-f");
}
command.arg("-m");
}
}
#[cfg(not(feature = "lt2016_1"))]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CASMode {
old_value: String,
new_value: String,
}
#[cfg(not(feature = "lt2016_1"))]
impl ExclusiveOption for CASMode {
fn inject_args(&self, command: &mut Command) {
command
.arg("--from")
.arg(&self.old_value)
.arg("--to")
.arg(&self.new_value);
}
}
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "The compare-and-set form can be used as a building block for higher-level tools and processes."
)]
#[cfg_attr(
feature = "lt2016_1",
doc = "p4 [g-opts] counter [-f] countername value"
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "p4 [g-opts] counter [-f -v] countername value"
)]
#[cfg_attr(feature = "lt2016_1", doc = "p4 [g-opts] counter [-f] -i countername")]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "p4 [g-opts] counter [-f -v] -i countername"
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "p4 [g-opts] counter --from oldvalue --to newvalue countername"
)]
#[cfg_attr(
feature = "lt2016_1",
doc = "[`Self::delete`], [`Self::increment`], or [`Self::multi`]:"
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "[`Self::show_previous`], [`Self::delete`], [`Self::increment`], [`Self::multi`], or [`Self::cas`]:"
)]
#[cfg_attr(
feature = "lt2016_1",
doc = "- [`SetMode`] (entered via [`Self::set`] or [`Self::force`]): set or create the counter; can transition to [`DeleteMode`], [`IncrementMode`], or [`MultiMode`] preserving `-f`."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "- [`SetMode`] (entered via [`Self::set`], [`Self::force`], or [`Self::show_previous`]): set or create the counter; can transition to [`DeleteMode`], [`IncrementMode`], or [`MultiMode`] preserving `-f`, and `Counter::show_previous` supplies `-v` by transitioning to the [`ShowPrevious`] state."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "- [`CASMode`] (`--from oldvalue --to newvalue`, entered via [`Self::cas`]): set the counter only if its current value matches."
)]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "`-v` cannot be combined with `-d` or `-m`: [`DeleteMode`] and [`MultiMode`] are only reachable from [`SetMode`] while it is in the [`Unselected`] state."
)]
#[derive(Debug, Clone, Default)]
pub struct Counter<M = Unselected> {
bin: PathBuf,
global_opts: GlobalOpts,
mode: M,
}
impl Counter<Unselected> {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
..Default::default()
}
}
#[cfg_attr(feature = "lt2016_1", doc = "Use `Counter::force` to supply `-f`.")]
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "Use `Counter::force` to supply `-f`, or `Counter::show_previous` to supply `-v`."
)]
pub fn set(self) -> Counter<SetMode<Unselected>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: SetMode::default(),
}
}
pub fn force(self, v: bool) -> Counter<SetMode<Unselected>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: SetMode {
force: v,
show_previous: Unselected,
},
}
}
#[cfg(not(feature = "lt2016_1"))]
pub fn show_previous(self) -> Counter<SetMode<ShowPrevious>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: SetMode {
force: false,
show_previous: ShowPrevious,
},
}
}
pub fn delete(self) -> Counter<DeleteMode> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: DeleteMode::default(),
}
}
pub fn increment(self) -> Counter<IncrementMode<Unselected>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: IncrementMode::default(),
}
}
pub fn multi(self) -> Counter<MultiMode> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: MultiMode::default(),
}
}
#[cfg(not(feature = "lt2016_1"))]
pub fn cas(
self,
old_value: impl Into<String>,
new_value: impl Into<String>,
) -> Counter<CASMode> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: CASMode {
old_value: old_value.into(),
new_value: new_value.into(),
},
}
}
}
impl<M> Counter<M> {
pub fn get_bin(&self) -> &Path {
&self.bin
}
pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
self.bin = bin.into();
self
}
pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
self.set_bin(bin);
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn get_global_opts(&self) -> &GlobalOpts {
&self.global_opts
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
self.global_opts = v;
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn global_opts(mut self, v: GlobalOpts) -> Self {
self.global_opts = v;
self
}
}
impl Counter<SetMode<Unselected>> {
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn get_force(&self) -> bool {
self.mode.force
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn set_force(&mut self, v: bool) -> &mut Self {
self.mode.force = v;
self
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn force(mut self, v: bool) -> Self {
self.mode.force = v;
self
}
#[cfg(not(feature = "lt2016_1"))]
pub fn show_previous(self) -> Counter<SetMode<ShowPrevious>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: SetMode {
force: self.mode.force,
show_previous: ShowPrevious,
},
}
}
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "This transition is unavailable from the [`ShowPrevious`] state because `-v` cannot be combined with `-d`."
)]
pub fn delete(self) -> Counter<DeleteMode> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: DeleteMode {
force: self.mode.force,
},
}
}
pub fn increment(self) -> Counter<IncrementMode<Unselected>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: IncrementMode {
force: self.mode.force,
show_previous: Unselected,
},
}
}
#[cfg_attr(
not(feature = "lt2016_1"),
doc = "This transition is unavailable from the [`ShowPrevious`] state because `-v` cannot be combined with `-m`."
)]
pub fn multi(self) -> Counter<MultiMode> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: MultiMode {
force: self.mode.force,
},
}
}
}
#[cfg(not(feature = "lt2016_1"))]
impl Counter<SetMode<ShowPrevious>> {
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn get_force(&self) -> bool {
self.mode.force
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn set_force(&mut self, v: bool) -> &mut Self {
self.mode.force = v;
self
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn force(mut self, v: bool) -> Self {
self.mode.force = v;
self
}
pub fn increment(self) -> Counter<IncrementMode<ShowPrevious>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: IncrementMode {
force: self.mode.force,
show_previous: ShowPrevious,
},
}
}
}
impl Counter<DeleteMode> {
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn get_force(&self) -> bool {
self.mode.force
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn set_force(&mut self, v: bool) -> &mut Self {
self.mode.force = v;
self
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn force(mut self, v: bool) -> Self {
self.mode.force = v;
self
}
}
impl<B> Counter<IncrementMode<B>> {
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn get_force(&self) -> bool {
self.mode.force
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn set_force(&mut self, v: bool) -> &mut Self {
self.mode.force = v;
self
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn force(mut self, v: bool) -> Self {
self.mode.force = v;
self
}
}
impl Counter<IncrementMode<Unselected>> {
#[cfg(not(feature = "lt2016_1"))]
pub fn show_previous(self) -> Counter<IncrementMode<ShowPrevious>> {
Counter {
bin: self.bin,
global_opts: self.global_opts,
mode: IncrementMode {
force: self.mode.force,
show_previous: ShowPrevious,
},
}
}
}
impl Counter<MultiMode> {
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn get_force(&self) -> bool {
self.mode.force
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn set_force(&mut self, v: bool) -> &mut Self {
self.mode.force = v;
self
}
#[cfg_attr(
all(feature = "lt2021_2", not(feature = "lt2016_1")),
doc = "Only operators or super users can use this flag."
)]
pub fn force(mut self, v: bool) -> Self {
self.mode.force = v;
self
}
}
#[cfg(not(feature = "lt2016_1"))]
impl Counter<CASMode> {
pub fn get_old_value(&self) -> &str {
&self.mode.old_value
}
pub fn set_old_value(&mut self, v: impl Into<String>) -> &mut Self {
self.mode.old_value = v.into();
self
}
pub fn old_value(mut self, v: impl Into<String>) -> Self {
self.mode.old_value = v.into();
self
}
pub fn get_new_value(&self) -> &str {
&self.mode.new_value
}
pub fn set_new_value(&mut self, v: impl Into<String>) -> &mut Self {
self.mode.new_value = v.into();
self
}
pub fn new_value(mut self, v: impl Into<String>) -> Self {
self.mode.new_value = v.into();
self
}
}
impl<M: ExclusiveOption> SubCommand for Counter<M> {
fn name(&self) -> &str {
"counter"
}
fn inject_local_args(&self, command: &mut Command) {
self.mode.inject_args(command);
}
fn global_opts(&self) -> Option<&GlobalOpts> {
Some(&self.global_opts)
}
}
impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Counter<Unselected> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (countername,): (I,)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.arg(countername)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<S, A, B> ParameterizedSpawn<(A, B)> for Counter<SetMode<S>>
where
SetMode<S>: ExclusiveOption,
A: AsRef<OsStr>,
B: AsRef<OsStr>,
{
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (countername, value): (A, B)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.arg(countername)
.arg(value)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Counter<DeleteMode> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (countername,): (I,)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.arg(countername)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<S, I> ParameterizedSpawn<(I,)> for Counter<IncrementMode<S>>
where
IncrementMode<S>: ExclusiveOption,
I: AsRef<OsStr>,
{
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (countername,): (I,)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.arg(countername)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<S, I> ParameterizedSpawn<(S,)> for Counter<MultiMode>
where
S: IntoIterator<Item = I>,
I: AsRef<OsStr>,
{
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (pairs,): (S,)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.args(pairs)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
#[cfg(not(feature = "lt2016_1"))]
impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for Counter<CASMode> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (countername,): (I,)) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.arg(countername)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::args_of;
#[test]
fn bin_accessors_update_executable_path() {
let mut command = Counter::new("p4", GlobalOpts::new());
assert_eq!(command.get_bin(), Path::new("p4"));
command.set_bin("/usr/local/bin/p4");
assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
let command = command.bin("p4.exe");
assert_eq!(command.get_bin(), Path::new("p4.exe"));
}
#[test]
fn query_form_without_options() {
let counter = Counter::new("p4", GlobalOpts::new());
assert_eq!(args_of(&counter.setup_command("p4")), ["counter"]);
}
#[test]
fn set_form_from_unselected() {
let counter = Counter::new("p4", GlobalOpts::new()).set();
assert_eq!(args_of(&counter.setup_command("p4")), ["counter"]);
}
#[test]
fn set_form_with_force() {
let counter = Counter::new("p4", GlobalOpts::new()).force(true);
assert_eq!(args_of(&counter.setup_command("p4")), ["counter", "-f"]);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn set_form_with_show_previous() {
let counter = Counter::new("p4", GlobalOpts::new()).show_previous();
assert_eq!(args_of(&counter.setup_command("p4")), ["counter", "-v"]);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn set_form_with_force_and_show_previous() {
let counter = Counter::new("p4", GlobalOpts::new())
.set()
.force(true)
.show_previous();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-v"]
);
}
#[test]
fn delete_form_from_unselected() {
let counter = Counter::new("p4", GlobalOpts::new()).delete();
assert_eq!(args_of(&counter.setup_command("p4")), ["counter", "-d"]);
}
#[test]
fn delete_form_preserves_force_from_set() {
let counter = Counter::new("p4", GlobalOpts::new()).force(true).delete();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-d"]
);
}
#[test]
fn delete_form_force_can_be_set_directly() {
let counter = Counter::new("p4", GlobalOpts::new()).delete().force(true);
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-d"]
);
}
#[test]
fn increment_form_from_unselected() {
let counter = Counter::new("p4", GlobalOpts::new()).increment();
assert_eq!(args_of(&counter.setup_command("p4")), ["counter", "-i"]);
}
#[test]
fn increment_form_preserves_force_from_set() {
let counter = Counter::new("p4", GlobalOpts::new())
.force(true)
.increment();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-i"]
);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn increment_form_preserves_force_and_show_previous() {
let counter = Counter::new("p4", GlobalOpts::new())
.show_previous()
.force(true)
.increment();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-v", "-i"]
);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn increment_form_show_previous_can_be_set_directly() {
let counter = Counter::new("p4", GlobalOpts::new())
.increment()
.show_previous();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-v", "-i"]
);
}
#[test]
fn multi_form_without_force() {
let counter = Counter::new("p4", GlobalOpts::new()).multi();
assert_eq!(args_of(&counter.setup_command("p4")), ["counter", "-m"]);
}
#[test]
fn multi_form_preserves_force_from_set() {
let counter = Counter::new("p4", GlobalOpts::new()).force(true).multi();
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-m"]
);
}
#[test]
fn multi_form_with_force() {
let counter = Counter::new("p4", GlobalOpts::new()).multi().force(true);
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "-f", "-m"]
);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn cas_form_injects_from_and_to_before_the_name() {
let counter = Counter::new("p4", GlobalOpts::new()).cas("3", "4");
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "--from", "3", "--to", "4"]
);
}
#[cfg(not(feature = "lt2016_1"))]
#[test]
fn cas_value_accessors_replace_values() {
let mut counter = Counter::new("p4", GlobalOpts::new()).cas("3", "4");
assert_eq!(counter.get_old_value(), "3");
assert_eq!(counter.get_new_value(), "4");
counter.set_old_value("5").set_new_value("6");
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "--from", "5", "--to", "6"]
);
let counter = counter.old_value("7").new_value("8");
assert_eq!(
args_of(&counter.setup_command("p4")),
["counter", "--from", "7", "--to", "8"]
);
}
#[test]
fn global_opts_are_injected_once() {
let global_opts = GlobalOpts::new().port("localhost:1666").quiet_mode(true);
let counter = Counter::new("p4", global_opts.clone()).delete();
assert_eq!(
args_of(&counter.setup_command("p4")),
["-p", "localhost:1666", "-q", "counter", "-d"]
);
let counter = Counter::new("p4", global_opts.clone()).multi().force(true);
assert_eq!(
args_of(&counter.setup_command("p4")),
["-p", "localhost:1666", "-q", "counter", "-f", "-m"]
);
#[cfg(not(feature = "lt2016_1"))]
let counter = Counter::new("p4", global_opts).cas("3", "4");
#[cfg(not(feature = "lt2016_1"))]
assert_eq!(
args_of(&counter.setup_command("p4")),
[
"-p",
"localhost:1666",
"-q",
"counter",
"--from",
"3",
"--to",
"4"
]
);
}
#[test]
fn global_opts_accessors_replace_the_stored_options() {
let mut counter = Counter::new("p4", GlobalOpts::new());
counter.set_global_opts(GlobalOpts::new().port("localhost:1666"));
assert_eq!(
args_of(&counter.setup_command("p4")),
["-p", "localhost:1666", "counter"]
);
let counter = counter.global_opts(GlobalOpts::new().quiet_mode(true));
assert_eq!(args_of(&counter.setup_command("p4")), ["-q", "counter"]);
}
}