Skip to content

nfnl Conversion Guide

Russ Tokuyama edited this page Aug 20, 2024 · 8 revisions

@Olical suggested these guidelines to follow when converting Conjure source files from Aniseed to nfnl.

See the nfnl API docs for the equivalent Aniseed functions.

To translate source:

  • Replace module with calls to nfnl's autoload to load deps.
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    (module conjure.buffer                     | (local {: autoload} (require :nfnl.module))
      {autoload {nvim conjure.aniseed.nvim     | (local nvim (autoload :conjure.aniseed.nvim))
                 a conjure.aniseed.core        | (local a (autoload :nfnl.core))
                 str conjure.aniseed.string    | (local str (autoload :nfnl.string))
                 text conjure.text}})          | (local text (autoload :conjure.text))
    
  • Remove import-macros statements.
    (import-macros {: module : def : defn : defonce : def- : defn- : defonce- : wrap-last-expr : wrap-module-body : deftest} :nfnl.macros.aniseed)
    
  • Replace defn with fn.
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    (defn form [opts]                          | (fn form [opts]
    
  • Replace defn- with fn (private function; don't export).
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    (defn- getpos [expr]                       | (fn getpos [expr]
    
  • Replace defonce with local.
  • Replace def- with local (private name; don't export).
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    (defonce conjure-source-directory          | (local conjure-source-directory
    (def- cats-and-dogs                        | (local cats-and-dogs
    
  • Replace if-let with a let and an if.
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
      (if-let [node (ts.get-leaf)]             |   (let [node (ts.get-leaf)]
                                               |     (if node
        {:range (ts.range node)                |       {:range (ts.range node)
         :content (ts.node->str node)}         |        :content (ts.node->str node)}
        {:range nil                            |       {:range nil
         :content nil})                        |        :content nil}))
    
  • Remove *module* at bottom of the file.
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    *module*                                   |
    
  • Add a table at the bottom of the file to export anything public.
    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
                                               | {: form
                                               |  : prompt
                                               |  : prompt-char}
    
    • defn- declares a private function so don't include them in the table.

To translate test files:

  • Create the equivalent of tests for aniseed-legacy-tests/ in fnl/conjure-spec/.

    Legacy                                     | Busted
    -------------------------------------------+------------------------------------------------
    `<module>-test.fnl`                        | `<module>_spec.fnl`
    legacy-aniseed-tests/fnl/conjure/          |  fnl/conjure-spec/
    
  • Remove module and replace with requires for plenary and busted.

    Legacy                                          | Busted
    ------------------------------------------------+------------------------------------------------
                                                    | (local {: describe : it} (require :plenary.busted))
                                                    | (local assert (require :luassert.assert))
                                                    |
    (module conjure.fs-test                         | (describe "fs"
                                                    |   (fn []
                                                    |     (describe ...) ;; a test
      {require {fs conjure.fs                       | (local fs (require :conjure.fs))
                nvim conjure.aniseed.nvim}})        | (local nvim (require :conjure.aniseed.nvim))
    
  • Replace deftest with describe.

  • Replace t.= with assert.are.equals.

  • Replace t.pr= with assert.same.

    Legacy                                                   | Busted
    ---------------------------------------------------------+------------------------------------------------
    (deftest config-dir                                      | (describe "config-dir"
      (t.= "/home/conjure/.config/conjure" (fs.config-dir))  |   (assert.are.equals "/home/conjure/.config/conjure"
                                                             |                      (fs.config-dir))
      (t.pr= [] (fs.split-path ""))                          |   (assert.same [] (fs.split-path "")))
    

To run the new tests

  • Run scripts/setup-test-deps to set up a hidden directory, .test-config/ which acts as the test environment.

  • Run scripts/test to execute the new tests under the lua/conjure-spec/ directory.

  • If your Neovim is set up with Conjure, Plenary, Aniseed, and nfnl, you can run the busted tests from the Neovim command line with:

    :PlenaryBustedDirectory lua/conjure-spec
    

    Or run a single test file with:

    :PlenaryBustedFile lua/conjure-spec/fs_spec.lua
    

Clone this wiki locally