diff --git a/rpgdm-tables.el b/rpgdm-tables.el index 6b60637..94d0fd4 100644 --- a/rpgdm-tables.el +++ b/rpgdm-tables.el @@ -25,10 +25,11 @@ ;; ;;; Code: -(defvar rpgdm-base ".") -(require 'rpgdm-dice (expand-file-name "rpgdm-dice.el" rpgdm-base) t) -(require 'rpgdm-tables-freq (expand-file-name "rpgdm-tables-freq.el" rpgdm-base) t) -(require 'rpgdm-tables-dice (expand-file-name "rpgdm-tables-dice.el" rpgdm-base) t) +(require 'ert) + +(require 'rpgdm-dice) +(require 'rpgdm-tables-freq) +(require 'rpgdm-tables-dice) (defvar rpgdm-tables-directory @@ -41,7 +42,11 @@ (defun rpgdm-tables-clear () "Clear previously loaded tables." (interactive) - (setq rpgdm-tables (make-hash-table :test 'equal))) + (clrhash rpgdm-tables)) + +(defun rpgdm-tables-empty-p () + "Return non-nil if no tables have been loaded." + (= (hash-table-count rpgdm-tables) 0)) (defun rpgdm-tables-load (&optional filepath) "Read and parse table files located in FILEPATH directory. @@ -118,12 +123,30 @@ dice table (see `rpgdm-tables--choose-dice-table')." For instance, the string: 'You found a [chair/box/statue]' would be converted randomly to something like: 'You found a box.'" (let ((regexp (rx "[" (+? any) "/" (+? any) "]")) - (subbed (lambda (str) (--> str - (substring it 1 -1) - (s-split (rx (*? space) "/" (*? space)) it) - (seq-random-elt it))))) + (subbed (lambda (str) (--> str + (substring it 1 -1) + (s-split (rx "/") it) + (seq-random-elt it) + (s-trim it))))) (replace-regexp-in-string regexp subbed str))) +(ert-deftest rpgdm-tables--choose-string-list () + (let ((empty-string "") + (no-op-string "This is just a phrase.") + (two-choices "We can have [this/that]") + (two-choices1 "We can have this") + (two-choices2 "We can have that") + (tri-choices "We can have [this / that / the other].") + (tri-choices1 "We can have this.") + (tri-choices2 "We can have that.") + (tri-choices3 "We can have the other.")) + + (should (equal (rpgdm-tables--choose-string-list empty-string) empty-string)) + (should (equal (rpgdm-tables--choose-string-list no-op-string) no-op-string)) + (let ((chosen (rpgdm-tables--choose-string-list two-choices))) + (should (or (equal chosen two-choices1) (equal chosen two-choices2)))) + (let ((chosen (rpgdm-tables--choose-string-list tri-choices))) + (should (or (equal chosen tri-choices1) (equal chosen tri-choices2) (equal chosen tri-choices3)))))) ;; I originally thought that I could have a single regular expression that ;; matched all possible tables, but that is a bit too complicated. The following