emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to bo


From: ELPA Syncer
Subject: [elpa] externals/tomelr ebe5959174 75/84: feat: Auto-coerce string to boolean
Date: Tue, 3 May 2022 09:58:15 -0400 (EDT)

branch: externals/tomelr
commit ebe5959174812ffc3cf7d88040b854599b15a88a
Author: Kaushal Modi <kaushal.modi@gmail.com>
Commit: Kaushal Modi <kaushal.modi@gmail.com>

    feat: Auto-coerce string to boolean
---
 test/all-tests.el |  1 +
 test/tcoerce.el   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tomelr.el         | 23 +++++++++++++++++++---
 3 files changed, 80 insertions(+), 3 deletions(-)

diff --git a/test/all-tests.el b/test/all-tests.el
index c25fd78e48..03f614f844 100644
--- a/test/all-tests.el
+++ b/test/all-tests.el
@@ -31,3 +31,4 @@
 (require 'ttable)
 (require 'ttable-array)
 (require 'tplist)
+(require 'tcoerce)
diff --git a/test/tcoerce.el b/test/tcoerce.el
new file mode 100644
index 0000000000..4e10dd7714
--- /dev/null
+++ b/test/tcoerce.el
@@ -0,0 +1,59 @@
+;; -*- lexical-binding: t; -*-
+
+;; Authors: Kaushal Modi <kaushal.modi@gmail.com>
+
+;; 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 <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Test type coercing from strings.
+
+;;; Code:
+(require 'tomelr)
+
+;;;; Boolean Coercing
+(ert-deftest test-coerce-boolean-yes ()
+  (let ((tomelr-coerce-to-types '(boolean))
+        (inp '(
+               ((key1 . "true"))
+               ((key2 . "false"))
+               ))
+        (ref '(
+               "key1 = true"
+               "key2 = false"
+               ))
+        out)
+    (dolist (el inp)
+      (push (tomelr-encode el) out))
+    (should (equal ref (nreverse out)))))
+
+(ert-deftest test-coerce-boolean-no ()
+  (let ((tomelr-coerce-to-types '())
+        (inp '(
+               ((key1 . "true"))
+               ((key2 . "false"))
+               ))
+        (ref '(
+               "key1 = \"true\""
+               "key2 = \"false\""
+               ))
+        out)
+    (dolist (el inp)
+      (push (tomelr-encode el) out))
+    (should (equal ref (nreverse out)))))
+
+
+(provide 'tcoerce)
diff --git a/tomelr.el b/tomelr.el
index c9a0694ced..08f877d562 100644
--- a/tomelr.el
+++ b/tomelr.el
@@ -35,13 +35,22 @@
 
 ;;; Variables
 
-(defvar tomelr-false '(:false 'false "false")
+(defvar tomelr-false '(:false 'false)
   "S-exp values to be interpreted as TOML `false'.")
 
 (defvar tomelr-encoding-default-indentation "  "
   "String used for a single indentation level during encoding.
 This value is repeated for each further nested element.")
 
+(defvar tomelr-coerce-to-types '(boolean)
+  "List of TOML types to which the TOML strings will be attempted to be 
coerced.
+
+Valid symbols that can be present in this list: boolean, integer, float
+
+For example, if this list contains `boolean' and if a string
+value is exactly \"true\", it will coerce to TOML boolean
+`true'.")
+
 ;;;; Internal Variables
 (defvar tomelr--print-indentation-prefix "\n"
   "String used to start indentation during encoding.")
@@ -117,8 +126,16 @@ This macro sets up appropriate variable bindings for
 (defun tomelr--print-boolean (object)
   "Insert TOML boolean true or false at point if OBJECT is a boolean.
 Return nil if OBJECT is not recognized as a TOML boolean."
-  (prog1 (setq object (cond ((eq object t) "true")
-                            ((member object tomelr-false) "false")))
+  (prog1 (setq object (cond ((or
+                              (eq object t)
+                              (and (member 'boolean tomelr-coerce-to-types)
+                                   (equal object "true")))
+                             "true")
+                            ((or
+                              (member object tomelr-false)
+                              (and (member 'boolean tomelr-coerce-to-types)
+                                   (equal object "false")))
+                             "false")))
     (and object (insert object))))
 
 ;;;; Strings



reply via email to

[Prev in Thread] Current Thread [Next in Thread]