fix: number format (#2445)

This commit is contained in:
Nathan.fooo
2023-05-03 16:47:02 +08:00
committed by GitHub
parent 76b94e363e
commit 1f187a3917
3 changed files with 240 additions and 393 deletions

View File

@ -8,7 +8,7 @@ edition = "2018"
name = "dart_ffi" name = "dart_ffi"
# this value will change depending on the target os # this value will change depending on the target os
# default static lib # default static lib
crate-type = ["staticlib"] crate-type = ["cdylib"]
[dependencies] [dependencies]

View File

@ -13,13 +13,23 @@ mod tests {
fn number_type_option_invalid_input_test() { fn number_type_option_invalid_input_test() {
let type_option = NumberTypeOption::default(); let type_option = NumberTypeOption::default();
let field_type = FieldType::Number; let field_type = FieldType::Number;
let field_rev = FieldBuilder::from_field_type(field_type.clone()).build(); let field = FieldBuilder::from_field_type(field_type.clone()).build();
// Input is empty String // Input is empty String
assert_number(&type_option, "", "", &field_type, &field_rev); assert_number(&type_option, "", "", &field_type, &field);
// Input is letter // Input is letter
assert_number(&type_option, "abc", "", &field_type, &field_rev); assert_number(&type_option, "abc", "", &field_type, &field);
assert_number(&type_option, "-123", "-123", &field_type, &field);
assert_number(&type_option, "abc-123", "-123", &field_type, &field);
assert_number(&type_option, "+123", "123", &field_type, &field);
assert_number(&type_option, "0.2", "0.2", &field_type, &field);
assert_number(&type_option, "-0.2", "-0.2", &field_type, &field);
} }
/// Testing the strip_currency_symbol function. It should return the string without the input symbol. /// Testing the strip_currency_symbol function. It should return the string without the input symbol.
@ -36,120 +46,110 @@ mod tests {
fn number_type_option_format_number_test() { fn number_type_option_format_number_test() {
let mut type_option = NumberTypeOption::default(); let mut type_option = NumberTypeOption::default();
let field_type = FieldType::Number; let field_type = FieldType::Number;
let field_rev = FieldBuilder::from_field_type(field_type.clone()).build(); let field = FieldBuilder::from_field_type(field_type.clone()).build();
for format in NumberFormat::iter() { for format in NumberFormat::iter() {
type_option.format = format; type_option.format = format;
match format { match format {
NumberFormat::Num => { NumberFormat::Num => {
assert_number(&type_option, "18443", "18443", &field_type, &field_rev); assert_number(&type_option, "18443", "18443", &field_type, &field);
}, },
NumberFormat::USD => { NumberFormat::USD => {
assert_number(&type_option, "18443", "$18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "$18,443", &field_type, &field);
}, },
NumberFormat::CanadianDollar => { NumberFormat::CanadianDollar => {
assert_number(&type_option, "18443", "CA$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "CA$18,443", &field_type, &field)
},
NumberFormat::EUR => {
assert_number(&type_option, "18443", "€18.443", &field_type, &field_rev)
},
NumberFormat::Pound => {
assert_number(&type_option, "18443", "£18,443", &field_type, &field_rev)
}, },
NumberFormat::EUR => assert_number(&type_option, "18443", "€18.443", &field_type, &field),
NumberFormat::Pound => assert_number(&type_option, "18443", "£18,443", &field_type, &field),
NumberFormat::Yen => { NumberFormat::Yen => {
assert_number(&type_option, "18443", "¥18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "¥18,443", &field_type, &field);
}, },
NumberFormat::Ruble => { NumberFormat::Ruble => {
assert_number(&type_option, "18443", "18.443RUB", &field_type, &field_rev) assert_number(&type_option, "18443", "18.443RUB", &field_type, &field)
},
NumberFormat::Rupee => {
assert_number(&type_option, "18443", "₹18,443", &field_type, &field_rev)
},
NumberFormat::Won => {
assert_number(&type_option, "18443", "₩18,443", &field_type, &field_rev)
}, },
NumberFormat::Rupee => assert_number(&type_option, "18443", "₹18,443", &field_type, &field),
NumberFormat::Won => assert_number(&type_option, "18443", "₩18,443", &field_type, &field),
NumberFormat::Yuan => { NumberFormat::Yuan => {
assert_number(&type_option, "18443", "CN¥18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "CN¥18,443", &field_type, &field);
}, },
NumberFormat::Real => { NumberFormat::Real => {
assert_number(&type_option, "18443", "R$18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "R$18,443", &field_type, &field);
}, },
NumberFormat::Lira => { NumberFormat::Lira => {
assert_number(&type_option, "18443", "TRY18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "TRY18.443", &field_type, &field)
}, },
NumberFormat::Rupiah => { NumberFormat::Rupiah => {
assert_number(&type_option, "18443", "IDR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "IDR18,443", &field_type, &field)
}, },
NumberFormat::Franc => { NumberFormat::Franc => {
assert_number(&type_option, "18443", "CHF18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "CHF18,443", &field_type, &field)
}, },
NumberFormat::HongKongDollar => { NumberFormat::HongKongDollar => {
assert_number(&type_option, "18443", "HZ$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "HZ$18,443", &field_type, &field)
}, },
NumberFormat::NewZealandDollar => { NumberFormat::NewZealandDollar => {
assert_number(&type_option, "18443", "NZ$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "NZ$18,443", &field_type, &field)
}, },
NumberFormat::Krona => { NumberFormat::Krona => {
assert_number(&type_option, "18443", "18 443SEK", &field_type, &field_rev) assert_number(&type_option, "18443", "18 443SEK", &field_type, &field)
}, },
NumberFormat::NorwegianKrone => { NumberFormat::NorwegianKrone => {
assert_number(&type_option, "18443", "18,443NOK", &field_type, &field_rev) assert_number(&type_option, "18443", "18,443NOK", &field_type, &field)
}, },
NumberFormat::MexicanPeso => { NumberFormat::MexicanPeso => {
assert_number(&type_option, "18443", "MX$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "MX$18,443", &field_type, &field)
}, },
NumberFormat::Rand => { NumberFormat::Rand => {
assert_number(&type_option, "18443", "ZAR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "ZAR18,443", &field_type, &field)
}, },
NumberFormat::NewTaiwanDollar => { NumberFormat::NewTaiwanDollar => {
assert_number(&type_option, "18443", "NT$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "NT$18,443", &field_type, &field)
}, },
NumberFormat::DanishKrone => { NumberFormat::DanishKrone => {
assert_number(&type_option, "18443", "18.443DKK", &field_type, &field_rev) assert_number(&type_option, "18443", "18.443DKK", &field_type, &field)
}, },
NumberFormat::Baht => { NumberFormat::Baht => {
assert_number(&type_option, "18443", "THB18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "THB18,443", &field_type, &field)
}, },
NumberFormat::Forint => { NumberFormat::Forint => {
assert_number(&type_option, "18443", "18 443HUF", &field_type, &field_rev) assert_number(&type_option, "18443", "18 443HUF", &field_type, &field)
}, },
NumberFormat::Koruna => { NumberFormat::Koruna => {
assert_number(&type_option, "18443", "18 443CZK", &field_type, &field_rev) assert_number(&type_option, "18443", "18 443CZK", &field_type, &field)
}, },
NumberFormat::Shekel => { NumberFormat::Shekel => {
assert_number(&type_option, "18443", "18 443Kč", &field_type, &field_rev) assert_number(&type_option, "18443", "18 443Kč", &field_type, &field)
}, },
NumberFormat::ChileanPeso => { NumberFormat::ChileanPeso => {
assert_number(&type_option, "18443", "CLP18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "CLP18.443", &field_type, &field)
}, },
NumberFormat::PhilippinePeso => { NumberFormat::PhilippinePeso => {
assert_number(&type_option, "18443", "₱18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "₱18,443", &field_type, &field)
}, },
NumberFormat::Dirham => { NumberFormat::Dirham => {
assert_number(&type_option, "18443", "18,443AED", &field_type, &field_rev) assert_number(&type_option, "18443", "18,443AED", &field_type, &field)
}, },
NumberFormat::ColombianPeso => { NumberFormat::ColombianPeso => {
assert_number(&type_option, "18443", "COP18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "COP18.443", &field_type, &field)
}, },
NumberFormat::Riyal => { NumberFormat::Riyal => {
assert_number(&type_option, "18443", "SAR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "SAR18,443", &field_type, &field)
}, },
NumberFormat::Ringgit => { NumberFormat::Ringgit => {
assert_number(&type_option, "18443", "MYR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "MYR18,443", &field_type, &field)
},
NumberFormat::Leu => {
assert_number(&type_option, "18443", "18.443RON", &field_type, &field_rev)
}, },
NumberFormat::Leu => assert_number(&type_option, "18443", "18.443RON", &field_type, &field),
NumberFormat::ArgentinePeso => { NumberFormat::ArgentinePeso => {
assert_number(&type_option, "18443", "ARS18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "ARS18.443", &field_type, &field)
}, },
NumberFormat::UruguayanPeso => { NumberFormat::UruguayanPeso => {
assert_number(&type_option, "18443", "UYU18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "UYU18.443", &field_type, &field)
}, },
NumberFormat::Percent => { NumberFormat::Percent => {
assert_number(&type_option, "18443", "18,443%", &field_type, &field_rev) assert_number(&type_option, "18443", "18,443%", &field_type, &field)
}, },
} }
} }
@ -160,364 +160,226 @@ mod tests {
fn number_type_option_format_str_test() { fn number_type_option_format_str_test() {
let mut type_option = NumberTypeOption::default(); let mut type_option = NumberTypeOption::default();
let field_type = FieldType::Number; let field_type = FieldType::Number;
let field_rev = FieldBuilder::from_field_type(field_type.clone()).build(); let field = FieldBuilder::from_field_type(field_type.clone()).build();
for format in NumberFormat::iter() { for format in NumberFormat::iter() {
type_option.format = format; type_option.format = format;
match format { match format {
NumberFormat::Num => { NumberFormat::Num => {
assert_number(&type_option, "18443", "18443", &field_type, &field_rev); assert_number(&type_option, "18443", "18443", &field_type, &field);
assert_number(&type_option, "0.2", "0.2", &field_type, &field_rev); assert_number(&type_option, "0.2", "0.2", &field_type, &field);
assert_number(&type_option, "", "", &field_type, &field_rev); assert_number(&type_option, "", "", &field_type, &field);
assert_number(&type_option, "abc", "", &field_type, &field_rev); assert_number(&type_option, "abc", "", &field_type, &field);
}, },
NumberFormat::USD => { NumberFormat::USD => {
assert_number(&type_option, "$18,44", "$1,844", &field_type, &field_rev); assert_number(&type_option, "$18,44", "$1,844", &field_type, &field);
assert_number(&type_option, "$0.2", "$0.2", &field_type, &field_rev); assert_number(&type_option, "$0.2", "$0.2", &field_type, &field);
assert_number(&type_option, "$1844", "$1,844", &field_type, &field_rev); assert_number(&type_option, "$1844", "$1,844", &field_type, &field);
assert_number(&type_option, "1844", "$1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "$1,844", &field_type, &field);
}, },
NumberFormat::CanadianDollar => { NumberFormat::CanadianDollar => {
assert_number( assert_number(&type_option, "CA$18,44", "CA$1,844", &field_type, &field);
&type_option, assert_number(&type_option, "CA$0.2", "CA$0.2", &field_type, &field);
"CA$18,44", assert_number(&type_option, "CA$1844", "CA$1,844", &field_type, &field);
"CA$1,844", assert_number(&type_option, "1844", "CA$1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "CA$0.2", "CA$0.2", &field_type, &field_rev);
assert_number(&type_option, "CA$1844", "CA$1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "CA$1,844", &field_type, &field_rev);
}, },
NumberFormat::EUR => { NumberFormat::EUR => {
assert_number(&type_option, "€18.44", "€18,44", &field_type, &field_rev); assert_number(&type_option, "€18.44", "€18,44", &field_type, &field);
assert_number(&type_option, "€0.5", "€0,5", &field_type, &field_rev); assert_number(&type_option, "€0.5", "€0,5", &field_type, &field);
assert_number(&type_option, "€1844", "€1.844", &field_type, &field_rev); assert_number(&type_option, "€1844", "€1.844", &field_type, &field);
assert_number(&type_option, "1844", "€1.844", &field_type, &field_rev); assert_number(&type_option, "1844", "€1.844", &field_type, &field);
}, },
NumberFormat::Pound => { NumberFormat::Pound => {
assert_number(&type_option, "£18,44", "£1,844", &field_type, &field_rev); assert_number(&type_option, "£18,44", "£1,844", &field_type, &field);
assert_number(&type_option, "£0.2", "£0.2", &field_type, &field_rev); assert_number(&type_option, "£0.2", "£0.2", &field_type, &field);
assert_number(&type_option, "£1844", "£1,844", &field_type, &field_rev); assert_number(&type_option, "£1844", "£1,844", &field_type, &field);
assert_number(&type_option, "1844", "£1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "£1,844", &field_type, &field);
}, },
NumberFormat::Yen => { NumberFormat::Yen => {
assert_number(&type_option, "¥18,44", "¥1,844", &field_type, &field_rev); assert_number(&type_option, "¥18,44", "¥1,844", &field_type, &field);
assert_number(&type_option, "¥0.2", "¥0.2", &field_type, &field_rev); assert_number(&type_option, "¥0.2", "¥0.2", &field_type, &field);
assert_number(&type_option, "¥1844", "¥1,844", &field_type, &field_rev); assert_number(&type_option, "¥1844", "¥1,844", &field_type, &field);
assert_number(&type_option, "1844", "¥1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "¥1,844", &field_type, &field);
}, },
NumberFormat::Ruble => { NumberFormat::Ruble => {
assert_number( assert_number(&type_option, "RUB18.44", "18,44RUB", &field_type, &field);
&type_option, assert_number(&type_option, "0.5", "0,5RUB", &field_type, &field);
"RUB18.44", assert_number(&type_option, "RUB1844", "1.844RUB", &field_type, &field);
"18,44RUB", assert_number(&type_option, "1844", "1.844RUB", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "0.5", "0,5RUB", &field_type, &field_rev);
assert_number(&type_option, "RUB1844", "1.844RUB", &field_type, &field_rev);
assert_number(&type_option, "1844", "1.844RUB", &field_type, &field_rev);
}, },
NumberFormat::Rupee => { NumberFormat::Rupee => {
assert_number(&type_option, "₹18,44", "₹1,844", &field_type, &field_rev); assert_number(&type_option, "₹18,44", "₹1,844", &field_type, &field);
assert_number(&type_option, "₹0.2", "₹0.2", &field_type, &field_rev); assert_number(&type_option, "₹0.2", "₹0.2", &field_type, &field);
assert_number(&type_option, "₹1844", "₹1,844", &field_type, &field_rev); assert_number(&type_option, "₹1844", "₹1,844", &field_type, &field);
assert_number(&type_option, "1844", "₹1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "₹1,844", &field_type, &field);
}, },
NumberFormat::Won => { NumberFormat::Won => {
assert_number(&type_option, "₩18,44", "₩1,844", &field_type, &field_rev); assert_number(&type_option, "₩18,44", "₩1,844", &field_type, &field);
assert_number(&type_option, "₩0.3", "₩0", &field_type, &field_rev); assert_number(&type_option, "₩0.3", "₩0", &field_type, &field);
assert_number(&type_option, "₩1844", "₩1,844", &field_type, &field_rev); assert_number(&type_option, "₩1844", "₩1,844", &field_type, &field);
assert_number(&type_option, "1844", "₩1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "₩1,844", &field_type, &field);
}, },
NumberFormat::Yuan => { NumberFormat::Yuan => {
assert_number( assert_number(&type_option, "CN¥18,44", "CN¥1,844", &field_type, &field);
&type_option, assert_number(&type_option, "CN¥0.2", "CN¥0.2", &field_type, &field);
"CN¥18,44", assert_number(&type_option, "CN¥1844", "CN¥1,844", &field_type, &field);
"CN¥1,844", assert_number(&type_option, "1844", "CN¥1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "CN¥0.2", "CN¥0.2", &field_type, &field_rev);
assert_number(&type_option, "CN¥1844", "CN¥1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "CN¥1,844", &field_type, &field_rev);
}, },
NumberFormat::Real => { NumberFormat::Real => {
assert_number(&type_option, "R$18,44", "R$1,844", &field_type, &field_rev); assert_number(&type_option, "R$18,44", "R$1,844", &field_type, &field);
assert_number(&type_option, "R$0.2", "R$0.2", &field_type, &field_rev); assert_number(&type_option, "R$0.2", "R$0.2", &field_type, &field);
assert_number(&type_option, "R$1844", "R$1,844", &field_type, &field_rev); assert_number(&type_option, "R$1844", "R$1,844", &field_type, &field);
assert_number(&type_option, "1844", "R$1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "R$1,844", &field_type, &field);
}, },
NumberFormat::Lira => { NumberFormat::Lira => {
assert_number( assert_number(&type_option, "TRY18.44", "TRY18,44", &field_type, &field);
&type_option, assert_number(&type_option, "TRY0.5", "TRY0,5", &field_type, &field);
"TRY18.44", assert_number(&type_option, "TRY1844", "TRY1.844", &field_type, &field);
"TRY18,44", assert_number(&type_option, "1844", "TRY1.844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "TRY0.5", "TRY0,5", &field_type, &field_rev);
assert_number(&type_option, "TRY1844", "TRY1.844", &field_type, &field_rev);
assert_number(&type_option, "1844", "TRY1.844", &field_type, &field_rev);
}, },
NumberFormat::Rupiah => { NumberFormat::Rupiah => {
assert_number( assert_number(&type_option, "IDR18,44", "IDR1,844", &field_type, &field);
&type_option, assert_number(&type_option, "IDR0.2", "IDR0.2", &field_type, &field);
"IDR18,44", assert_number(&type_option, "IDR1844", "IDR1,844", &field_type, &field);
"IDR1,844", assert_number(&type_option, "1844", "IDR1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "IDR0.2", "IDR0.2", &field_type, &field_rev);
assert_number(&type_option, "IDR1844", "IDR1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "IDR1,844", &field_type, &field_rev);
}, },
NumberFormat::Franc => { NumberFormat::Franc => {
assert_number( assert_number(&type_option, "CHF18,44", "CHF1,844", &field_type, &field);
&type_option, assert_number(&type_option, "CHF0.2", "CHF0.2", &field_type, &field);
"CHF18,44", assert_number(&type_option, "CHF1844", "CHF1,844", &field_type, &field);
"CHF1,844", assert_number(&type_option, "1844", "CHF1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "CHF0.2", "CHF0.2", &field_type, &field_rev);
assert_number(&type_option, "CHF1844", "CHF1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "CHF1,844", &field_type, &field_rev);
}, },
NumberFormat::HongKongDollar => { NumberFormat::HongKongDollar => {
assert_number( assert_number(&type_option, "HZ$18,44", "HZ$1,844", &field_type, &field);
&type_option, assert_number(&type_option, "HZ$0.2", "HZ$0.2", &field_type, &field);
"HZ$18,44", assert_number(&type_option, "HZ$1844", "HZ$1,844", &field_type, &field);
"HZ$1,844", assert_number(&type_option, "1844", "HZ$1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "HZ$0.2", "HZ$0.2", &field_type, &field_rev);
assert_number(&type_option, "HZ$1844", "HZ$1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "HZ$1,844", &field_type, &field_rev);
}, },
NumberFormat::NewZealandDollar => { NumberFormat::NewZealandDollar => {
assert_number( assert_number(&type_option, "NZ$18,44", "NZ$1,844", &field_type, &field);
&type_option, assert_number(&type_option, "NZ$0.2", "NZ$0.2", &field_type, &field);
"NZ$18,44", assert_number(&type_option, "NZ$1844", "NZ$1,844", &field_type, &field);
"NZ$1,844", assert_number(&type_option, "1844", "NZ$1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "NZ$0.2", "NZ$0.2", &field_type, &field_rev);
assert_number(&type_option, "NZ$1844", "NZ$1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "NZ$1,844", &field_type, &field_rev);
}, },
NumberFormat::Krona => { NumberFormat::Krona => {
assert_number( assert_number(&type_option, "SEK18,44", "18,44SEK", &field_type, &field);
&type_option, assert_number(&type_option, "SEK0.2", "0,2SEK", &field_type, &field);
"SEK18,44", assert_number(&type_option, "SEK1844", "1 844SEK", &field_type, &field);
"18,44SEK", assert_number(&type_option, "1844", "1 844SEK", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "SEK0.2", "0,2SEK", &field_type, &field_rev);
assert_number(&type_option, "SEK1844", "1 844SEK", &field_type, &field_rev);
assert_number(&type_option, "1844", "1 844SEK", &field_type, &field_rev);
}, },
NumberFormat::NorwegianKrone => { NumberFormat::NorwegianKrone => {
assert_number( assert_number(&type_option, "NOK18,44", "1,844NOK", &field_type, &field);
&type_option, assert_number(&type_option, "NOK0.2", "0.2NOK", &field_type, &field);
"NOK18,44", assert_number(&type_option, "NOK1844", "1,844NOK", &field_type, &field);
"1,844NOK", assert_number(&type_option, "1844", "1,844NOK", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "NOK0.2", "0.2NOK", &field_type, &field_rev);
assert_number(&type_option, "NOK1844", "1,844NOK", &field_type, &field_rev);
assert_number(&type_option, "1844", "1,844NOK", &field_type, &field_rev);
}, },
NumberFormat::MexicanPeso => { NumberFormat::MexicanPeso => {
assert_number( assert_number(&type_option, "MX$18,44", "MX$1,844", &field_type, &field);
&type_option, assert_number(&type_option, "MX$0.2", "MX$0.2", &field_type, &field);
"MX$18,44", assert_number(&type_option, "MX$1844", "MX$1,844", &field_type, &field);
"MX$1,844", assert_number(&type_option, "1844", "MX$1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "MX$0.2", "MX$0.2", &field_type, &field_rev);
assert_number(&type_option, "MX$1844", "MX$1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "MX$1,844", &field_type, &field_rev);
}, },
NumberFormat::Rand => { NumberFormat::Rand => {
assert_number( assert_number(&type_option, "ZAR18,44", "ZAR1,844", &field_type, &field);
&type_option, assert_number(&type_option, "ZAR0.2", "ZAR0.2", &field_type, &field);
"ZAR18,44", assert_number(&type_option, "ZAR1844", "ZAR1,844", &field_type, &field);
"ZAR1,844", assert_number(&type_option, "1844", "ZAR1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "ZAR0.2", "ZAR0.2", &field_type, &field_rev);
assert_number(&type_option, "ZAR1844", "ZAR1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "ZAR1,844", &field_type, &field_rev);
}, },
NumberFormat::NewTaiwanDollar => { NumberFormat::NewTaiwanDollar => {
assert_number( assert_number(&type_option, "NT$18,44", "NT$1,844", &field_type, &field);
&type_option, assert_number(&type_option, "NT$0.2", "NT$0.2", &field_type, &field);
"NT$18,44", assert_number(&type_option, "NT$1844", "NT$1,844", &field_type, &field);
"NT$1,844", assert_number(&type_option, "1844", "NT$1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "NT$0.2", "NT$0.2", &field_type, &field_rev);
assert_number(&type_option, "NT$1844", "NT$1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "NT$1,844", &field_type, &field_rev);
}, },
NumberFormat::DanishKrone => { NumberFormat::DanishKrone => {
assert_number( assert_number(&type_option, "DKK18.44", "18,44DKK", &field_type, &field);
&type_option, assert_number(&type_option, "DKK0.5", "0,5DKK", &field_type, &field);
"DKK18.44", assert_number(&type_option, "DKK1844", "1.844DKK", &field_type, &field);
"18,44DKK", assert_number(&type_option, "1844", "1.844DKK", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "DKK0.5", "0,5DKK", &field_type, &field_rev);
assert_number(&type_option, "DKK1844", "1.844DKK", &field_type, &field_rev);
assert_number(&type_option, "1844", "1.844DKK", &field_type, &field_rev);
}, },
NumberFormat::Baht => { NumberFormat::Baht => {
assert_number( assert_number(&type_option, "THB18,44", "THB1,844", &field_type, &field);
&type_option, assert_number(&type_option, "THB0.2", "THB0.2", &field_type, &field);
"THB18,44", assert_number(&type_option, "THB1844", "THB1,844", &field_type, &field);
"THB1,844", assert_number(&type_option, "1844", "THB1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "THB0.2", "THB0.2", &field_type, &field_rev);
assert_number(&type_option, "THB1844", "THB1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "THB1,844", &field_type, &field_rev);
}, },
NumberFormat::Forint => { NumberFormat::Forint => {
assert_number(&type_option, "HUF18,44", "18HUF", &field_type, &field_rev); assert_number(&type_option, "HUF18,44", "18HUF", &field_type, &field);
assert_number(&type_option, "HUF0.3", "0HUF", &field_type, &field_rev); assert_number(&type_option, "HUF0.3", "0HUF", &field_type, &field);
assert_number(&type_option, "HUF1844", "1 844HUF", &field_type, &field_rev); assert_number(&type_option, "HUF1844", "1 844HUF", &field_type, &field);
assert_number(&type_option, "1844", "1 844HUF", &field_type, &field_rev); assert_number(&type_option, "1844", "1 844HUF", &field_type, &field);
}, },
NumberFormat::Koruna => { NumberFormat::Koruna => {
assert_number( assert_number(&type_option, "CZK18,44", "18,44CZK", &field_type, &field);
&type_option, assert_number(&type_option, "CZK0.2", "0,2CZK", &field_type, &field);
"CZK18,44", assert_number(&type_option, "CZK1844", "1 844CZK", &field_type, &field);
"18,44CZK", assert_number(&type_option, "1844", "1 844CZK", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "CZK0.2", "0,2CZK", &field_type, &field_rev);
assert_number(&type_option, "CZK1844", "1 844CZK", &field_type, &field_rev);
assert_number(&type_option, "1844", "1 844CZK", &field_type, &field_rev);
}, },
NumberFormat::Shekel => { NumberFormat::Shekel => {
assert_number(&type_option, "Kč18,44", "18,44Kč", &field_type, &field_rev); assert_number(&type_option, "Kč18,44", "18,44Kč", &field_type, &field);
assert_number(&type_option, "Kč0.2", "0,2Kč", &field_type, &field_rev); assert_number(&type_option, "Kč0.2", "0,2Kč", &field_type, &field);
assert_number(&type_option, "Kč1844", "1 844Kč", &field_type, &field_rev); assert_number(&type_option, "Kč1844", "1 844Kč", &field_type, &field);
assert_number(&type_option, "1844", "1 844Kč", &field_type, &field_rev); assert_number(&type_option, "1844", "1 844Kč", &field_type, &field);
}, },
NumberFormat::ChileanPeso => { NumberFormat::ChileanPeso => {
assert_number(&type_option, "CLP18.44", "CLP18", &field_type, &field_rev); assert_number(&type_option, "CLP18.44", "CLP18", &field_type, &field);
assert_number(&type_option, "0.5", "CLP0", &field_type, &field_rev); assert_number(&type_option, "0.5", "CLP0", &field_type, &field);
assert_number(&type_option, "CLP1844", "CLP1.844", &field_type, &field_rev); assert_number(&type_option, "CLP1844", "CLP1.844", &field_type, &field);
assert_number(&type_option, "1844", "CLP1.844", &field_type, &field_rev); assert_number(&type_option, "1844", "CLP1.844", &field_type, &field);
}, },
NumberFormat::PhilippinePeso => { NumberFormat::PhilippinePeso => {
assert_number(&type_option, "₱18,44", "₱1,844", &field_type, &field_rev); assert_number(&type_option, "₱18,44", "₱1,844", &field_type, &field);
assert_number(&type_option, "₱0.2", "₱0.2", &field_type, &field_rev); assert_number(&type_option, "₱0.2", "₱0.2", &field_type, &field);
assert_number(&type_option, "₱1844", "₱1,844", &field_type, &field_rev); assert_number(&type_option, "₱1844", "₱1,844", &field_type, &field);
assert_number(&type_option, "1844", "₱1,844", &field_type, &field_rev); assert_number(&type_option, "1844", "₱1,844", &field_type, &field);
}, },
NumberFormat::Dirham => { NumberFormat::Dirham => {
assert_number( assert_number(&type_option, "AED18,44", "1,844AED", &field_type, &field);
&type_option, assert_number(&type_option, "AED0.2", "0.2AED", &field_type, &field);
"AED18,44", assert_number(&type_option, "AED1844", "1,844AED", &field_type, &field);
"1,844AED", assert_number(&type_option, "1844", "1,844AED", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "AED0.2", "0.2AED", &field_type, &field_rev);
assert_number(&type_option, "AED1844", "1,844AED", &field_type, &field_rev);
assert_number(&type_option, "1844", "1,844AED", &field_type, &field_rev);
}, },
NumberFormat::ColombianPeso => { NumberFormat::ColombianPeso => {
assert_number( assert_number(&type_option, "COP18.44", "COP18,44", &field_type, &field);
&type_option, assert_number(&type_option, "0.5", "COP0,5", &field_type, &field);
"COP18.44", assert_number(&type_option, "COP1844", "COP1.844", &field_type, &field);
"COP18,44", assert_number(&type_option, "1844", "COP1.844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "0.5", "COP0,5", &field_type, &field_rev);
assert_number(&type_option, "COP1844", "COP1.844", &field_type, &field_rev);
assert_number(&type_option, "1844", "COP1.844", &field_type, &field_rev);
}, },
NumberFormat::Riyal => { NumberFormat::Riyal => {
assert_number( assert_number(&type_option, "SAR18,44", "SAR1,844", &field_type, &field);
&type_option, assert_number(&type_option, "SAR0.2", "SAR0.2", &field_type, &field);
"SAR18,44", assert_number(&type_option, "SAR1844", "SAR1,844", &field_type, &field);
"SAR1,844", assert_number(&type_option, "1844", "SAR1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "SAR0.2", "SAR0.2", &field_type, &field_rev);
assert_number(&type_option, "SAR1844", "SAR1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "SAR1,844", &field_type, &field_rev);
}, },
NumberFormat::Ringgit => { NumberFormat::Ringgit => {
assert_number( assert_number(&type_option, "MYR18,44", "MYR1,844", &field_type, &field);
&type_option, assert_number(&type_option, "MYR0.2", "MYR0.2", &field_type, &field);
"MYR18,44", assert_number(&type_option, "MYR1844", "MYR1,844", &field_type, &field);
"MYR1,844", assert_number(&type_option, "1844", "MYR1,844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "MYR0.2", "MYR0.2", &field_type, &field_rev);
assert_number(&type_option, "MYR1844", "MYR1,844", &field_type, &field_rev);
assert_number(&type_option, "1844", "MYR1,844", &field_type, &field_rev);
}, },
NumberFormat::Leu => { NumberFormat::Leu => {
assert_number( assert_number(&type_option, "RON18.44", "18,44RON", &field_type, &field);
&type_option, assert_number(&type_option, "0.5", "0,5RON", &field_type, &field);
"RON18.44", assert_number(&type_option, "RON1844", "1.844RON", &field_type, &field);
"18,44RON", assert_number(&type_option, "1844", "1.844RON", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "0.5", "0,5RON", &field_type, &field_rev);
assert_number(&type_option, "RON1844", "1.844RON", &field_type, &field_rev);
assert_number(&type_option, "1844", "1.844RON", &field_type, &field_rev);
}, },
NumberFormat::ArgentinePeso => { NumberFormat::ArgentinePeso => {
assert_number( assert_number(&type_option, "ARS18.44", "ARS18,44", &field_type, &field);
&type_option, assert_number(&type_option, "0.5", "ARS0,5", &field_type, &field);
"ARS18.44", assert_number(&type_option, "ARS1844", "ARS1.844", &field_type, &field);
"ARS18,44", assert_number(&type_option, "1844", "ARS1.844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "0.5", "ARS0,5", &field_type, &field_rev);
assert_number(&type_option, "ARS1844", "ARS1.844", &field_type, &field_rev);
assert_number(&type_option, "1844", "ARS1.844", &field_type, &field_rev);
}, },
NumberFormat::UruguayanPeso => { NumberFormat::UruguayanPeso => {
assert_number( assert_number(&type_option, "UYU18.44", "UYU18,44", &field_type, &field);
&type_option, assert_number(&type_option, "0.5", "UYU0,5", &field_type, &field);
"UYU18.44", assert_number(&type_option, "UYU1844", "UYU1.844", &field_type, &field);
"UYU18,44", assert_number(&type_option, "1844", "UYU1.844", &field_type, &field);
&field_type,
&field_rev,
);
assert_number(&type_option, "0.5", "UYU0,5", &field_type, &field_rev);
assert_number(&type_option, "UYU1844", "UYU1.844", &field_type, &field_rev);
assert_number(&type_option, "1844", "UYU1.844", &field_type, &field_rev);
}, },
NumberFormat::Percent => { NumberFormat::Percent => {
assert_number(&type_option, "1", "1%", &field_type, &field_rev); assert_number(&type_option, "1", "1%", &field_type, &field);
assert_number(&type_option, "10.1", "10.1%", &field_type, &field_rev); assert_number(&type_option, "10.1", "10.1%", &field_type, &field);
assert_number(&type_option, "100", "100%", &field_type, &field_rev); assert_number(&type_option, "100", "100%", &field_type, &field);
}, },
} }
} }
@ -531,120 +393,116 @@ mod tests {
..Default::default() ..Default::default()
}; };
let field_type = FieldType::Number; let field_type = FieldType::Number;
let field_rev = FieldBuilder::from_field_type(field_type.clone()).build(); let field = FieldBuilder::from_field_type(field_type.clone()).build();
for format in NumberFormat::iter() { for format in NumberFormat::iter() {
type_option.format = format; type_option.format = format;
match format { match format {
NumberFormat::Num => { NumberFormat::Num => {
assert_number(&type_option, "18443", "18443", &field_type, &field_rev); assert_number(&type_option, "18443", "18443", &field_type, &field);
}, },
NumberFormat::USD => { NumberFormat::USD => {
assert_number(&type_option, "18443", "-$18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "-$18,443", &field_type, &field);
}, },
NumberFormat::CanadianDollar => { NumberFormat::CanadianDollar => {
assert_number(&type_option, "18443", "-CA$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-CA$18,443", &field_type, &field)
},
NumberFormat::EUR => {
assert_number(&type_option, "18443", "-€18.443", &field_type, &field_rev)
}, },
NumberFormat::EUR => assert_number(&type_option, "18443", "-€18.443", &field_type, &field),
NumberFormat::Pound => { NumberFormat::Pound => {
assert_number(&type_option, "18443", "-£18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-£18,443", &field_type, &field)
}, },
NumberFormat::Yen => { NumberFormat::Yen => {
assert_number(&type_option, "18443", "-¥18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "-¥18,443", &field_type, &field);
}, },
NumberFormat::Ruble => { NumberFormat::Ruble => {
assert_number(&type_option, "18443", "-18.443RUB", &field_type, &field_rev) assert_number(&type_option, "18443", "-18.443RUB", &field_type, &field)
}, },
NumberFormat::Rupee => { NumberFormat::Rupee => {
assert_number(&type_option, "18443", "-₹18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-₹18,443", &field_type, &field)
},
NumberFormat::Won => {
assert_number(&type_option, "18443", "-₩18,443", &field_type, &field_rev)
}, },
NumberFormat::Won => assert_number(&type_option, "18443", "-₩18,443", &field_type, &field),
NumberFormat::Yuan => { NumberFormat::Yuan => {
assert_number(&type_option, "18443", "-CN¥18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "-CN¥18,443", &field_type, &field);
}, },
NumberFormat::Real => { NumberFormat::Real => {
assert_number(&type_option, "18443", "-R$18,443", &field_type, &field_rev); assert_number(&type_option, "18443", "-R$18,443", &field_type, &field);
}, },
NumberFormat::Lira => { NumberFormat::Lira => {
assert_number(&type_option, "18443", "-TRY18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "-TRY18.443", &field_type, &field)
}, },
NumberFormat::Rupiah => { NumberFormat::Rupiah => {
assert_number(&type_option, "18443", "-IDR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-IDR18,443", &field_type, &field)
}, },
NumberFormat::Franc => { NumberFormat::Franc => {
assert_number(&type_option, "18443", "-CHF18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-CHF18,443", &field_type, &field)
}, },
NumberFormat::HongKongDollar => { NumberFormat::HongKongDollar => {
assert_number(&type_option, "18443", "-HZ$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-HZ$18,443", &field_type, &field)
}, },
NumberFormat::NewZealandDollar => { NumberFormat::NewZealandDollar => {
assert_number(&type_option, "18443", "-NZ$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-NZ$18,443", &field_type, &field)
}, },
NumberFormat::Krona => { NumberFormat::Krona => {
assert_number(&type_option, "18443", "-18 443SEK", &field_type, &field_rev) assert_number(&type_option, "18443", "-18 443SEK", &field_type, &field)
}, },
NumberFormat::NorwegianKrone => { NumberFormat::NorwegianKrone => {
assert_number(&type_option, "18443", "-18,443NOK", &field_type, &field_rev) assert_number(&type_option, "18443", "-18,443NOK", &field_type, &field)
}, },
NumberFormat::MexicanPeso => { NumberFormat::MexicanPeso => {
assert_number(&type_option, "18443", "-MX$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-MX$18,443", &field_type, &field)
}, },
NumberFormat::Rand => { NumberFormat::Rand => {
assert_number(&type_option, "18443", "-ZAR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-ZAR18,443", &field_type, &field)
}, },
NumberFormat::NewTaiwanDollar => { NumberFormat::NewTaiwanDollar => {
assert_number(&type_option, "18443", "-NT$18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-NT$18,443", &field_type, &field)
}, },
NumberFormat::DanishKrone => { NumberFormat::DanishKrone => {
assert_number(&type_option, "18443", "-18.443DKK", &field_type, &field_rev) assert_number(&type_option, "18443", "-18.443DKK", &field_type, &field)
}, },
NumberFormat::Baht => { NumberFormat::Baht => {
assert_number(&type_option, "18443", "-THB18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-THB18,443", &field_type, &field)
}, },
NumberFormat::Forint => { NumberFormat::Forint => {
assert_number(&type_option, "18443", "-18 443HUF", &field_type, &field_rev) assert_number(&type_option, "18443", "-18 443HUF", &field_type, &field)
}, },
NumberFormat::Koruna => { NumberFormat::Koruna => {
assert_number(&type_option, "18443", "-18 443CZK", &field_type, &field_rev) assert_number(&type_option, "18443", "-18 443CZK", &field_type, &field)
}, },
NumberFormat::Shekel => { NumberFormat::Shekel => {
assert_number(&type_option, "18443", "-18 443Kč", &field_type, &field_rev) assert_number(&type_option, "18443", "-18 443Kč", &field_type, &field)
}, },
NumberFormat::ChileanPeso => { NumberFormat::ChileanPeso => {
assert_number(&type_option, "18443", "-CLP18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "-CLP18.443", &field_type, &field)
}, },
NumberFormat::PhilippinePeso => { NumberFormat::PhilippinePeso => {
assert_number(&type_option, "18443", "-₱18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-₱18,443", &field_type, &field)
}, },
NumberFormat::Dirham => { NumberFormat::Dirham => {
assert_number(&type_option, "18443", "-18,443AED", &field_type, &field_rev) assert_number(&type_option, "18443", "-18,443AED", &field_type, &field)
}, },
NumberFormat::ColombianPeso => { NumberFormat::ColombianPeso => {
assert_number(&type_option, "18443", "-COP18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "-COP18.443", &field_type, &field)
}, },
NumberFormat::Riyal => { NumberFormat::Riyal => {
assert_number(&type_option, "18443", "-SAR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-SAR18,443", &field_type, &field)
}, },
NumberFormat::Ringgit => { NumberFormat::Ringgit => {
assert_number(&type_option, "18443", "-MYR18,443", &field_type, &field_rev) assert_number(&type_option, "18443", "-MYR18,443", &field_type, &field)
}, },
NumberFormat::Leu => { NumberFormat::Leu => {
assert_number(&type_option, "18443", "-18.443RON", &field_type, &field_rev) assert_number(&type_option, "18443", "-18.443RON", &field_type, &field)
}, },
NumberFormat::ArgentinePeso => { NumberFormat::ArgentinePeso => {
assert_number(&type_option, "18443", "-ARS18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "-ARS18.443", &field_type, &field)
}, },
NumberFormat::UruguayanPeso => { NumberFormat::UruguayanPeso => {
assert_number(&type_option, "18443", "-UYU18.443", &field_type, &field_rev) assert_number(&type_option, "18443", "-UYU18.443", &field_type, &field)
}, },
NumberFormat::Percent => { NumberFormat::Percent => {
assert_number(&type_option, "18443", "-18,443%", &field_type, &field_rev) assert_number(&type_option, "18443", "-18,443%", &field_type, &field)
}, },
} }
} }

View File

@ -130,18 +130,14 @@ impl NumberTypeOption {
Err(_) => Ok(NumberCellFormat::new()), Err(_) => Ok(NumberCellFormat::new()),
} }
} else { } else {
let draw_numer_string = NUM_REGEX.replace_all(&num_cell_data.0, ""); let num = match EXTRACT_NUM_REGEX.captures(&num_cell_data.0) {
let strnum = match draw_numer_string.matches('.').count() { Ok(Some(captures)) => captures
0 | 1 => draw_numer_string.to_string(), .get(0)
_ => match EXTRACT_NUM_REGEX.captures(&draw_numer_string) { .map(|m| m.as_str().to_string())
Ok(captures) => match captures { .unwrap_or_default(),
Some(capture) => capture[1].to_string(), _ => "".to_string(),
None => "".to_string(),
},
Err(_) => "".to_string(),
},
}; };
match Decimal::from_str(&strnum) { match Decimal::from_str(&num) {
Ok(value, ..) => Ok(NumberCellFormat::from_decimal(value)), Ok(value, ..) => Ok(NumberCellFormat::from_decimal(value)),
Err(_) => Ok(NumberCellFormat::new()), Err(_) => Ok(NumberCellFormat::new()),
} }
@ -264,14 +260,7 @@ impl std::default::Default for NumberTypeOption {
} }
} }
lazy_static! {
static ref NUM_REGEX: Regex = Regex::new(r"[^\d\.]").unwrap();
}
lazy_static! { lazy_static! {
static ref SCIENTIFIC_NOTATION_REGEX: Regex = Regex::new(r"([+-]?\d*\.?\d+)e([+-]?\d+)").unwrap(); static ref SCIENTIFIC_NOTATION_REGEX: Regex = Regex::new(r"([+-]?\d*\.?\d+)e([+-]?\d+)").unwrap();
} static ref EXTRACT_NUM_REGEX: Regex = Regex::new(r"-?\d+(\.\d+)?").unwrap();
lazy_static! {
static ref EXTRACT_NUM_REGEX: Regex = Regex::new(r"^(\d+\.\d+)(?:\.\d+)*$").unwrap();
} }