Skip to content
quarry Artifact [8f27767196]
Login

Artifact [8f27767196]

Artifact 8f27767196627b0147da922df932f1ce743c91dd394df52738e9a8c02ea3b0b5:


package highlight

import (
	"strings"
	"testing"
)

// Highlighting cost tracks token density and was paid on every request:
// sqlite's src/btree.c, 407KB of dense C, took 490ms and rendered 2.3MB
// of HTML. Past MaxBytes the lexer is skipped so the page arrives.
func TestOversizeContentIsNotHighlighted(t *testing.T) {
	big := []byte(strings.Repeat("int x = 1; /* filler */\n", (MaxBytes/24)+64))
	if len(big) <= MaxBytes {
		t.Fatalf("fixture is %d bytes, not over the %d limit", len(big), MaxBytes)
	}
	if !TooBig(big) {
		t.Error("TooBig said no for content past the limit")
	}
	if html, ok := Code(big, "", "big.c"); ok || html != "" {
		t.Errorf("oversize content was highlighted (%d bytes of html)", len(html))
	}
	// Just under the limit still gets colour: the gate must not swallow
	// ordinary files.
	small := []byte("int x = 1;\n")
	if _, ok := Code(small, "", "small.c"); !ok {
		t.Error("a small C file was not highlighted")
	}
}

// A blob is immutable, so its highlighting is too. The second call must
// not tokenise again — and must return the same bytes.
func TestHighlightingIsCachedByContent(t *testing.T) {
	src := []byte("package main\n\nfunc main() { println(\"x\") }\n")
	first, ok := Code(src, "", "a.go")
	if !ok || first == "" {
		t.Fatal("first highlight failed")
	}
	cache.Lock()
	n := len(cache.m)
	cache.Unlock()
	second, ok2 := Code(src, "", "a.go")
	if !ok2 || second != first {
		t.Error("cached result differs from the first render")
	}
	cache.Lock()
	after := len(cache.m)
	cache.Unlock()
	if after != n {
		t.Errorf("a repeat of identical content added an entry (%d -> %d)", n, after)
	}
	// Same bytes, different filename, is a different lexer question.
	if _, _ = Code(src, "", "a.txt"); true {
		cache.Lock()
		defer cache.Unlock()
		if len(cache.m) == after {
			t.Error("the filename is not part of the cache key")
		}
	}
}

// The budget is in BYTES because entries are wildly uneven. Overflowing
// it must not grow without bound.
func TestCacheStaysWithinItsBudget(t *testing.T) {
	cache.Lock()
	cache.m, cache.bytes = map[string]string{}, 0
	cache.Unlock()
	cachePut("k", strings.Repeat("x", cacheBudget-1))
	cachePut("k2", strings.Repeat("y", 1024))
	cache.Lock()
	defer cache.Unlock()
	if cache.bytes > cacheBudget {
		t.Errorf("cache holds %d bytes, over the %d budget", cache.bytes, cacheBudget)
	}
}