Lines
100 %
Functions
94.74 %
Branches
//! Tests for the select-reference resolver.
use forms::Source;
use super::super::{FormOption, resolve as resolve_in};
use crate::CommandError;
const CASH: &str = "550e8400-e29b-41d4-a716-446655440001";
const OTHER_CASH: &str = "550e8400-e29b-41d4-a716-446655440002";
const ODD: &str = "550e8400-e29b-41d4-a716-446655440003";
fn option(id: &str, label: &str, alias: &str) -> FormOption {
FormOption {
id: id.to_string(),
label: label.to_string(),
alias: alias.to_string(),
commodity: None,
}
fn options() -> Vec<FormOption> {
vec![
option(CASH, "Assets:Cash", "Cash"),
option(OTHER_CASH, "Other:Cash", "Cash"),
option(ODD, "A¦B", "A|B"),
]
fn resolve(options: &[FormOption], flag: &str, input: &str) -> Result<String, CommandError> {
resolve_in(options, Source::Accounts, flag, input)
fn message(result: Result<String, CommandError>) -> String {
result.map_or_else(|e| e.to_string(), |id| format!("resolved {id}"))
#[test]
fn an_own_uuid_passes() {
assert_eq!(
resolve(&options(), "--to", CASH).ok().as_deref(),
Some(CASH)
);
fn an_exact_label_maps_to_its_id() {
let result = resolve(&options(), "--to", "Other:Cash");
assert_eq!(result.ok().as_deref(), Some(OTHER_CASH));
fn pipe_and_broken_bar_both_map() {
for input in ["A|B", "A¦B"] {
let result = resolve(&options(), "--to", input);
assert_eq!(result.ok().as_deref(), Some(ODD), "{input}");
fn a_unique_alias_maps_to_its_id() {
let only = [option(CASH, "Assets:Cash", "Cash")];
assert_eq!(resolve(&only, "--to", "Cash").ok().as_deref(), Some(CASH));
fn an_ambiguous_alias_lists_the_candidates() {
let text = message(resolve(&options(), "--parent", "Cash"));
assert!(text.contains("--parent"), "{text}");
assert!(text.contains("Assets:Cash, Other:Cash"), "{text}");
fn unknown_input_names_the_flag() {
let text = message(resolve(&options(), "--row to", "Nowhere"));
assert!(
text.contains("--row to") && text.contains("Nowhere"),
"{text}"
fn a_uuid_that_is_not_an_option_is_unknown() {
let text = message(resolve(
&options(),
"--to",
"550e8400-e29b-41d4-a716-446655449999",
));
assert!(text.contains("matches no entry"), "{text}");
fn a_path_label_matches_its_pipe_alias_both_ways() {
let options = [option(ODD, "Parent:A¦B", "A|B")];
let result = resolve(&options, "--to", input);
fn commodities_match_case_insensitively() {
let options = [option(CASH, "EUR", "EUR"), option(ODD, "USD", "USD")];
let result = resolve_in(&options, Source::Commodities, "--row from-commodity", "eur");
assert_eq!(result.ok().as_deref(), Some(CASH));
let accounts = resolve(&options, "--to", "eur");
assert!(accounts.is_err(), "accounts stay case-sensitive");
fn a_case_only_commodity_alias_pair_is_ambiguous() {
let options = [
option(CASH, "USD ‹5500›", "USD"),
option(OTHER_CASH, "usd ‹5501›", "usd"),
];
let result = resolve_in(&options, Source::Commodities, "--row from-commodity", "Usd");
let text = message(result);
text.contains("ambiguous") && text.contains("USD ‹5500›, usd ‹5501›"),
fn map_values_names_the_field_with_the_given_prefix() {
let mut values = forms::FormValues::default();
values.set("target", "Nowhere");
let fetched = vec![(Source::Commodities, vec![option(CASH, "USD", "USD")])];
let name = |prefix| {
super::super::map_values(&forms::REPORT_BALANCE, &values, &fetched, prefix)
.err()
.map(|e| e.to_string())
.unwrap_or_default()
};
assert!(name("--").contains("--target"), "{}", name("--"));
assert!(!name("").contains("--target"), "{}", name(""));