From 6b2fb4354a803620ac5d165fbc1511a79ba25406 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 12 Jun 2018 13:05:33 -0400 Subject: [PATCH] Add more functionality to localization unit test program. --- cups/testlang.c | 247 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 169 insertions(+), 78 deletions(-) diff --git a/cups/testlang.c b/cups/testlang.c index 08160ba740..613ae32d66 100644 --- a/cups/testlang.c +++ b/cups/testlang.c @@ -1,6 +1,10 @@ /* * Localization test program for CUPS. * + * Usage: + * + * ./testlang [-l locale] [-p ppd] ["String to localize"] + * * Copyright 2007-2017 by Apple Inc. * Copyright 1997-2006 by Easy Software Products. * @@ -22,7 +26,9 @@ * Local functions... */ -static int test_string(cups_lang_t *language, const char *msgid); +static int show_ppd(const char *filename); +static int test_string(cups_lang_t *language, const char *msgid); +static void usage(void); /* @@ -34,9 +40,12 @@ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int i; /* Looping var */ + const char *opt; /* Current option */ int errors = 0; /* Number of errors */ - cups_lang_t *language; /* Message catalog */ - cups_lang_t *language2; /* Message catalog */ + int dotests = 1; /* Do standard tests? */ + cups_lang_t *language = NULL;/* Message catalog */ + cups_lang_t *language2 = NULL; + /* Message catalog (second time) */ struct lconv *loc; /* Locale data */ char buffer[1024]; /* String buffer */ double number; /* Number */ @@ -49,21 +58,84 @@ main(int argc, /* I - Number of command-line arguments */ }; - if (argc == 1) - { - language = cupsLangDefault(); - language2 = cupsLangDefault(); - } - else + /* + * Parse command-line... + */ + + _cupsSetLocale(argv); + + for (i = 1; i < argc; i ++) { - language = cupsLangGet(argv[1]); - language2 = cupsLangGet(argv[1]); + if (argv[i][0] == '-') + { + if (!strcmp(argv[i], "--help")) + { + usage(); + } + else + { + for (opt = argv[i] + 1; *opt; opt ++) + { + switch (*opt) + { + case 'l' : + i ++; + if (i >= argc) + { + usage(); + return (1); + } + + language = cupsLangGet(argv[i]); + language2 = cupsLangGet(argv[i]); + + setenv("LANG", argv[i], 1); + setenv("SOFTWARE", "CUPS/" CUPS_SVERSION, 1); + break; + + case 'p' : + i ++; + if (i >= argc) + { + usage(); + return (1); + } + + if (!language) + { + language = cupsLangDefault(); + language2 = cupsLangDefault(); + } + + dotests = 0; + errors += show_ppd(argv[i]); + break; + + default : + usage(); + return (1); + } + } + } + } + else + { + if (!language) + { + language = cupsLangDefault(); + language2 = cupsLangDefault(); + } - setenv("LANG", argv[1], 1); - setenv("SOFTWARE", "CUPS/" CUPS_SVERSION, 1); + dotests = 0; + errors += test_string(language, argv[i]); + } } - _cupsSetLocale(argv); + if (!language) + { + language = cupsLangDefault(); + language2 = cupsLangDefault(); + } if (language != language2) { @@ -76,81 +148,41 @@ main(int argc, /* I - Number of command-line arguments */ printf("Language = \"%s\"\n", language->language); printf("Encoding = \"%s\"\n", _cupsEncodingName(language->encoding)); - errors += test_string(language, "No"); - errors += test_string(language, "Yes"); - - if (language != language2) + if (dotests) { - puts("Second result from cupsLangGet:"); - - printf("Language = \"%s\"\n", language2->language); - printf("Encoding = \"%s\"\n", _cupsEncodingName(language2->encoding)); - printf("No = \"%s\"\n", _cupsLangString(language2, "No")); - printf("Yes = \"%s\"\n", _cupsLangString(language2, "Yes")); - } - - loc = localeconv(); - - for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i ++) - { - number = _cupsStrScand(tests[i], NULL, loc); - - printf("_cupsStrScand(\"%s\") number=%f\n", tests[i], number); + errors += test_string(language, "No"); + errors += test_string(language, "Yes"); - _cupsStrFormatd(buffer, buffer + sizeof(buffer), number, loc); - - printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number, buffer); - - if (strcmp(buffer, tests[i])) + if (language != language2) { - errors ++; - puts("**** ERROR: Bad formatted number! ****"); + puts("Second result from cupsLangGet:"); + + printf("Language = \"%s\"\n", language2->language); + printf("Encoding = \"%s\"\n", _cupsEncodingName(language2->encoding)); + printf("No = \"%s\"\n", _cupsLangString(language2, "No")); + printf("Yes = \"%s\"\n", _cupsLangString(language2, "Yes")); } - } - if (argc == 3) - { - ppd_file_t *ppd; /* PPD file */ - ppd_option_t *option; /* PageSize option */ - ppd_choice_t *choice; /* PageSize/Letter choice */ + loc = localeconv(); - if ((ppd = ppdOpenFile(argv[2])) == NULL) - { - printf("Unable to open PPD file \"%s\".\n", argv[2]); - errors ++; - } - else + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i ++) { - ppdLocalize(ppd); + number = _cupsStrScand(tests[i], NULL, loc); - if ((option = ppdFindOption(ppd, "PageSize")) == NULL) - { - puts("No PageSize option."); - errors ++; - } - else - { - printf("PageSize: %s\n", option->text); + printf("_cupsStrScand(\"%s\") number=%f\n", tests[i], number); - if ((choice = ppdFindChoice(option, "Letter")) == NULL) - { - puts("No Letter PageSize choice."); - errors ++; - } - else - { - printf("Letter: %s\n", choice->text); - } - } + _cupsStrFormatd(buffer, buffer + sizeof(buffer), number, loc); - printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd, "media-empty", NULL, buffer, sizeof(buffer))); + printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number, buffer); - ppdClose(ppd); + if (strcmp(buffer, tests[i])) + { + errors ++; + puts("**** ERROR: Bad formatted number! ****"); + } } - } + #ifdef __APPLE__ - else - { /* * Test all possible language IDs for compatibility with _cupsAppleLocale... */ @@ -236,16 +268,65 @@ main(int argc, /* I - Number of command-line arguments */ # if TEST_COUNTRY_CODES CFRelease(country_codes); # endif /* TEST_COUNTRY_CODES */ - } #endif /* __APPLE__ */ + } - if (errors == 0) + if (errors == 0 && dotests) puts("ALL TESTS PASSED"); return (errors > 0); } +/* + * 'show_ppd()' - Show localized strings in a PPD file. + */ + +static int /* O - Number of errors */ +show_ppd(const char *filename) /* I - Filename */ +{ + ppd_file_t *ppd; /* PPD file */ + ppd_option_t *option; /* PageSize option */ + ppd_choice_t *choice; /* PageSize/Letter choice */ + char buffer[1024]; /* String buffer */ + + + if ((ppd = ppdOpenFile(filename)) == NULL) + { + printf("Unable to open PPD file \"%s\".\n", filename); + return (1); + } + + ppdLocalize(ppd); + + if ((option = ppdFindOption(ppd, "PageSize")) == NULL) + { + puts("No PageSize option."); + return (1); + } + else + { + printf("PageSize: %s\n", option->text); + + if ((choice = ppdFindChoice(option, "Letter")) == NULL) + { + puts("No Letter PageSize choice."); + return (1); + } + else + { + printf("Letter: %s\n", choice->text); + } + } + + printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd, "media-empty", NULL, buffer, sizeof(buffer))); + + ppdClose(ppd); + + return (0); +} + + /* * 'test_string()' - Test the localization of a string. */ @@ -281,3 +362,13 @@ test_string(cups_lang_t *language, /* I - Language */ return (0); } + +/* + * 'usage()' - Show program usage. + */ + +static void +usage(void) +{ + puts("./testlang [-l locale] [-p ppd] [\"String to localize\"]"); +} -- 2.47.2