A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.4 KiB

  1. ;;; indium-structs-test.el --- Unit tests for indium-structs.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; Keywords: test
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'buttercup)
  19. (require 'indium-structs)
  20. (require 'assess)
  21. (require 'seq)
  22. (describe "Locations"
  23. (it "Should be able to make locations at point"
  24. (assess-with-filesystem '(("index.js" "let foo = 1;\nlet bar = 2;"))
  25. (with-current-buffer (find-file-noselect "index.js")
  26. (goto-char (point-max))
  27. (let ((loc (indium-location-at-point)))
  28. (expect (indium-location-file loc) :to-equal (expand-file-name "index.js"))
  29. (expect (indium-location-line loc) :to-equal 2)))))
  30. (it "Should be able to make locations from alists"
  31. (let ((loc (indium-location-from-alist '((file . "index.js")
  32. (line . 22)
  33. (column . 0)))))
  34. (expect (indium-location-file loc) :to-equal "index.js")
  35. (expect (indium-location-line loc) :to-equal 22)
  36. (expect (indium-location-column loc) :to-equal 0))))
  37. (describe "Breakpoints"
  38. (it "Should be able to make breakpoints"
  39. (let ((brk (indium-breakpoint-create
  40. :id 'id
  41. :condition "foo === bar")))
  42. (expect (indium-breakpoint-id brk) :to-be 'id)
  43. (expect (indium-breakpoint-condition brk)
  44. :to-equal "foo === bar")
  45. (expect (indium-breakpoint-location brk)
  46. :to-be nil)))
  47. (it "Should be have the location of its overlay"
  48. (with-temp-buffer
  49. (insert "let foo = 1;\nlet bar = 2;")
  50. (goto-char (point-min))
  51. (let* ((ov (make-overlay (point) (point)))
  52. (brk (indium-breakpoint-create :overlay ov)))
  53. (expect (indium-location-line (indium-breakpoint-location brk))
  54. :to-equal 1)
  55. (expect (indium-location-file (indium-breakpoint-location brk))
  56. :to-equal (buffer-file-name (overlay-buffer ov))))))
  57. (it "Should follow the location of the overlay when it changes"
  58. (with-temp-buffer
  59. (insert "let foo = 1;\nlet bar = 2;")
  60. (goto-char (point-min))
  61. (let* ((ov (make-overlay (point) (point)))
  62. (brk (indium-breakpoint-create :overlay ov)))
  63. (move-overlay ov (point-at-eol) (point-at-eol))
  64. (save-excursion
  65. (goto-char (point-max))
  66. (expect (indium-location-line (indium-breakpoint-location brk))
  67. :to-equal 2))))))
  68. (describe "Scopes"
  69. (it "Should be able to make scopes from alists"
  70. (let ((s (indium-scope-from-alist '((type . "closure")
  71. (name . "this.foo")
  72. (id . "25")))))
  73. (expect (indium-scope-id s) :to-equal "25")
  74. (expect (indium-scope-type s) :to-equal "closure")
  75. (expect (indium-scope-name s) :to-equal "this.foo"))))
  76. (describe "Frames"
  77. (it "Should be able to make frames from alists"
  78. (let ((f (indium-frame-from-alist '((id . "some id")
  79. (scriptId . "22")
  80. (functionName . "foo")
  81. (location . ((file . "index.js")
  82. (line . 22)
  83. (column . 0)))
  84. (scopeChain . [((type . "closure")
  85. (name . "this.foo")
  86. (id . "25"))
  87. ((type . "local")
  88. (name . "bar")
  89. (id . "26"))])))))
  90. (expect (indium-frame-id f) :to-equal "some id")
  91. (expect (indium-frame-script-id f) :to-equal "22")
  92. (expect (indium-frame-function-name f) :to-equal "foo")
  93. (expect (length (indium-frame-scope-chain f)) :to-equal 2)
  94. (expect (indium-scope-type (seq-elt (indium-frame-scope-chain f) 0))
  95. :to-equal "closure")
  96. (expect (indium-scope-name (seq-elt (indium-frame-scope-chain f) 0))
  97. :to-equal "this.foo")
  98. (expect (indium-scope-id (seq-elt (indium-frame-scope-chain f) 0))
  99. :to-equal "25")
  100. (expect (indium-scope-type (seq-elt (indium-frame-scope-chain f) 1))
  101. :to-equal "local")
  102. (expect (indium-scope-name (seq-elt (indium-frame-scope-chain f) 1))
  103. :to-equal "bar")
  104. (expect (indium-scope-id (seq-elt (indium-frame-scope-chain f) 1))
  105. :to-equal "26")
  106. (expect (indium-location-file (indium-frame-location f))
  107. :to-equal "index.js")
  108. (expect (indium-location-line (indium-frame-location f))
  109. :to-equal 22)
  110. (expect (indium-location-column (indium-frame-location f))
  111. :to-equal 0))))
  112. (describe "Native properties"
  113. :var (native non-native)
  114. (before-all
  115. (setq native (indium-property-from-alist
  116. '((name . "foo")
  117. (value . ((description . "function f() { [native code] }"))))))
  118. (setq non-native (indium-property-from-alist
  119. '((name . "foo")
  120. (value . ((description . "42")))))))
  121. (it "can detect native code property"
  122. (expect (indium-property-native-p native)
  123. :to-be-truthy))
  124. (it "can detect non-native code property"
  125. (expect (indium-property-native-p non-native)
  126. :to-be nil)))
  127. (provide 'indium-structs-test)
  128. ;;; indium-structs-test.el ends here