From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "Basil L. Contovounesios" Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Unit tests and lexical-binding for Tempo Date: Tue, 14 May 2019 01:49:35 +0100 Message-ID: <87k1etamhs.fsf@tcd.ie> References: <87h89yqiwu.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="46739"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: emacs-devel@gnu.org To: Federico Tedin Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue May 14 03:05:08 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hQLsQ-000Bxr-HJ for ged-emacs-devel@m.gmane.org; Tue, 14 May 2019 03:05:06 +0200 Original-Received: from localhost ([127.0.0.1]:37134 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQLsP-0001lY-Hg for ged-emacs-devel@m.gmane.org; Mon, 13 May 2019 21:05:05 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:42694) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQLnr-0008CM-6p for emacs-devel@gnu.org; Mon, 13 May 2019 21:04:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hQLda-0007hV-3K for emacs-devel@gnu.org; Mon, 13 May 2019 20:50:22 -0400 Original-Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]:44205) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hQLdT-0007ad-Vy for emacs-devel@gnu.org; Mon, 13 May 2019 20:49:44 -0400 Original-Received: by mail-ed1-x541.google.com with SMTP id b8so20175175edm.11 for ; Mon, 13 May 2019 17:49:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tcd-ie.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=+0xj5ztsjCpTNNO99kMslO0yDRbOHnyMkNTBl8TkQfI=; b=HxZ9jXDjVF7ph0bEoA7CqQdx3AssZ/SZ46ei6ht3RUhXpHUSWLdxW/11yf69xnO/hu J+jaPtyGukuqkxjBznpuvUYEWN++tgKNPUQ1lggS3/XHjskHCyu5H37AadTFcqRjtaEV wmPucwrK8mKW1gcCWXB8HvKRrn27nQ24LrG9OQkRUQyjvHNlQ6iCYwdm4PCj1p15U/sW 5mVxMd9qvTYxucLhq5CDg/DHKiLSizvbN0Gyqw7hFmB1h4Ca2639oSQ51BFqV+6d3b4q l6QMoLPDT6Cqr3GuaTx/TACgkJ4g2kEOF0xyFnLbq+TrTTf+pEw41JIq3njd7FKb/Awh 6sBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=+0xj5ztsjCpTNNO99kMslO0yDRbOHnyMkNTBl8TkQfI=; b=Cc0Iw1b2P77ipMLp7n6fOGvAXrksdmgm2LC1zJUPbxkOoPRs5Aqdze5gDBQIatBsR8 WjC7JvRNg9CxaW1PRYJrFYnmTPf2JjYnOagqhClx8VRzsqO0HRwVKAerxPSYMcY1va/s hqwzukSjbU8HTn7lfRx46kGPwQn99fiMgtXVZUGiW7b0pwgNxk/2O1meblWBQDim72cJ wDLu+FSmfmocV5i2ZPBwuP22/08mrtGq5nnzkzTF1DVtk2wUHdIFD0A3M8ZMC4E1FiMj OHXVTZbHgJFE1sP2GaN658650y6BuNpA4lFbTbklesmKiRzj3avWrK3XoFbY0PgfSngZ JShQ== X-Gm-Message-State: APjAAAWQnNDX85B8j8OG/bi1F53wPDtVDSpW7wvIbm/qZFzQmPOwmgcg e4uSurWRD3ElxMn2dwfzv9GRoF09VEVURw== X-Google-Smtp-Source: APXvYqwT9yMSbkMGyeOXxwggPQfUzJ8+fjW8Li6uLdtiLKL/kjpPtEajzPTB+8Hby/g6KZNl+YoI5w== X-Received: by 2002:a17:906:948:: with SMTP id j8mr24753837ejd.240.1557794977402; Mon, 13 May 2019 17:49:37 -0700 (PDT) Original-Received: from localhost ([2a02:8084:20e2:c380:8cad:ae29:555d:852d]) by smtp.gmail.com with ESMTPSA id s57sm4117490edd.54.2019.05.13.17.49.36 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 13 May 2019 17:49:36 -0700 (PDT) In-Reply-To: <87h89yqiwu.fsf@gmail.com> (Federico Tedin's message of "Mon, 13 May 2019 16:00:17 -0300") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::541 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:236487 Archived-At: Thanks for working on this. Federico Tedin writes: > Any feedback is greatly appreciated. Just some minor comments and questions from me. > diff --git a/lisp/tempo.el b/lisp/tempo.el > index 28afbec0f4..66192439cc 100644 > --- a/lisp/tempo.el > +++ b/lisp/tempo.el > @@ -1,4 +1,4 @@ > -;;; tempo.el --- Flexible template insertion > +;;; tempo.el --- Flexible template insertion -*- lexical-binding: t; -*- > > ;; Copyright (C) 1994-1995, 2001-2019 Free Software Foundation, Inc. > > @@ -211,6 +211,9 @@ tempo-region-stop > (make-variable-buffer-local 'tempo-match-finder) > (make-variable-buffer-local 'tempo-collection) > (make-variable-buffer-local 'tempo-dirty-collection) > +(make-variable-buffer-local 'tempo-named-insertions) > +(make-variable-buffer-local 'tempo-region-start) > +(make-variable-buffer-local 'tempo-region-stop) Why not define these with defvar-local instead? > ;;; Functions > > @@ -268,11 +271,14 @@ tempo-define-template > - `n>': Inserts a newline and indents line. > - `o': Like `%' but leaves the point before the newline. > - nil: It is ignored. > - - Anything else: It is evaluated and the result is treated as an > - element to be inserted. One additional tag is useful for these > - cases. If an expression returns a list (l foo bar), the elements > - after `l' will be inserted according to the usual rules. This makes > - it possible to return several elements from one expression." > + - Anything else: Each function in `tempo-user-elements' is called > + with it as argument until one of them returns non-nil, and the > + result is inserted. If all of them return nil, it is evaluated and ^^^ Missing space. > + the result is treated as an element to be inserted. One additional > + tag is useful for these cases. If an expression returns a list (l > + foo bar), the elements after `l' will be inserted according to the > + usual rules. This makes it possible to return several elements > + from one expression." > (let* ((template-name (intern (concat "tempo-template-" > name))) > (command-name template-name)) > diff --git a/test/lisp/tempo-tests.el b/test/lisp/tempo-tests.el > new file mode 100644 > index 0000000000..4ce0830700 > --- /dev/null > +++ b/test/lisp/tempo-tests.el > @@ -0,0 +1,229 @@ > +;;; tempo-tests.el --- Test suite for tempo.el -*- lexical-binding: t; -*- > + > +;; Copyright (C) 2019 Free Software Foundation, Inc. > + > +;; Author: Federico Tedin > +;; Keywords: tempo No need for this; see (info "(elisp) Library Headers") and M-x finder-list-keywords RET for valid keywords and their meanings. > + > +;; This file is part of GNU Emacs. > + > +;; GNU Emacs is free software: you can redistribute it and/or modify > +;; it under the terms of the GNU General Public License as published by > +;; the Free Software Foundation, either version 3 of the License, or > +;; (at your option) any later version. > + > +;; GNU Emacs is distributed in the hope that it will be useful, > +;; but WITHOUT ANY WARRANTY; without even the implied warranty of > +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +;; GNU General Public License for more details. > + > +;; You should have received a copy of the GNU General Public License > +;; along with GNU Emacs. If not, see . > + > +;;; Code: > + > +(require 'tempo) > + > +(ert-deftest string-element-test () Shouldn't test names have a package-specific prefix? > + "Test a template containing a string element." > + (with-temp-buffer > + (tempo-define-template "test" '("GNU Emacs Tempo test")) > + (tempo-insert-template 'tempo-template-test nil) > + (should (equal (buffer-string) "GNU Emacs Tempo test")))) > + > +(ert-deftest p-bare-element-test () > + "Test a template containing a bare `p' element." > + (with-temp-buffer > + (tempo-define-template "test" '("abcde" p)) > + (tempo-insert-template 'tempo-template-test nil) > + (tempo-forward-mark) > + (should (equal (point) 6)))) > + > +(ert-deftest r-bare-element-test () > + "Test a template containing a bare `r' element." > + (with-temp-buffer > + (tempo-define-template "test" '("abcde" r "ghijk")) > + (insert "F") > + (set-mark-command nil) Why not set-mark? > + (goto-char (point-min)) > + (tempo-insert-template 'tempo-template-test t) > + (should (equal (buffer-string) "abcdeFghijk")))) > + > +(ert-deftest p-element-test () > + "Testing template containing a `p' (prompt) element." > + (with-temp-buffer > + (tempo-define-template "test" '("hello " (p ">"))) > + (setq tempo-interactive t) AFAICT this changes the user option's global value, which both normal programs and tests should avoid doing; see (info "(ert) Tests and Their Environment"). Just let-bind instead. > + (cl-letf (((symbol-function 'read-string) (lambda (_) "world"))) Before calling cl-letf you need (eval-when-compile (require 'cl-lib)). Also, shouldn't the lambda accept (&rest _) args? > + (tempo-insert-template 'tempo-template-test nil)) > + (should (equal (buffer-string) "hello world")))) > + > +(ert-deftest P-element-test () > + "Testing template containing a `P' (prompt) element." > + (with-temp-buffer > + (tempo-define-template "test" '("hello " (P ">"))) > + (setq tempo-interactive nil) ;; `P' will ignore this RHS comments are usually written with a single semicolon; see (info "(elisp) Comment Tips"). > + (cl-letf (((symbol-function 'read-string) (lambda (_) "world"))) > + (tempo-insert-template 'tempo-template-test nil)) > + (should (equal (buffer-string) "hello world")))) [...] > +(ert-deftest tempo-user-elements-test () > + "Testing a template containing an element to be used as an argument > +in a call to a function in `tempo-user-elements'." > + (with-temp-buffer > + (make-variable-buffer-local 'tempo-user-elements) This makes tempo-user-elements automatically buffer-local globally. I think you're after make-local-variable instead. > + (add-to-list 'tempo-user-elements (lambda (x) (int-to-string (* x x)))) > + (tempo-define-template "test" '(1 " " 2 " " 3 " " 4)) > + (tempo-insert-template 'tempo-template-test nil) > + (should (equal (buffer-string) "1 4 9 16")))) > + > +(ert-deftest expand-tag-test () > + "Testing expansion of a template with a tag." > + (with-temp-buffer > + (tempo-define-template "test" '("Hello, World!") "hello") > + (insert "hello") > + (tempo-complete-tag) > + (should (equal (buffer-string) "Hello, World!")))) > + > +(ert-deftest expand-partial-tag-test () > + "Testing expansion of a template with a tag, with a partial match." > + (with-temp-buffer > + (tempo-define-template "test" '("Hello, World!") "hello") > + (insert "hel") > + (tempo-complete-tag) > + (should (equal (buffer-string) "Hello, World!")))) > + > +(provide 'tempo-tests) > +;;; tempo-tests.el ends here -- Basil