From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 85dcdee5 for ; Tue, 24 Dec 2019 18:26:23 +0000 (UTC) Received: (qmail 21781 invoked by alias); 24 Dec 2019 18:25:55 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 45138 Received: (qmail 21210 invoked by uid 1010); 24 Dec 2019 18:25:55 -0000 X-Qmail-Scanner-Diagnostics: from wout2-smtp.messagingengine.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.1/25670. spamassassin: 3.4.2. Clear:RC:0(64.147.123.25):SA:0(-1.9/5.0):. Processed in 4.743684 secs); 24 Dec 2019 18:25:55 -0000 X-Envelope-From: danielsh@apache.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: softfail (ns1.primenet.com.au: transitioning SPF record at amazonses.com does not designate 64.147.123.25 as permitted sender) X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvddvvddgheekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofestddtredtredttd enucfhrhhomhepffgrnhhivghlucfuhhgrhhgrfhcuoegurghnihgvlhhshhesrghprggt hhgvrdhorhhgqeenucffohhmrghinhepnhhordhnohdphigvshdrnhhonecukfhppeejle drudektddrheejrdduudelnecurfgrrhgrmhepmhgrihhlfhhrohhmpegurghnihgvlhhs hhesrghprggthhgvrdhorhhgnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: From: Daniel Shahaf To: zsh-workers@zsh.org Subject: [PATCH 1/2] Add zformat unit tests. Date: Tue, 24 Dec 2019 18:25:08 +0000 Message-Id: <20191224182509.23800-1-danielsh@apache.org> X-Mailer: git-send-email 2.11.0 --- Src/Modules/zutil.c | 6 ++--- Test/V13zformat.ztst | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Test/V13zformat.ztst diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c index 24659cb16..de5fe8034 100644 --- a/Src/Modules/zutil.c +++ b/Src/Modules/zutil.c @@ -913,13 +913,13 @@ bin_zformat(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) switch (opt) { case 'f': { - char **ap, *specs[256], *out; + char **ap, *specs[256] = {0}, *out; int olen, oused = 0; - memset(specs, 0, 256 * sizeof(char *)); - specs['%'] = "%"; specs[')'] = ")"; + + /* Parse the specs in argv. */ for (ap = args + 2; *ap; ap++) { if (!ap[0][0] || ap[0][0] == '-' || ap[0][0] == '.' || idigit(ap[0][0]) || ap[0][1] != ':') { diff --git a/Test/V13zformat.ztst b/Test/V13zformat.ztst new file mode 100644 index 000000000..d8de2bb04 --- /dev/null +++ b/Test/V13zformat.ztst @@ -0,0 +1,65 @@ +# Test the use of zformat, if the zsh/zutil module is available. + +%prep + + if ! zmodload zsh/zutil 2>/dev/null; then + ZTST_unimplemented="can't load the zsh/zutil module for testing" + fi + + # Helper function. Expects a single format using %s and a value for it. + zformat_and_print_s() { + zformat -f REPLY "$1" "s:$2" + print -r - ${(qq)REPLY} + } + +%test + + zformat_and_print_s '%s' foo + zformat_and_print_s '%5s' min + zformat_and_print_s '%-5s' neg + zformat_and_print_s '%.5s' max + zformat_and_print_s '%.5s' truncated +0:basic zformat test +>'foo' +>'min ' +>' neg' +>'max' +>'trunc' + + # There may be a set of digits either before or after the opening parenthesis. + zformat_and_print_s 'The answer is "%3(s.yes.no)".' 3 + zformat_and_print_s 'The answer is "%(3s.yes.no)".' 3 + # The test number defaults to zero. + zformat_and_print_s '%(s.equal.unequal)' -1 + zformat_and_print_s '%(s.equal.unequal)' 0 + zformat_and_print_s '%(s.equal.unequal)' 1 + # Negative numbers are allowed + # The delimiter is arbitrary + zformat_and_print_s '%-4(s.minus four.)' -4 + zformat_and_print_s '%(-4s//minus four)' -4 + # The argument is evaluated as a math expression + zformat_and_print_s '%18(s.math.)' '6*3' +0:basic conditionals test +>'The answer is "yes".' +>'The answer is "yes".' +>'unequal' +>'equal' +>'unequal' +>'minus four' +>'' +>'math' + + () { + zformat -f 1 '%(8n.%(5j.yes.no).no)' 'n:8' 'j:5' + echo $1 + } +0:nested conditionals test +>yes + + zformat -a argv . foo:lorem ipsum:bar bazbaz '\\esc\:ape' + print -rl -- "$@" +0:basic -a test +>foo .lorem +>ipsum.bar +>bazbaz +>\esc:ape