|
| 1 | +module gui |
| 2 | + |
| 3 | +import math |
| 4 | + |
| 5 | +fn test_numeric_parse_en_locale() { |
| 6 | + value := numeric_parse('1,234.50', NumericLocaleCfg{}) or { |
| 7 | + assert false |
| 8 | + return |
| 9 | + } |
| 10 | + assert math.abs(value - 1234.5) < 0.000001 |
| 11 | +} |
| 12 | + |
| 13 | +fn test_numeric_parse_de_locale() { |
| 14 | + locale := NumericLocaleCfg{ |
| 15 | + decimal_sep: `,` |
| 16 | + group_sep: `.` |
| 17 | + } |
| 18 | + value := numeric_parse('1.234,50', locale) or { |
| 19 | + assert false |
| 20 | + return |
| 21 | + } |
| 22 | + assert math.abs(value - 1234.5) < 0.000001 |
| 23 | +} |
| 24 | + |
| 25 | +fn test_numeric_parse_invalid_string() { |
| 26 | + assert numeric_parse('1,,234', NumericLocaleCfg{}) == none |
| 27 | + assert numeric_parse('abc', NumericLocaleCfg{}) == none |
| 28 | +} |
| 29 | + |
| 30 | +fn test_numeric_format_en_locale() { |
| 31 | + assert numeric_format(1234.5, 2, NumericLocaleCfg{}) == '1,234.50' |
| 32 | +} |
| 33 | + |
| 34 | +fn test_numeric_format_de_locale() { |
| 35 | + locale := NumericLocaleCfg{ |
| 36 | + decimal_sep: `,` |
| 37 | + group_sep: `.` |
| 38 | + } |
| 39 | + assert numeric_format(1234.5, 2, locale) == '1.234,50' |
| 40 | +} |
| 41 | + |
| 42 | +fn test_numeric_format_group_sizes() { |
| 43 | + locale := NumericLocaleCfg{ |
| 44 | + group_sep: `,` |
| 45 | + group_sizes: [3, 2] |
| 46 | + } |
| 47 | + assert numeric_format(1234567, 0, locale) == '12,34,567' |
| 48 | +} |
| 49 | + |
| 50 | +fn test_numeric_commit_result_clamps() { |
| 51 | + value, text := numeric_input_commit_result('1,250.30', none, 0.0, 1000.0, 2, NumericLocaleCfg{}) |
| 52 | + assert text == '1,000.00' |
| 53 | + if parsed := value { |
| 54 | + assert math.abs(parsed - 1000.0) < 0.000001 |
| 55 | + } else { |
| 56 | + assert false |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn test_numeric_commit_result_invalid_fallback_to_value() { |
| 61 | + value, text := numeric_input_commit_result('abc', 12.5, none, none, 1, NumericLocaleCfg{}) |
| 62 | + assert text == '12.5' |
| 63 | + if parsed := value { |
| 64 | + assert math.abs(parsed - 12.5) < 0.000001 |
| 65 | + } else { |
| 66 | + assert false |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn test_numeric_commit_result_invalid_without_value() { |
| 71 | + value, text := numeric_input_commit_result('abc', none, none, none, 2, NumericLocaleCfg{}) |
| 72 | + assert value == none |
| 73 | + assert text == '' |
| 74 | +} |
| 75 | + |
| 76 | +fn test_numeric_step_result_uses_min_seed() { |
| 77 | + value, text := numeric_input_step_result('', none, 10.0, none, 2, NumericStepCfg{}, |
| 78 | + NumericLocaleCfg{}, 1.0, .none) |
| 79 | + assert text == '11.00' |
| 80 | + if parsed := value { |
| 81 | + assert math.abs(parsed - 11.0) < 0.000001 |
| 82 | + } else { |
| 83 | + assert false |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn test_numeric_step_result_modifiers() { |
| 88 | + cfg := NumericStepCfg{ |
| 89 | + step: 1.0 |
| 90 | + shift_multiplier: 10.0 |
| 91 | + alt_multiplier: 0.1 |
| 92 | + } |
| 93 | + value_shift, text_shift := numeric_input_step_result('5', none, none, none, 2, cfg, |
| 94 | + NumericLocaleCfg{}, 1.0, .shift) |
| 95 | + assert text_shift == '15.00' |
| 96 | + if parsed := value_shift { |
| 97 | + assert math.abs(parsed - 15.0) < 0.000001 |
| 98 | + } else { |
| 99 | + assert false |
| 100 | + } |
| 101 | + |
| 102 | + value_alt, text_alt := numeric_input_step_result('5', none, none, none, 2, cfg, NumericLocaleCfg{}, |
| 103 | + 1.0, .alt) |
| 104 | + assert text_alt == '5.10' |
| 105 | + if parsed := value_alt { |
| 106 | + assert math.abs(parsed - 5.1) < 0.000001 |
| 107 | + } else { |
| 108 | + assert false |
| 109 | + } |
| 110 | +} |
0 commit comments