~technomancy/fennel

f3805da822b61178da83658f942ef2c77d539400 — Jesse Wertheim 3 months ago 1a20b5e fix-gh-354-windows-docstrings
fix: on windows, make doc special output crlf

Resolves #354 (on github)

This updates the doc special so that it checks if running in windows
(stupidly, by comparing the path separator  against "\\"), and
conditionally outputs the docstring with \n replaced by \r\n.

This also conditionally modifies the docstring test cases when run on
windows.
2 files changed, 9 insertions(+), 2 deletions(-)

M src/fennel/specials.fnl
M test/docstring.fnl
M src/fennel/specials.fnl => src/fennel/specials.fnl +6 -1
@@ 298,6 298,11 @@ and lacking args will be nil, use lambda for arity-checked functions." true)
  (assert utils.root.options.useMetadata
          "can't look up doc with metadata disabled.")
  (compiler.assert (= (length ast) 2) "expected one argument" ast)
  ;; if path-sep is \, we'll replace all bare instances of \n with \r\n
  (local normalize-newlines (if (not= "\\" (string.sub package.config 1 1))
                              #$ ; identity fn
                              #(string.gsub $ "([^\r]?)\n"
                                            (if (= 0 ($1:len)) "\r\n" (.. $1 "\r\n")))))
  (let [target (utils.deref (. ast 2))
        special-or-macro (or (. scope.specials target) (. scope.macros target))]
    (if special-or-macro


@@ 308,7 313,7 @@ and lacking args will be nil, use lambda for arity-checked functions." true)
          ;; declared from.
          (: "print(require('%s').doc(%s, '%s'))" :format
             (or utils.root.options.moduleName :fennel) (tostring value)
             (tostring (. ast 2)))))))
             (normalize-newlines (tostring (. ast 2))))))))

(doc-special :doc [:x]
             "Print the docstring and arglist for a function, macro, or special form.")

M test/docstring.fnl => test/docstring.fnl +3 -1
@@ 4,7 4,6 @@

(local doc-env (setmetatable {:print #$ :fennel fennel}
                             {:__index _G}))

(local cases
       [["(doc doc)" "(doc x)\n  Print the docstring and arglist for a function, macro, or special form." "docstrings for special forms"]
        ["(doc doto)" "(doto val ...)\n  Evaluates val and splices it into the first argument of subsequent forms." "docstrings for built-in macros" ]


@@ 29,6 28,9 @@

(fn test-docstrings []
  (each [_ [code expected msg] (ipairs cases)]
    ;; when test is running on windows, we expect the docstrings to output with CRLF
    (when (= "\\" (string.sub package.config 1 1))
      (set-forcibly! expected (string.gsub expected "\n" "\r\n")))
    (l.assertEquals (eval code) expected msg)))

(fn test-no-undocumented []