#+test package markdown import ts "../treesitter" import "core:mem" import "core:strings" import "core:testing" // highlight_block returns the input string unchanged (borrowed) when it can't // highlight — no grammar, no query, or zero captures — and a freshly-allocated // string when it does. Free only the latter; identity (raw_data) tells them // apart regardless of content. @(private = "file") free_highlight :: proc(out, code: string) { if raw_data(out) != raw_data(code) { delete(out) } } // A builtin grammar (css) highlights into hl- spans, preserving the source text. @(test) test_highlight_block_css :: proc(t: ^testing.T) { code := "a{color:red}" out := highlight_block(code, "css", "test") defer free_highlight(out, code) testing.expect(t, strings.contains(out, "")) testing.expect(t, strings.contains(out, "color")) testing.expect(t, strings.contains(out, "red")) } // The other builtin grammar (html) highlights, and the source's own '<'/'>' are // HTML-escaped in the output (so they can't be confused with the wrapping spans). @(test) test_highlight_block_html_escapes_source :: proc(t: ^testing.T) { code := "

hi

" out := highlight_block(code, "html", "test") defer free_highlight(out, code) testing.expect(t, strings.contains(out, " shell. // Run under a scratch arena (as production does) so the intermediate allocations // highlight_code doesn't individually free are reclaimed in one shot. @(test) test_highlight_code_wraps_css_block :: proc(t: ^testing.T) { arena: mem.Dynamic_Arena mem.dynamic_arena_init(&arena) defer mem.dynamic_arena_destroy(&arena) context.allocator = mem.dynamic_arena_allocator(&arena) html := `
a{color:red}
` out := highlight_code(html, "test") testing.expect(t, strings.contains(out, `
`))
	testing.expect(t, strings.contains(out, "
")) testing.expect( t, strings.contains(out, "") } // Same contract on the no-entity path: the buggy version returns the borrowed // input, so freeing it is a bad free (pointer not from the allocator). @(test) test_unescape_html_passthrough_owned :: proc(t: ^testing.T) { got := unescape_html("plain text") defer delete(got) testing.expect_value(t, got, "plain text") }