From f99667fbf56e1d370cbf2e9d797be52c954a1da9 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 20 Aug 2024 15:39:13 +0200 Subject: [PATCH] Add fennel loader --- .busted | 5 ++++- glass/fennel.lua | 18 ++++++++++++++++++ spec/fennel/fennel_loader_spec.moon | 6 ++++++ spec/fixtures/test.fnl | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 glass/fennel.lua create mode 100644 spec/fennel/fennel_loader_spec.moon create mode 100644 spec/fixtures/test.fnl diff --git a/.busted b/.busted index bf887f6..5d1b48a 100644 --- a/.busted +++ b/.busted @@ -1,5 +1,8 @@ return { _all = { lpath = "?.lua;?/init.lua" - } + }; + core = { + recursive = false + }; } diff --git a/glass/fennel.lua b/glass/fennel.lua new file mode 100644 index 0000000..630dc27 --- /dev/null +++ b/glass/fennel.lua @@ -0,0 +1,18 @@ +--- Loader for Fennel files +-- @module glass.fennel + +local fennel = require "fennel" +local read = require 'glass.raw' + +--- Loads and compiles a Fennel file and runs it. +-- Returns the result of running the Fennel file. +-- If running the code immediately is not desired, +-- it has to be returned as a function. +-- @return The result of the Fennel file after running it +-- @function load +return function(file) + local success, raw = read(file..'.fnl') + if success then + return true, fennel.eval(raw) + end +end diff --git a/spec/fennel/fennel_loader_spec.moon b/spec/fennel/fennel_loader_spec.moon new file mode 100644 index 0000000..5f802a8 --- /dev/null +++ b/spec/fennel/fennel_loader_spec.moon @@ -0,0 +1,6 @@ +describe 'fennel loader', -> + before_each -> export loader = require 'glass.fennel' + it 'loads Fennel files', -> + success, result = loader 'spec/fixtures/test' + assert.true success + assert.same { foo: 'bar', tab: {} }, result diff --git a/spec/fixtures/test.fnl b/spec/fixtures/test.fnl new file mode 100644 index 0000000..9b70311 --- /dev/null +++ b/spec/fixtures/test.fnl @@ -0,0 +1,2 @@ +{"foo" "bar" + "tab" []}