From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id cEfrFnq+114bWAAA0tVLHw (envelope-from ) for ; Wed, 03 Jun 2020 15:15:06 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id +GfsEnq+117fDQAAbx9fmQ (envelope-from ) for ; Wed, 03 Jun 2020 15:15:06 +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 D15FA9404C8 for ; Wed, 3 Jun 2020 15:15:05 +0000 (UTC) Received: from localhost ([::1]:46526 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jgV6e-00045r-PO for larch@yhetil.org; Wed, 03 Jun 2020 11:15:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38614) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jgV5q-0003Os-Ua for emacs-orgmode@gnu.org; Wed, 03 Jun 2020 11:14:14 -0400 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]:41704) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jgV5p-00076s-BA for emacs-orgmode@gnu.org; Wed, 03 Jun 2020 11:14:14 -0400 Received: by mail-qv1-xf42.google.com with SMTP id er17so1256039qvb.8 for ; Wed, 03 Jun 2020 08:14:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=BNw9i2U5rUGOHsfW3oUe6ssHundO1ijIvLT8uNBtF8E=; b=HjIAcUwOis+tnd4GYEvOIGP57/wY+f4IUFxOjMPjwCFwdFijRvt6w2Nmn+dSI5puSI vk/x829kOSNMVgXILzr4u3Or6lMIHwlNJzq7jawhnhf4ZRnljKB+Um7R9/gw/o/JdKrP a6KPrczLeZv7mLj9kaFNd7Ffghcqsku+lmIBx9VQcopZ1QV7lq9XbSv09jxfoMQNnx8S shZqRJLHHqDG6FYxbxOnoZ35MdW5eAFxkf+bbrndlsj8KVaQ0xQx0dgfzMRjtB2foOyd tH3rkS9twwEDCv0RLjhhO615v3In29qKfq3oslLeYMoqASguu4Rhf2bK6ia/433OvN4B JtGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=BNw9i2U5rUGOHsfW3oUe6ssHundO1ijIvLT8uNBtF8E=; b=KKWQRPXDfAIkQlPa0Df53DmTHQ/dHv820F0CuXb1F6zhztG7wsxKxbByjj+iHA5y2Z cJsOFRrXjnsk5Nq5/XbyJYak+Ofu3Bo+1Vnpq5SRFHNc8elxh0dTptyoAQMwHRZVULDJ m5EmvJLBKl4JOq88hgB8k6GF/v/CtL5W89QlfsmW/6rObX/G2XYb5q9CFnM6zeaE6fxF AigfC5VGolI2Lma7Fsz0HiKvZjDSBML9BawCidCJiyrUBko2FvOddC6WIeXaH7y6ZP8o XIOQgYOyiTv43biOnz+RZpoZs+7NqDctTKFGapudJ8D5EzJv/R/s0ll1bxDa8Ay9e9Lj d6YQ== X-Gm-Message-State: AOAM530rL8HeHr999us/cIF/FPsAEst1IJX/oeejRcxD3ilYRBALJNEz f3mh1bZHzmR1PHtev3kUic4= X-Google-Smtp-Source: ABdhPJyucpYXbSknJ2XxFKqtTSU1w2lOulMyS06CgeG7hTs5J0bX3Yp6Z5RScWs1BwuplcnJp2b5RA== X-Received: by 2002:a0c:a899:: with SMTP id x25mr273632qva.114.1591197252396; Wed, 03 Jun 2020 08:14:12 -0700 (PDT) Received: from [192.168.43.226] ([181.179.18.165]) by smtp.gmail.com with ESMTPSA id n1sm1718880qkf.54.2020.06.03.08.14.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Jun 2020 08:14:10 -0700 (PDT) Subject: Re: [PATCH] some minimal refactoring for the sake of unit-testing, and a, couple of tests. From: Mario Frasca To: emacs-orgmode@gnu.org References: <2abe19ad-2237-991d-5325-82b3f870485e@gmail.com> Message-ID: <3a1d33dd-edec-1a7f-9115-10691442a877@gmail.com> Date: Wed, 3 Jun 2020 10:13:54 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 In-Reply-To: <2abe19ad-2237-991d-5325-82b3f870485e@gmail.com> Content-Type: multipart/mixed; boundary="------------2083A74A5CC1AA57B939391F" Content-Language: en-US Received-SPF: pass client-ip=2607:f8b0:4864:20::f42; envelope-from=mariotomo@gmail.com; helo=mail-qv1-xf42.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-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=HjIAcUwO; 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.19 X-TUID: bO/SYs0EBsHP This is a multi-part message in MIME format. --------------2083A74A5CC1AA57B939391F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit see attachment --------------2083A74A5CC1AA57B939391F Content-Type: text/x-patch; charset=UTF-8; name="0001-org-plot.el-adding-unit-tests.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-org-plot.el-adding-unit-tests.patch" >From 065ee9e35e71b0b7c0c0c2a8842909830225d962 Mon Sep 17 00:00:00 2001 From: mfrasca Date: Tue, 2 Jun 2020 15:48:46 -0500 Subject: [PATCH] org-plot.el: adding unit tests this patch contains some minimal refactoring for the sake of unit-testing, and a couple of tests in the (new) file `testing/lisp/test-org-plot.el'. --- 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 --------------2083A74A5CC1AA57B939391F--