From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id WPAIGHC/1l4xKQAA0tVLHw (envelope-from ) for ; Tue, 02 Jun 2020 21:06:56 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id iBrvE3C/1l4IKAAA1q6Kng (envelope-from ) for ; Tue, 02 Jun 2020 21:06:56 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 97ADB9400AF for ; Tue, 2 Jun 2020 21:06:55 +0000 (UTC) Received: from localhost ([::1]:56116 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgE7a-0008UQ-4w for larch@yhetil.org; Tue, 02 Jun 2020 17:06:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39874) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgDsl-0004tN-Rg for emacs-orgmode@gnu.org; Tue, 02 Jun 2020 16:51:35 -0400 Received: from mail-qk1-x72a.google.com ([2607:f8b0:4864:20::72a]:35521) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jgDsk-0005Fo-E0 for emacs-orgmode@gnu.org; Tue, 02 Jun 2020 16:51:35 -0400 Received: by mail-qk1-x72a.google.com with SMTP id n141so14005608qke.2 for ; Tue, 02 Jun 2020 13:51:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=8Jubyk7xvpEquhZoV4Ww3RNZhFZQCTtcTLu/cA/35lY=; b=n0M+GBxZR8vkiCr4u2GA1+KJ8Pd1t8fThV0CVJNaA+Y/5fGGZD498DQ240fdD1kvFj gNuOE3jdIM+ktdw4td85soZc64ALaLjZGIUhSbH2TexQrf6PSpRRPe+5vkRmFesNQ0Da NAFsrQ94guY5yp1qfy1B8/sl2+eYsMTZYyJR2xavd6PXGGE2jr0jhM7iWQRSR8QkEWsz pwB3GLMkprQyLCALZAorX5M1/p+762XNPsJMlguJJphkVH+cqz9lQbtX42DjhQrcl+NY DXdS14EqQG+sCG+10Q82kz6cibUimlAORHdvfBQfekxaNOZi1rV7XfpA3lh+UC19H6u0 4KrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=8Jubyk7xvpEquhZoV4Ww3RNZhFZQCTtcTLu/cA/35lY=; b=Ocd5one73SWCu67kM+B1dRnykAoJ2PJOdw10En/WjZB+T9Jt0NcqkcLfWJjwvgPTcQ HeqE1NTnjJMqnoYlD7+ijDVeKT1VeLRKUKuEN+PFoIIaL4ydoIDScf9atmQx8Zdm+JC1 79mRRH2o9Y4rWBX2zP3xKQfsn70RyTnhFLOIp3ZaB2FGY/bDgYgG5Gm7UcRpVxs5iIiR UnRIiY+KkhWgTb4FGm77FaTOwjsIRAy0KXuN1aVrOfIiq2ZgpuDeEaT4QrEGxI1z8Nn4 4mhRiGrDPx06hTskuDlBYPAtj9gqnfH9YqNNdSh9HHn2SIw4P4351v2U+ji4X6VF2JeG 1ozg== X-Gm-Message-State: AOAM530b5SQUgqcdtJbTD/MnooJga4ob2c7ov7MdjGCj2YLNXX9epkx4 GzTtj2xYozPrhsx/Dh66AZs= X-Google-Smtp-Source: ABdhPJzPgXf3FLPu66+/qWc0BtV+7u2a7DNkx0Y4nE7e+pS/D2YTMQA8+XqQXobAmqZoJ4fJevl5Fg== X-Received: by 2002:ae9:e88c:: with SMTP id a134mr22909146qkg.316.1591131092502; Tue, 02 Jun 2020 13:51:32 -0700 (PDT) Received: from [192.168.43.226] ([181.179.18.165]) by smtp.gmail.com with ESMTPSA id h22sm31340qtk.63.2020.06.02.13.51.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 Jun 2020 13:51:31 -0700 (PDT) To: emacs-orgmode@gnu.org From: Mario Frasca Subject: [PATCH] some minimal refactoring for the sake of unit-testing, and a, couple of tests. Message-ID: <2abe19ad-2237-991d-5325-82b3f870485e@gmail.com> Date: Tue, 2 Jun 2020 15:51:13 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Received-SPF: pass client-ip=2607:f8b0:4864:20::72a; envelope-from=mariotomo@gmail.com; helo=mail-qk1-x72a.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-Mailman-Approved-At: Tue, 02 Jun 2020 17:06:28 -0400 X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (body hash did not verify) header.d=gmail.com header.s=20161025 header.b=n0M+GBxZ; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: 0.09 X-TUID: 7bJ30SQbDroC From 0ee824cccf793734fa18d42b67d44dae2a2e136e Mon Sep 17 00:00:00 2001 From: mfrasca Date: Tue, 2 Jun 2020 15:48:46 -0500 Subject: [PATCH] some minimal refactoring for the sake of unit-testing, and a  couple of tests. ---  lisp/org-plot.el              |  18 ++-  testing/lisp/test-org-plot.el | 207 ++++++++++++++++++++++++++++++++++  2 files changed, 220 insertions(+), 5 deletions(-)  create mode 100644 testing/lisp/test-org-plot.el diff --git a/lisp/org-plot.el b/lisp/org-plot.el index a23195d2a..f4526db13 100644 --- a/lisp/org-plot.el +++ b/lisp/org-plot.el @@ -91,8 +91,8 @@ Return value is the point at the beginning of the table."    (while (not (or (org-at-table-p) (< 0 (forward-line 1)))))    (goto-char (org-table-begin))) -(defun org-plot/collect-options (&optional params) -  "Collect options from an org-plot `#+Plot:' line. +(defun org-plot/collect-line-options (&optional params) +  "Collect options from the org-plot `#+Plot:' line at point.  Accepts an optional property list PARAMS, to which the options  will be added.  Returns the resulting property list."    (interactive) @@ -101,6 +101,16 @@ will be added.  Returns the resulting property list."      (org-plot/add-options-to-plist params (match-string 1 line))        params))) +(defun org-plot/collect-table-options (&optional params) +  "Collect options from the `#+Plot:' lines preceding the current table. +Point must immediately after last `#+Plot:' line. +Accepts an optional property list PARAMS, to which the options will be added. +Returns the accumulated property list." +  (save-excursion (while (and (equal 0 (forward-line -1)) +                  (looking-at "[[:space:]]*#\\+")) +            (setq params (org-plot/collect-line-options params)))) +  params) +  (defun org-plot-quote-timestamp-field (s)    "Convert field S from timestamp to Unix time and export to gnuplot."    (format-time-string org-plot-timestamp-fmt (org-time-string-to-time s))) @@ -299,9 +309,7 @@ line directly before or after the table."            (plist-put params :labels (nth 0 table))) ; headers to labels      (setf table (delq 'hline (cdr table)))) ; clean non-data from table        ;; Collect options. -      (save-excursion (while (and (equal 0 (forward-line -1)) -                  (looking-at "[[:space:]]*#\\+")) -            (setf params (org-plot/collect-options params)))) +      (setq params (org-plot/collect-table-options params))        ;; Dump table to datafile (very different for grid).        (pcase (plist-get params :plot-type)      (`2d   (org-plot/gnuplot-to-data table data-file params)) diff --git a/testing/lisp/test-org-plot.el b/testing/lisp/test-org-plot.el new file mode 100644 index 000000000..b23075050 --- /dev/null +++ b/testing/lisp/test-org-plot.el @@ -0,0 +1,207 @@ +;;; test-org-plot.el --- Tests for Org Plot library -*- lexical-binding: t; -*- + +;; Copyright (C) 2020  Mario Frasca + +;; Author: Mario Frasca + +;; This file is not part of GNU Emacs. + +;; This program 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. + +;; This program 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 this program.  If not, see . + +;;; Code: + +(require 'org-test) +(require 'org-plot) + + +;; General auxiliaries + + +(ert-deftest test-org-plot/collect-line-options () +  "Test `org-plot/collect-line-options' specifications." +  ;; no options specified in table, no defaults +  (should +   (equal nil +          (org-test-with-temp-text +              "| 1 |" +            (org-plot/collect-line-options)))) +  ;; no options specified in table, keeps all defaults +  (should +   (equal '(:ind 1 :deps 2 3) +          (org-test-with-temp-text +              "| 1 |" +            (org-plot/collect-line-options '(:ind 1 :deps 2 3))))) +  ;; the independent column +  (should +   (equal '(:ind 1) +          (org-test-with-temp-text +              "#+PLOT: ind:1\n| 1 |" +            (org-plot/collect-line-options)))) +  ;; overruling default +  (should +   (equal '(:ind 2) +          (org-test-with-temp-text +              "#+PLOT: ind:2\n| 1 | 2 |" +            (org-plot/collect-line-options '(:ind 1))))) +  ;; appends to already collected +  (should +   (equal '(:deps (1 3) :ind 2 ) +          (org-test-with-temp-text +              "#+PLOT: ind:2\n| 1 | 2 | 3 |" +            (org-plot/collect-line-options '(:deps (1 3)))))) +  ;; appends to already collected +  (should +   (equal '(:ind 2 :deps (1 3) ) +          (org-test-with-temp-text +              "#+PLOT: ind:2\n| 1 | 2 | 3 |" +            (org-plot/collect-line-options '(:ind 1 :deps (1 3)))))) +  ;; multiple options from single line +  (should  ; `op' in add-options-to-plist defines order of options +   (equal '(:plot-type 2d :title "example table" :ind 1 :deps (2 3) :with (lines points)) +          (org-test-with-temp-text +              "#+PLOT: title:\"example table\" ind:1 deps:(2 3) type:2d with:(lines points)" +            (org-plot/collect-line-options)))) +  ;; with, as a single string +  (should +   (equal '(:with lines) +          (org-test-with-temp-text +              "#+PLOT: with:lines\n| 1 | 2 | 3 |" +            (org-plot/collect-line-options)))) +  ;; with, as a list +  (should +   (equal '(:with (lines hist)) +          (org-test-with-temp-text +              "#+PLOT: with:(lines hist)\n| 1 | 2 | 3 |" +            (org-plot/collect-line-options))))) + + +(ert-deftest test-org-plot/collect-table-options () +  "Test `org-plot/collect-table-options' specifications." +  ;; all options on a single lines +  (should  ; `op' in add-options-to-plist defines order of options +   (equal '(:plot-type 2d :title "example table" :ind 1 :deps (2 3) :with (lines points)) +          (org-test-with-temp-text +              "#+PLOT: title:\"example table\" ind:1 deps:(2 3) type:2d with:(lines points)\n" +        (goto-char (point-max))  ; point must be on line after last options line +            (org-plot/collect-table-options)))) +  ;; multiple options from multiple lines +  (should  ; `op' in add-options-to-plist defines order of options +   (equal '(:plot-type 2d :with (lines points) :title "example table" :ind 1 :deps (2 3)) +          (org-test-with-temp-text +              "#+PLOT: title:\"example table\" ind:1 deps:(2 3)\n#+PLOT: type:2d with:(lines points)\n" +        (goto-char (point-max))  ; point must be on line after last options line +            (org-plot/collect-table-options)))) +  ;; one option per line, several lines (collected going up) +  (should +   (equal '(:with (lines points) :plot-type 2d :deps (2 3) :ind 1 :title "example table") +          (org-test-with-temp-text +              "#+PLOT: title:\"example table\"\n#+PLOT: ind:1\n#+PLOT: deps:(2 3)\n#+PLOT: type:2d\n#+PLOT: with:(lines points)\n" +        (goto-char (point-max))  ; point must be on line after last options line +            (org-plot/collect-table-options))))) + + +(ert-deftest test-org-plot/goto-nearest-table () +  "Test `org-plot/goto-nearest-table' specifications." +  ;; at table beginning stays put +  (should +   (= 1 +      (org-test-with-temp-text +       "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |" +       (org-plot/goto-nearest-table)))) +  ;; anywhere within the table moves back to table beginning +  (should +   (= 1 +      (org-test-with-temp-text +       "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |" +       (goto-char 4) +       (org-plot/goto-nearest-table)))) +  ;; on newline at end of table, goes back to table beginning +  (should +   (= 1 +      (org-test-with-temp-text +       "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n" +       (goto-char (point-max)) +       (org-plot/goto-nearest-table)))) +  ;; farther away than newline at end of last table, moves to end of buffer +  (should +   (= 44 +      (org-test-with-temp-text +       "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n\n" +       (goto-char (point-max)) +       (org-plot/goto-nearest-table)))) +  ;; moves to beginning of data, not metadata +  (should +   (= 26 +      (org-test-with-temp-text +       "#+PLOT: ind:1 deps:(2 3)\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |" +       (org-plot/goto-nearest-table)))) +  ;; in case there's two tables +  (should +   (= 30 +      (org-test-with-temp-text +       "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n\n| 1 | 2 | 3 |" +       (goto-char (point-max)) +       (org-plot/goto-nearest-table))))) + + +(ert-deftest test-org-plot/gnuplot-script () +  "Regression Test for `org-plot/gnuplot-script'." +  (should +   (equal "reset +set datafile separator \"\\t\" +plot FILENAME using 1:2 with points title '2',\\ +    FILENAME using 1:3 with points title '3'" +          (org-test-with-temp-text +           "| 1 | 2 | 3 |\n| 1 | 2 | 3 |\n| 1 | 2 | 3 |" +           (let ((data-file (make-temp-file "org-plot")) +                 (table (org-table-to-lisp)) +                 (params '(:ind 1 :deps (2 3) :plot-type 2d :with points))) +             (org-plot/gnuplot-to-data table data-file params) +             (org-plot/gnuplot-script data-file 3 params t) +             (replace-regexp-in-string (concat "'" (regexp-quote data-file) "'") +                                       "FILENAME" + (org-plot/gnuplot-script data-file 3 params) +                                       nil 'LITERAL))))) +  (should +   (equal "reset +set title 'example table' +set datafile separator \"\\t\" +plot FILENAME using 1:2 with lines title 'ylab',\\ +    FILENAME using 1:3 with lines title 'zlab'" +          (org-test-with-temp-text +       "\ +#+PLOT: title:\"example table\" ind:1 deps:(2 3) type:2d with:lines +#+PLOT: labels:(\"xlab\" \"ylab\" \"zlab\") +#+TBLNAME:org-plot-example-1 +|   x |    y |    z | +|-----+------+------| +| 0.1 | 0.42 | 0.37 | +| 0.2 | 0.31 | 0.33 | +| 0.3 | 0.24 | 0.28 | +| 0.4 | 0.27 | 0.23 | +" +           (org-plot/goto-nearest-table) +           (let ((data-file (make-temp-file "org-plot")) +                 (table (org-table-to-lisp)) +                 params) +         (setq params (org-plot/collect-table-options params)) +             (org-plot/gnuplot-to-data table data-file params) +             (org-plot/gnuplot-script data-file 3 params t) +             (replace-regexp-in-string (concat "'" (regexp-quote data-file) "'") +                                       "FILENAME" + (org-plot/gnuplot-script data-file 3 params) +                                       nil 'LITERAL)))))) + +(provide 'test-org-plot) +;;; test-org-plot.el end here -- 2.20.1