kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/serving/pipeline/beam_test.go (about) 1 /* 2 * Copyright 2018 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package pipeline 18 19 import ( 20 "testing" 21 22 "kythe.io/kythe/go/serving/pipeline/beamtest" 23 24 "github.com/apache/beam/sdks/go/pkg/beam" 25 "github.com/apache/beam/sdks/go/pkg/beam/testing/passert" 26 "github.com/apache/beam/sdks/go/pkg/beam/testing/ptest" 27 "github.com/apache/beam/sdks/go/pkg/beam/x/debug" 28 "github.com/google/go-cmp/cmp" 29 "github.com/google/go-cmp/cmp/cmpopts" 30 "google.golang.org/protobuf/proto" 31 32 cpb "kythe.io/kythe/proto/common_go_proto" 33 ppb "kythe.io/kythe/proto/pipeline_go_proto" 34 scpb "kythe.io/kythe/proto/schema_go_proto" 35 srvpb "kythe.io/kythe/proto/serving_go_proto" 36 spb "kythe.io/kythe/proto/storage_go_proto" 37 ) 38 39 func TestReferences(t *testing.T) { 40 testNodes := []*scpb.Node{{ 41 Source: &spb.VName{Path: "path", Signature: "anchor1"}, 42 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 43 Fact: []*scpb.Fact{{ 44 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 45 Value: []byte("0"), 46 }, { 47 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 48 Value: []byte("4"), 49 }, { 50 Name: &scpb.Fact_KytheName{scpb.FactName_BUILD_CONFIG}, 51 Value: []byte("test-build-config"), 52 }}, 53 Edge: []*scpb.Edge{{ 54 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_REF}, 55 Target: &spb.VName{Signature: "node1"}, 56 }}, 57 }, { 58 Source: &spb.VName{Path: "path", Signature: "anchor2"}, 59 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 60 Fact: []*scpb.Fact{{ 61 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 62 Value: []byte("5"), 63 }, { 64 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 65 Value: []byte("9"), 66 }}, 67 Edge: []*scpb.Edge{{ 68 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_CHILD_OF}, 69 Target: &spb.VName{Path: "path", Signature: "anchor2_parent"}, 70 }, { 71 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_REF_CALL}, 72 Target: &spb.VName{Signature: "node2"}, 73 }}, 74 }, { 75 Source: &spb.VName{Path: "path"}, 76 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 77 Fact: []*scpb.Fact{{ 78 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 79 Value: []byte("some text\n"), 80 }}, 81 }} 82 83 expected := []*ppb.Reference{{ 84 Source: &spb.VName{Signature: "node1"}, 85 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_REF}, 86 Anchor: &srvpb.ExpandedAnchor{ 87 Ticket: "kythe:?path=path#anchor1", 88 Text: "some", 89 Span: &cpb.Span{ 90 Start: &cpb.Point{ 91 LineNumber: 1, 92 }, 93 End: &cpb.Point{ 94 ByteOffset: 4, 95 LineNumber: 1, 96 ColumnOffset: 4, 97 }, 98 }, 99 Snippet: "some text", 100 SnippetSpan: &cpb.Span{ 101 Start: &cpb.Point{ 102 LineNumber: 1, 103 }, 104 End: &cpb.Point{ 105 ByteOffset: 9, 106 LineNumber: 1, 107 ColumnOffset: 9, 108 }, 109 }, 110 BuildConfiguration: "test-build-config", 111 }, 112 }, { 113 Source: &spb.VName{Signature: "node2"}, 114 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_REF_CALL}, 115 Scope: &spb.VName{Path: "path", Signature: "anchor2_parent"}, 116 Anchor: &srvpb.ExpandedAnchor{ 117 Ticket: "kythe:?path=path#anchor2", 118 Text: "text", 119 Span: &cpb.Span{ 120 Start: &cpb.Point{ 121 ByteOffset: 5, 122 LineNumber: 1, 123 ColumnOffset: 5, 124 }, 125 End: &cpb.Point{ 126 ByteOffset: 9, 127 LineNumber: 1, 128 ColumnOffset: 9, 129 }, 130 }, 131 Snippet: "some text", 132 SnippetSpan: &cpb.Span{ 133 Start: &cpb.Point{ 134 LineNumber: 1, 135 }, 136 End: &cpb.Point{ 137 ByteOffset: 9, 138 LineNumber: 1, 139 ColumnOffset: 9, 140 }, 141 }, 142 }, 143 }} 144 145 beam.Init() 146 p, s, nodes := ptest.CreateList(testNodes) 147 refs := FromNodes(s, nodes).References() 148 debug.Print(s, refs) 149 passert.Equals(s, refs, beam.CreateList(s, expected)) 150 151 ptest.RunAndValidate(t, p) 152 } 153 154 func TestDecorations_targetNode(t *testing.T) { 155 testNodes := []*scpb.Node{{ 156 Source: &spb.VName{Path: "path", Signature: "anchor1"}, 157 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 158 Fact: []*scpb.Fact{{ 159 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 160 Value: []byte("5"), 161 }, { 162 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 163 Value: []byte("9"), 164 }}, 165 Edge: []*scpb.Edge{{ 166 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_REF}, 167 Target: &spb.VName{Signature: "node1"}, 168 }}, 169 }, { 170 Source: &spb.VName{Path: "path"}, 171 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 172 Fact: []*scpb.Fact{{ 173 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 174 Value: []byte("some text\n"), 175 }}, 176 }, { 177 Source: &spb.VName{Signature: "node1"}, 178 Kind: &scpb.Node_KytheKind{scpb.NodeKind_RECORD}, 179 Fact: []*scpb.Fact{{ 180 Name: &scpb.Fact_GenericName{"/unknown/fact/name"}, 181 Value: []byte("something"), 182 }}, 183 }} 184 185 expected := []*srvpb.FileDecorations{{ 186 File: &srvpb.File{ 187 Text: []byte("some text\n"), 188 }, 189 Decoration: []*srvpb.FileDecorations_Decoration{{ 190 Anchor: &srvpb.RawAnchor{ 191 StartOffset: 5, 192 EndOffset: 9, 193 }, 194 Kind: "/kythe/edge/ref", 195 Target: "kythe:#node1", 196 }}, 197 Target: []*srvpb.Node{{ 198 Ticket: "kythe:#node1", 199 Fact: []*cpb.Fact{{ 200 Name: "/kythe/node/kind", 201 Value: []byte("record"), 202 }, { 203 Name: "/unknown/fact/name", 204 Value: []byte("something"), 205 }}, 206 }}, 207 }} 208 209 beam.Init() 210 p, s, nodes := ptest.CreateList(testNodes) 211 decor := FromNodes(s, nodes).Decorations() 212 debug.Print(s, decor) 213 passert.Equals(s, beam.DropKey(s, decor), beam.CreateList(s, expected)) 214 215 ptest.RunAndValidate(t, p) 216 } 217 218 func TestDecorations_decoration(t *testing.T) { 219 testNodes := []*scpb.Node{{ 220 Source: &spb.VName{Path: "path", Signature: "anchor1"}, 221 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 222 Fact: []*scpb.Fact{{ 223 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 224 Value: []byte("5"), 225 }, { 226 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 227 Value: []byte("9"), 228 }}, 229 Edge: []*scpb.Edge{{ 230 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_REF}, 231 Target: &spb.VName{Signature: "node1"}, 232 }}, 233 }, { 234 Source: &spb.VName{Path: "path"}, 235 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 236 Fact: []*scpb.Fact{{ 237 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 238 Value: []byte("some text\n"), 239 }}, 240 }} 241 242 expected := []*srvpb.FileDecorations{{ 243 File: &srvpb.File{ 244 Text: []byte("some text\n"), 245 }, 246 Decoration: []*srvpb.FileDecorations_Decoration{{ 247 Anchor: &srvpb.RawAnchor{ 248 StartOffset: 5, 249 EndOffset: 9, 250 }, 251 Kind: "/kythe/edge/ref", 252 Target: "kythe:#node1", 253 }}, 254 }} 255 256 beam.Init() 257 p, s, nodes := ptest.CreateList(testNodes) 258 decor := FromNodes(s, nodes).Decorations() 259 debug.Print(s, decor) 260 passert.Equals(s, beam.DropKey(s, decor), beam.CreateList(s, expected)) 261 262 ptest.RunAndValidate(t, p) 263 } 264 265 func TestDecorations_diagnostics(t *testing.T) { 266 testNodes := []*scpb.Node{{ 267 Source: &spb.VName{Path: "path", Signature: "anchor1"}, 268 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 269 Fact: []*scpb.Fact{{ 270 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 271 Value: []byte("5"), 272 }, { 273 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 274 Value: []byte("9"), 275 }}, 276 Edge: []*scpb.Edge{{ 277 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_TAGGED}, 278 Target: &spb.VName{Signature: "diagnostic"}, 279 }}, 280 }, { 281 Source: &spb.VName{Path: "path"}, 282 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 283 Fact: []*scpb.Fact{{ 284 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 285 Value: []byte("some text\n"), 286 }}, 287 Edge: []*scpb.Edge{{ 288 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_TAGGED}, 289 Target: &spb.VName{Signature: "diagnostic"}, 290 }}, 291 }, { 292 Source: &spb.VName{Signature: "diagnostic"}, 293 Kind: &scpb.Node_KytheKind{scpb.NodeKind_DIAGNOSTIC}, 294 Fact: []*scpb.Fact{{ 295 Name: &scpb.Fact_KytheName{scpb.FactName_MESSAGE}, 296 Value: []byte("msg"), 297 }, { 298 Name: &scpb.Fact_KytheName{scpb.FactName_DETAILS}, 299 Value: []byte("deets"), 300 }, { 301 Name: &scpb.Fact_KytheName{scpb.FactName_CONTEXT_URL}, 302 Value: []byte("https://kythe.io"), 303 }}, 304 }} 305 306 expected := []*srvpb.FileDecorations{{ 307 File: &srvpb.File{Text: []byte("some text\n")}, 308 Diagnostic: []*cpb.Diagnostic{{ 309 Message: "msg", 310 Details: "deets", 311 ContextUrl: "https://kythe.io", 312 }, { 313 Span: &cpb.Span{ 314 Start: &cpb.Point{ 315 ByteOffset: 5, 316 LineNumber: 1, 317 ColumnOffset: 5, 318 }, 319 End: &cpb.Point{ 320 ByteOffset: 9, 321 LineNumber: 1, 322 ColumnOffset: 9, 323 }, 324 }, 325 Message: "msg", 326 Details: "deets", 327 ContextUrl: "https://kythe.io", 328 }}, 329 }} 330 331 beam.Init() 332 p, s, nodes := ptest.CreateList(testNodes) 333 decor := FromNodes(s, nodes).Decorations() 334 debug.Print(s, decor) 335 passert.Equals(s, beam.DropKey(s, decor), beam.CreateList(s, expected)) 336 337 ptest.RunAndValidate(t, p) 338 } 339 340 func TestDecorations_targetDefinition(t *testing.T) { 341 testNodes := []*scpb.Node{{ 342 Source: &spb.VName{Path: "path", Signature: "anchor1"}, 343 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 344 Fact: []*scpb.Fact{{ 345 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 346 Value: []byte("5"), 347 }, { 348 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 349 Value: []byte("9"), 350 }}, 351 Edge: []*scpb.Edge{{ 352 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_REF}, 353 Target: &spb.VName{Signature: "node1"}, 354 }}, 355 }, { 356 Source: &spb.VName{Path: "path"}, 357 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 358 Fact: []*scpb.Fact{{ 359 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 360 Value: []byte("some text\n"), 361 }}, 362 }, { 363 Source: &spb.VName{Path: "path2", Signature: "def1"}, 364 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 365 Fact: []*scpb.Fact{{ 366 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 367 Value: []byte("5"), 368 }, { 369 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 370 Value: []byte("8"), 371 }}, 372 Edge: []*scpb.Edge{{ 373 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DEFINES_BINDING}, 374 Target: &spb.VName{Signature: "node1"}, 375 }}, 376 }, { 377 Source: &spb.VName{Path: "path2"}, 378 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 379 Fact: []*scpb.Fact{{ 380 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 381 Value: []byte("some def\n"), 382 }}, 383 }} 384 385 def := &srvpb.ExpandedAnchor{ 386 Ticket: "kythe:?path=path2#def1", 387 Text: "def", 388 Span: &cpb.Span{ 389 Start: &cpb.Point{ 390 ByteOffset: 5, 391 LineNumber: 1, 392 ColumnOffset: 5, 393 }, 394 End: &cpb.Point{ 395 ByteOffset: 8, 396 LineNumber: 1, 397 ColumnOffset: 8, 398 }, 399 }, 400 Snippet: "some def", 401 SnippetSpan: &cpb.Span{ 402 Start: &cpb.Point{ 403 LineNumber: 1, 404 }, 405 End: &cpb.Point{ 406 ByteOffset: 8, 407 LineNumber: 1, 408 ColumnOffset: 8, 409 }, 410 }, 411 } 412 expected := []*srvpb.FileDecorations{{ 413 File: &srvpb.File{Text: []byte("some text\n")}, 414 Decoration: []*srvpb.FileDecorations_Decoration{{ 415 Anchor: &srvpb.RawAnchor{ 416 StartOffset: 5, 417 EndOffset: 9, 418 }, 419 Kind: "/kythe/edge/ref", 420 Target: "kythe:#node1", 421 TargetDefinition: "kythe:?path=path2#def1", 422 }}, 423 TargetDefinitions: []*srvpb.ExpandedAnchor{def}, 424 }, { 425 File: &srvpb.File{Text: []byte("some def\n")}, 426 Decoration: []*srvpb.FileDecorations_Decoration{{ 427 Anchor: &srvpb.RawAnchor{ 428 StartOffset: 5, 429 EndOffset: 8, 430 }, 431 Kind: "/kythe/edge/defines/binding", 432 Target: "kythe:#node1", 433 434 // TODO(schroederc): ellide TargetDefinition for actual definitions 435 TargetDefinition: "kythe:?path=path2#def1", 436 }}, 437 TargetDefinitions: []*srvpb.ExpandedAnchor{def}, 438 }} 439 440 beam.Init() 441 p, s, nodes := ptest.CreateList(testNodes) 442 decor := FromNodes(s, nodes).Decorations() 443 debug.Print(s, decor) 444 passert.Equals(s, beam.DropKey(s, decor), beam.CreateList(s, expected)) 445 446 ptest.RunAndValidate(t, p) 447 } 448 449 func TestDecorations_overrides(t *testing.T) { 450 testNodes := []*scpb.Node{{ 451 Source: &spb.VName{Path: "path"}, 452 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 453 Fact: []*scpb.Fact{{ 454 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 455 Value: []byte("some text\n"), 456 }}, 457 }, { 458 Source: &spb.VName{Path: "path2", Signature: "def1"}, 459 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 460 Fact: []*scpb.Fact{{ 461 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 462 Value: []byte("5"), 463 }, { 464 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 465 Value: []byte("8"), 466 }}, 467 Edge: []*scpb.Edge{{ 468 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DEFINES_BINDING}, 469 Target: &spb.VName{Signature: "node1"}, 470 }}, 471 }, { 472 Source: &spb.VName{Path: "path2", Signature: "def2"}, 473 Kind: &scpb.Node_KytheKind{scpb.NodeKind_ANCHOR}, 474 Fact: []*scpb.Fact{{ 475 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_START}, 476 Value: []byte("4"), 477 }, { 478 Name: &scpb.Fact_KytheName{scpb.FactName_LOC_END}, 479 Value: []byte("7"), 480 }}, 481 Edge: []*scpb.Edge{{ 482 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DEFINES_BINDING}, 483 Target: &spb.VName{Signature: "node2"}, 484 }}, 485 }, { 486 Source: &spb.VName{Signature: "node2"}, 487 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FUNCTION}, 488 Edge: []*scpb.Edge{{ 489 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_OVERRIDES}, 490 Target: &spb.VName{Signature: "node1"}, 491 }}, 492 }, { 493 Source: &spb.VName{Path: "path2"}, 494 Kind: &scpb.Node_KytheKind{scpb.NodeKind_FILE}, 495 Fact: []*scpb.Fact{{ 496 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 497 Value: []byte("some def\n"), 498 }}, 499 }} 500 501 expected := []*srvpb.FileDecorations{{ 502 File: &srvpb.File{Text: []byte("some def\n")}, 503 Decoration: []*srvpb.FileDecorations_Decoration{{ 504 Anchor: &srvpb.RawAnchor{StartOffset: 4, EndOffset: 7}, 505 Kind: "/kythe/edge/defines/binding", 506 Target: "kythe:#node2", 507 TargetDefinition: "kythe:?path=path2#def2", 508 }, { 509 Anchor: &srvpb.RawAnchor{StartOffset: 5, EndOffset: 8}, 510 Kind: "/kythe/edge/defines/binding", 511 Target: "kythe:#node1", 512 TargetDefinition: "kythe:?path=path2#def1", 513 }}, 514 Target: []*srvpb.Node{{ 515 Ticket: "kythe:#node2", 516 Fact: []*cpb.Fact{{Name: "/kythe/node/kind", Value: []byte("function")}}, 517 }}, 518 TargetDefinitions: []*srvpb.ExpandedAnchor{{ 519 Ticket: "kythe:?path=path2#def1", 520 Text: "def", 521 Span: &cpb.Span{ 522 Start: &cpb.Point{ByteOffset: 5, LineNumber: 1, ColumnOffset: 5}, 523 End: &cpb.Point{ByteOffset: 8, LineNumber: 1, ColumnOffset: 8}, 524 }, 525 Snippet: "some def", 526 SnippetSpan: &cpb.Span{ 527 Start: &cpb.Point{LineNumber: 1}, 528 End: &cpb.Point{ByteOffset: 8, LineNumber: 1, ColumnOffset: 8}, 529 }, 530 }, { 531 Ticket: "kythe:?path=path2#def2", 532 Text: " de", 533 Span: &cpb.Span{ 534 Start: &cpb.Point{ByteOffset: 4, LineNumber: 1, ColumnOffset: 4}, 535 End: &cpb.Point{ByteOffset: 7, LineNumber: 1, ColumnOffset: 7}, 536 }, 537 Snippet: "some def", 538 SnippetSpan: &cpb.Span{ 539 Start: &cpb.Point{LineNumber: 1}, 540 End: &cpb.Point{ByteOffset: 8, LineNumber: 1, ColumnOffset: 8}, 541 }, 542 }}, 543 TargetOverride: []*srvpb.FileDecorations_Override{{ 544 Overriding: "kythe:#node2", 545 Overridden: "kythe:#node1", 546 OverriddenDefinition: "kythe:?path=path2#def1", 547 }}, 548 }, { 549 File: &srvpb.File{Text: []byte("some text\n")}, 550 }} 551 552 beam.Init() 553 p, s, nodes := ptest.CreateList(testNodes) 554 decor := FromNodes(s, nodes).Decorations() 555 debug.Print(s, decor) 556 passert.Equals(s, beam.DropKey(s, decor), beam.CreateList(s, expected)) 557 558 ptest.RunAndValidate(t, p) 559 } 560 561 func TestCrossReferences(t *testing.T) { 562 testNodes := []*scpb.Node{{ 563 Source: &spb.VName{Signature: "node1"}, 564 }} 565 testRefs := []*ppb.Reference{{ 566 Source: &spb.VName{Signature: "node1"}, 567 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_REF}, 568 Anchor: &srvpb.ExpandedAnchor{ 569 Ticket: "kythe:?path=path#anchor1", 570 Text: "some", 571 Span: &cpb.Span{ 572 Start: &cpb.Point{ 573 LineNumber: 1, 574 }, 575 End: &cpb.Point{ 576 ByteOffset: 4, 577 LineNumber: 1, 578 ColumnOffset: 4, 579 }, 580 }, 581 Snippet: "some text", 582 SnippetSpan: &cpb.Span{ 583 Start: &cpb.Point{ 584 LineNumber: 1, 585 }, 586 End: &cpb.Point{ 587 ByteOffset: 9, 588 LineNumber: 1, 589 ColumnOffset: 9, 590 }, 591 }, 592 }, 593 }, { 594 Source: &spb.VName{Signature: "node1"}, 595 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_REF}, 596 Anchor: &srvpb.ExpandedAnchor{ 597 Ticket: "kythe:?path=path2#anchor3", 598 Span: &cpb.Span{ 599 Start: &cpb.Point{ByteOffset: 42}, 600 End: &cpb.Point{ByteOffset: 45}, 601 }, 602 }, 603 }, { 604 Source: &spb.VName{Signature: "node2"}, 605 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_REF_CALL}, 606 Scope: &spb.VName{Path: "path", Signature: "anchor2_parent"}, 607 Anchor: &srvpb.ExpandedAnchor{ 608 Ticket: "kythe:?path=path#anchor2", 609 Text: "text", 610 Span: &cpb.Span{ 611 Start: &cpb.Point{ 612 ByteOffset: 5, 613 LineNumber: 1, 614 ColumnOffset: 5, 615 }, 616 End: &cpb.Point{ 617 ByteOffset: 9, 618 LineNumber: 1, 619 ColumnOffset: 9, 620 }, 621 }, 622 Snippet: "some text", 623 SnippetSpan: &cpb.Span{ 624 Start: &cpb.Point{ 625 LineNumber: 1, 626 }, 627 End: &cpb.Point{ 628 ByteOffset: 9, 629 LineNumber: 1, 630 ColumnOffset: 9, 631 }, 632 }, 633 }, 634 }, { 635 Source: &spb.VName{Path: "path", Signature: "anchor2_parent"}, 636 Kind: &ppb.Reference_KytheKind{scpb.EdgeKind_DEFINES_BINDING}, 637 Anchor: &srvpb.ExpandedAnchor{ 638 Ticket: "kythe:?path=path#anchor3", 639 Text: "text", 640 Span: &cpb.Span{ 641 Start: &cpb.Point{ 642 ByteOffset: 5, 643 LineNumber: 1, 644 ColumnOffset: 5, 645 }, 646 End: &cpb.Point{ 647 ByteOffset: 9, 648 LineNumber: 1, 649 ColumnOffset: 9, 650 }, 651 }, 652 Snippet: "some text", 653 SnippetSpan: &cpb.Span{ 654 Start: &cpb.Point{ 655 LineNumber: 1, 656 }, 657 End: &cpb.Point{ 658 ByteOffset: 9, 659 LineNumber: 1, 660 ColumnOffset: 9, 661 }, 662 }, 663 }, 664 }} 665 expectedSets := []*srvpb.PagedCrossReferences{{ 666 SourceTicket: "kythe:#node1", 667 Group: []*srvpb.PagedCrossReferences_Group{{ 668 Kind: "/kythe/edge/ref", 669 Anchor: []*srvpb.ExpandedAnchor{{ 670 Ticket: "kythe:?path=path#anchor1", 671 Text: "some", 672 Span: &cpb.Span{ 673 Start: &cpb.Point{ 674 LineNumber: 1, 675 }, 676 End: &cpb.Point{ 677 ByteOffset: 4, 678 LineNumber: 1, 679 ColumnOffset: 4, 680 }, 681 }, 682 Snippet: "some text", 683 SnippetSpan: &cpb.Span{ 684 Start: &cpb.Point{ 685 LineNumber: 1, 686 }, 687 End: &cpb.Point{ 688 ByteOffset: 9, 689 LineNumber: 1, 690 ColumnOffset: 9, 691 }, 692 }, 693 }, { 694 Ticket: "kythe:?path=path2#anchor3", 695 Span: &cpb.Span{ 696 Start: &cpb.Point{ByteOffset: 42}, 697 End: &cpb.Point{ByteOffset: 45}, 698 }, 699 }}, 700 }}, 701 }, { 702 SourceTicket: "kythe:#node2", 703 Group: []*srvpb.PagedCrossReferences_Group{{ 704 Kind: "#internal/ref/call/direct", 705 Caller: []*srvpb.PagedCrossReferences_Caller{{ 706 SemanticCaller: "kythe:?path=path#anchor2_parent", 707 Caller: &srvpb.ExpandedAnchor{ 708 Ticket: "kythe:?path=path#anchor3", 709 Text: "text", 710 Span: &cpb.Span{ 711 Start: &cpb.Point{ 712 ByteOffset: 5, 713 LineNumber: 1, 714 ColumnOffset: 5, 715 }, 716 End: &cpb.Point{ 717 ByteOffset: 9, 718 LineNumber: 1, 719 ColumnOffset: 9, 720 }, 721 }, 722 Snippet: "some text", 723 SnippetSpan: &cpb.Span{ 724 Start: &cpb.Point{ 725 LineNumber: 1, 726 }, 727 End: &cpb.Point{ 728 ByteOffset: 9, 729 LineNumber: 1, 730 ColumnOffset: 9, 731 }, 732 }, 733 }, 734 Callsite: []*srvpb.ExpandedAnchor{{ 735 Ticket: "kythe:?path=path#anchor2", 736 Text: "text", 737 Span: &cpb.Span{ 738 Start: &cpb.Point{ 739 ByteOffset: 5, 740 LineNumber: 1, 741 ColumnOffset: 5, 742 }, 743 End: &cpb.Point{ 744 ByteOffset: 9, 745 LineNumber: 1, 746 ColumnOffset: 9, 747 }, 748 }, 749 Snippet: "some text", 750 SnippetSpan: &cpb.Span{ 751 Start: &cpb.Point{ 752 LineNumber: 1, 753 }, 754 End: &cpb.Point{ 755 ByteOffset: 9, 756 LineNumber: 1, 757 ColumnOffset: 9, 758 }, 759 }, 760 }}, 761 }}, 762 }, { 763 Kind: "/kythe/edge/ref/call", 764 Anchor: []*srvpb.ExpandedAnchor{{ 765 Ticket: "kythe:?path=path#anchor2", 766 Text: "text", 767 Span: &cpb.Span{ 768 Start: &cpb.Point{ 769 ByteOffset: 5, 770 LineNumber: 1, 771 ColumnOffset: 5, 772 }, 773 End: &cpb.Point{ 774 ByteOffset: 9, 775 LineNumber: 1, 776 ColumnOffset: 9, 777 }, 778 }, 779 Snippet: "some text", 780 SnippetSpan: &cpb.Span{ 781 Start: &cpb.Point{ 782 LineNumber: 1, 783 }, 784 End: &cpb.Point{ 785 ByteOffset: 9, 786 LineNumber: 1, 787 ColumnOffset: 9, 788 }, 789 }, 790 }}, 791 }}, 792 }, { 793 SourceTicket: "kythe:?path=path#anchor2_parent", 794 Group: []*srvpb.PagedCrossReferences_Group{{ 795 Kind: "/kythe/edge/defines/binding", 796 Anchor: []*srvpb.ExpandedAnchor{{ 797 Ticket: "kythe:?path=path#anchor3", 798 Text: "text", 799 Span: &cpb.Span{ 800 Start: &cpb.Point{ 801 ByteOffset: 5, 802 LineNumber: 1, 803 ColumnOffset: 5, 804 }, 805 End: &cpb.Point{ 806 ByteOffset: 9, 807 LineNumber: 1, 808 ColumnOffset: 9, 809 }, 810 }, 811 Snippet: "some text", 812 SnippetSpan: &cpb.Span{ 813 Start: &cpb.Point{ 814 LineNumber: 1, 815 }, 816 End: &cpb.Point{ 817 ByteOffset: 9, 818 LineNumber: 1, 819 ColumnOffset: 9, 820 }, 821 }, 822 }}, 823 }}, 824 }} 825 826 beam.Init() 827 p, s, refs, nodes := ptest.CreateList2(testRefs, testNodes) 828 k := &KytheBeam{s: s, refs: refs, nodes: nodes} 829 sets, _ := k.CrossReferences() 830 debug.Print(s, sets) 831 passert.Equals(s, beam.DropKey(s, sets), beam.CreateList(s, expectedSets)) 832 833 ptest.RunAndValidate(t, p) 834 } 835 836 func TestEdges_grouping(t *testing.T) { 837 testNodes := []*scpb.Node{{ 838 Source: &spb.VName{Signature: "node1"}, 839 Edge: []*scpb.Edge{{ 840 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_PARAM}, 841 Target: &spb.VName{Signature: "node1"}, 842 }, { 843 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_PARAM}, 844 Ordinal: 1, 845 Target: &spb.VName{Signature: "node1"}, 846 }}, 847 }} 848 expectedSets := []*srvpb.PagedEdgeSet{{ 849 Source: &srvpb.Node{Ticket: "kythe:#node1"}, 850 Group: []*srvpb.EdgeGroup{{ 851 Kind: "%/kythe/edge/param", 852 Edge: []*srvpb.EdgeGroup_Edge{{ 853 Target: &srvpb.Node{Ticket: "kythe:#node1"}, 854 }, { 855 Target: &srvpb.Node{Ticket: "kythe:#node1"}, 856 Ordinal: 1, 857 }}, 858 }, { 859 Kind: "/kythe/edge/param", 860 Edge: []*srvpb.EdgeGroup_Edge{{ 861 Target: &srvpb.Node{Ticket: "kythe:#node1"}, 862 }, { 863 Target: &srvpb.Node{Ticket: "kythe:#node1"}, 864 Ordinal: 1, 865 }}, 866 }}, 867 }} 868 869 beam.Init() 870 p, s, nodes := ptest.CreateList(testNodes) 871 k := FromNodes(s, nodes) 872 sets, _ := k.Edges() 873 debug.Print(s, sets) 874 passert.Equals(s, beam.DropKey(s, sets), beam.CreateList(s, expectedSets)) 875 876 ptest.RunAndValidate(t, p) 877 } 878 879 func TestEdges_reverses(t *testing.T) { 880 testNodes := []*scpb.Node{{ 881 Source: &spb.VName{Signature: "node1"}, 882 Kind: &scpb.Node_KytheKind{scpb.NodeKind_RECORD}, 883 Edge: []*scpb.Edge{{ 884 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_CHILD_OF}, 885 Target: &spb.VName{Signature: "node2"}, 886 }}, 887 }} 888 expectedSets := []*srvpb.PagedEdgeSet{{ 889 Source: &srvpb.Node{ 890 Ticket: "kythe:#node1", 891 Fact: []*cpb.Fact{{ 892 Name: "/kythe/node/kind", 893 Value: []byte("record"), 894 }}, 895 }, 896 Group: []*srvpb.EdgeGroup{{ 897 Kind: "/kythe/edge/childof", 898 Edge: []*srvpb.EdgeGroup_Edge{{ 899 Target: &srvpb.Node{Ticket: "kythe:#node2"}, 900 }}, 901 }}, 902 }, { 903 Source: &srvpb.Node{Ticket: "kythe:#node2"}, 904 Group: []*srvpb.EdgeGroup{{ 905 Kind: "%/kythe/edge/childof", 906 Edge: []*srvpb.EdgeGroup_Edge{{ 907 Target: &srvpb.Node{ 908 Ticket: "kythe:#node1", 909 Fact: []*cpb.Fact{{ 910 Name: "/kythe/node/kind", 911 Value: []byte("record"), 912 }}, 913 }, 914 }}, 915 }}, 916 }} 917 918 beam.Init() 919 p, s, nodes := ptest.CreateList(testNodes) 920 k := FromNodes(s, nodes) 921 sets, _ := k.Edges() 922 debug.Print(s, sets) 923 passert.Equals(s, beam.DropKey(s, sets), beam.CreateList(s, expectedSets)) 924 925 ptest.RunAndValidate(t, p) 926 } 927 928 func TestDocuments_text(t *testing.T) { 929 testNodes := []*scpb.Node{{ 930 Source: &spb.VName{Signature: "doc1"}, 931 Kind: &scpb.Node_KytheKind{scpb.NodeKind_DOC}, 932 Fact: []*scpb.Fact{{ 933 Name: &scpb.Fact_KytheName{scpb.FactName_TEXT}, 934 Value: []byte("raw document text"), 935 }}, 936 Edge: []*scpb.Edge{{ 937 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DOCUMENTS}, 938 Target: &spb.VName{Signature: "node1"}, 939 }}, 940 }} 941 expectedDocs := []*srvpb.Document{{ 942 Ticket: "kythe:#node1", 943 RawText: "raw document text", 944 }} 945 946 beam.Init() 947 p, s, nodes := ptest.CreateList(testNodes) 948 docs := FromNodes(s, nodes).Documents() 949 debug.Print(s, docs) 950 passert.Equals(s, beam.DropKey(s, docs), beam.CreateList(s, expectedDocs)) 951 952 ptest.RunAndValidate(t, p) 953 } 954 955 func TestDocuments_children(t *testing.T) { 956 testNodes := []*scpb.Node{{ 957 Source: &spb.VName{Signature: "child1"}, 958 Edge: []*scpb.Edge{{ 959 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_CHILD_OF}, 960 Target: &spb.VName{Signature: "node1"}, 961 }}, 962 }, { 963 Source: &spb.VName{Signature: "doc1"}, 964 Kind: &scpb.Node_KytheKind{scpb.NodeKind_DOC}, 965 Edge: []*scpb.Edge{{ 966 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DOCUMENTS}, 967 Target: &spb.VName{Signature: "node1"}, 968 }}, 969 }} 970 expectedDocs := []*srvpb.Document{{ 971 Ticket: "kythe:#node1", 972 ChildTicket: []string{"kythe:#child1"}, 973 }} 974 975 beam.Init() 976 p, s, nodes := ptest.CreateList(testNodes) 977 docs := FromNodes(s, nodes).Documents() 978 debug.Print(s, docs) 979 passert.Equals(s, beam.DropKey(s, docs), beam.CreateList(s, expectedDocs)) 980 981 ptest.RunAndValidate(t, p) 982 } 983 984 func TestDocuments_markedSource(t *testing.T) { 985 ms := &cpb.MarkedSource{ 986 Kind: cpb.MarkedSource_IDENTIFIER, 987 PreText: "Some_MarkedSource_identifier", 988 } 989 rec, err := proto.Marshal(ms) 990 if err != nil { 991 t.Fatal(err) 992 } 993 994 testNodes := []*scpb.Node{{ 995 Source: &spb.VName{Signature: "node1"}, 996 Fact: []*scpb.Fact{{ 997 Name: &scpb.Fact_KytheName{scpb.FactName_CODE}, 998 Value: rec, 999 }}, 1000 }, { 1001 Source: &spb.VName{Signature: "doc1"}, 1002 Kind: &scpb.Node_KytheKind{scpb.NodeKind_DOC}, 1003 Edge: []*scpb.Edge{{ 1004 Kind: &scpb.Edge_KytheKind{scpb.EdgeKind_DOCUMENTS}, 1005 Target: &spb.VName{Signature: "node1"}, 1006 }}, 1007 }} 1008 expectedDocs := []*srvpb.Document{{ 1009 Ticket: "kythe:#node1", 1010 MarkedSource: ms, 1011 }} 1012 1013 beam.Init() 1014 p, s, nodes := ptest.CreateList(testNodes) 1015 docs := FromNodes(s, nodes).Documents() 1016 debug.Print(s, docs) 1017 passert.Equals(s, beam.DropKey(s, docs), beam.CreateList(s, expectedDocs)) 1018 1019 ptest.RunAndValidate(t, p) 1020 } 1021 1022 func TestFileTree_registrations(t *testing.T) { 1023 testNodes := []*scpb.Node{{}} 1024 p, s, nodes := ptest.CreateList(testNodes) 1025 k := FromNodes(s, nodes) 1026 k.CorpusRoots() 1027 k.Directories() 1028 beamtest.CheckRegistrations(t, p) 1029 } 1030 1031 func TestDecorations_registrations(t *testing.T) { 1032 testNodes := []*scpb.Node{{}} 1033 p, s, nodes := ptest.CreateList(testNodes) 1034 k := FromNodes(s, nodes) 1035 k.Decorations() 1036 k.SplitDecorations() 1037 beamtest.CheckRegistrations(t, p) 1038 } 1039 1040 func TestCrossReferences_registrations(t *testing.T) { 1041 testNodes := []*scpb.Node{{}} 1042 p, s, nodes := ptest.CreateList(testNodes) 1043 k := FromNodes(s, nodes) 1044 k.CrossReferences() 1045 k.SplitCrossReferences() 1046 beamtest.CheckRegistrations(t, p) 1047 } 1048 1049 func TestEdges_registrations(t *testing.T) { 1050 testNodes := []*scpb.Node{{}} 1051 p, s, nodes := ptest.CreateList(testNodes) 1052 k := FromNodes(s, nodes) 1053 k.Edges() 1054 k.SplitEdges() 1055 beamtest.CheckRegistrations(t, p) 1056 } 1057 1058 func TestDocuments_registrations(t *testing.T) { 1059 testNodes := []*scpb.Node{{}} 1060 p, s, nodes := ptest.CreateList(testNodes) 1061 FromNodes(s, nodes).Documents() 1062 beamtest.CheckRegistrations(t, p) 1063 } 1064 1065 func TestCombineDecorPieces_mergeAccumulators(t *testing.T) { 1066 accum := &srvpb.FileDecorations{ 1067 Decoration: []*srvpb.FileDecorations_Decoration{{ 1068 Target: "target1", 1069 }}, 1070 Target: []*srvpb.Node{{ 1071 Ticket: "ticket1", 1072 }}, 1073 TargetDefinitions: []*srvpb.ExpandedAnchor{{ 1074 Ticket: "ticket1", 1075 }}, 1076 Diagnostic: []*cpb.Diagnostic{{ 1077 Message: "msg1", 1078 }}, 1079 } 1080 1081 other := &srvpb.FileDecorations{ 1082 Decoration: []*srvpb.FileDecorations_Decoration{{ 1083 Target: "target2", 1084 }}, 1085 Target: []*srvpb.Node{{ 1086 Ticket: "ticket2", 1087 }}, 1088 TargetDefinitions: []*srvpb.ExpandedAnchor{{ 1089 Ticket: "ticket2", 1090 }}, 1091 Diagnostic: []*cpb.Diagnostic{{ 1092 Message: "msg2", 1093 }}, 1094 File: &srvpb.File{ 1095 Text: []byte("some text\n"), 1096 }, 1097 } 1098 1099 expected := &srvpb.FileDecorations{ 1100 Decoration: []*srvpb.FileDecorations_Decoration{{ 1101 Target: "target1", 1102 }, { 1103 Target: "target2", 1104 }}, 1105 Target: []*srvpb.Node{{ 1106 Ticket: "ticket1", 1107 }, { 1108 Ticket: "ticket2", 1109 }}, 1110 TargetDefinitions: []*srvpb.ExpandedAnchor{{ 1111 Ticket: "ticket1", 1112 }, { 1113 Ticket: "ticket2", 1114 }}, 1115 Diagnostic: []*cpb.Diagnostic{{ 1116 Message: "msg1", 1117 }, { 1118 Message: "msg2", 1119 }}, 1120 File: &srvpb.File{ 1121 Text: []byte("some text\n"), 1122 }, 1123 } 1124 1125 actual := (&combineDecorPieces{}).MergeAccumulators(accum, other) 1126 if d := cmp.Diff(actual, expected, cmpopts.IgnoreUnexported(cpb.Diagnostic{}, srvpb.ExpandedAnchor{}, srvpb.Node{}, srvpb.FileDecorations_Decoration{}, srvpb.File{}, srvpb.FileDecorations{})); d != "" { 1127 t.Errorf("Expected %v but was %v. Diff: %v", expected, actual, d) 1128 } 1129 }