Rename Euros::from_cents to from_cents_as_i64
The constructor takes a raw i64 count of cents. Naming the unit at the call site keeps it distinct from the Decimal-based constructor added next, where the argument is an amount in euros rather than a cent count. Also fixes a typo in a test name. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
85b0836d6e
commit
4506231f02
2 changed files with 21 additions and 21 deletions
|
|
@ -8,7 +8,7 @@ pub struct Euros {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Euros {
|
impl Euros {
|
||||||
pub const fn from_cents(value_as_cents: i64) -> Self {
|
pub const fn from_cents_as_i64(value_as_cents: i64) -> Self {
|
||||||
Euros { value_as_cents }
|
Euros { value_as_cents }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,13 +33,13 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_cents_to_euros_to_cents_stays_equals() {
|
fn from_cents_to_euros_to_cents_stays_equals() {
|
||||||
let euros = Euros::from_cents(123_456_00);
|
let euros = Euros::from_cents_as_i64(123_456_00);
|
||||||
assert_eq!(euros.as_cents(), 123_456_00);
|
assert_eq!(euros.as_cents(), 123_456_00);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_positive_cents_to_decimal_has_scale_2_and_correct_mantissa() {
|
fn from_positive_cents_to_decimal_has_scale_2_and_correct_mantissa() {
|
||||||
let euros = Euros::from_cents(123_456_78);
|
let euros = Euros::from_cents_as_i64(123_456_78);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
euros.as_decimal().scale(),
|
euros.as_decimal().scale(),
|
||||||
2,
|
2,
|
||||||
|
|
@ -54,7 +54,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_negative_cents_to_decimal_has_correct_scale_and_mantissa() {
|
fn from_negative_cents_to_decimal_has_correct_scale_and_mantissa() {
|
||||||
let euros = Euros::from_cents(-123_456_78);
|
let euros = Euros::from_cents_as_i64(-123_456_78);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
euros.as_decimal().scale(),
|
euros.as_decimal().scale(),
|
||||||
2,
|
2,
|
||||||
|
|
@ -69,7 +69,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_max_i64_cents_to_decimal_has_correct_scale_and_mantissa() {
|
fn from_max_i64_cents_to_decimal_has_correct_scale_and_mantissa() {
|
||||||
let euros = Euros::from_cents(i64::MAX);
|
let euros = Euros::from_cents_as_i64(i64::MAX);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
euros.as_decimal().scale(),
|
euros.as_decimal().scale(),
|
||||||
2,
|
2,
|
||||||
|
|
@ -84,26 +84,26 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_0_cents_to_decimal_stays_zero() {
|
fn from_0_cents_to_decimal_stays_zero() {
|
||||||
let euros = Euros::from_cents(0);
|
let euros = Euros::from_cents_as_i64(0);
|
||||||
assert_eq!(euros.as_decimal().scale(), 2);
|
assert_eq!(euros.as_decimal().scale(), 2);
|
||||||
assert_eq!(euros.as_decimal(), Decimal::ZERO);
|
assert_eq!(euros.as_decimal(), Decimal::ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn standard_euros_prints_correctly() {
|
fn standard_euros_prints_correctly() {
|
||||||
let euros = Euros::from_cents(123_45);
|
let euros = Euros::from_cents_as_i64(123_45);
|
||||||
assert_eq!(format!("{}", euros), "123.45 €");
|
assert_eq!(format!("{}", euros), "123.45 €");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn euros_without_cents_prints_with_trailing_zeros() {
|
fn euros_without_cents_prints_with_trailing_zeros() {
|
||||||
let euros = Euros::from_cents(123_00);
|
let euros = Euros::from_cents_as_i64(123_00);
|
||||||
assert_eq!(format!("{}", euros), "123.00 €");
|
assert_eq!(format!("{}", euros), "123.00 €");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn euros_with_negative_amount_print_with_minus() {
|
fn euros_with_negative_amount_prints_with_minus() {
|
||||||
let euros = Euros::from_cents(-123_00);
|
let euros = Euros::from_cents_as_i64(-123_00);
|
||||||
assert_eq!(format!("{}", euros), "-123.00 €");
|
assert_eq!(format!("{}", euros), "-123.00 €");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl LoanOffer {
|
||||||
Err(LoanOfferError::NegativeInterestRate(annual_rate_in_percent))
|
Err(LoanOfferError::NegativeInterestRate(annual_rate_in_percent))
|
||||||
} else if loan_duration_in_months == 0 {
|
} else if loan_duration_in_months == 0 {
|
||||||
Err(LoanOfferError::ZeroLoanDuration)
|
Err(LoanOfferError::ZeroLoanDuration)
|
||||||
} else if borrowed_capital <= Euros::from_cents(0) {
|
} else if borrowed_capital <= Euros::from_cents_as_i64(0) {
|
||||||
Err(LoanOfferError::NonPositiveBorrowedCapital(borrowed_capital))
|
Err(LoanOfferError::NonPositiveBorrowedCapital(borrowed_capital))
|
||||||
} else {
|
} else {
|
||||||
Ok(LoanOffer {
|
Ok(LoanOffer {
|
||||||
|
|
@ -48,7 +48,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_negative_interest_rate_returns_negative_interest_rate_error() {
|
fn creating_loan_offer_with_negative_interest_rate_returns_negative_interest_rate_error() {
|
||||||
let ret = LoanOffer::new(Euros::from_cents(123), Decimal::new(-45, 2), 5);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(123), Decimal::new(-45, 2), 5);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ret,
|
ret,
|
||||||
Err(LoanOfferError::NegativeInterestRate(Decimal::new(-45, 2)))
|
Err(LoanOfferError::NegativeInterestRate(Decimal::new(-45, 2)))
|
||||||
|
|
@ -57,18 +57,18 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_null_loan_duration_returns_zero_loan_duration_error() {
|
fn creating_loan_offer_with_null_loan_duration_returns_zero_loan_duration_error() {
|
||||||
let ret = LoanOffer::new(Euros::from_cents(123), Decimal::new(45, 2), 0);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(123), Decimal::new(45, 2), 0);
|
||||||
assert_eq!(ret, Err(LoanOfferError::ZeroLoanDuration));
|
assert_eq!(ret, Err(LoanOfferError::ZeroLoanDuration));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_negative_borrowed_capital_returns_non_positive_borrowed_capital_error()
|
fn creating_loan_offer_with_negative_borrowed_capital_returns_non_positive_borrowed_capital_error()
|
||||||
{
|
{
|
||||||
let ret = LoanOffer::new(Euros::from_cents(-1), Decimal::new(45, 2), 15);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(-1), Decimal::new(45, 2), 15);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ret,
|
ret,
|
||||||
Err(LoanOfferError::NonPositiveBorrowedCapital(
|
Err(LoanOfferError::NonPositiveBorrowedCapital(
|
||||||
Euros::from_cents(-1)
|
Euros::from_cents_as_i64(-1)
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -76,22 +76,22 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_null_borrowed_capital_returns_non_positive_borrowed_capital_error()
|
fn creating_loan_offer_with_null_borrowed_capital_returns_non_positive_borrowed_capital_error()
|
||||||
{
|
{
|
||||||
let ret = LoanOffer::new(Euros::from_cents(0), Decimal::new(45, 2), 15);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(0), Decimal::new(45, 2), 15);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ret,
|
ret,
|
||||||
Err(LoanOfferError::NonPositiveBorrowedCapital(
|
Err(LoanOfferError::NonPositiveBorrowedCapital(
|
||||||
Euros::from_cents(0)
|
Euros::from_cents_as_i64(0)
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_correct_inputs_works() {
|
fn creating_loan_offer_with_correct_inputs_works() {
|
||||||
let ret = LoanOffer::new(Euros::from_cents(10), Decimal::new(45, 2), 15);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(10), Decimal::new(45, 2), 15);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ret,
|
ret,
|
||||||
Ok(LoanOffer {
|
Ok(LoanOffer {
|
||||||
borrowed_capital: Euros::from_cents(10),
|
borrowed_capital: Euros::from_cents_as_i64(10),
|
||||||
annual_rate_in_percent: Decimal::new(45, 2),
|
annual_rate_in_percent: Decimal::new(45, 2),
|
||||||
loan_duration_in_months: 15
|
loan_duration_in_months: 15
|
||||||
})
|
})
|
||||||
|
|
@ -100,11 +100,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn creating_loan_offer_with_null_interest_rate_works() {
|
fn creating_loan_offer_with_null_interest_rate_works() {
|
||||||
let ret = LoanOffer::new(Euros::from_cents(10), Decimal::ZERO, 15);
|
let ret = LoanOffer::new(Euros::from_cents_as_i64(10), Decimal::ZERO, 15);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ret,
|
ret,
|
||||||
Ok(LoanOffer {
|
Ok(LoanOffer {
|
||||||
borrowed_capital: Euros::from_cents(10),
|
borrowed_capital: Euros::from_cents_as_i64(10),
|
||||||
annual_rate_in_percent: Decimal::ZERO,
|
annual_rate_in_percent: Decimal::ZERO,
|
||||||
loan_duration_in_months: 15
|
loan_duration_in_months: 15
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue