|
| 1 | +package java |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/studyzy/codei18n/core/domain" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAdapter_Parse_Class(t *testing.T) { |
| 11 | + src := `package com.example; |
| 12 | +
|
| 13 | +// 计算器类 |
| 14 | +public class Calculator { |
| 15 | +} |
| 16 | +` |
| 17 | + adapter := NewAdapter() |
| 18 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 19 | + assert.NoError(t, err) |
| 20 | + assert.Len(t, comments, 1) |
| 21 | + |
| 22 | + assert.Equal(t, "// 计算器类", comments[0].SourceText) |
| 23 | + assert.Equal(t, "com.example.Calculator", comments[0].Symbol) |
| 24 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 25 | +} |
| 26 | + |
| 27 | +func TestAdapter_Parse_Method(t *testing.T) { |
| 28 | + src := `package com.example; |
| 29 | +
|
| 30 | +public class Calculator { |
| 31 | + // 计算两个数的和 |
| 32 | + public int add(int a, int b) { |
| 33 | + return a + b; |
| 34 | + } |
| 35 | +} |
| 36 | +` |
| 37 | + adapter := NewAdapter() |
| 38 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 39 | + assert.NoError(t, err) |
| 40 | + assert.Len(t, comments, 1) |
| 41 | + |
| 42 | + assert.Equal(t, "// 计算两个数的和", comments[0].SourceText) |
| 43 | + assert.Equal(t, "com.example.Calculator#add", comments[0].Symbol) |
| 44 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 45 | +} |
| 46 | + |
| 47 | +func TestAdapter_Parse_Field(t *testing.T) { |
| 48 | + src := `package com.example; |
| 49 | +
|
| 50 | +public class Config { |
| 51 | + // 默认超时时间(毫秒) |
| 52 | + private int timeoutMs = 1000; |
| 53 | +} |
| 54 | +` |
| 55 | + adapter := NewAdapter() |
| 56 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 57 | + assert.NoError(t, err) |
| 58 | + assert.Len(t, comments, 1) |
| 59 | + |
| 60 | + assert.Equal(t, "// 默认超时时间(毫秒)", comments[0].SourceText) |
| 61 | + assert.Equal(t, "com.example.Config#timeoutMs", comments[0].Symbol) |
| 62 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAdapter_Parse_Interface(t *testing.T) { |
| 66 | + src := `package com.example; |
| 67 | +
|
| 68 | +// 用户接口 |
| 69 | +public interface User { |
| 70 | + String getName(); |
| 71 | +} |
| 72 | +` |
| 73 | + adapter := NewAdapter() |
| 74 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.Len(t, comments, 1) |
| 77 | + |
| 78 | + assert.Equal(t, "// 用户接口", comments[0].SourceText) |
| 79 | + assert.Equal(t, "com.example.User", comments[0].Symbol) |
| 80 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 81 | +} |
| 82 | + |
| 83 | +func TestAdapter_Parse_Enum(t *testing.T) { |
| 84 | + src := `package com.example; |
| 85 | +
|
| 86 | +// 账户类型 |
| 87 | +public enum AccountType { |
| 88 | + STANDARD, |
| 89 | + PREMIUM |
| 90 | +} |
| 91 | +` |
| 92 | + adapter := NewAdapter() |
| 93 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 94 | + assert.NoError(t, err) |
| 95 | + assert.Len(t, comments, 1) |
| 96 | + |
| 97 | + assert.Equal(t, "// 账户类型", comments[0].SourceText) |
| 98 | + assert.Equal(t, "com.example.AccountType", comments[0].Symbol) |
| 99 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 100 | +} |
| 101 | + |
| 102 | +func TestAdapter_Parse_BlockComment(t *testing.T) { |
| 103 | + src := `package com.example; |
| 104 | +
|
| 105 | +/* 这是一个块注释 */ |
| 106 | +public class Test { |
| 107 | +} |
| 108 | +` |
| 109 | + adapter := NewAdapter() |
| 110 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 111 | + assert.NoError(t, err) |
| 112 | + assert.Len(t, comments, 1) |
| 113 | + |
| 114 | + assert.Equal(t, "/* 这是一个块注释 */", comments[0].SourceText) |
| 115 | + assert.Equal(t, "com.example.Test", comments[0].Symbol) |
| 116 | + assert.Equal(t, domain.CommentTypeBlock, comments[0].Type) |
| 117 | +} |
| 118 | + |
| 119 | +func TestAdapter_Parse_Javadoc(t *testing.T) { |
| 120 | + src := `package com.example; |
| 121 | +
|
| 122 | +/** |
| 123 | + * 计算器类 |
| 124 | + * 提供基本的数学运算 |
| 125 | + */ |
| 126 | +public class Calculator { |
| 127 | + /** |
| 128 | + * 计算两个数的和 |
| 129 | + * @param a 第一个数 |
| 130 | + * @param b 第二个数 |
| 131 | + * @return 两数之和 |
| 132 | + */ |
| 133 | + public int add(int a, int b) { |
| 134 | + return a + b; |
| 135 | + } |
| 136 | +} |
| 137 | +` |
| 138 | + adapter := NewAdapter() |
| 139 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.Len(t, comments, 2) |
| 142 | + |
| 143 | + // 类的 Javadoc |
| 144 | + assert.Contains(t, comments[0].SourceText, "/**") |
| 145 | + assert.Equal(t, "com.example.Calculator", comments[0].Symbol) |
| 146 | + assert.Equal(t, domain.CommentTypeDoc, comments[0].Type) |
| 147 | + |
| 148 | + // 方法的 Javadoc |
| 149 | + assert.Contains(t, comments[1].SourceText, "/**") |
| 150 | + assert.Equal(t, "com.example.Calculator#add", comments[1].Symbol) |
| 151 | + assert.Equal(t, domain.CommentTypeDoc, comments[1].Type) |
| 152 | +} |
| 153 | + |
| 154 | +func TestAdapter_Parse_NoPackage(t *testing.T) { |
| 155 | + src := ` |
| 156 | +// 简单类 |
| 157 | +public class Simple { |
| 158 | +} |
| 159 | +` |
| 160 | + adapter := NewAdapter() |
| 161 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 162 | + assert.NoError(t, err) |
| 163 | + assert.Len(t, comments, 1) |
| 164 | + |
| 165 | + assert.Equal(t, "// 简单类", comments[0].SourceText) |
| 166 | + assert.Equal(t, "Simple", comments[0].Symbol) |
| 167 | + assert.Equal(t, domain.CommentTypeLine, comments[0].Type) |
| 168 | +} |
| 169 | + |
| 170 | +func TestAdapter_Parse_FileLevel(t *testing.T) { |
| 171 | + src := `// Copyright (c) 2025 Example Corp |
| 172 | +// 本文件包含示例代码 |
| 173 | +package com.example; |
| 174 | +
|
| 175 | +public class Test { |
| 176 | +} |
| 177 | +` |
| 178 | + adapter := NewAdapter() |
| 179 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 180 | + assert.NoError(t, err) |
| 181 | + // 文件顶部的注释可能不会附加到 package 声明 |
| 182 | + // 具体行为取决于 Tree-sitter 的解析 |
| 183 | + assert.Greater(t, len(comments), 0) |
| 184 | +} |
| 185 | + |
| 186 | +func TestAdapter_Parse_Range(t *testing.T) { |
| 187 | + src := `package com.example; |
| 188 | +
|
| 189 | +public class Test { |
| 190 | + // 测试方法 |
| 191 | + public void test() { |
| 192 | + } |
| 193 | +} |
| 194 | +` |
| 195 | + adapter := NewAdapter() |
| 196 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 197 | + assert.NoError(t, err) |
| 198 | + assert.Len(t, comments, 1) |
| 199 | + |
| 200 | + // 验证位置信息 |
| 201 | + comment := comments[0] |
| 202 | + assert.Greater(t, comment.Range.StartLine, 0) |
| 203 | + assert.Greater(t, comment.Range.StartCol, 0) |
| 204 | + assert.Greater(t, comment.Range.EndLine, 0) |
| 205 | + assert.Greater(t, comment.Range.EndCol, 0) |
| 206 | + assert.GreaterOrEqual(t, comment.Range.EndLine, comment.Range.StartLine) |
| 207 | + if comment.Range.EndLine == comment.Range.StartLine { |
| 208 | + assert.Greater(t, comment.Range.EndCol, comment.Range.StartCol) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func TestAdapter_Parse_SyntaxError(t *testing.T) { |
| 213 | + src := `package com.example; |
| 214 | +
|
| 215 | +public class Broken { |
| 216 | + // 未完成的方法定义 |
| 217 | + public void doSomething( { |
| 218 | +} |
| 219 | +` |
| 220 | + adapter := NewAdapter() |
| 221 | + comments, err := adapter.Parse("test.java", []byte(src)) |
| 222 | + // Tree-sitter 应该能够容错处理,至少返回注释 |
| 223 | + assert.NoError(t, err) |
| 224 | + assert.Greater(t, len(comments), 0) |
| 225 | + |
| 226 | + // 验证至少能提取到注释 |
| 227 | + found := false |
| 228 | + for _, c := range comments { |
| 229 | + if c.SourceText == "// 未完成的方法定义" { |
| 230 | + found = true |
| 231 | + break |
| 232 | + } |
| 233 | + } |
| 234 | + assert.True(t, found, "应该能提取到注释,即使代码有语法错误") |
| 235 | +} |
0 commit comments