|
| 1 | +package scip |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFlattenDocuments(t *testing.T) { |
| 10 | + s1 := []*SymbolInformation{{Symbol: "foo"}, {Symbol: "bar"}} |
| 11 | + o1 := []*Occurrence{{Range: []int32{1, 2, 3}, Symbol: "foo"}, {Range: []int32{2, 3, 4}, Symbol: "bar"}} |
| 12 | + s2 := []*SymbolInformation{{Symbol: "baz"}, {Symbol: "bonk"}} |
| 13 | + o2 := []*Occurrence{{Range: []int32{3, 4, 5}, Symbol: "baz"}, {Range: []int32{4, 5, 6}, Symbol: "bonk"}} |
| 14 | + s3 := []*SymbolInformation{{Symbol: "quux"}} |
| 15 | + o3 := []*Occurrence{{Range: []int32{5, 6, 7}, Symbol: "quux"}} |
| 16 | + |
| 17 | + documents := []*Document{ |
| 18 | + {RelativePath: "foo.go", Symbols: s1, Occurrences: o1}, |
| 19 | + {RelativePath: "bar.go", Symbols: s2, Occurrences: o2}, |
| 20 | + {RelativePath: "foo.go", Symbols: s3, Occurrences: o3}, |
| 21 | + } |
| 22 | + flattenedDocuments := SortDocuments(FlattenDocuments(documents)) |
| 23 | + |
| 24 | + expectedDocuments := []*Document{ |
| 25 | + {RelativePath: "bar.go", Symbols: s2, Occurrences: o2}, |
| 26 | + {RelativePath: "foo.go", Symbols: append(s1, s3...), Occurrences: append(o1, o3...)}, |
| 27 | + } |
| 28 | + if diff := cmp.Diff(expectedDocuments, flattenedDocuments, compareDocuments); diff != "" { |
| 29 | + t.Fatalf("unexpected documents (-want +got):\n%s", diff) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +var compareDocuments = cmp.Comparer(func(a, b *Document) bool { |
| 34 | + if a.Language != b.Language || a.RelativePath != b.RelativePath || a.Text != b.Text { |
| 35 | + return false |
| 36 | + } |
| 37 | + if !compareSlices(a.Symbols, b.Symbols, compareSymbol) { |
| 38 | + return false |
| 39 | + } |
| 40 | + if !compareSlices(a.Occurrences, b.Occurrences, compareOccurrence) { |
| 41 | + return false |
| 42 | + } |
| 43 | + |
| 44 | + return true |
| 45 | +}) |
| 46 | + |
| 47 | +func compareSlices[T any](as, bs []T, f func(a, b T) bool) bool { |
| 48 | + if len(as) != len(bs) { |
| 49 | + return false |
| 50 | + } |
| 51 | + |
| 52 | + for i, a := range as { |
| 53 | + if !f(a, bs[i]) { |
| 54 | + return false |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return true |
| 59 | +} |
| 60 | + |
| 61 | +func compareSymbol(a, b *SymbolInformation) bool { |
| 62 | + if a.Symbol != b.Symbol { |
| 63 | + return false |
| 64 | + } |
| 65 | + if a.Kind != b.Kind { |
| 66 | + return false |
| 67 | + } |
| 68 | + if a.DisplayName != b.DisplayName { |
| 69 | + return false |
| 70 | + } |
| 71 | + if a.SignatureDocumentation != b.SignatureDocumentation { |
| 72 | + return false |
| 73 | + } |
| 74 | + if a.EnclosingSymbol != b.EnclosingSymbol { |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + if !compareSlices(a.Documentation, b.Documentation, compareValue[string]) { |
| 79 | + return false |
| 80 | + } |
| 81 | + if !compareSlices(a.Relationships, b.Relationships, compareRelationship) { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + return true |
| 86 | +} |
| 87 | + |
| 88 | +func compareOccurrence(a, b *Occurrence) bool { |
| 89 | + if a.Symbol != b.Symbol { |
| 90 | + return false |
| 91 | + } |
| 92 | + if a.SymbolRoles != b.SymbolRoles { |
| 93 | + return false |
| 94 | + } |
| 95 | + if a.SyntaxKind != b.SyntaxKind { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + if !compareSlices(a.Range, b.Range, compareValue[int32]) { |
| 100 | + return false |
| 101 | + } |
| 102 | + if !compareSlices(a.OverrideDocumentation, b.OverrideDocumentation, compareValue[string]) { |
| 103 | + return false |
| 104 | + } |
| 105 | + if !compareSlices(a.Diagnostics, b.Diagnostics, compareDiagnostic) { |
| 106 | + return false |
| 107 | + } |
| 108 | + if !compareSlices(a.EnclosingRange, b.EnclosingRange, compareValue[int32]) { |
| 109 | + return false |
| 110 | + } |
| 111 | + |
| 112 | + return true |
| 113 | +} |
| 114 | + |
| 115 | +func compareRelationship(a, b *Relationship) bool { |
| 116 | + if a.Symbol != b.Symbol { |
| 117 | + return false |
| 118 | + } |
| 119 | + if a.IsReference != b.IsReference { |
| 120 | + return false |
| 121 | + } |
| 122 | + if a.IsImplementation != b.IsImplementation { |
| 123 | + return false |
| 124 | + } |
| 125 | + if a.IsTypeDefinition != b.IsTypeDefinition { |
| 126 | + return false |
| 127 | + } |
| 128 | + if a.IsDefinition != b.IsDefinition { |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + return true |
| 133 | +} |
| 134 | + |
| 135 | +func compareDiagnostic(a, b *Diagnostic) bool { |
| 136 | + if a.Severity != b.Severity { |
| 137 | + return false |
| 138 | + } |
| 139 | + if a.Code != b.Code { |
| 140 | + return false |
| 141 | + } |
| 142 | + if a.Message != b.Message { |
| 143 | + return false |
| 144 | + } |
| 145 | + if a.Source != b.Source { |
| 146 | + return false |
| 147 | + } |
| 148 | + if !compareSlices(a.Tags, b.Tags, compareValue[DiagnosticTag]) { |
| 149 | + return false |
| 150 | + } |
| 151 | + |
| 152 | + return true |
| 153 | +} |
| 154 | + |
| 155 | +func compareValue[T comparable](a, b T) bool { |
| 156 | + return a == b |
| 157 | +} |
0 commit comments