mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Improved the way sites are loaded.
This commit is contained in:
+196
@@ -0,0 +1,196 @@
|
||||
#+feature dynamic-literals
|
||||
#+test
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:testing"
|
||||
|
||||
write_temp_config :: proc(name: string, content: string) -> string {
|
||||
path := fmt.tprintf("./test_thor_%s.json", name)
|
||||
write_err := os.write_entire_file_from_string(path, content)
|
||||
if write_err != nil {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_load_site_config :: proc(t: ^testing.T) {
|
||||
path := write_temp_config(
|
||||
"valid",
|
||||
`{"title":"Test Site","description":"Test desc","base_url":"https://example.com","author":"Tester","social":[{"name":"github","url":"https://github.com/test"},{"name":"rss","url":"/index.xml"}]}`,
|
||||
)
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
ok := load_site_config(&site, path, context.temp_allocator)
|
||||
|
||||
testing.expect(t, ok)
|
||||
testing.expect_value(t, site.title, "Test Site")
|
||||
testing.expect_value(t, site.description, "Test desc")
|
||||
testing.expect_value(t, site.base_url, "https://example.com")
|
||||
testing.expect_value(t, site.author, "Tester")
|
||||
testing.expect_value(t, len(site.social), 2)
|
||||
testing.expect_value(t, site.social[0].name, "github")
|
||||
testing.expect_value(t, site.social[0].url, "https://github.com/test")
|
||||
testing.expect_value(t, site.social[1].name, "rss")
|
||||
testing.expect_value(t, site.social[1].url, "/index.xml")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_load_site_config_missing_file :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
ok := load_site_config(&site, "./nonexistent_thor_test.json", context.temp_allocator)
|
||||
testing.expect(t, !ok)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_load_site_config_invalid_json :: proc(t: ^testing.T) {
|
||||
path := write_temp_config("invalid", `{not valid json}`)
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
ok := load_site_config(&site, path, context.temp_allocator)
|
||||
testing.expect(t, !ok)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_load_site_config_partial :: proc(t: ^testing.T) {
|
||||
path := write_temp_config("partial", `{"title":"Partial"}`)
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
ok := load_site_config(&site, path, context.temp_allocator)
|
||||
|
||||
testing.expect(t, ok)
|
||||
testing.expect_value(t, site.title, "Partial")
|
||||
testing.expect_value(t, site.description, "")
|
||||
testing.expect_value(t, site.author, "")
|
||||
testing.expect_value(t, len(site.social), 0)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_site_merge_overrides :: proc(t: ^testing.T) {
|
||||
config := Site {
|
||||
base_url = "https://original.com",
|
||||
content_dir = "./content",
|
||||
}
|
||||
flags := Site {
|
||||
base_url = "https://override.com",
|
||||
}
|
||||
|
||||
site_merge(&config, flags)
|
||||
|
||||
testing.expect_value(t, config.base_url, "https://override.com")
|
||||
testing.expect_value(t, config.content_dir, "./content")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_site_merge_empty_flags_keep_config :: proc(t: ^testing.T) {
|
||||
config := Site {
|
||||
base_url = "https://keep.com",
|
||||
content_dir = "./keep",
|
||||
}
|
||||
flags := Site{}
|
||||
|
||||
site_merge(&config, flags)
|
||||
|
||||
testing.expect_value(t, config.base_url, "https://keep.com")
|
||||
testing.expect_value(t, config.content_dir, "./keep")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_site_merge_drafts_true :: proc(t: ^testing.T) {
|
||||
config := Site {
|
||||
drafts = false,
|
||||
}
|
||||
flags := Site {
|
||||
drafts = true,
|
||||
}
|
||||
|
||||
site_merge(&config, flags)
|
||||
testing.expect(t, config.drafts)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_site_merge_drafts_false_preserves :: proc(t: ^testing.T) {
|
||||
config := Site {
|
||||
drafts = false,
|
||||
}
|
||||
flags := Site {
|
||||
drafts = false,
|
||||
}
|
||||
|
||||
site_merge(&config, flags)
|
||||
testing.expect(t, !config.drafts)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_site_merge_config_path :: proc(t: ^testing.T) {
|
||||
config := Site{}
|
||||
flags := Site {
|
||||
config_path = "./custom/thor.json",
|
||||
}
|
||||
|
||||
site_merge(&config, flags)
|
||||
testing.expect_value(t, config.config_path, "./custom/thor.json")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_init_site_defaults_no_config :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./nonexistent.json"}
|
||||
init_site(&site, args)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.content_dir, "./content")
|
||||
testing.expect_value(t, site.output_dir, "./public")
|
||||
testing.expect_value(t, site.layouts_dir, "./layouts")
|
||||
testing.expect_value(t, site.base_url, "http://localhost:8080")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_init_site_config_dir_relative :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./sub/nonexistent.json"}
|
||||
init_site(&site, args)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.content_dir, "./sub/content")
|
||||
testing.expect_value(t, site.output_dir, "./sub/public")
|
||||
testing.expect_value(t, site.layouts_dir, "./sub/layouts")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_init_site_flag_overrides_default :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./nonexistent.json", "-drafts", "-base-url:https://flag.com"}
|
||||
init_site(&site, args)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect(t, site.drafts)
|
||||
testing.expect_value(t, site.base_url, "https://flag.com")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_init_site_full_pipeline :: proc(t: ^testing.T) {
|
||||
path := write_temp_config(
|
||||
"pipeline",
|
||||
`{"title":"Pipeline Test","description":"Full","base_url":"https://config.com","author":"Author"}`,
|
||||
)
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", fmt.tprintf("-config:%s", path), "-drafts"}
|
||||
init_site(&site, args)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.title, "Pipeline Test")
|
||||
testing.expect_value(t, site.description, "Full")
|
||||
testing.expect_value(t, site.author, "Author")
|
||||
testing.expect(t, site.drafts)
|
||||
testing.expect_value(t, site.base_url, "https://config.com")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user