use anyhow::Result;
#[test]
fn test_go_struct_comments() -> Result<()> {
use probe_code::language::parser::parse_file_for_code_blocks;
use std::collections::HashSet;
let code = r#"
package main
// First struct represents something
type First struct {
Field string `json:"field"`
}
// Second struct also represents something
type Second struct {
Data string `json:"data"`
}
"#;
let mut line_numbers = HashSet::new();
line_numbers.insert(4); line_numbers.insert(9);
let blocks = parse_file_for_code_blocks(code, "go", &line_numbers, true, None)?;
assert_eq!(
blocks.len(),
2,
"Expected exactly 2 blocks, got {}",
blocks.len()
);
assert_eq!(
blocks[0].node_type, "type_declaration",
"First block should be a type_declaration"
);
assert_eq!(
blocks[0].start_row + 1,
4,
"First block should start at line 4"
);
assert_eq!(blocks[0].end_row + 1, 7, "First block should end at line 7");
assert_eq!(
blocks[1].node_type, "type_declaration",
"Second block should be a type_declaration"
);
assert_eq!(
blocks[1].start_row + 1,
9,
"Second block should start at line 9"
);
assert_eq!(
blocks[1].end_row + 1,
12,
"Second block should end at line 12"
);
assert_ne!(blocks[0].start_row, blocks[1].start_row);
Ok(())
}
#[test]
fn test_go_nested_structs() -> Result<()> {
use probe_code::language::parser::parse_file_for_code_blocks;
use std::collections::HashSet;
let code = r#"
package main
// OuterType represents a container
type OuterType struct {
// InnerType represents nested data
InnerType struct {
Field string `json:"field"`
}
}
"#;
let mut line_numbers = HashSet::new();
line_numbers.insert(4); line_numbers.insert(6);
let blocks = parse_file_for_code_blocks(code, "go", &line_numbers, true, None)?;
assert_eq!(
blocks.len(),
1,
"Expected exactly 1 block after deduplication, got {}",
blocks.len()
);
assert_eq!(
blocks[0].node_type, "type_declaration",
"Block should be a type_declaration"
);
assert_eq!(blocks[0].start_row + 1, 4, "Block should start at line 4");
assert_eq!(blocks[0].end_row + 1, 10, "Block should end at line 10");
Ok(())
}
#[test]
fn test_go_mixed_declarations() -> Result<()> {
use probe_code::language::parser::parse_file_for_code_blocks;
use std::collections::HashSet;
let code = r#"
package main
// CommentA describes interface
type InterfaceA interface {
Method()
}
// CommentB describes struct
type StructB struct {
Field string
}
"#;
let mut line_numbers = HashSet::new();
line_numbers.insert(4); line_numbers.insert(9);
let blocks = parse_file_for_code_blocks(code, "go", &line_numbers, true, None)?;
assert_eq!(
blocks.len(),
2,
"Expected exactly 2 blocks, got {}",
blocks.len()
);
assert_eq!(
blocks[0].node_type, "type_declaration",
"First block should be a type_declaration"
);
assert_eq!(
blocks[0].start_row + 1,
4,
"First block should start at line 4"
);
assert_eq!(blocks[0].end_row + 1, 7, "First block should end at line 7");
assert_eq!(
blocks[1].node_type, "type_declaration",
"Second block should be a type_declaration"
);
assert_eq!(
blocks[1].start_row + 1,
9,
"Second block should start at line 9"
);
assert_eq!(
blocks[1].end_row + 1,
12,
"Second block should end at line 12"
);
assert_ne!(blocks[0].start_row, blocks[1].start_row);
Ok(())
}
#[test]
fn test_go_comment_code_block_extraction() -> Result<()> {
use probe_code::language::parser::parse_file_for_code_blocks;
use std::collections::HashSet;
let code = r#"
package main
// DatasourceResponse represents the response for datasource-related operations
// @Description Datasource response model
type DatasourceResponse struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes struct {
Name string `json:"name"`
}
}
"#;
println!("Code lines:");
for (i, line) in code.lines().enumerate() {
println!("{}: {}", i + 1, line);
}
let mut line_numbers = HashSet::new();
line_numbers.insert(5);
std::env::set_var("DEBUG", "1");
let blocks = parse_file_for_code_blocks(code, "go", &line_numbers, true, None)?;
println!("Found {} blocks:", blocks.len());
for (i, block) in blocks.iter().enumerate() {
println!(
"Block {}: type={}, lines={}-{}",
i,
block.node_type,
block.start_row + 1,
block.end_row + 1
);
}
assert_eq!(
blocks.len(),
1,
"Expected exactly 1 block, got {}",
blocks.len()
);
assert_eq!(
blocks[0].node_type, "type_declaration",
"Block should be a type_declaration"
);
assert_eq!(
blocks[0].start_row + 1,
5, "Block should start at line 5 (second comment line)"
);
assert_eq!(blocks[0].end_row + 1, 12, "Block should end at line 12");
Ok(())
}