aboutsummaryrefslogtreecommitdiff
path: root/tests/config-expand.scm
blob: 8badbf50d24db833c45938c1e7d7b1062874788f (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;; This file is part of scheme-GNUnet.
;; Copyright (C) 2021 Maxime Devos
;;
;; scheme-GNUnet is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; scheme-GNUnet 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
;; Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-License-Identifier: AGPL3.0-or-later

(use-modules (gnu gnunet config parser)
	     (gnu gnunet config expand)
	     (srfi srfi-64))

(define (region=? line1 start1 end1
		  line2 start2 end2)
  (string=? (substring/shared line1 start1 end1)
	    (substring/shared line2 start2 end2)))

(define (query/not line start end)
  (error "this test should not call the query procedure"))

(define (region=?/not line1 start1 end1
		      line2 start2 end2)
  (error "this test should not call the region=? procedure"))

;; § Literals
(test-equal "literal"
  "text"
  (expand->string query/not region=?/not "text"
		  (list (make-literal-position 0 4))))

(test-equal "part of literal (1)"
  "text"
  (expand->string query/not region=?/not "some text"
		  (list (make-literal-position 5 9))))

(test-equal "part of literal (2)"
  "some"
  (expand->string query/not region=?/not "some text"
		  (list (make-literal-position 0 4))))

(test-equal "quotes are not removed"
  "'text'"
  (expand->string query/not region=?/not "'text'"
		  (list (make-literal-position 0 6))))

(test-equal "zero literals"
  ""
  (expand->string query/not region=?/not 'anything '()))

(test-equal "two overlapping literals"
  "spoon"
  (expand->string query/not region=?/not "spon"
		  (list (make-literal-position 0 3)
			(make-literal-position 2 4))))
;; § Variable references
(define (alist->query alist)
  (lambda (line start end)
    (let ((entry (assoc (substring line start end) alist)))
      (if entry
	  (apply values (cdr entry))
	  (error "this variable was not meant to be encountered"
		 line start end)))))

(test-equal "variable reference ($)"
  "iable"
  (expand->string (alist->query `(("var" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "$var"
		  (list (make-$-position 1 4))))

(test-equal "variable reference (${})"
  "iable"
  (expand->string (alist->query `(("var" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "${var}"
		  (list (#{make-${}-position}# 2 5))))

(test-equal "variable reference (${:-})"
  "iable"
  (expand->string (alist->query `(("var" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "${var:-default}"
		  (list (#{make-${:-}-position}# 2 5 7 14 '()))))

;; This is the expander, not the parser.
(test-equal "expander does not care about delimiters ($)"
  "iable"
  (expand->string (alist->query `(("#@}!/" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "${pre}#@}!/${post}"
		  (list (make-$-position 6 11))))

(test-equal "expander does not care about delimiters (${})"
  "iable"
  (expand->string (alist->query `(("#@}!/" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "${pre}#@}!/${post}"
		  (list (#{make-${}-position}# 6 11))))

(test-equal "expander does not care about delimiters (${:-})"
  "iable"
  (expand->string (alist->query `(("#@}!/" "variable"
				   (,(make-literal-position 3 8)))))
		  region=?/not "${pre}#@}!/${post}"
		  (list (#{make-${:-}-position}# 6 11 13 15 '()))))

(test-equal "undefined variable -> default (${:-})"
  "default"
  (expand->string (alist->query '(("var")))
		  region=?/not "var  default"
		  (list (#{make-${:-}-position}# 0 3 5 12
			 (list (make-literal-position 5 12))))))

(test-equal "undefined variable -> default (${:-}, recursive)"
  "default"
  (expand->string (alist->query `(("var")
				  ("var2" "default"
				   (,(make-literal-position 0 7)))))
		  region=?/not "var  var2"
		  (list (#{make-${:-}-position}# 0 3 5 9
			 (list (make-$-position 5 9))))))

;; § Exceptions (undefined variable)
;;
;; Convert the exception into a S-expression
;; to be able to compare results with @code{equal?}.
(define (expand->string/catch query region=? line expo-list)
  (with-exception-handler
      (lambda (e)
	(cond ((undefined-variable-error? e)
	       `(undefined-variable-error
		 (line ,(undefined-variable-line e))
		 (start ,(undefined-variable-start e))
		 (end ,(undefined-variable-end e))))
	      ((expansion-loop-error? e)
	       `(expansion-loop-error
		 (visited . ,(expansion-loop-error-visited e))))
	      (#t (error "what is this madness"))))
    (lambda ()
      (expand->string query region=? line expo-list))
    #:unwind? #t
    #:unwind-for-type &expansion-error))

(test-equal "undefined variable -> exception ($)"
  `(undefined-variable-error
    (line "var")
    (start 0)
    (end 3))
  (expand->string/catch (alist->query '(("var")))
			region=?/not "var"
			(list (make-$-position 0 3))))

(test-equal "undefined variable -> exception (${})"
  `(undefined-variable-error
    (line "var")
    (start 0)
    (end 3))
  (expand->string/catch (alist->query '(("var")))
			region=?/not "var"
			(list (#{make-${}-position}# 0 3))))

;; Like @code{region=?}, but #(line start end) must be in @var{acceptable}.
(define (region=?/restricted . acceptable)
  (lambda (line1 start1 end1 line2 start2 end2)
    (unless (and (member (vector line1 start1 end1) acceptable)
		 (member (vector line2 start2 end2) acceptable))
      (error "where did this variable reference come from?"
	     (vector line1 start1 end1)
	     (vector line2 start2 end2)))
    (region=? line1 start1 end1 line2 start2 end2)))

(test-equal "undefined variable (nested) -> exception ($, correct line)"
  `(undefined-variable-error
    (line "var1 = $var2")
    (start 8)
    (end 12))
  (expand->string/catch (alist->query `(("var1" "var1 = $var2"
					 (,(make-$-position 8 12)))
					("var2")))
			(region=?/restricted
			 #("$var1" 1 5)
			 #("var1 = $var2" 8 12))
			"$var1"
			(list (make-$-position 1 5))))

(test-equal "undefined variable (nested) -> exception (${}, correct line)"
  `(undefined-variable-error
    (line "var1 = ${var2}")
    (start 9)
    (end 13))
  (expand->string/catch (alist->query `(("var1" "var1 = ${var2}"
					 (,(#{make-${}-position}# 9 13)))
					("var2")))
			(region=?/restricted
			 #("$var1" 1 5)
			 #("var1 = ${var2}" 9 13))
			"$var1"
			(list (make-$-position 1 5))))

;; § Exceptions (loops)

;; Verify the line number information and verify the loopiness is
;; visible in the ‘visited’ list.

(test-equal "loop ($, $)"
  `(expansion-loop-error
    (visited #("var = the $variable" 11 19)
	     #("variable = $var" 12 15)
	     #("$variable" 1 9)))
  (expand->string/catch (alist->query `(("variable"
					 "variable = $var"
					 (,(make-$-position 12 15)))
					("var"
					 "var = the $variable"
					 (,(make-$-position 11 19)))))
			(region=?/restricted
			 #("variable = $var" 12 15)
			 #("var = the $variable" 11 19)
			 #("$variable" 1 9))
			"$variable"
			(list (make-$-position 1 9))))

(test-equal "loop (${}, ${})"
  `(expansion-loop-error
    (visited #("variable = ${var}" 13 16)
	     #("var = the ${variable}" 12 20)
	     #("$var" 1 4)))
  (expand->string/catch (alist->query `(("variable"
					 "variable = ${var}"
					 (,(#{make-${}-position}# 13 16)))
					("var"
					 "var = the ${variable}"
					 (,(#{make-${}-position}# 12 20)))))
			(region=?/restricted
			 #("variable = ${var}" 13 16)
			 #("var = the ${variable}" 12 20)
			 #("$var" 1 4))
			"$var"
			(list (#{make-$-position}# 1 4))))


(test-equal "loop (${:-}, ${:-})"
  `(expansion-loop-error
    (visited #("variable = ${var:-}" 13 16)
	     #("var = the ${variable:-}" 12 20)
	     #("$var" 1 4)))
  (expand->string/catch
   (alist->query `(("variable"
		    "variable = ${var:-}"
		    (,(#{make-${:-}-position}# 13 16 18 18 '())))
		   ("var"
		    "var = the ${variable:-}"
		    (,(#{make-${:-}-position}# 12 20 22 22 '())))))
   (region=?/restricted
    #("variable = ${var:-}" 13 16)
    #("var = the ${variable:-}" 12 20)
    #("$var" 1 4))
   "$var"
   (list (make-$-position 1 4))))

(test-equal "${:-} with default --> no visited entry"
  `(expansion-loop-error
    (visited #("var = $var" 7 10)
	     #("${does-not-exist:-$var}" 19 22)))
  (expand->string/catch
   (alist->query `(("var" "var = $var"
		    (,(make-$-position 7 10)))
		   ("does-not-exist")))
   (region=?/restricted
    #("var = $var" 7 10)
    #("${does-not-exist:-$var}" 19 22))
   "${does-not-exist:-$var}"
   (list (#{make-${:-}-position}# 2 16 18 22
	  (list (make-$-position 19 22))))))

;; This should _not_ lead to an &expansion-loop-error.
(test-equal "variable expanded multiple times"
  "example example"
  (expand->string/catch
   (alist->query `(("var" "example"
		    (,(make-literal-position 0 7)))))
   region=?/not
   "var "
   (list (make-$-position 0 3)
	 (make-literal-position 3 4)
	 (make-$-position 0 3))))