MCPcopy Create free account
hub / github.com/cli/cli / TestFormatMatch

Function TestFormatMatch

pkg/cmd/search/code/code_test.go:415–470  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

413}
414
415func TestFormatMatch(t *testing.T) {
416 const (
417 hl = "\x1b[0;30;43m"
418 reset = "\x1b[0m"
419 )
420 tests := []struct {
421 name string
422 fragment string
423 matches []search.Match
424 want []string
425 }{
426 {
427 name: "match ending mid-line",
428 fragment: "foo bar baz",
429 matches: []search.Match{{Indices: []int{4, 7}}},
430 want: []string{"foo " + hl + "bar" + reset + " baz"},
431 },
432 {
433 name: "match ending at newline",
434 fragment: "foo bar\nbaz",
435 matches: []search.Match{{Indices: []int{4, 7}}},
436 want: []string{"foo " + hl + "bar" + reset},
437 },
438 {
439 name: "match ending at end of fragment",
440 fragment: "foo bar",
441 matches: []search.Match{{Indices: []int{4, 7}}},
442 want: []string{"foo " + hl + "bar" + reset},
443 },
444 {
445 name: "match spanning newline",
446 fragment: "foo bar\nbaz qux",
447 matches: []search.Match{{Indices: []int{4, 11}}},
448 want: []string{"foo " + hl + "bar" + reset},
449 },
450 {
451 name: "closed match is not reset again at newline",
452 fragment: "foo bar \nbaz",
453 matches: []search.Match{{Indices: []int{0, 3}}},
454 want: []string{hl + "foo" + reset + " bar "},
455 },
456 {
457 name: "matches on multiple lines",
458 fragment: "foo\nbar\nbaz",
459 matches: []search.Match{{Indices: []int{0, 3}}, {Indices: []int{8, 11}}},
460 want: []string{hl + "foo" + reset, hl + "baz" + reset},
461 },
462 }
463 for _, tt := range tests {
464 t.Run(tt.name, func(t *testing.T) {
465 ios, _, _, _ := iostreams.Test()
466 ios.SetColorEnabled(true)
467 assert.Equal(t, tt.want, formatMatch(tt.fragment, tt.matches, ios))
468 })
469 }
470}

Callers

nothing calls this directly

Calls 5

TestFunction · 0.92
formatMatchFunction · 0.85
SetColorEnabledMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected