oxedyne/fe2o3/fe2o3_datime/tests/format_locale.rs
7.7 KiB, 9 runs
created by r1870400018:6411, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use oxedyne_fe2o3_core::{ |
| 5 | prelude::*, |
| 6 | test::test_it, |
| 7 | }; |
| 8 | use oxedyne_fe2o3_datime::{ |
| 9 | format::{CalClockFormatter, Locale}, |
| 10 | time::{CalClock, CalClockZone}, |
| 11 | }; |
| 12 | |
| 13 | pub fn test_format_locale(filter: &str) -> Outcome<()> { |
| 14 | |
| 15 | res!(test_it(filter, &["locale_basic", "all", "format", "locale"], || { |
| 16 | // Test basic locale creation and properties |
| 17 | let us_locale = Locale::us(); |
| 18 | assert_eq!(us_locale.id(), "en-US"); |
| 19 | assert_eq!(us_locale.display_name(), "English (United States)"); |
| 20 | |
| 21 | let german_locale = Locale::germany(); |
| 22 | assert_eq!(german_locale.id(), "de-DE"); |
| 23 | assert_eq!(german_locale.display_name(), "Deutsch (Deutschland)"); |
| 24 | |
| 25 | let iso_locale = Locale::iso(); |
| 26 | assert_eq!(iso_locale.id(), "ISO"); |
| 27 | |
| 28 | println!("✅ Basic locale creation and properties work correctly"); |
| 29 | Ok(()) |
| 30 | })); |
| 31 | |
| 32 | res!(test_it(filter, &["locale_patterns", "all", "format", "locale"], || { |
| 33 | // Test that different locales have different format patterns |
| 34 | let us_locale = Locale::us(); |
| 35 | let uk_locale = Locale::uk(); |
| 36 | let german_locale = Locale::germany(); |
| 37 | let iso_locale = Locale::iso(); |
| 38 | |
| 39 | // US uses MM/dd/yyyy |
| 40 | assert_eq!(us_locale.date_pattern().pattern_string(), "MM/dd/yyyy"); |
| 41 | |
| 42 | // UK uses dd/MM/yyyy |
| 43 | assert_eq!(uk_locale.date_pattern().pattern_string(), "dd/MM/yyyy"); |
| 44 | |
| 45 | // Germany uses dd.MM.yyyy |
| 46 | assert_eq!(german_locale.date_pattern().pattern_string(), "dd.MM.yyyy"); |
| 47 | |
| 48 | // ISO uses yyyy-MM-dd |
| 49 | assert_eq!(iso_locale.date_pattern().pattern_string(), "yyyy-MM-dd"); |
| 50 | |
| 51 | // Check time patterns differ too |
| 52 | assert_eq!(us_locale.time_pattern().pattern_string(), "h:mm:ss a"); // 12-hour |
| 53 | assert_eq!(uk_locale.time_pattern().pattern_string(), "HH:mm:ss"); // 24-hour |
| 54 | |
| 55 | println!("✅ Different locales have appropriate format patterns"); |
| 56 | Ok(()) |
| 57 | })); |
| 58 | |
| 59 | res!(test_it(filter, &["locale_formatting", "all", "format", "locale"], || { |
| 60 | // Test actual formatting with different locales |
| 61 | let formatter = CalClockFormatter::new(); |
| 62 | |
| 63 | // Create a test CalClock: January 15, 2024, 14:30:45 |
| 64 | let zone = CalClockZone::utc(); |
| 65 | let test_calclock = res!(CalClock::new(2024, 1, 15, 14, 30, 45, 123_456_789, zone)); |
| 66 | |
| 67 | // Test US formatting |
| 68 | let us_locale = Locale::us(); |
| 69 | let us_date = res!(formatter.format_date_with_locale(&test_calclock, &us_locale)); |
| 70 | assert_eq!(us_date, "01/15/2024"); // MM/dd/yyyy |
| 71 | |
| 72 | let us_time = res!(formatter.format_time_with_locale(&test_calclock, &us_locale)); |
| 73 | assert_eq!(us_time, "2:30:45 PM"); // h:mm:ss a |
| 74 | |
| 75 | // Test German formatting |
| 76 | let german_locale = Locale::germany(); |
| 77 | let german_date = res!(formatter.format_date_with_locale(&test_calclock, &german_locale)); |
| 78 | assert_eq!(german_date, "15.01.2024"); // dd.MM.yyyy |
| 79 | |
| 80 | let german_time = res!(formatter.format_time_with_locale(&test_calclock, &german_locale)); |
| 81 | assert_eq!(german_time, "14:30:45"); // HH:mm:ss |
| 82 | |
| 83 | // Test ISO formatting |
| 84 | let iso_locale = Locale::iso(); |
| 85 | let iso_date = res!(formatter.format_date_with_locale(&test_calclock, &iso_locale)); |
| 86 | assert_eq!(iso_date, "2024-01-15"); // yyyy-MM-dd |
| 87 | |
| 88 | let iso_time = res!(formatter.format_time_with_locale(&test_calclock, &iso_locale)); |
| 89 | assert_eq!(iso_time, "14:30:45"); // HH:mm:ss |
| 90 | |
| 91 | println!("✅ Locale-specific formatting produces correct output"); |
| 92 | Ok(()) |
| 93 | })); |
| 94 | |
| 95 | res!(test_it(filter, &["locale_from_id", "all", "format", "locale"], || { |
| 96 | // Test locale creation from ID strings |
| 97 | let us_locale = Locale::from_id("en-US"); |
| 98 | assert_eq!(us_locale.id(), "en-US"); |
| 99 | |
| 100 | let japanese_locale = Locale::from_id("ja-JP"); |
| 101 | assert_eq!(japanese_locale.id(), "ja-JP"); |
| 102 | assert_eq!(japanese_locale.date_pattern().pattern_string(), "yyyy/MM/dd"); |
| 103 | |
| 104 | let chinese_locale = Locale::from_id("zh-CN"); |
| 105 | assert_eq!(chinese_locale.id(), "zh-CN"); |
| 106 | assert_eq!(chinese_locale.date_pattern().pattern_string(), "yyyy/M/d"); |
| 107 | |
| 108 | // Test fallback for unknown locale |
| 109 | let unknown_locale = Locale::from_id("xx-XX"); |
| 110 | assert_eq!(unknown_locale.id(), "en-US"); // Should fall back to US |
| 111 | |
| 112 | println!("✅ Locale creation from ID strings works correctly"); |
| 113 | Ok(()) |
| 114 | })); |
| 115 | |
| 116 | res!(test_it(filter, &["locale_available_list", "all", "format", "locale"], || { |
| 117 | // Test available locales functionality |
| 118 | let available_locales = Locale::available_locales(); |
| 119 | assert!(available_locales.contains(&"en-US".to_string())); |
| 120 | assert!(available_locales.contains(&"de-DE".to_string())); |
| 121 | assert!(available_locales.contains(&"ja-JP".to_string())); |
| 122 | assert!(available_locales.contains(&"ISO".to_string())); |
| 123 | assert!(available_locales.len() >= 7); |
| 124 | |
| 125 | let locales_with_names = Locale::available_locales_with_names(); |
| 126 | assert!(locales_with_names.iter().any(|(id, name)| |
| 127 | id == "en-US" && name == "English (United States)")); |
| 128 | assert!(locales_with_names.iter().any(|(id, name)| |
| 129 | id == "de-DE" && name == "Deutsch (Deutschland)")); |
| 130 | |
| 131 | println!("✅ Available locales listing works correctly"); |
| 132 | Ok(()) |
| 133 | })); |
| 134 | |
| 135 | res!(test_it(filter, &["locale_display", "all", "format", "locale"], || { |
| 136 | // Test locale display formatting |
| 137 | let us_locale = Locale::us(); |
| 138 | let display_string = format!("{}", us_locale); |
| 139 | assert!(display_string.contains("English (United States)")); |
| 140 | assert!(display_string.contains("en-US")); |
| 141 | |
| 142 | let german_locale = Locale::germany(); |
| 143 | let german_display = format!("{}", german_locale); |
| 144 | assert!(german_display.contains("Deutsch (Deutschland)")); |
| 145 | assert!(german_display.contains("de-DE")); |
| 146 | |
| 147 | println!("✅ Locale display formatting works correctly"); |
| 148 | Ok(()) |
| 149 | })); |
| 150 | |
| 151 | res!(test_it(filter, &["locale_practical_example", "all", "format", "locale"], || { |
| 152 | // Test a practical real-world example |
| 153 | let formatter = CalClockFormatter::new(); |
| 154 | |
| 155 | // Create a CalClock for testing - February 29, 2024 (leap year), 9:05:03 AM |
| 156 | let zone = CalClockZone::utc(); |
| 157 | let test_calclock = res!(CalClock::new(2024, 2, 29, 9, 5, 3, 0, zone)); |
| 158 | |
| 159 | // Test different locale outputs for the same date/time |
| 160 | let locales_and_expected = vec![ |
| 161 | (Locale::us(), "02/29/2024", "9:05:03 AM"), |
| 162 | (Locale::uk(), "29/02/2024", "09:05:03"), |
| 163 | (Locale::germany(), "29.02.2024", "09:05:03"), |
| 164 | (Locale::france(), "29/02/2024", "09:05:03"), |
| 165 | (Locale::japan(), "2024/02/29", "09:05:03"), |
| 166 | (Locale::china(), "2024/2/29", "09:05:03"), |
| 167 | (Locale::iso(), "2024-02-29", "09:05:03"), |
| 168 | ]; |
| 169 | |
| 170 | for (locale, expected_date, expected_time) in locales_and_expected { |
| 171 | let actual_date = res!(formatter.format_date_with_locale(&test_calclock, &locale)); |
| 172 | let actual_time = res!(formatter.format_time_with_locale(&test_calclock, &locale)); |
| 173 | |
| 174 | assert_eq!(actual_date, expected_date, "Date mismatch for locale {}", locale.id()); |
| 175 | assert_eq!(actual_time, expected_time, "Time mismatch for locale {}", locale.id()); |
| 176 | } |
| 177 | |
| 178 | println!("✅ Practical locale formatting example works correctly"); |
| 179 | Ok(()) |
| 180 | })); |
| 181 | |
| 182 | Ok(()) |
| 183 | } |