Skip to content

Commit e6c9604

Browse files
committed
Simplify idris-generate-def, remove dead code and
add test for `idris-generate-def-next` Also remove uncessary backtick
1 parent 5e0f177 commit e6c9604

File tree

2 files changed

+102
-9
lines changed

2 files changed

+102
-9
lines changed

idris-commands.el

+4-9
Original file line numberDiff line numberDiff line change
@@ -785,15 +785,15 @@ Idris 2 only."
785785
(prefix (save-excursion
786786
(goto-char (point-min))
787787
(forward-line (1- (cdr what)))
788-
(goto-char (line-beginning-position))
788+
(beginning-of-line)
789789
(re-search-forward "\\(^>?\\s-*\\)" nil t)
790790
(or (match-string 1) ""))))
791791
(if (string= result "")
792792
(error "Nothing found")
793-
(goto-char (line-beginning-position))
793+
(beginning-of-line)
794794
(forward-line)
795795
(while (and (not (eobp))
796-
(progn (goto-char (line-beginning-position))
796+
(progn (beginning-of-line)
797797
(looking-at-p (concat prefix "\\s-+"))))
798798
(forward-line))
799799
(insert prefix)
@@ -803,11 +803,6 @@ Idris 2 only."
803803
(setq def-region-end (point))
804804
(newline)
805805
(goto-char final-point)
806-
; (save-excursion
807-
; (forward-line 1)
808-
; (setq def-region-start (point))
809-
; (insert result)
810-
; (setq def-region-end (point)))
811806
)))))
812807

813808
(defun idris-generate-def-next ()
@@ -816,7 +811,7 @@ Idris 2 only."
816811
(interactive)
817812
(if (not def-region-start)
818813
(user-error "You must program search first before looking for subsequent program results")
819-
(let ((result (car (idris-eval `:generate-def-next))))
814+
(let ((result (car (idris-eval :generate-def-next))))
820815
(if (string= result "No more results")
821816
(message "No more results")
822817
(save-excursion

test/idris-commands-test.el

+98
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,104 @@ Matrix i j = i ?hole2"
574574
(advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub)
575575
(advice-remove 'idris-eval #'idris-eval-stub))))
576576

577+
(ert-deftest idris-generate-def ()
578+
"Test `idris-generate-def'."
579+
(cl-flet ((idris-load-file-sync-stub () nil)
580+
(idris-eval-stub (sexp &optional no-errors)
581+
(apply #'funcall eval-result)))
582+
(advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub)
583+
(advice-add 'idris-eval :override #'idris-eval-stub)
584+
(unwind-protect
585+
(progn
586+
;; hapy path
587+
(with-temp-buffer
588+
(insert "data Foo = A | B
589+
590+
testf : Foo -> Int
591+
")
592+
(goto-char (point-min))
593+
(re-search-forward "test")
594+
(let ((eval-result (list #'identity '("testf A = testf B\ntestf B = testf A"))))
595+
(funcall-interactively 'idris-generate-def))
596+
;; (message (buffer-substring-no-properties (point-min) (point-max)))
597+
(should (string= "data Foo = A | B
598+
599+
testf : Foo -> Int
600+
testf A = testf B
601+
testf B = testf A
602+
"
603+
(buffer-substring-no-properties (point-min) (point-max)))))
604+
;; sad path
605+
(with-temp-buffer
606+
(insert "data Foo = A | B
607+
608+
testf : Foo -> Int
609+
")
610+
(goto-char (point-min))
611+
(re-search-forward "test")
612+
(let ((eval-result (list #'identity '(""))))
613+
(should-error
614+
(funcall-interactively 'idris-generate-def))))
615+
616+
;; sad path 2
617+
(with-temp-buffer
618+
(insert "data Foo = A | B
619+
620+
testf : Foo -> Int
621+
")
622+
(goto-char (point-min))
623+
(re-search-forward "test")
624+
(let ((eval-result (list #'error "%s (synchronous Idris evaluation failed)" "Nothing found")))
625+
(should-error
626+
(funcall-interactively 'idris-generate-def)))))
627+
628+
(advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub)
629+
(advice-remove 'idris-eval #'idris-eval-stub))))
630+
631+
(ert-deftest idris-generate-def-next ()
632+
"Test `idris-generate-def-next'."
633+
(skip-unless (string-match-p "idris2$" idris-interpreter-path))
634+
(cl-flet ((idris-load-file-sync-stub () nil)
635+
(idris-eval-stub (sexp &optional no-errors)
636+
(apply #'funcall eval-result)))
637+
(advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub)
638+
(advice-add 'idris-eval :override #'idris-eval-stub)
639+
(unwind-protect
640+
(progn
641+
(with-temp-buffer
642+
(insert "data Foo = A | B
643+
644+
testf : Foo -> Int
645+
")
646+
(goto-char (point-min))
647+
(re-search-forward "test")
648+
(let ((eval-result (list #'identity '("testf A = testf B\ntestf B = testf A"))))
649+
(funcall-interactively 'idris-generate-def))
650+
651+
(let ((eval-result (list #'identity '("testf A = 1\ntestf B = 2"))))
652+
(funcall-interactively 'idris-generate-def-next))
653+
(should (string= "data Foo = A | B
654+
655+
testf : Foo -> Int
656+
testf A = 1
657+
testf B = 2
658+
"
659+
(buffer-substring-no-properties (point-min) (point-max))))
660+
661+
(let ((eval-result (list #'identity '("third definition"))))
662+
(funcall-interactively 'idris-generate-def-next))
663+
(should (string= "data Foo = A | B
664+
665+
testf : Foo -> Int
666+
third definition
667+
"
668+
(buffer-substring-no-properties (point-min) (point-max))))
669+
(let ((eval-result (list #'error "%s (synchronous Idris evaluation failed)" "No more results")))
670+
(should-error (funcall-interactively 'idris-generate-def-next)))))
671+
672+
(advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub)
673+
(advice-remove 'idris-eval #'idris-eval-stub))))
674+
577675
;; Tests by Yasuhiko Watanabe
578676
;; https://github.com/idris-hackers/idris-mode/pull/537/files
579677
(idris-ert-command-action "test-data/CaseSplit.idr" idris-case-split idris-test-eq-buffer)

0 commit comments

Comments
 (0)