-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathprettier-js-test.el
More file actions
685 lines (576 loc) · 27.6 KB
/
Copy pathprettier-js-test.el
File metadata and controls
685 lines (576 loc) · 27.6 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
;;; prettier-js-test.el --- Tests for prettier-js -*- lexical-binding: t; -*-
;; Copyright (c) 2025 The prettier-js Authors. All rights reserved.
;;; Commentary:
;; Tests for prettier-js.el
;; These tests will fail unless node/npm are installed, as well as prettier and
;; prettierd. Run the following command to install those latter dependencies:
;; npm install -g prettier @fsouza/prettierd
;;; Code:
(require 'ert)
(require 'prettier-js)
(require 'files)
(require 'subr-x)
(defun prettier-js-test--copy-directory (source destination)
"Copy SOURCE directory to DESTINATION recursively."
(if (file-directory-p source)
(progn
(make-directory destination t)
(let ((files (directory-files source t)))
(dolist (file files)
(let ((name (file-name-nondirectory file)))
(unless (or (string= name ".") (string= name ".."))
(let ((dest-file (expand-file-name name destination)))
(if (file-directory-p file)
(prettier-js-test--copy-directory file dest-file)
(copy-file file dest-file t))))))))
(copy-file source destination t)))
(defun prettier-js-test--run-process (program &rest args)
"Run PROGRAM with ARGS in the current directory."
(let ((exit-code (apply #'call-process program nil nil nil args)))
(unless (= exit-code 0)
(error "Process %s exited with code %d" program exit-code))))
(ert-deftest prettier-js-test-formatting ()
"Test that prettier-js correctly formats JavaScript code."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(clean-file (expand-file-name "fixtures/clean.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js")))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Format the buffer
(prettier-js-prettify)
;; Get the expected clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected clean content
(should (string= actual-content expected-content))
(kill-buffer))))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-node-modules ()
"Test that prettier-js can use the local node_modules/.bin/prettier."
(let* ((project-dir (expand-file-name "fixtures/project"))
(temp-dir (make-temp-file "prettier-project-" t))
(temp-src-dir (expand-file-name "src" temp-dir))
(temp-src-file (expand-file-name "messy.js" temp-src-dir))
(clean-file (expand-file-name "fixtures/clean.js"))
(default-directory temp-dir)
(prettier-js-use-modules-bin t))
(unwind-protect
(progn
;; Copy project to temp directory
(prettier-js-test--copy-directory project-dir temp-dir)
;; Install dependencies
(message "Installing npm dependencies in %s..." temp-dir)
(prettier-js-test--run-process "npm" "install")
;; Visit the messy file
(with-current-buffer (find-file-noselect temp-src-file)
;; Format the buffer using node_modules/.bin/prettier
(prettier-js-prettify)
;; Get the expected clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected clean content
(should (string= actual-content expected-content))
(kill-buffer))))
;; Clean up temp directory
(when (file-exists-p temp-dir)
(delete-directory temp-dir t)))))
(ert-deftest prettier-js-test-indirect-buffer ()
"Test that prettier-js works correctly with indirect buffers."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(clean-file (expand-file-name "fixtures/clean.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js")))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
(let (direct-buffer indirect-buffer)
;; Visit the temp file and clone its buffer
(setq direct-buffer (find-file-noselect temp-file))
(with-current-buffer direct-buffer
(setq indirect-buffer (clone-indirect-buffer nil nil)))
;; Switch to the indirect buffer and format it
(with-current-buffer indirect-buffer
(prettier-js-prettify)
;; Get the expected clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected clean content
(should (string= actual-content expected-content))
(kill-buffer)))
;; Kill the direct buffer too
(kill-buffer direct-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-no-file-buffer ()
"Test that prettier-js signals an error when buffer is not visiting a file."
(with-temp-buffer
;; Add some JavaScript content to the buffer
(insert "function test(a,b) { return a + b; }")
(js-mode)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string-match-p "Buffer .* is not visiting a file" (cadr err))))))
(ert-deftest prettier-js-test-executable-not-found ()
"Test that prettier-js signals an error when prettier executable is not found."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js"))
(prettier-js-command "non-existent-prettier-command")
(prettier-js-use-modules-bin nil))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string-match-p "Could not find prettier executable" (cadr err))))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-node-modules-not-found ()
"Test that prettier-js signals an error when node_modules/.bin/prettier is not found."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(temp-dir (make-temp-file "prettier-test-dir-" t))
(temp-file (expand-file-name "test.js" temp-dir))
(default-directory temp-dir)
(prettier-js-use-modules-bin t))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string= "Could not find node_modules/.bin/prettier executable" (cadr err))))
(kill-buffer)))
;; Clean up temp directory
(when (file-exists-p temp-dir)
(delete-directory temp-dir t)))))
(ert-deftest prettier-js-test-diff-not-found ()
"Test that prettier-js signals an error when diff executable is not found."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js"))
(prettier-js-diff-command "non-existent-diff-command"))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string-match-p "Could not find diff executable" (cadr err))))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-diff-error ()
"Test that prettier-js signals an error when diff command returns an error exit code."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js"))
(orig-call-process-region (symbol-function 'call-process-region)))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Mock call-process-region to return error code 2 for diff
(cl-letf (((symbol-function 'call-process-region)
(lambda (start end program &rest args)
(if (string-match-p "diff$" program)
2 ;; Return error code 2 for diff
(apply orig-call-process-region start end program args)))))
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string= "Error calling diff; is GNU diff on your path?" (cadr err))))
(kill-buffer))))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-node-not-found-error ()
"Test that prettier-js handles 'env: node: No such file or directory' error correctly."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js"))
(orig-call-process (symbol-function 'call-process)))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Mock call-process to simulate the node not found error
(cl-letf (((symbol-function 'call-process)
(lambda (program &rest args)
(if (string= program prettier-js-command)
(progn
;; Write the error message to the error file
(let ((error-file (nth 1 args)))
(when (and (listp error-file) (eq (caar error-file) :file))
(with-temp-file (cadr error-file)
(insert "env: node: No such file or directory"))))
1) ; Return error code
(apply orig-call-process program args)))))
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify) :type 'user-error)))
(should (string-match-p "Could not find node executable" (cadr err))))
(kill-buffer))))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-error-display ()
"Test that errors from prettier are displayed in the error buffer."
(let* ((syntax-error-file (expand-file-name "fixtures/syntax-error.js"))
(temp-dir (make-temp-file "prettier-test-dir-" t))
(temp-file (expand-file-name "syntax-error.js" temp-dir))
(prettier-js-command "prettier")
(prettier-js-show-errors 'buffer)
(default-directory temp-dir))
(unwind-protect
(progn
;; Copy syntax error file to temp directory
(copy-file syntax-error-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Run prettier-js which should display the error
(prettier-js-prettify)
;; Check that the error buffer exists and contains the error message
(let ((error-buffer (get-buffer "*prettier errors*")))
(should error-buffer)
(with-current-buffer error-buffer
(should (string-match-p "SyntaxError: Unexpected token" (buffer-string)))))
(kill-buffer)
;; Clean up error buffer
(when-let ((buf (get-buffer "*prettier errors*")))
(kill-buffer buf))))
;; Clean up temp directory
(when (file-exists-p temp-dir)
(delete-directory temp-dir t)))))
(ert-deftest prettier-js-test-prettierd-error-display ()
"Test that errors from prettierd are displayed in the error buffer."
(let* ((syntax-error-file (expand-file-name "fixtures/syntax-error.js"))
(temp-dir (make-temp-file "prettier-test-dir-" t))
(temp-file (expand-file-name "syntax-error.js" temp-dir))
(prettier-js-command "prettierd")
(prettier-js-show-errors 'buffer)
(default-directory temp-dir))
(unwind-protect
(progn
;; Copy syntax error file to temp directory
(copy-file syntax-error-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Run prettier-js which should display the error
(prettier-js-prettify)
;; Check that the error buffer exists and contains the error message
(let ((error-buffer (get-buffer "*prettier errors*")))
(should error-buffer)
(with-current-buffer error-buffer
(should (string-match-p "SyntaxError: Unexpected token" (buffer-string)))))
(kill-buffer)
;; Clean up error buffer
(when-let ((buf (get-buffer "*prettier errors*")))
(kill-buffer buf))))
;; Stop prettierd daemon to ensure the temp directory can be deleted on
;; Windows; I think prettierd "locks" the directory where it's started
(when (executable-find "prettierd")
(message "Stopping prettierd daemon...")
(call-process "prettierd" nil nil nil "stop"))
;; Clean up temp directory
(when (file-exists-p temp-dir)
(delete-directory temp-dir t)))))
(ert-deftest prettier-js-test-prettify-region ()
"Test that prettier-js-prettify-region correctly formats a region of JavaScript code."
(let* ((dirty-file (expand-file-name "fixtures/dirty.js"))
(partial-clean-file (expand-file-name "fixtures/partial-clean.js"))
(temp-file (make-temp-file "prettier-test-" nil ".js")))
(unwind-protect
(progn
;; Copy dirty content to temp file
(copy-file dirty-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
;; Find the object declaration and format just that region
(goto-char (point-min))
(search-forward "const obj = {")
(beginning-of-line)
(let ((start (point))
(end (progn
(search-forward "};")
(point))))
;; Set the region and format it
(push-mark start)
(goto-char end)
(activate-mark)
(prettier-js-prettify-region)
;; Get the expected partial clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents partial-clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected partial clean content
(should (string= actual-content expected-content))))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-prettify-code-block ()
"Test that prettier-js-prettify-code-block correctly formats code blocks in org-mode."
(let* ((messy-file (expand-file-name "fixtures/messy.org"))
(clean-file (expand-file-name "fixtures/clean.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy messy content to temp file
(copy-file messy-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Format the JavaScript code block
(goto-char (point-min))
(search-forward "#+BEGIN_SRC js")
(forward-line 1)
(prettier-js-prettify-code-block)
;; Format the TypeScript code block
(goto-char (point-min))
(search-forward "#+BEGIN_SRC typescript")
(forward-line 1)
(prettier-js-prettify-code-block)
;; Get the expected clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected clean content
(should (string= actual-content expected-content)))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-prettify-code-block-not-in-org-mode ()
"Test that prettier-js-prettify-code-block signals an error when not in org-mode."
(with-temp-buffer
(insert "function test() { return 42; }")
(js-mode)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify-code-block) :type 'user-error)))
(should (string= "Not in org-mode" (cadr err))))))
(ert-deftest prettier-js-test-prettify-code-block-not-in-src-block ()
"Test that prettier-js-prettify-code-block signals an error when not in a source block."
(with-temp-buffer
(insert "#+TITLE: Test Org File\n\n* Heading\nSome regular text, not in a code block.")
(org-mode)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify-code-block) :type 'user-error)))
(should (string= "No source code block at point" (cadr err))))))
(ert-deftest prettier-js-test-prettify-code-blocks ()
"Test that prettier-js-prettify-code-blocks formats all code blocks in an org file."
(let* ((messy-file (expand-file-name "fixtures/messy.org"))
(clean-file (expand-file-name "fixtures/clean.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy messy content to temp file
(copy-file messy-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Format all code blocks at once
(prettier-js-prettify-code-blocks)
;; Get the expected clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected clean content
(should (string= actual-content expected-content)))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-prettify-code-blocks-not-in-org-mode ()
"Test that prettier-js-prettify-code-blocks signals an error when not in org-mode."
(with-temp-buffer
(insert "function test() { return 42; }")
(js-mode)
;; Verify that the appropriate error is signaled with the correct message
(let ((err (should-error (prettier-js-prettify-code-blocks) :type 'user-error)))
(should (string= "Not in org-mode" (cadr err))))))
(ert-deftest prettier-js-test-unrecognizable-language-blocks ()
"Test that prettier-js-prettify-code-blocks skips unrecognizable language blocks."
(let* ((unrecognizable-file (expand-file-name "fixtures/unrecognizable.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy unrecognizable content to temp file
(copy-file unrecognizable-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Remember the original content
(let ((original-content (buffer-string)))
;; Format all code blocks
(prettier-js-prettify-code-blocks)
;; Verify the content hasn't changed after formatting all blocks
(should (string= (buffer-string) original-content)))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-unrecognizable-language-block ()
"Test that prettier-js-prettify-code-block skips unrecognizable language blocks."
(let* ((unrecognizable-file (expand-file-name "fixtures/unrecognizable.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy unrecognizable content to temp file
(copy-file unrecognizable-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Remember the original content
(let ((original-content (buffer-string)))
;; Try to format the specific unrecognizable block
(goto-char (point-min))
(search-forward "#+BEGIN_SRC unrecognizable-language")
(forward-line 1)
(prettier-js-prettify-code-block)
;; Verify the content hasn't changed after trying to format the specific block
(should (string= (buffer-string) original-content)))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-prettify-org-region ()
"Test that prettier-js-prettify-region correctly formats regions in org-mode files."
(let* ((messy-file (expand-file-name "fixtures/messy.org"))
(partial-clean-file (expand-file-name "fixtures/partial-clean.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy messy content to temp file
(copy-file messy-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Format the JavaScript object region
(goto-char (point-min))
(search-forward "const obj = {")
(beginning-of-line)
(let ((start (point))
(end (progn
(search-forward "};")
(point))))
;; Set the region and format it
(push-mark start)
(goto-char end)
(activate-mark)
(prettier-js-prettify-region))
;; Format the TypeScript interface region
(goto-char (point-min))
(search-forward "interface Person")
(beginning-of-line)
(let ((start (point))
(end (progn
(search-forward "}")
(point))))
;; Set the region and format it
(push-mark start)
(goto-char end)
(activate-mark)
(prettier-js-prettify-region))
;; Get the expected partial clean content
(let ((expected-content
(with-temp-buffer
(insert-file-contents partial-clean-file)
(buffer-string)))
(actual-content (buffer-string)))
;; Compare the formatted content with the expected partial clean content
(should (string= actual-content expected-content)))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(ert-deftest prettier-js-test-prettify-org-region-validation ()
"Test that prettier-js-prettify-region validates regions in org-mode files."
(let* ((messy-file (expand-file-name "fixtures/messy.org"))
(temp-file (make-temp-file "prettier-test-" nil ".org")))
(unwind-protect
(progn
;; Copy messy content to temp file
(copy-file messy-file temp-file t)
;; Visit the temp file
(with-current-buffer (find-file-noselect temp-file)
(org-mode)
;; Test case 1: Region start outside a code block
(goto-char (point-min))
(search-forward "* JavaScript Code Block")
(let ((start (point))
(end (progn
(search-forward "#+BEGIN_SRC js")
(forward-line 2)
(point))))
;; Set the region and try to format it
(push-mark start)
(goto-char end)
(activate-mark)
(let ((err (should-error (prettier-js-prettify-region) :type 'user-error)))
(should (string= "Region is not wholly inside a source code block" (cadr err)))))
;; Test case 2: Region end outside the code block
(goto-char (point-min))
(search-forward "#+BEGIN_SRC js")
(forward-line 1)
(let ((start (point))
(end (progn
(search-forward "#+END_SRC")
(forward-line 1)
(point))))
;; Set the region and try to format it
(push-mark start)
(goto-char end)
(activate-mark)
(let ((err (should-error (prettier-js-prettify-region) :type 'user-error)))
(should (string= "Region is not wholly inside a source code block" (cadr err)))))
;; Test case 3: Region spans multiple code blocks
(goto-char (point-min))
(search-forward "#+BEGIN_SRC js")
(forward-line 1)
(let ((start (point))
(end (progn
(search-forward "#+BEGIN_SRC typescript")
(forward-line 2)
(point))))
;; Set the region and try to format it
(push-mark start)
(goto-char end)
(activate-mark)
(let ((err (should-error (prettier-js-prettify-region) :type 'user-error)))
(should (string= "Region is not wholly inside a source code block" (cadr err)))))
(kill-buffer)))
;; Clean up temp file
(when (file-exists-p temp-file)
(delete-file temp-file)))))
(provide 'prettier-js-test)
;;; prettier-js-test.el ends here