generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use topiary_core::{ | ||
comments::{ | ||
extract_comments, AnchoredComment, | ||
Commented::{CommentedAfter, CommentedBefore}, | ||
SeparatedInput, | ||
}, | ||
tree_sitter::parse, | ||
types::InputSection, | ||
Position, | ||
}; | ||
|
||
const OCAML_WITH_COMMENTS: &str = r#"(* starting comment *) | ||
fun (* fun comment *) x (* var comment *) -> | ||
(** multi-lined | ||
* body comment | ||
*) | ||
body | ||
(* final comment *) | ||
"#; | ||
|
||
const OCAML_WITHOUT_COMMENTS: &str = r#" | ||
fun x -> | ||
body | ||
"#; | ||
|
||
// The section corresponding to `fun` in the curated code | ||
const FUN_SECTION: InputSection = InputSection { | ||
start: Position { row: 2, column: 1 }, | ||
end: Position { row: 2, column: 4 }, | ||
}; | ||
|
||
// The section corresponding to `x` in the curated code | ||
const VAR_SECTION: InputSection = InputSection { | ||
start: Position { row: 2, column: 6 }, | ||
end: Position { row: 2, column: 7 }, | ||
}; | ||
|
||
// The section corresponding to `body` in the curated code | ||
const BODY_SECTION: InputSection = InputSection { | ||
start: Position { row: 4, column: 3 }, | ||
end: Position { row: 4, column: 7 }, | ||
}; | ||
|
||
#[test] | ||
fn test_extract_comments() { | ||
let input = OCAML_WITH_COMMENTS; | ||
let ocaml = tree_sitter_ocaml::LANGUAGE_OCAML; | ||
|
||
let (tree, _grammar) = parse(input, &ocaml.into(), false).unwrap(); | ||
|
||
let SeparatedInput { | ||
input_tree: _, | ||
input_string: new_input_string, | ||
mut comments, | ||
} = extract_comments(&tree, input, &ocaml.into()).unwrap(); | ||
|
||
let mut expected_comments: Vec<AnchoredComment> = vec![ | ||
AnchoredComment { | ||
comment_text: "(* starting comment *)".into(), | ||
commented: CommentedAfter(FUN_SECTION), | ||
}, | ||
AnchoredComment { | ||
comment_text: "(* fun comment *)".into(), | ||
commented: CommentedBefore(FUN_SECTION), | ||
}, | ||
AnchoredComment { | ||
comment_text: "(* var comment *)".into(), | ||
commented: CommentedBefore(VAR_SECTION), | ||
}, | ||
AnchoredComment { | ||
comment_text: "(** multi-lined\n * body comment\n *)".into(), | ||
commented: CommentedAfter(BODY_SECTION), | ||
}, | ||
AnchoredComment { | ||
comment_text: "(* final comment *)".into(), | ||
commented: CommentedBefore(BODY_SECTION), | ||
}, | ||
]; | ||
|
||
// sort the comments so that we're order-independent | ||
comments.sort_by_key(|comment| comment.comment_text.clone()); | ||
expected_comments.sort_by_key(|comment| comment.comment_text.clone()); | ||
|
||
assert_eq!(new_input_string, OCAML_WITHOUT_COMMENTS); | ||
|
||
assert_eq!(comments.len(), 5); | ||
for (comment, expected_comment) in comments.iter().zip(expected_comments.iter()) { | ||
assert_eq!(comment, expected_comment) | ||
} | ||
} |