errors.lisp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. (import test ())
  2. (import io/term ())
  3. (import urn/resolve/loop resolve)
  4. (import urn/backend/lua lua)
  5. (import urn/error (compiler-error?))
  6. (import tests/compiler/compiler-helpers ())
  7. (defun resolve (nodes)
  8. (let* [(compiler (create-compiler))
  9. (logger (tracking-logger))
  10. (compile-state (lua/create-state))]
  11. (.<! compiler :log logger)
  12. (.<! compiler :compile-state compile-state)
  13. (with ((ok err) (pcall resolve/compile
  14. compiler
  15. (wrap-node nodes)
  16. (.> compiler :root-scope)
  17. "init.lisp"))
  18. (cond
  19. [ok false]
  20. [(compiler-error? err) (.> logger :errors)]
  21. [else err]))))
  22. (describe "The resolver will error"
  23. (section "on malformed"
  24. (it "definitions"
  25. (affirm (eq? '("Expected name, got nothing") (resolve '((define))))
  26. (eq? '("Expected name, got number") (resolve '((define 2))))
  27. (eq? '("Expected value, got nothing") (resolve '((define x))))))
  28. (it "lambdas"
  29. (affirm (eq? '("Expected argument list, got nothing") (resolve '((lambda))))
  30. (eq? '("Expected argument list, got symbol") (resolve '((lambda x))))
  31. (eq? '("Expected argument, got number") (resolve '((lambda (2)))))
  32. (eq? '("Cannot have multiple variadic arguments") (resolve '((lambda (&x &y)))))
  33. (eq? '("Expected a symbol for variadic argument.\nDid you mean '&x'?") (resolve '((lambda (& x)))))))
  34. (it "set!s"
  35. (affirm (eq? '("Expected symbol, got nothing") (resolve '((set!))))
  36. (eq? '("Expected symbol, got number") (resolve '((set! 2))))
  37. (eq? '("Expected value, got nothing") (resolve '((set! x))))
  38. (eq? '("Unexpected node in 'set!' (expected 3 values, got 4)") (resolve '((set! x 2 3))))
  39. (eq? '("Cannot rebind immutable definition 'x'") (resolve '((define x 1) (set! x 2))))))
  40. (it "struct-literals"
  41. (affirm (eq? '("Expected an even number of arguments, got 3") (resolve '({ a b c }))))))
  42. (section "on missing variables"
  43. (it "with simple errors"
  44. (affirm (eq? '("Cannot find variable 'x'") (resolve '((x))))))
  45. (it "with a single candidate"
  46. (affirm (eq? (list (format nil "Cannot find variable 'xyy'\nDid you mean '{}'?"
  47. (coloured "1;32" "xyz")))
  48. (resolve '((define xyz 1) (xyy)))))))
  49. (section "on looping macros"
  50. (it "which are recursive"
  51. (affirm (eq? '("Loop in macros x -> x\nx used in x") (resolve '((define-macro x (lambda () (x))))))))
  52. (it "which are mutually recursive"
  53. (affirm (eq? '("Loop in macros y -> x -> y\nx used in y\ny used in x")
  54. (resolve '((define-macro x (lambda () (y)))
  55. (define-macro y (lambda () (x))))))))
  56. (it "which depend on functions"
  57. (affirm (eq? '("Loop in macros x -> y -> x\ny used in x\nx used in y")
  58. (resolve '((define-macro x (lambda () (y)))
  59. (define y (lambda () (x)))))))))
  60. (section "on malformed metadata"
  61. (it "with docstrings"
  62. (affirm (eq? '("Multiple doc strings in definition")
  63. (resolve '((define x "Foo" "Bar" 1))))))
  64. (it "with :deprecated"
  65. (affirm (eq? '("This definition is already deprecated")
  66. (resolve '((define x :deprecated :deprecated 1))))))
  67. (it "with :mutable"
  68. (affirm (eq? '("This definition is already mutable")
  69. (resolve '((define x :mutable :mutable 1))))
  70. (eq? '("Can only set conventional definitions as mutable")
  71. (resolve '((define-macro x :mutable 1))))
  72. (eq? '("Can only set conventional definitions as mutable")
  73. (resolve '((define-native x :mutable))))))
  74. (section "with native definitions"
  75. (it "using :pure"
  76. (affirm (eq? '("Can only set native definitions as pure")
  77. (resolve '((define x :pure 1))))
  78. (eq? '("This definition is already pure")
  79. (resolve '((define-native x :pure :pure))))))
  80. (it "using :bind-to"
  81. (affirm (eq? '("Can only bind native definitions")
  82. (resolve '((define x :bind-to 1))))
  83. (eq? '("Expected bind expression, got nothing")
  84. (resolve '((define-native y :bind-to))))
  85. (eq? '("Multiple bind expressions set")
  86. (resolve '((define-native z :bind-to "z" :bind-to "z"))))))
  87. (it "using :syntax"
  88. (affirm (eq? '("Can only set syntax for native definitions")
  89. (resolve '((define x :syntax 1))))
  90. (eq? '("Expected syntax, got nothing")
  91. (resolve '((define-native x :syntax))))
  92. (eq? '("Expected syntax, got key")
  93. (resolve '((define-native x :syntax :something))))
  94. (eq? '("Expected syntax element, got key")
  95. (resolve '((define-native x :syntax (:foo)))))
  96. (eq? '("Multiple syntaxes set")
  97. (resolve '((define-native x :syntax "" :syntax ""))))
  98. (eq? '("Cannot specify :syntax and :bind-to in native definition")
  99. (resolve '((define-native x :syntax "" :bind-to ""))))))
  100. (it "using :syntax-stmt"
  101. (affirm (eq? '("Can only set native definitions as statements")
  102. (resolve '((define x :stmt 1))))
  103. (eq? '("This definition is already a statement")
  104. (resolve '((define-native x :stmt :stmt))))
  105. (eq? '("Cannot have a statement when no syntax given")
  106. (resolve '((define-native x :stmt))))))
  107. (it "using :syntax-fold"
  108. (affirm (eq? '("Can only set syntax of native definitions")
  109. (resolve '((define x :syntax-fold 1))))
  110. (eq? '("Multiple fold directions set")
  111. (resolve '((define-native x :syntax-fold "left" :syntax-fold "left"))))
  112. (eq? '("Expected fold direction, got nothing")
  113. (resolve '((define-native x :syntax-fold))))
  114. (eq? '("Unknown fold direction \"Left\"")
  115. (resolve '((define-native x :syntax-fold "Left"))))
  116. (eq? '("Cannot specify a fold direction when no syntax given")
  117. (resolve '((define-native x :syntax-fold "left"))))
  118. (eq? '("Cannot specify a fold direction with arity 1 (must be 2)")
  119. (resolve '((define-native x :syntax "${1}" :syntax-fold "left"))))))
  120. (it "using :syntax-precedence"
  121. (affirm (eq? '("Can only set syntax of native definitions")
  122. (resolve '((define x :syntax-precedence e1))))
  123. (eq? '("Multiple precedences set")
  124. (resolve '((define-native x :syntax-precedence 0 :syntax-precedence 0))))
  125. (eq? '("Expected precedence, got nothing")
  126. (resolve '((define-native x :syntax-precedence))))
  127. (eq? '("Expected precedence, got key")
  128. (resolve '((define-native x :syntax-precedence (:key)))))
  129. (eq? '("Cannot specify a precedence when no syntax given")
  130. (resolve '((define-native x :syntax-precedence 0))))
  131. (eq? '("Cannot specify a precedence when no syntax given")
  132. (resolve '((define-native x :syntax-precedence 0))))
  133. (eq? '("Definition has arity 1, but precedence has 2 values")
  134. (resolve '((define-native x :syntax "${1}" :syntax-precedence (1 2)))))))
  135. ))
  136. )