Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/torque/server-data.h"
6 : #include "src/torque/torque-compiler.h"
7 : #include "test/unittests/test-utils.h"
8 :
9 : namespace v8 {
10 : namespace internal {
11 : namespace torque {
12 :
13 : namespace {
14 :
15 6 : struct TestCompiler {
16 : SourceFileMap::Scope file_map_scope;
17 : LanguageServerData::Scope server_data_scope;
18 :
19 2 : void Compile(const std::string& source) {
20 : TorqueCompilerOptions options;
21 2 : options.abort_on_lint_errors = false;
22 : options.output_directory = "";
23 2 : options.verbose = false;
24 2 : options.collect_language_server_data = true;
25 :
26 4 : CompileTorque(source, options);
27 2 : }
28 : };
29 :
30 : } // namespace
31 :
32 15373 : TEST(LanguageServer, GotoTypeDefinition) {
33 : const std::string source =
34 : "type void;\n"
35 : "type never;\n"
36 : "type T1 generates 'TNode<Object>';\n"
37 : "type T2 generates 'TNode<Object>';\n"
38 1 : "macro SomeMacro(a: T1, b: T2): T1 { return a; }";
39 :
40 1 : TestCompiler compiler;
41 1 : compiler.Compile(source);
42 :
43 : // Find the definition for type 'T1' of argument 'a' on line 4.
44 2 : const SourceId id = SourceFileMap::GetSourceId("<torque>");
45 1 : auto maybe_position = LanguageServerData::FindDefinition(id, {4, 19});
46 1 : ASSERT_TRUE(maybe_position.has_value());
47 2 : EXPECT_EQ(*maybe_position, (SourcePosition{id, {2, 5}, {2, 7}}));
48 :
49 : // Find the defintion for type 'T2' of argument 'b' on line 4.
50 2 : maybe_position = LanguageServerData::FindDefinition(id, {4, 26});
51 1 : ASSERT_TRUE(maybe_position.has_value());
52 2 : EXPECT_EQ(*maybe_position, (SourcePosition{id, {3, 5}, {3, 7}}));
53 : }
54 :
55 15373 : TEST(LanguageServer, GotoTypeDefinitionExtends) {
56 : const std::string source =
57 : "type void;\n"
58 : "type never;\n"
59 : "type T1 generates 'TNode<T1>';\n"
60 1 : "type T2 extends T1 generates 'TNode<T2>';";
61 :
62 1 : TestCompiler compiler;
63 1 : compiler.Compile(source);
64 :
65 : // Find the definition for 'T1' of the extends clause on line 3.
66 2 : const SourceId id = SourceFileMap::GetSourceId("<torque>");
67 1 : auto maybe_position = LanguageServerData::FindDefinition(id, {3, 16});
68 1 : ASSERT_TRUE(maybe_position.has_value());
69 2 : EXPECT_EQ(*maybe_position, (SourcePosition{id, {2, 5}, {2, 7}}));
70 : }
71 :
72 : } // namespace torque
73 : } // namespace internal
74 9222 : } // namespace v8
|