tp/tp-core-tests.el
Kinneyzhang 972b6d4e4c Complete text-property facade and managed lifecycle
Add canonical query semantics, managed metadata and transactions, overlay-aware lookup, reproducible benchmarks, and synchronized API documentation.
2026-07-28 22:42:55 +08:00

203 lines
8.4 KiB
EmacsLisp

;;; tp-core-tests.el --- ERT tests for tp-core.el -*- lexical-binding: t -*-
;;; Commentary:
;; Unit tests for the tp-core foundation module.
;;; Code:
(require 'ert)
(require 'tp-core)
;;; tp--map-intervals
(ert-deftest tp-core-test-map-intervals-string-clips ()
"Intervals extending beyond the range are clipped to it."
(let ((str (copy-sequence "hello world")))
(put-text-property 0 11 'face 'bold str)
(should (equal (tp--map-intervals str 3 7 #'list)
'((3 7 (face bold)))))))
(ert-deftest tp-core-test-map-intervals-string-full ()
"Full-range walk over a string returns each property run."
(let ((str (copy-sequence "hello world")))
(put-text-property 0 5 'face 'bold str)
(should (equal (tp--map-intervals str nil nil #'list)
'((0 5 (face bold)) (5 11 nil))))))
(ert-deftest tp-core-test-map-intervals-single-property ()
"PROPERTY narrows runs to that property and passes its value."
(let ((str (copy-sequence "hello world")))
(put-text-property 0 5 'face 'bold str)
(put-text-property 2 8 'help-echo "tip" str)
(should (equal (tp--map-intervals str nil nil #'list 'face)
'((0 5 bold) (5 11 nil))))))
(ert-deftest tp-core-test-map-intervals-buffer-clips ()
"Buffer walk clips to the requested range with 1-based positions."
(with-temp-buffer
(insert "hello world")
(put-text-property 1 12 'face 'bold)
(should (equal (tp--map-intervals nil 4 8 #'list)
'((4 8 (face bold)))))))
(ert-deftest tp-core-test-map-intervals-buffer-multiple-runs ()
"Multiple runs in a buffer are visited in order, gaps included."
(with-temp-buffer
(insert "hello world")
(put-text-property 1 6 'face 'bold)
(put-text-property 7 12 'face 'italic)
(should (equal (tp--map-intervals nil nil nil #'list 'face)
'((1 6 bold) (6 7 nil) (7 12 italic))))))
(ert-deftest tp-core-test-map-intervals-out-of-range-normalized ()
"Out-of-bounds START/END are clamped, not signaled."
(let ((str (copy-sequence "abc")))
(put-text-property 0 3 'p 1 str)
(should (equal (tp--map-intervals str -5 99 #'list 'p)
'((0 3 1))))))
(ert-deftest tp-core-test-map-intervals-empty-range ()
"An empty range visits nothing."
(let ((str (copy-sequence "abc")))
(should (equal (tp--map-intervals str 1 1 #'list) nil))))
;;; tp-face-properties
(ert-deftest tp-core-test-face-properties ()
"The face-family property list contains the three face properties."
(should (equal tp-face-properties '(face font-lock-face mouse-face))))
;;; Stage 2 canonical façade
(ert-deftest tp-core-test-native-range-string-and-buffer-coordinates ()
"Canonical ranges store concrete objects and native coordinates."
(let* ((str (copy-sequence "abcd"))
(range (tp--native-range-from-object str 1 3)))
(should (tp--native-range-p range))
(should (eq (tp--native-range-kind range) :string))
(should (eq (tp--native-range-object range) str))
(should (equal (list (tp--native-range-start range)
(tp--native-range-end range))
'(1 3))))
(with-temp-buffer
(insert "abcd")
(narrow-to-region 2 4)
(let ((range (tp--native-range-from-object nil nil nil)))
(should (eq (tp--native-range-kind range) :buffer))
(should (eq (tp--native-range-object range) (current-buffer)))
(should (equal (list (tp--native-range-start range)
(tp--native-range-end range))
'(2 4))))))
(ert-deftest tp-core-test-presence-distinguishes-three-value-states ()
"Canonical presence distinguishes absence, explicit nil, and a value."
(let ((str (copy-sequence "abc")))
(put-text-property 0 1 'state nil str)
(put-text-property 1 2 'state 'ready str)
(let ((nil-value (tp--presence-at 0 'state str))
(value (tp--presence-at 1 'state str))
(absent (tp--presence-at 2 'state str)))
(should (tp--presence-present-p nil-value))
(should-not (tp--presence-value nil-value))
(should (tp--presence-present-p value))
(should (eq (tp--presence-value value) 'ready))
(should-not (tp--presence-present-p absent))
(should-not (tp--presence-value absent)))))
(ert-deftest tp-core-test-canonical-request-match-and-result-adapters ()
"Canonical records preserve policy and adapt only at public boundaries."
(let* ((str (copy-sequence "abcd"))
(range (tp--native-range-from-object str 1 3))
(request (tp--make-request
:operation :set :range range :props '(face bold)
:mutation :copy :read-only :respect
:noerror nil :public-return :range))
(match (tp--make-match range 'face 'bold))
(result (tp--make-result
:request request :matches (list match)
:count 1 :object str :native 'native-value)))
(should (tp--request-p request))
(should (eq (tp--request-operation request) :set))
(should (eq (tp--request-mutation request) :copy))
(should (equal (tp--match-to-list match) '(1 3 bold)))
(let ((native-match (tp--match-to-prop-match match)))
(should (= (prop-match-beginning native-match) 1))
(should (= (prop-match-end native-match) 3))
(should (eq (prop-match-value native-match) 'bold)))
(should (equal (tp--result-public-value result) '(1 . 3)))
(setf (tp--request-public-return request) :matches)
(should (equal (tp--result-public-value result) '((1 3 bold))))
(setf (tp--request-public-return request) :count)
(should (= (tp--result-public-value result) 1))
(setf (tp--request-public-return request) :native)
(should (eq (tp--result-public-value result) 'native-value))))
;;; API-COORD-01: ABSOLUTE coordinates in tp-intervals / tp-intervals-map
(ert-deftest tp-core-test-intervals-buffer-relative-default ()
"Without ABSOLUTE, buffer intervals stay START-relative (legacy)."
(with-temp-buffer
(insert "hello world")
(put-text-property 4 8 'face 'bold)
(should (equal (tp-intervals 3 9)
'((0 1 nil) (1 5 (face bold)) (5 6 nil))))))
(ert-deftest tp-core-test-intervals-buffer-absolute ()
"With ABSOLUTE, buffer intervals use native 1-based positions."
(with-temp-buffer
(insert "hello world")
(put-text-property 4 8 'face 'bold)
(should (equal (tp-intervals 3 9 nil t)
'((3 4 nil) (4 8 (face bold)) (8 9 nil))))
;; Clipping still applies in native coordinates.
(should (equal (tp-intervals 5 7 nil t)
'((5 7 (face bold)))))))
(ert-deftest tp-core-test-intervals-string-ignores-absolute ()
"String intervals are already absolute; ABSOLUTE changes nothing."
(let ((s (copy-sequence "hello world")))
(put-text-property 3 7 'face 'bold s)
(should (equal (tp-intervals 2 9 s) (tp-intervals 2 9 s t)))
(should (equal (tp-intervals 2 9 s t)
'((2 3 nil) (3 7 (face bold)) (7 9 nil))))))
(ert-deftest tp-core-test-intervals-map-absolute ()
"tp-intervals-map passes ABSOLUTE through to native positions."
(with-temp-buffer
(insert "hello world")
(put-text-property 4 8 'face 'bold)
(should (equal (tp-intervals-map #'list 3 9)
'((0 1 nil nil) (1 5 (face bold) nil) (5 6 nil nil))))
(should (equal (tp-intervals-map #'list 3 9 nil t)
'((3 4 nil nil) (4 8 (face bold) nil) (8 9 nil nil))))))
(ert-deftest tp-core-test-intervals-map-splits-layer-stack ()
"tp-intervals-map hands the tp-layers stack to FUNCTION separately."
(with-temp-buffer
(insert "hello")
(set-text-properties
1 6 '(face bold tp-layers ((face italic tp-name below))))
(let ((res (tp-intervals-map #'list 1 6 nil t)))
(should (= (length res) 1))
(pcase-let ((`(,beg ,end ,top ,below) (car res)))
(should (= beg 1))
(should (= end 6))
(should (eq (plist-get top 'face) 'bold))
(should-not (plist-member top 'tp-layers))
(should (equal below '((face italic tp-name below))))))))
(ert-deftest tp-core-test-intervals-map-drops-nil-results ()
"nil results from FUNCTION are removed from the returned list."
(with-temp-buffer
(insert "hello world")
(put-text-property 4 8 'face 'bold)
(should (equal (tp-intervals-map
(lambda (beg end top _below)
(when (plist-get top 'face) (cons beg end)))
1 12 nil t)
'((4 . 8))))))
(provide 'tp-core-tests)
;;; tp-core-tests.el ends here