Skip to content

Commit

Permalink
Add tests for comment extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
nbacquey committed Dec 4, 2024
1 parent e88c62a commit ebeb5db
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
12 changes: 6 additions & 6 deletions topiary-core/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ fn find_comments<'a>(

/// The section of code to which a comment refers. We also remember whether the comment
/// is positioned before or after the section.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Commented {
/// The code section is before the comment, as in:
/// ```
/// struct Foo {
/// baz: Baz, // this is baz
/// quz: Qux, // this is qux
/// baz: usize, // this is baz
/// quz: usize, // this is qux
/// }
/// ```
CommentedBefore(InputSection),
/// The code section is after the comment, as in:
/// ```
/// struct Foo {
/// // let's have a baz
/// baz: Baz,
/// baz: usize,
/// // and a qux
/// qux: Qux,
/// qux: usize,
/// }
/// ```
CommentedAfter(InputSection),
Expand Down Expand Up @@ -282,7 +282,7 @@ fn find_anchor<'tree>(node: &'tree Node<'tree>, input: &str) -> FormatterResult<
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AnchoredComment {
pub comment_text: String,
pub commented: Commented,
Expand Down
6 changes: 3 additions & 3 deletions topiary-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub use crate::{
};

mod atom_collection;
mod comments;
pub mod comments;
mod error;
mod graphviz;
mod language;
mod pretty;
mod tree_sitter;
mod types;
pub mod tree_sitter;
pub mod types;

#[doc(hidden)]
pub mod test_utils;
Expand Down
92 changes: 92 additions & 0 deletions topiary-core/tests/comment_tests.rs
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)
}
}

0 comments on commit ebeb5db

Please sign in to comment.