aboutsummaryrefslogtreecommitdiff
path: root/tests/location.test
blob: 6c3dac59857c1beae6eb6c50ca0329ecca51c462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; Check the AST source location info.                  -*- Scheme -*-
;;;
;;; Copyright (C) 2012, 2020, 2021  Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; Skribilo 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 Lesser
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (tests location)
  #:use-module (skribilo ast)
  #:use-module (skribilo reader)
  #:use-module (skribilo evaluator)
  #:use-module (skribilo package base)
  #:use-module (skribilo location)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (ice-9 match))

(define-syntax call-with-code
  (syntax-rules ()
    ((_ string thunk)
     (call-with-input-string string
       (lambda (p)
         (set-port-filename! p "the-file.skb")
         (set-port-line! p 0)
         (set-port-column! p 0)
         (thunk p))))))

(define (location->list loc)
  (and (location? loc)
       (list (location-file loc)
             (location-line loc)
             (location-column loc))))

(define (locations ast)
  ;; Return a location tree for AST and its children.
  (let loop ((ast ast))
    (cond ((node? ast)
           (and=> (location->list (ast-loc ast))
                  (lambda (loc)
                    (append loc (map loop (node-children ast))))))
          ((ast? ast)
           (location->list (ast-loc ast)))
          (else
           '()))))

(define-syntax test-location
  (syntax-rules ()
    ((_ name expected doc)
     (test-equal name
       'expected
       (call-with-code doc
         (lambda (p)
           (pk (locations (evaluate-ast-from-port p)))))))))

(*document-reader* (make-reader 'skribe))


(test-begin "location")

(test-location "document"
  ("the-file.skb" 1 1)
  "(document)")

(test-location "document + sections"
  ("the-file.skb" 2 5
     ("the-file.skb" 3 7)
     ("the-file.skb" 4 7
        ("the-file.skb" 5 9
           ("the-file.skb" 6 11))))
  "                                               ; 1
    (document                                     ; 2
      (chapter :title \"foo\")                    ; 3
      (chapter :title \"bar\"                     ; 4
        (section :title \"baz\"                   ; 5
          (p [Paragraph.]))))")                   ; 6

(test-end "location")

;; Local Variables:
;; eval: (put 'call-with-code 'scheme-indent-function 1)
;; eval: (put 'test-location 'scheme-indent-function 1)
;; End: