// SPDX-FileCopyrightText: 2023 - 2024 Markus Haug (Korrat)
//
// SPDX-License-Identifier: EUPL-1.2

use core::fmt::Debug;
use core::hash::Hash;
use core::str::FromStr;

use beancount_amount::Amount;
use static_assertions::assert_impl_all;
use test_case::test_case;

assert_impl_all!(
    Amount: Copy,
    Debug,
    FromStr,
    Hash,
    PartialOrd,
    TryFrom<&'static str>
);

#[test_case("0 EUR"; "zero")]
#[test_case("15 USD"; "integer amount")]
#[test_case("0.30 CAD"; "fractional amount")]
#[test_case("0.0000067 ETH"; "arbitrary precision")]
#[test_case("-16.93 USD"; "negative amount")]
fn parse_when_valid(amount: &str) {
    let account = Amount::try_from(amount).unwrap();
    assert_eq!(account.to_string(), amount);
}

#[test_case(" EUR"; "missing units")]
#[test_case("15 "; "missing commodity")]
#[test_case("15,000.00 USD"; "thousands separator")]
#[test_case("15000,00 USD"; "invalid decimal separator")]
fn do_not_parse_when_invalid(amount: &str) {
    Amount::try_from(amount).unwrap_err();
}