@@ -430,22 +430,35 @@ func Test_SearchCode(t *testing.T) {
430430 IncompleteResults : github .Ptr (false ),
431431 CodeResults : []* github.CodeResult {
432432 {
433- Name : github .Ptr ("file1.go" ),
434- Path : github .Ptr ("path/to/file1.go" ),
435- SHA : github .Ptr ("abc123def456" ),
436- HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/path/to/file1.go" ),
437- Repository : & github.Repository {Name : github .Ptr ("repo" ), FullName : github .Ptr ("owner/repo" )},
433+ Name : github .Ptr ("file1.go" ),
434+ Path : github .Ptr ("path/to/file1.go" ),
435+ SHA : github .Ptr ("abc123def456" ),
436+ Repository : & github.Repository {
437+ Name : github .Ptr ("repo" ),
438+ FullName : github .Ptr ("owner/repo" ),
439+ },
440+ TextMatches : []* github.TextMatch {
441+ {
442+ Fragment : github .Ptr ("func main() { fmt.Println(\" hello\" ) }" ),
443+ },
444+ },
438445 },
439446 {
440- Name : github .Ptr ("file2.go" ),
441- Path : github .Ptr ("path/to/file2.go" ),
442- SHA : github .Ptr ("def456abc123" ),
443- HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/path/to/file2.go" ),
444- Repository : & github.Repository {Name : github .Ptr ("repo" ), FullName : github .Ptr ("owner/repo" )},
447+ Name : github .Ptr ("file2.go" ),
448+ Path : github .Ptr ("path/to/file2.go" ),
449+ SHA : github .Ptr ("def456abc123" ),
450+ Repository : & github.Repository {
451+ Name : github .Ptr ("repo" ),
452+ FullName : github .Ptr ("owner/repo" ),
453+ },
445454 },
446455 },
447456 }
448457
458+ textMatchAcceptHeader := map [string ]string {
459+ "Accept" : "text-match" ,
460+ }
461+
449462 tests := []struct {
450463 name string
451464 mockedClient * http.Client
@@ -463,7 +476,7 @@ func Test_SearchCode(t *testing.T) {
463476 "order" : "desc" ,
464477 "page" : "1" ,
465478 "per_page" : "30" ,
466- }).andThen (
479+ }).withHeaders ( textMatchAcceptHeader ). andThen (
467480 mockResponse (t , http .StatusOK , mockSearchResult ),
468481 ),
469482 }),
@@ -484,7 +497,7 @@ func Test_SearchCode(t *testing.T) {
484497 "q" : "fmt.Println language:go" ,
485498 "page" : "1" ,
486499 "per_page" : "30" ,
487- }).andThen (
500+ }).withHeaders ( textMatchAcceptHeader ). andThen (
488501 mockResponse (t , http .StatusOK , mockSearchResult ),
489502 ),
490503 }),
@@ -537,22 +550,28 @@ func Test_SearchCode(t *testing.T) {
537550 require .NoError (t , err )
538551 require .False (t , result .IsError )
539552
540- // Parse the result and get the text content if no error
541553 textContent := getTextResult (t , result )
542554
543- // Unmarshal and verify the result
544- var returnedResult github.CodeSearchResult
555+ var returnedResult MinimalCodeSearchResult
545556 err = json .Unmarshal ([]byte (textContent .Text ), & returnedResult )
546557 require .NoError (t , err )
547- assert .Equal (t , * tc .expectedResult .Total , * returnedResult .Total )
548- assert .Equal (t , * tc .expectedResult .IncompleteResults , * returnedResult .IncompleteResults )
549- assert .Len (t , returnedResult .CodeResults , len (tc .expectedResult .CodeResults ))
550- for i , code := range returnedResult .CodeResults {
551- assert .Equal (t , * tc .expectedResult .CodeResults [i ].Name , * code .Name )
552- assert .Equal (t , * tc .expectedResult .CodeResults [i ].Path , * code .Path )
553- assert .Equal (t , * tc .expectedResult .CodeResults [i ].SHA , * code .SHA )
554- assert .Equal (t , * tc .expectedResult .CodeResults [i ].HTMLURL , * code .HTMLURL )
555- assert .Equal (t , * tc .expectedResult .CodeResults [i ].Repository .FullName , * code .Repository .FullName )
558+ assert .Equal (t , * tc .expectedResult .Total , returnedResult .TotalCount )
559+ assert .Equal (t , * tc .expectedResult .IncompleteResults , returnedResult .IncompleteResults )
560+ assert .Len (t , returnedResult .Items , len (tc .expectedResult .CodeResults ))
561+ for i , code := range returnedResult .Items {
562+ assert .Equal (t , tc .expectedResult .CodeResults [i ].GetName (), code .Name )
563+ assert .Equal (t , tc .expectedResult .CodeResults [i ].GetPath (), code .Path )
564+ assert .Equal (t , tc .expectedResult .CodeResults [i ].GetSHA (), code .SHA )
565+ assert .Equal (t , tc .expectedResult .CodeResults [i ].Repository .GetFullName (), code .Repository )
566+ }
567+
568+ // Verify text matches are included when present
569+ if len (tc .expectedResult .CodeResults [0 ].TextMatches ) > 0 {
570+ require .NotEmpty (t , returnedResult .Items [0 ].TextMatches )
571+ assert .Equal (t ,
572+ tc .expectedResult .CodeResults [0 ].TextMatches [0 ].GetFragment (),
573+ returnedResult .Items [0 ].TextMatches [0 ].GetFragment (),
574+ )
556575 }
557576 })
558577 }
0 commit comments