From fbb0ee29c4b84316a7cc640d57f37a31800258a1 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Wed, 13 Mar 2024 00:52:56 -0700 Subject: [PATCH] gptel-org-test: Add mores tests for org conversion * test/gptel-org-test.el: Rename file from test-gptel-org.el, and add more tests. --- test/gptel-org-test.el | 97 ++++++++++++++++++++++++++++++++++++++++++ test/test-gptel-org.el | 32 -------------- 2 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 test/gptel-org-test.el delete mode 100644 test/test-gptel-org.el diff --git a/test/gptel-org-test.el b/test/gptel-org-test.el new file mode 100644 index 0000000..13f8af3 --- /dev/null +++ b/test/gptel-org-test.el @@ -0,0 +1,97 @@ +;; -*- lexical-binding: t; -*- +(require 'ert) +(require 'gptel) +(require 'cl-generic) + +;;; Methods for collecting data from HTTP logs +(cl-defgeneric gptel-test--read-response (backend &optional from to)) +(cl-defmethod gptel-test--read-response ((_backend gptel-openai) &optional from to) + (setq from (or from (point-min)) + to (or to (point-max))) + (save-restriction + (narrow-to-region from to) + (goto-char from) + (let ((strs) (json-object-type 'plist)) + (while (re-search-forward "^data: *" nil t) + ;; (forward-char) + (condition-case-unless-debug err + (thread-first + (json-parse-buffer :object-type 'plist) + ;; (json-read) + (map-nested-elt '(:choices 0 :delta :content)) + (push strs)) + (json-readtable-error strs) + (:success strs))) + (setq strs (delq nil (nreverse strs)))))) + +;;; Basic tests for markdown to org conversion +(let ((string-sequences + '(("" "```" "cpp" " +" "#include" " <" "cstdio" "> + +" "int" " main" "()" " { +" " " " printf" "(\"" "``" "``" "`" "\n" "\"); +" " " " return" " " "0" "; +" "} +" "```") + ("" "Here" " is" " a" " simple" " C" "++" " program" " that" " uses" " the" " `" "printf" "`" " function" " to" " print" " the" " string" " containing" " " "5" " back" "ticks" ": + +" "```" "cpp" " +" "#include" " <" "iostream" "> + +" "int" " main" "()" " { +" " " " //" " Using" " printf" " to" " print" " " "5" " back" "ticks" " +" " " " printf" "(\"" "``" "``" "`" "\n" "\"); + +" " " " return" " " "0" "; +" "} +" "``" "` + +" "In" " this" " code" " snippet" "," " `" "printf" "(\"" "``" "``" "`" "\n" "\");" "`" " is" " used" " to" " print" " the" " string" " \"" "``" "```" "\"" " followed" " by" " a" " newline" " character" ".") + ("" "In" " the" " definition" " of" " the" " `" "struct" " json" "_parser" "`," " the" " line" " `" "L" "isp" "_Object" " *" "object" "_workspace" ";" "`" " declares" " a" " pointer" " named" " `" "object" "_workspace" "`" " of" " type" " `" "L" "isp" "_Object" "`.\n\n" "The" " aster" "isk" " (*)" " in" " `" "L" "isp" "_Object" " *" "object" "_workspace" ";" "`" " is" " the" " pointer" " ind" "irection" " operator" " in" " C" "." " It" " indicates" " that" " `" "object" "_workspace" "`" " is" " a" " pointer" " to" " an" " object" " of" " type" " `" "L" "isp" "_Object" "`." " This" " means" " that" " `" "object" "_workspace" "`" " will" " store" " the" " memory" " address" " (" "location" ")" " of" " a" " `" "L" "isp" "_Object" "`" " variable" " rather" " than" " storing" " the" " actual" " `" "L" "isp" "_Object" "`" " value" ".\n\n" "Therefore" "," " `" "object" "_workspace" "`" " will" " be" " used" " to" " point" " to" " or" " reference" " locations" " in" " memory" " where" " `" "L" "isp" "_Object" "`" " instances" " are" " stored" "." " This" " allows" " the" " `" "struct" " json" "_parser" "`" " to" " store" " and" " work" " with" " `" "L" "isp" "_Object" "`" " instances" " indirectly" " through" " pointers" "."))) + (converted-sequences + '("#+begin_src cpp +#include + +int main() { + printf(\"`````\n\"); + return 0; +} +#+end_src" + "Here is a simple C++ program that uses the =printf= function to print the string containing 5 backticks: + +#+begin_src cpp +#include + +int main() { + // Using printf to print 5 backticks + printf(\"`````\n\"); + + return 0; +} +#+end_src + +In this code snippet, =printf(\"`````\n\");= is used to print the string \"=\" followed by a newline character." + "In the definition of the =struct json_parser=, the line =Lisp_Object *object_workspace;= declares a pointer named =object_workspace= of type =Lisp_Object=. + +The asterisk (*) in =Lisp_Object *object_workspace;= is the pointer indirection operator in C. It indicates that =object_workspace= is a pointer to an object of type =Lisp_Object=. This means that =object_workspace= will store the memory address (location) of a =Lisp_Object= variable rather than storing the actual =Lisp_Object= value. + +Therefore, =object_workspace= will be used to point to or reference locations in memory where =Lisp_Object= instances are stored. This allows the =struct json_parser= to store and work with =Lisp_Object= instances indirectly through pointers."))) + (ert-deftest test--gptel--convert-markdown->org () + (cl-loop + for input in string-sequences + for output in converted-sequences + do + (should (string= (gptel--convert-markdown->org (apply #'concat input)) + output)))) + + (ert-deftest test--gptel--stream-convert-markdown->org () + (cl-loop + for input in string-sequences + for output in converted-sequences + for func = (gptel--stream-convert-markdown->org) + do + (should + (string= (apply #'concat (mapcar func input)) + output))))) + diff --git a/test/test-gptel-org.el b/test/test-gptel-org.el deleted file mode 100644 index 8ecc689..0000000 --- a/test/test-gptel-org.el +++ /dev/null @@ -1,32 +0,0 @@ -;; -*- lexical-binding: t; -*- -(require 'ert) -(require 'gptel) - -(let ((string-sequence - '("" "```" "cpp" " -" "#include" " <" "cstdio" "> - -" "int" " main" "()" " { -" " " " printf" "(\"" "``" "``" "`" "\n" "\"); -" " " " return" " " "0" "; -" "} -" "```")) - (converted-sequence - "#+begin_src cpp -#include - -int main() { - printf(\"`````\n\"); - return 0; -} -#+end_src")) - (ert-deftest test--gptel--convert-markdown->org () - (should (string= (gptel--convert-markdown->org (apply #'concat string-sequence)) - converted-sequence))) - - (ert-deftest test--gptel--stream-convert-markdown->org () - (let ((func (gptel--stream-convert-markdown->org))) - (should - (string= (apply #'concat (mapcar func string-sequence)) - converted-sequence))))) -