nspkg / engine / locale_format_test.v
87 lines · 80 sloc · 3.55 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import nspkg.tests.localefixtures
4import os
5
6#include <locale.h>
7
8struct LocaleActionTrace {
9mut:
10 lines []string
11}
12
13fn locale_action_error(context voidptr, level LogLevel, message string) {
14 if level != .error { return }
15 mut trace := unsafe { &LocaleActionTrace(context) }
16 trace.lines << message
17}
18
19fn test_localized_formats_match_printf_types_and_argument_reordering() {
20 mut output := []u8{len: 4096}
21 values := [MessageValue('entry'), int(-7), u32(9), i64(-1099511627776), u64(1125899906842624),
22 u32(0o755)]
23 arguments := values.map(message_argument(it))
24 format := '%s %d %u %jd %ju %o'
25 assert C.snprintf(unsafe { &char(output.data) }, uint(output.len), unsafe { &char(format.str) },
26 c'entry', i32(-7), u32(9), i64(-1099511627776), u64(1125899906842624), u32(0o755)) > 0
27 assert render_localized_message(format, arguments)? == unsafe { tos_clone(output.data) }
28 ordered := '%2$d [%1$s] %2$04d %%'
29 assert C.snprintf(unsafe { &char(output.data) }, uint(output.len), unsafe { &char(ordered.str) },
30 c'entry', i32(-7)) > 0
31 assert render_localized_message(ordered, arguments)? == unsafe { tos_clone(output.data) }
32 styled := '%3$#o:%2$+8jd:%1$-10.3s'
33 styled_values := [MessageValue('abcdef'), i64(-123), u32(0o71)]
34 assert C.snprintf(unsafe { &char(output.data) }, uint(output.len), unsafe { &char(styled.str) },
35 c'abcdef', i64(-123), u32(0o71)) > 0
36 assert render_localized_message(styled, styled_values.map(message_argument(it)))? == unsafe { tos_clone(output.data) }
37 assert localized_message('path %s', 'entry\x00ignored') == 'path entry'
38 assert localized_message('literal %%') == 'literal %'
39}
40
41fn test_localized_formats_reject_unsafe_or_mismatched_catalog_fields() {
42 arguments := [MessageValue('entry')].map(message_argument(it))
43 for format in ['%0$s', '%2$s', '%n', '%*s', 'trailing %', '%Ls', '%ls', '%d', '%1$s %s'] {
44 assert render_localized_message(format, arguments) == none, format
45 }
46}
47
48fn test_translated_extraction_errors_leave_action_records_in_english() ! {
49 root := os.join_path(os.vtmp_dir(), 'alpm-localized-action-${os.getpid()}')
50 os.mkdir_all(root)!
51 defer { os.rmdir_all(root) or { eprintln(err) } }
52 previous_locale := unsafe { tos_clone(C.setlocale(C.LC_ALL, nil)) }
53 previous_domain := unsafe { tos_clone(C.bindtextdomain(c'libalpm', nil)) }
54 previous_language := os.getenv_opt('LANGUAGE')
55 defer {
56 C.setlocale(C.LC_ALL, unsafe { &char(previous_locale.str) })
57 C.bindtextdomain(c'libalpm', unsafe { &char(previous_domain.str) })
58 if language := previous_language {
59 os.setenv('LANGUAGE', language, true)
60 } else {
61 os.unsetenv('LANGUAGE')
62 }
63 }
64 translated := C.setlocale(C.LC_ALL, c'en_US.UTF-8') != unsafe { nil }
65 if !translated {
66 assert C.setlocale(C.LC_ALL, c'C') != unsafe { nil }
67 }
68 assert os.setenv('LANGUAGE', 'zz', true) == 0
69 localefixtures.write(os.join_path(root, 'catalog', 'zz', 'LC_MESSAGES', 'libalpm.mo'), {
70 'could not extract %s (%s)\n': 'zz: %2$s @ %1$s\n'
71 })!
72 mut handle := initialize('/', root)!
73 defer { handle.release() or { eprintln(err) } }
74 logfile := os.join_path(root, 'action.log')
75 handle.set_string_option(.logfile, logfile)!
76 catalog := os.join_path(root, 'catalog')
77 C.bindtextdomain(c'libalpm', unsafe { &char(catalog.str) })
78 mut trace := LocaleActionTrace{}
79 handle.set_log_callback(locale_action_error, &trace)!
80 handle.record_extraction_failure('/package/file', error('reason'))
81 assert trace.lines == [if translated {
82 'zz: reason @ /package/file\n'
83 } else {
84 'could not extract /package/file (reason)\n'
85 }]
86 assert os.read_file(logfile)!.ends_with('] [ALPM] error: could not extract /package/file (reason)\n')
87}
88