Files
wehub-resource-sync c4536f7e05
CI / test (push) Failing after 1s
CI / macOS amd64 (push) Has been cancelled
CI / macOS arm64 (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:31 +08:00

108 lines
3.0 KiB
Go

package syntax
import (
"image/color"
"strings"
"testing"
)
func TestNormalizeSyntaxStyle(t *testing.T) {
if got := NormalizeStyle("jetbrains-dark"); got != StyleDarcula {
t.Fatalf("expected darcula alias, got %q", got)
}
if got := NormalizeStyle("unknown"); got != StyleGoLand {
t.Fatalf("expected unknown style to fall back to goland, got %q", got)
}
}
func TestHighlightGoLine(t *testing.T) {
palette := testSyntaxPalette()
spans := HighlightGo(`if len(s) > 0 { return "x" // ok }`, palette)
assertSpanColor(t, spans, "if", palette.Keyword)
assertSpanColor(t, spans, "len", palette.Builtin)
assertSpanColor(t, spans, "0", palette.Number)
assertSpanColor(t, spans, `"x"`, palette.String)
assertSpanStyle(t, spans, "// ok }", palette.Comment, false, true)
}
func TestHighlightAsmLine(t *testing.T) {
palette := testSyntaxPalette()
spans := HighlightAsm("CALL runtime.morestack_noctxt(SB); tail", "runtime.morestack_noctxt", palette)
assertSpanStyle(t, spans, "CALL", palette.Mnemonic, true, false)
assertSpanColor(t, spans, "runtime.morestack_noctxt", palette.CallTarget)
assertSpanColor(t, spans, "SB", palette.Register)
assertSpanStyle(t, spans, "; tail", palette.Comment, false, true)
}
func TestHighlightNativeAsmLine(t *testing.T) {
palette := testSyntaxPalette()
spans := HighlightAsm("MOVQ $0X10, %RAX", "", palette)
assertSpanStyle(t, spans, "MOVQ", palette.Mnemonic, true, false)
assertSpanColor(t, spans, "$0X10", palette.Number)
assertSpanColor(t, spans, "%RAX", palette.Register)
}
func testSyntaxPalette() Palette {
return Palette{
Plain: testColor(1),
Keyword: testColor(2),
Builtin: testColor(3),
String: testColor(4),
Number: testColor(5),
Comment: testColor(6),
Operator: testColor(7),
Register: testColor(8),
Mnemonic: testColor(9),
Symbol: testColor(10),
LineNumber: testColor(11),
CallTarget: testColor(12),
}
}
func testColor(v uint8) color.NRGBA {
return color.NRGBA{R: v, G: v, B: v, A: 0xff}
}
func assertSpanColor(t *testing.T, spans []Span, text string, col color.NRGBA) {
t.Helper()
span, ok := findSpan(spans, text)
if !ok {
t.Fatalf("span %q not found in %q", text, joinedSpanText(spans))
}
if span.Color != col {
t.Fatalf("span %q color = %#v, want %#v", text, span.Color, col)
}
}
func assertSpanStyle(t *testing.T, spans []Span, text string, col color.NRGBA, bold, italic bool) {
t.Helper()
span, ok := findSpan(spans, text)
if !ok {
t.Fatalf("span %q not found in %q", text, joinedSpanText(spans))
}
if span.Color != col || span.Bold != bold || span.Italic != italic {
t.Fatalf("span %q style = %#v, bold=%v italic=%v; want %#v, bold=%v italic=%v",
text, span.Color, span.Bold, span.Italic, col, bold, italic)
}
}
func findSpan(spans []Span, text string) (Span, bool) {
for _, span := range spans {
if span.Text == text {
return span, true
}
}
return Span{}, false
}
func joinedSpanText(spans []Span) string {
var b strings.Builder
for _, span := range spans {
b.WriteString(span.Text)
}
return b.String()
}