From 74a00525886bf10ec36c89c9379c532843f69d19 Mon Sep 17 00:00:00 2001 From: Jason Paris Date: Sun, 6 Jul 2025 21:20:25 -0400 Subject: [PATCH] test: add comprehensive tests for user-defined template variables - Add config test for merging user template variables with defaults - Add integration test for config-defined template variables in substitution - Add test for custom variable override behavior - Verify both function and static value template variables work correctly --- tests/markdown-notes/config_spec.lua | 26 +++++++++++++ tests/markdown-notes/templates_spec.lua | 49 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/tests/markdown-notes/config_spec.lua b/tests/markdown-notes/config_spec.lua index 8c17649..c14ebed 100644 --- a/tests/markdown-notes/config_spec.lua +++ b/tests/markdown-notes/config_spec.lua @@ -42,6 +42,32 @@ describe("config", function() assert.is_not_nil(config.options.mappings.new_note) end) + it("merges user-defined template variables with defaults", function() + local user_opts = { + template_vars = { + author = function() return "Test Author" end, + project = function() return "test-project" end, + custom_string = "static-value" + } + } + + config.setup(user_opts) + + -- User variables should be present + assert.is_function(config.options.template_vars.author) + assert.is_function(config.options.template_vars.project) + assert.are.equal("static-value", config.options.template_vars.custom_string) + + -- Default variables should still be present + assert.is_function(config.options.template_vars.date) + assert.is_function(config.options.template_vars.time) + assert.is_function(config.options.template_vars.title) + + -- Test that user functions work + assert.are.equal("Test Author", config.options.template_vars.author()) + assert.are.equal("test-project", config.options.template_vars.project()) + end) + describe("workspaces", function() it("sets up workspace with custom config", function() local workspace_opts = { diff --git a/tests/markdown-notes/templates_spec.lua b/tests/markdown-notes/templates_spec.lua index d956625..fd0b013 100644 --- a/tests/markdown-notes/templates_spec.lua +++ b/tests/markdown-notes/templates_spec.lua @@ -42,5 +42,54 @@ describe("templates", function() assert.matches("%d%d%d%d%-%d%d%-%d%d", result[1]) assert.matches("%d%d:%d%d", result[1]) end) + + it("uses config-defined template variables", function() + -- Setup config with custom template variables + config.setup({ + template_vars = { + author = function() return "Test Author" end, + project = function() return "my-project" end, + custom_static = "static-content" + } + }) + + local content = { + "Author: {{author}}", + "Project: {{project}}", + "Static: {{custom_static}}", + "Date: {{date}}" + } + + local result = templates.substitute_template_vars(content) + + -- Custom variables should be substituted + assert.are.equal("Author: Test Author", result[1]) + assert.are.equal("Project: my-project", result[2]) + assert.are.equal("Static: static-content", result[3]) + + -- Default variables should still work + assert.matches("Date: %d%d%d%d%-%d%d%-%d%d", result[4]) + end) + + it("overrides config variables with custom vars parameter", function() + -- Setup config with a template variable + config.setup({ + template_vars = { + author = function() return "Config Author" end, + } + }) + + local content = {"Author: {{author}}"} + + -- Override with custom vars + local custom_vars = { + author = function() return "Override Author" end + } + + local result = templates.substitute_template_vars(content, custom_vars) + + -- Should use the override, not the config value + assert.are.equal("Author: Override Author", result[1]) + end) end) end) \ No newline at end of file