1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-21 19:20:21 +02:00

format: Add specifier ~h for localized number output.

* doc/ref/misc-modules.texi (Formatted Output): Document ~h.  Recommend
  use of ~h instead of ~:d.

* module/ice-9/format.scm (format): Add support for ~h.

* test-suite/tests/format.test ("~h localized number"): New test prefix.

* test-suite/tests/i18n.test (%american-english-locale-name,
  %american-english-locale): New variables.
  (under-american-english-locale-or-unresolved): New procedure.
  ("format ~h"): New test prefix.
This commit is contained in:
Ludovic Courtès 2012-02-03 16:35:06 +01:00
parent c76fdf69a8
commit afd08fdf87
4 changed files with 109 additions and 7 deletions

View file

@ -1,7 +1,7 @@
;;;; format.test --- test suite for Guile's CL-ish format -*- scheme -*-
;;;; Matthias Koeppe <mkoeppe@mail.math.uni-magdeburg.de> --- June 2001
;;;;
;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011 Free Software Foundation, Inc.
;;;; Copyright (C) 2001, 2003, 2004, 2006, 2010, 2011, 2012 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
@ -19,6 +19,7 @@
(define-module (test-format)
#:use-module (test-suite lib)
#:use-module (ice-9 i18n)
#:use-module (ice-9 format))
@ -96,6 +97,31 @@
(pass-if "string 02.5"
(string=? "2.5" (format #f "~f" "02.5"))))
;;;
;;; ~h
;;;
(setlocale LC_ALL "C")
(with-test-prefix "~h localized number"
(pass-if "1234.5"
(string=? (format #f "~h" 1234.5) "1234.5"))
(pass-if "padding"
(string=? (format #f "~6h" 123.2) " 123.2"))
(pass-if "padchar"
(string=? (format #f "~8,,'*h" 123.2) "***123.2"))
(pass-if "decimals"
(string=? (format #f "~,2h" 123.4567)
"123.45"))
(pass-if "locale"
(string=? (format #f "~,3:h, ~a" 1234.5678
%global-locale "approximately")
"1234.567, approximately")))
;;;
;;; ~{
;;;

View file

@ -18,9 +18,10 @@
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-suite i18n)
:use-module (ice-9 i18n)
:use-module (srfi srfi-1)
:use-module (test-suite lib))
#:use-module (ice-9 i18n)
#:use-module (ice-9 format)
#:use-module (srfi srfi-1)
#:use-module (test-suite lib))
;; Start from a pristine locale state.
(setlocale LC_ALL "C")
@ -94,6 +95,9 @@
(define %greek-utf8-locale-name
"el_GR.UTF-8")
(define %american-english-locale-name
"en_US")
(define %french-locale
(false-if-exception
(make-locale (list LC_CTYPE LC_COLLATE LC_NUMERIC LC_TIME)
@ -119,6 +123,11 @@
(make-locale LC_ALL
%turkish-utf8-locale-name)))
(define %american-english-locale
(false-if-exception
(make-locale LC_ALL
%american-english-locale-name)))
(define (under-locale-or-unresolved locale thunk)
;; On non-GNU systems, an exception may be raised only when the locale is
;; actually used rather than at `make-locale'-time. Thus, we must guard
@ -153,6 +162,10 @@
(define (under-greek-utf8-locale-or-unresolved thunk)
(under-locale-or-unresolved %greek-utf8-locale thunk))
(define (under-american-english-locale-or-unresolved thunk)
(under-locale-or-unresolved %american-english-locale thunk))
(with-test-prefix "text collation (French)"
(pass-if "string-locale<?"
@ -480,6 +493,25 @@
(string=? "1 234,5"
(number->locale-string 1234.567 1 fr))))))))
(with-test-prefix "format ~h"
(with-test-prefix "French"
(pass-if "12345.5678"
(under-french-locale-or-unresolved
(lambda ()
(string=? "12 345,6789"
(format #f "~:h" 12345.6789 %french-locale))))))
(with-test-prefix "English"
(pass-if "12345.5678"
(under-american-english-locale-or-unresolved
(lambda ()
(string=? "12,345.6789"
(format #f "~:h" 12345.6789
%american-english-locale)))))))
(with-test-prefix "monetary-amount->locale-string"
(with-test-prefix "French"