[READ-ONLY] Mirror of https://github.com/probablykasper/cpc. Text calculator with support for units and conversion cpc.kasper.space
calculator cli conversion converter library math package units units-converter wasm website
0

Configure Feed

Select the types of activity you want to include in your feed.

Update tests

Kasper (Jun 29, 2026, 11:56 AM +0200) ea5d4677 d647e516

+31 -20
+6 -6
src/currency.rs
··· 18 18 pub const BASE_CURRENCY: Unit = Unit::EUR; 19 19 20 20 #[derive(Deserialize, Debug)] 21 - struct CurrencyRate { 21 + pub struct CurrencyRate { 22 22 #[allow(dead_code)] 23 - date: String, 24 - base: String, 25 - quote: String, 26 - rate: serde_json::Number, 23 + pub date: String, 24 + pub base: String, 25 + pub quote: String, 26 + pub rate: serde_json::Number, 27 27 } 28 28 29 - fn set_currency_cache(rates: Vec<CurrencyRate>) -> Result<(), String> { 29 + pub fn set_currency_cache(rates: Vec<CurrencyRate>) -> Result<(), String> { 30 30 let mut cache = HashMap::with_capacity(rates.len() + 1); 31 31 32 32 // Add EUR as base
+25 -14
src/units.rs
··· 1014 1014 1015 1015 #[cfg(test)] 1016 1016 mod tests { 1017 + use std::str::FromStr; 1018 + 1017 1019 use fastnum::decimal::Context; 1018 1020 1019 1021 use super::*; ··· 1419 1421 } 1420 1422 1421 1423 #[test] 1424 + fn test_currency() { 1425 + use crate::currency::{CurrencyRate, set_currency_cache}; 1426 + use serde_json::Number; 1427 + 1428 + set_currency_cache(vec![CurrencyRate { 1429 + date: "2000-01-01".to_string(), 1430 + base: "EUR".to_string(), 1431 + quote: "NOK".to_string(), 1432 + rate: Number::from_str("11.2839").unwrap(), 1433 + }]) 1434 + .unwrap(); 1435 + 1436 + eval_test("1 EUR to NOK", "11.292 NOK"); 1437 + eval_test("11.292 EUR to NOK", "≈ 1.0000003236 EUR"); 1438 + eval_test("1 NOK to EUR", "≈ 0.0885583 EUR"); 1439 + eval_test("1 EUR/liter to NOK/liter", "11.292 NOK/liter"); 1440 + eval_test( 1441 + "1 EUR/gallon to NOK/liter", 1442 + "≈ 2.98303081522821190646982991481066303987 NOK/liter", 1443 + ); 1444 + } 1445 + 1446 + #[test] 1422 1447 fn test_unit_evals() { 1423 1448 eval_test("100kg*sqm / 2s^2", "50j"); 1424 1449 eval_test("3.6km/1h", "3.6 kph"); ··· 1445 1470 eval_test("0 C to K", "273.15 K"); 1446 1471 eval_test("8 megabytes per second * 1 minute", "480mb"); 1447 1472 eval_test("8 megaFLOP per second * 1 minute", "480megaFLOP"); 1448 - 1449 - // Currency unit tests - these test parsing only 1450 - // Currency conversions require network access, so they're tested separately 1451 - #[cfg(not(target_arch = "wasm32"))] // Skip network-dependent tests on WASM 1452 - { 1453 - // Test that currency units can be parsed and basic conversions work 1454 - let result = crate::eval("100 USD", true, false).unwrap(); 1455 - assert_eq!(result.unit, vec![(USD, 1)]); 1456 - 1457 - let result = crate::eval("1 EUR", true, false).unwrap(); 1458 - assert_eq!(result.unit, vec![(EUR, 1)]); 1459 - 1460 - // Skip currency conversion tests as they require network access 1461 - } 1462 1473 } 1463 1474 }