documentation.lisp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. (import test ())
  2. (import urn/documentation ())
  3. (import urn/resolve/loop resolve)
  4. (import tests/compiler/compiler-helpers ())
  5. (defun teq? (a b)
  6. "Determine whether tokens A and B are equal."
  7. :hidden
  8. (and
  9. (= (n a) (n b))
  10. (all id (map (lambda (x y)
  11. (and (= (.> x :kind) (.> y :kind))
  12. (= (.> x :contents) (.> y :contents))
  13. (= (.> x :whole) (.> y :whole)))) a b))))
  14. (defun tok (kind contents whole)
  15. "Create a new token with the given KIND, CONTENTS and WHOLE."
  16. :hidden
  17. { :kind kind :contents contents :whole whole })
  18. (defun affirm-signature (node expected)
  19. "Affirm the signature of NODE is EXPECTED."
  20. :hidden
  21. (let* [(compiler (create-compiler))
  22. (resolved (resolve/compile
  23. compiler
  24. (wrap-node (list node))
  25. (.> compiler :root-scope)
  26. "init.lisp"))
  27. (var (.> (car resolved) :def-var))]
  28. (affirm (eq? expected (extract-signature var)))))
  29. (describe "The Urn compiler handles documentation"
  30. (section "which can parse docstrings"
  31. (it "with arguments"
  32. (affirm (teq? (parse-docstring "FOO")
  33. (list (tok "arg" "FOO" "FOO")))
  34. (teq? (parse-docstring "FOO-BAR")
  35. (list (tok "arg" "FOO-BAR" "FOO-BAR")))
  36. (teq? (parse-docstring "FOO-BAR, baz")
  37. (list (tok "arg" "FOO-BAR" "FOO-BAR") (tok "text" ", baz")))
  38. (teq? (parse-docstring "FOO-bar")
  39. (list (tok "text" "FOO-bar")))
  40. (teq? (parse-docstring "f-OO-bar")
  41. (list (tok "text" "f-OO-bar")))
  42. (teq? (parse-docstring "FOO.")
  43. (list (tok "arg" "FOO" "FOO") (tok "text" ".")))))
  44. (it "with inline code blocks"
  45. (affirm (teq? (parse-docstring "`code block`")
  46. (list (tok "mono" "code block" "`code block`")))
  47. (teq? (parse-docstring "before `code block` after")
  48. (list (tok "text" "before ")
  49. (tok "mono" "code block" "`code block`")
  50. (tok "text" " after")))
  51. (teq? (parse-docstring "`multiline\ncode block`")
  52. (list (tok "text" "`multiline\ncode block`")))))
  53. (it "with bold and italic"
  54. (affirm (teq? (parse-docstring "**foo**")
  55. (list (tok "bold" "**foo**" "**foo**")))
  56. (teq? (parse-docstring "*foo*")
  57. (list (tok "italic" "*foo*" "*foo*")))
  58. (teq? (parse-docstring "***foo***")
  59. (list (tok "bolic" "***foo***" "***foo***")))))
  60. (it "with links to other code"
  61. (affirm (teq? (parse-docstring "[[foo]]")
  62. (list (tok "link" "foo" "[[foo]]"))))))
  63. (section "can extract signatures"
  64. (it "for constant definitions"
  65. (affirm-signature
  66. '(define x 1)
  67. nil))
  68. (it "for lambda definitions"
  69. (affirm-signature
  70. '(define x (lambda (a b c)))
  71. '("a" "b" "c")))
  72. (it "for macros"
  73. (affirm-signature
  74. '(define-macro x (lambda (a b c)))
  75. '("a" "b" "c")))
  76. (it "for native definitions"
  77. (affirm-signature
  78. '(define-native x)
  79. nil))
  80. (it "for definitions with variadic arguments"
  81. (affirm-signature
  82. '(define x (lambda (a b &c)))
  83. '("a" "b" "&c")))
  84. (it "for definitions with custom names"
  85. (affirm-signature
  86. ~(define x (lambda (,{ :tag "symbol"
  87. :display-name "a"
  88. :contents "r_1" })))
  89. '("a"))))
  90. (section "can extract a summary"
  91. (affirm (teq?
  92. (extract-summary
  93. (parse-docstring
  94. "This is a summary of my complex module. It has lots and lots of methods"))
  95. (list (tok "text" "This is a summary of my complex module.")))
  96. (teq?
  97. (extract-summary
  98. (parse-docstring
  99. "This is a summary of my complex module
  100. It has lots and lots of methods"))
  101. (list (tok "text" "This is a summary of my complex module")))
  102. (teq?
  103. (extract-summary
  104. (parse-docstring
  105. "This is a summary of my complex module"))
  106. (list (tok "text" "This is a summary of my complex module")))
  107. (teq?
  108. (extract-summary
  109. (parse-docstring
  110. "This is a summary of my complex **module. And some more**"))
  111. (list (tok "text" "This is a summary of my complex ")
  112. (tok "bold" "**module.**"))))))