diff --git a/src/ast.rs b/src/ast.rs index 47a691f..09dab7c 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1052,25 +1052,25 @@ mod tests { fn heights() { let ctx = ctx(); - assert_eq!(ctx.parse_json("null").height(), 1); - assert_eq!(ctx.parse_json("[1]").height(), 2); - assert_eq!(ctx.parse_json("{\"foo\": 3}").height(), 4); + assert_eq!(ctx.parse("a.json", "null").height(), 1); + assert_eq!(ctx.parse("a.json", "[1]").height(), 2); + assert_eq!(ctx.parse("a.json", "{\"foo\": 3}").height(), 4); } #[test] fn sizes() { let ctx = ctx(); - assert_eq!(ctx.parse_json("null").size(), 2); - assert_eq!(ctx.parse_json("[1]").size(), 5); - assert_eq!(ctx.parse_json("{\"foo\": 3}").size(), 11); + assert_eq!(ctx.parse("a.json", "null").size(), 2); + assert_eq!(ctx.parse("a.json", "[1]").size(), 5); + assert_eq!(ctx.parse("a.json", "{\"foo\": 3}").size(), 11); } #[test] fn children_by_field_names() { let ctx = ctx(); - let root = ctx.parse_json("{\"foo\": 3}"); + let root = ctx.parse("a.json", "{\"foo\": 3}"); let object = root[0]; let pair = object[1]; assert_eq!(root.children_by_field_name("non_existent"), None); @@ -1084,7 +1084,7 @@ mod tests { fn children_by_field_names_with_modifiers() { let ctx = ctx(); - let root = ctx.parse_java("public class MyCls {}"); + let root = ctx.parse("a.java", "public class MyCls {}"); let class_declaration = root[0]; assert_eq!( class_declaration.children_by_field_name("name"), @@ -1096,7 +1096,7 @@ mod tests { fn atomic_nodes() { let ctx = ctx(); - let root = ctx.parse_java("import java.io.InputStream;"); + let root = ctx.parse("a.java", "import java.io.InputStream;"); let import_statement = root[0]; assert_eq!(import_statement.children.len(), 0); } @@ -1104,7 +1104,7 @@ mod tests { #[test] fn trailing_newlines_are_stripped_from_nodes() { let ctx = ctx(); - let tree = ctx.parse_rust(" /// test\n fn foo() {\n ()\n }\n"); + let tree = ctx.parse("a.rs", " /// test\n fn foo() {\n ()\n }\n"); let comment = tree[0][0][0]; assert_eq!(comment.grammar_name, "line_outer_doc_comment"); // tree-sitter-rust includes a newline at the end of the source for this node, @@ -1116,16 +1116,16 @@ mod tests { fn hashing_does_not_depend_on_whitespace_but_on_content() { let ctx = ctx(); - let hash_1 = &ctx.parse_rust("fn x() -> i32 { 7 - 1 }").hash; - let hash_2 = &ctx.parse_rust("fn x() -> i32 {\n 7-1 }").hash; - let hash_3 = &ctx.parse_rust("fn x() -> i32 {\n 9-2 }").hash; + let hash_1 = &ctx.parse("a.rs", "fn x() -> i32 { 7 - 1 }").hash; + let hash_2 = &ctx.parse("a.rs", "fn x() -> i32 {\n 7-1 }").hash; + let hash_3 = &ctx.parse("a.rs", "fn x() -> i32 {\n 9-2 }").hash; assert_eq!(hash_1, hash_2); // whitespace and indentation differences are insignificant assert_ne!(hash_2, hash_3); - let hash_4 = &ctx.parse_rust("fn x() { \"some string\" }").hash; - let hash_5 = &ctx.parse_rust("fn x() { \" some string\" }").hash; - let hash_6 = &ctx.parse_rust("fn x() { \"some string\" }").hash; + let hash_4 = &ctx.parse("a.rs", "fn x() { \"some string\" }").hash; + let hash_5 = &ctx.parse("a.rs", "fn x() { \" some string\" }").hash; + let hash_6 = &ctx.parse("a.rs", "fn x() { \"some string\" }").hash; assert_ne!(hash_4, hash_5); // whitespace inside of a string is significant assert_eq!(hash_4, hash_6); } @@ -1134,8 +1134,8 @@ mod tests { fn isomorphism_is_not_just_hashing() { let ctx = ctx(); - let node_1 = ctx.parse_rust("fn x() -> i32 { 7 - 1 }"); - let node_2 = ctx.parse_rust("fn x() -> i32 { 8 - 1 }"); + let node_1 = ctx.parse("a.rs", "fn x() -> i32 { 7 - 1 }"); + let node_2 = ctx.parse("a.rs", "fn x() -> i32 { 8 - 1 }"); let fake_hash_collision = AstNode { hash: node_1.hash, parent: UnsafeCell::new(None), @@ -1154,8 +1154,8 @@ mod tests { #[test] fn isomorphism_of_empty_roots() { let ctx = ctx(); - let tree_1 = ctx.parse_rust(" "); - let tree_2 = ctx.parse_rust(" "); + let tree_1 = ctx.parse("a.rs", " "); + let tree_2 = ctx.parse("a.rs", " "); assert!(tree_1.isomorphic_to(tree_2)); assert!(tree_2.isomorphic_to(tree_1)); } @@ -1164,8 +1164,8 @@ mod tests { fn isomorphism_for_different_languages() { let ctx = ctx(); - let tree_python = ctx.parse_python("foo()"); - let tree_java = ctx.parse_java("foo();"); + let tree_python = ctx.parse("a.py", "foo()"); + let tree_java = ctx.parse("a.java", "foo();"); let arguments_python = tree_python[0][0][1]; let arguments_java = tree_java[0][0][1]; @@ -1184,7 +1184,7 @@ mod tests { #[test] fn parents_are_accessible() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3}"); + let tree = ctx.parse("a.json", "{\"foo\": 3}"); let root = tree; let first_child = root.child(0).expect("AST node is missing a child"); let second_child = first_child @@ -1199,7 +1199,7 @@ mod tests { #[test] fn dfs_traversal() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3}"); + let tree = ctx.parse("a.json", "{\"foo\": 3}"); let node_types = tree.dfs().map(|n| n.grammar_name).collect_vec(); @@ -1224,7 +1224,7 @@ mod tests { #[test] fn dfs_exact_size_iterator() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3}"); + let tree = ctx.parse("a.json", "{\"foo\": 3}"); // using the cached version let mut nodes = tree.dfs(); @@ -1246,7 +1246,7 @@ mod tests { #[test] fn postfix_traversal() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3}"); + let tree = ctx.parse("a.json", "{\"foo\": 3}"); let node_types = tree.postfix().map(|n| n.grammar_name).collect_vec(); @@ -1271,7 +1271,7 @@ mod tests { #[test] fn truncate() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3, \"bar\": 4}"); + let tree = ctx.parse("a.json", "{\"foo\": 3, \"bar\": 4}"); let arena = Arena::new(); let truncated = tree.truncate(|node| node.grammar_name == "pair", &arena); @@ -1295,7 +1295,7 @@ mod tests { #[test] fn leading_source() { let ctx = ctx(); - let tree = ctx.parse_rust("\n let x = 1;\n"); + let tree = ctx.parse("a.rs", "\n let x = 1;\n"); assert_eq!(tree.byte_range.start, 0); assert_eq!(tree.leading_source(), Some("\n ")); } @@ -1303,7 +1303,7 @@ mod tests { #[test] fn preceding_whitespace() { let ctx = ctx(); - let tree = ctx.parse_json("[1, 2,\n 3]"); + let tree = ctx.parse("a.json", "[1, 2,\n 3]"); let root = tree[0]; let [bracket, one, comma, two, _, three] = root[0..=5] else { @@ -1321,7 +1321,7 @@ mod tests { #[test] fn preceding_whitespace_go() { let ctx = ctx(); - let tree = ctx.parse_go("import (\n \"fmt\"\n \"core\"\n)\n"); + let tree = ctx.parse("a.go", "import (\n \"fmt\"\n \"core\"\n)\n"); let root = tree[0]; let import_list = root[1]; let core = import_list[2]; @@ -1333,7 +1333,7 @@ mod tests { #[test] fn trailing_whitespace_toml() { let ctx = ctx(); - let tree = ctx.parse_toml("[foo]\na = 1\n\n[bar]\nb = 2"); + let tree = ctx.parse("a.toml", "[foo]\na = 1\n\n[bar]\nb = 2"); let first_table = tree[0]; let second_table = tree[1]; assert_eq!(first_table.source, "[foo]\na = 1"); @@ -1345,7 +1345,7 @@ mod tests { #[test] fn preceding_indentation_shift() { let ctx = ctx(); - let tree = ctx.parse_java("\nclass MyCls {\n int attr;\n}"); + let tree = ctx.parse("a.java", "\nclass MyCls {\n int attr;\n}"); let class_decl = tree[0]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1356,7 +1356,10 @@ mod tests { #[test] fn preceding_indentation_shift_tabs() { let ctx = ctx(); - let tree = ctx.parse_java("class Outer {\n\tclass MyCls {\n\t\tint attr;\n\t}\n}\n"); + let tree = ctx.parse( + "a.java", + "class Outer {\n\tclass MyCls {\n\t\tint attr;\n\t}\n}\n", + ); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1367,7 +1370,10 @@ mod tests { #[test] fn preceding_indentation_shift_mixed_spaces_and_tabs() { let ctx = ctx(); - let tree = ctx.parse_java("class Outer {\n\tclass MyCls {\n int attr;\n\t}\n}\n"); + let tree = ctx.parse( + "a.java", + "class Outer {\n\tclass MyCls {\n int attr;\n\t}\n}\n", + ); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1378,7 +1384,10 @@ mod tests { #[test] fn preceding_indentation_shift_mixed_tabs_and_spaces() { let ctx = ctx(); - let tree = ctx.parse_java("class Outer {\n class MyCls {\n\t\tint attr;\n }\n}\n"); + let tree = ctx.parse( + "a.java", + "class Outer {\n class MyCls {\n\t\tint attr;\n }\n}\n", + ); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1389,7 +1398,7 @@ mod tests { #[test] fn reindent_yaml() { let ctx = ctx(); - let tree = ctx.parse_yaml("hello:\n foo: 2\nbar: 4\n"); + let tree = ctx.parse("a.yaml", "hello:\n foo: 2\nbar: 4\n"); let block_node = tree[0][0]; assert_eq!(block_node.grammar_name, "block_node"); let value = block_node[0][0][2]; @@ -1402,7 +1411,7 @@ mod tests { #[test] fn source_with_whitespace() { let ctx = ctx(); - let tree = ctx.parse_json(" [ 1 , 2,\n 3]"); + let tree = ctx.parse("a.json", " [ 1 , 2,\n 3]"); let root = tree[0]; let [bracket, one, comma, two, comma_2] = root[0..=4] else { @@ -1420,7 +1429,8 @@ mod tests { fn removing_indentation() { let ctx = ctx(); - let tree = ctx.parse_json( + let tree = ctx.parse( + "a.json", r#" { "a": [ @@ -1458,10 +1468,14 @@ mod tests { fn multiline_comments_are_isomorphic() { let ctx = ctx(); - let comment_1 = - ctx.parse_java("/**\n * This is a comment\n * spanning on many lines\n*/")[0]; - let comment_2 = - ctx.parse_java(" /**\n * This is a comment\n * spanning on many lines\n */")[0]; + let comment_1 = ctx.parse( + "a.java", + "/**\n * This is a comment\n * spanning on many lines\n*/", + )[0]; + let comment_2 = ctx.parse( + "a.java", + " /**\n * This is a comment\n * spanning on many lines\n */", + )[0]; assert!(comment_1.isomorphic_to(comment_2)); assert_eq!(comment_1.children.len(), 4); @@ -1497,7 +1511,7 @@ mod tests { #[test] fn print_as_ascii_art() { let ctx = ctx(); - let tree = ctx.parse_json("{\"foo\": 3, \"bar\": 4}"); + let tree = ctx.parse("a.json", "{\"foo\": 3, \"bar\": 4}"); let ascii_tree = tree.ascii_tree(); @@ -1529,12 +1543,12 @@ mod tests { #[test] fn commutative_isomorphism() { let ctx = ctx(); - let obj_1 = ctx.parse_json("{\"foo\": 3, \"bar\": 4}"); - let obj_2 = ctx.parse_json("{\"bar\": 4, \"foo\": 3}"); - let obj_3 = ctx.parse_json("{\"bar\": 3, \"foo\": 4}"); - let obj_4 = ctx.parse_json("{\n \"foo\": 3,\n \"bar\": 4\n}"); - let array_1 = ctx.parse_json("[ 1, 2 ]"); - let array_2 = ctx.parse_json("[ 2, 1 ]"); + let obj_1 = ctx.parse("a.json", "{\"foo\": 3, \"bar\": 4}"); + let obj_2 = ctx.parse("a.json", "{\"bar\": 4, \"foo\": 3}"); + let obj_3 = ctx.parse("a.json", "{\"bar\": 3, \"foo\": 4}"); + let obj_4 = ctx.parse("a.json", "{\n \"foo\": 3,\n \"bar\": 4\n}"); + let array_1 = ctx.parse("a.json", "[ 1, 2 ]"); + let array_2 = ctx.parse("a.json", "[ 2, 1 ]"); assert!(obj_1.commutatively_isomorphic_to(obj_2)); assert!(!obj_1.commutatively_isomorphic_to(obj_3)); @@ -1543,8 +1557,8 @@ mod tests { assert!(!obj_1.commutatively_isomorphic_to(array_1)); assert!(!array_1.commutatively_isomorphic_to(array_2)); - let method1 = ctx.parse_java("public final void main();"); - let method2 = ctx.parse_java("public final static void main();"); + let method1 = ctx.parse("a.java", "public final void main();"); + let method2 = ctx.parse("a.java", "public final static void main();"); // `public`, `final` and `static` are all commutative children of (function) `modifiers`, // but the second tree doesn't have `static`. A naive `zip` would only check the first two @@ -1560,7 +1574,7 @@ mod tests { let src = r#"let foo = "line 1 line 2 line 3";"#; - let tree = ctx.parse_rust(src); + let tree = ctx.parse("a.rs", src); let root = tree; assert_eq!(root.id, 13); @@ -1602,7 +1616,7 @@ line 3";"#; let src = r#"let foo = "line 1 line 2 line 3";"#; - let tree = ctx.parse_rust(src); + let tree = ctx.parse("a.rs", src); let ids = tree.dfs().map(|n| n.id).collect_vec(); @@ -1616,7 +1630,10 @@ line 3";"#; #[test] fn parse_html_with_js() { let ctx = ctx(); - let html = ctx.parse_html(""); + let html = ctx.parse( + "a.html", + "", + ); assert_eq!(html.grammar_name, "document"); assert_eq!(html.lang_profile.name, "HTML"); @@ -1633,7 +1650,10 @@ line 3";"#; #[test] fn parse_injection_with_syntax_error() { let ctx = ctx(); - let html = ctx.parse_html(""); + let html = ctx.parse( + "a.html", + "", + ); assert_eq!(html.grammar_name, "document"); assert_eq!(html.lang_profile.name, "HTML"); @@ -1659,7 +1679,7 @@ A list: - Hello "#; - let markdown = ctx.parse_markdown(source); + let markdown = ctx.parse("a.md", source); let paragraph = markdown[0][1][0][1]; assert_eq!(paragraph.grammar_name, "paragraph"); @@ -1672,7 +1692,7 @@ A list: #[test] fn commutative_parent_via_query() { let ctx = ctx(); - let python = ctx.parse_python("__all__ = [ 'foo', 'bar' ]\nother = [ 1, 2 ]\n"); + let python = ctx.parse("a.py", "__all__ = [ 'foo', 'bar' ]\nother = [ 1, 2 ]\n"); let first_list = python[0][0][2]; let second_list = python[1][0][2]; diff --git a/src/changeset.rs b/src/changeset.rs index 5fd6315..c8c8c92 100644 --- a/src/changeset.rs +++ b/src/changeset.rs @@ -190,7 +190,7 @@ mod tests { fn from_tree() { let ctx = ctx(); - let tree = ctx.parse_json("[1, [2, 3]]"); + let tree = ctx.parse("a.json", "[1, [2, 3]]"); let classmapping = ClassMapping::new(); let mut changeset = ChangeSet::new(); @@ -237,7 +237,7 @@ mod tests { fn single_tree_has_no_conflicts() { let ctx = ctx(); - let tree = ctx.parse_json("[1, [2, 3]]"); + let tree = ctx.parse("a.json", "[1, [2, 3]]"); let classmapping = ClassMapping::new(); let mut changeset = ChangeSet::new(); @@ -257,7 +257,7 @@ mod tests { fn write_to_file() { let ctx = ctx(); - let tree = ctx.parse_json("[1, 2]"); + let tree = ctx.parse("a.json", "[1, 2]"); let classmapping = ClassMapping::new(); let mut changeset = ChangeSet::new(); diff --git a/src/class_mapping.rs b/src/class_mapping.rs index 86e770b..5ec28c4 100644 --- a/src/class_mapping.rs +++ b/src/class_mapping.rs @@ -450,9 +450,9 @@ mod tests { fn left_right_matching_does_not_override_base_matchings() { let ctx = ctx(); - let base_tree = ctx.parse_rust("struct Foo;\nstruct Bar;\n"); - let left_tree = ctx.parse_rust("struct Foo;\n"); - let right_tree = ctx.parse_rust("struct Bar;\n"); + let base_tree = ctx.parse("a.rs", "struct Foo;\nstruct Bar;\n"); + let left_tree = ctx.parse("a.rs", "struct Foo;\n"); + let right_tree = ctx.parse("a.rs", "struct Bar;\n"); let foo_base = RevNode::new(Revision::Base, base_tree[0]); assert_eq!(foo_base.node.source, "struct Foo;"); @@ -491,9 +491,18 @@ mod tests { fn classes_are_properly_merged() { let ctx = ctx(); - let base_tree = ctx.parse_rust("struct FooBase;\nstruct BarBase;\nstruct HeyBase;\n"); - let left_tree = ctx.parse_rust("struct FooLeft;\nstruct BarLeft;\nstruct HeyLeft;\n"); - let right_tree = ctx.parse_rust("struct FooRight;\nstruct BarRight;\nstruct HeyRight;\n"); + let base_tree = ctx.parse( + "a.rs", + "struct FooBase;\nstruct BarBase;\nstruct HeyBase;\n", + ); + let left_tree = ctx.parse( + "a.rs", + "struct FooLeft;\nstruct BarLeft;\nstruct HeyLeft;\n", + ); + let right_tree = ctx.parse( + "a.rs", + "struct FooRight;\nstruct BarRight;\nstruct HeyRight;\n", + ); let foo_base = RevNode::new(Revision::Base, base_tree[0]); let foo_left = RevNode::new(Revision::Left, left_tree[0]); diff --git a/src/lang_profile.rs b/src/lang_profile.rs index fd860e4..5c8150e 100644 --- a/src/lang_profile.rs +++ b/src/lang_profile.rs @@ -380,8 +380,8 @@ mod tests { fn has_signature_conflicts() { let ctx = ctx(); - let with_conflicts = ctx.parse_json("[{\"a\":1, \"b\":2, \"a\":3}]"); - let without_conflicts = ctx.parse_json("{\"a\": [4], \"b\": [4]}"); + let with_conflicts = ctx.parse("a.json", "[{\"a\":1, \"b\":2, \"a\":3}]"); + let without_conflicts = ctx.parse("a.json", "{\"a\": [4], \"b\": [4]}"); assert!(with_conflicts.has_signature_conflicts()); assert!(!without_conflicts.has_signature_conflicts()); diff --git a/src/matching.rs b/src/matching.rs index 82a23c2..264ac8f 100644 --- a/src/matching.rs +++ b/src/matching.rs @@ -222,8 +222,8 @@ mod tests { fn retrieve_match() { let ctx = ctx(); - let tree = ctx.parse_rust("fn t() { 3 }"); - let tree2 = ctx.parse_rust("fn t() { 1 }"); + let tree = ctx.parse("a.rs", "fn t() { 3 }"); + let tree2 = ctx.parse("a.rs", "fn t() { 1 }"); let mut matching = Matching::new(); assert_eq!(matching.len(), 0); @@ -237,8 +237,8 @@ mod tests { fn remove_previously_matched() { let ctx = ctx(); - let tree1 = ctx.parse_json("[1, 2, 3]"); - let tree2 = ctx.parse_json("[4, 5, 6]"); + let tree1 = ctx.parse("a.json", "[1, 2, 3]"); + let tree2 = ctx.parse("a.json", "[4, 5, 6]"); let mut matching = Matching::new(); @@ -267,7 +267,7 @@ mod tests { fn dice() { let ctx = ctx(); - let root = ctx.parse_rust("fn t() { 3 }"); + let root = ctx.parse("a.rs", "fn t() { 3 }"); let mut matching = Matching::new(); assert_eq!(matching.dice(root, root), 0.0_f32); diff --git a/src/merge_3dm.rs b/src/merge_3dm.rs index bb208af..f8f1692 100644 --- a/src/merge_3dm.rs +++ b/src/merge_3dm.rs @@ -329,9 +329,9 @@ mod tests { fn single_tree_has_no_conflicts() { let ctx = ctx(); - let base = ctx.parse_json("[1, {\"a\":2}]"); - let left = ctx.parse_json("[0, 1, {\"a\":2}]"); - let right = ctx.parse_json("[1, {\"a\":2}, 3]"); + let base = ctx.parse("a.json", "[1, {\"a\":2}]"); + let left = ctx.parse("a.json", "[0, 1, {\"a\":2}]"); + let right = ctx.parse("a.json", "[1, {\"a\":2}, 3]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -357,9 +357,9 @@ mod tests { fn merge_conflict() { let ctx = ctx(); - let base = ctx.parse_json("[1, 2]"); - let left = ctx.parse_json("[1, 3, 2]"); - let right = ctx.parse_json("[1, 4, 2]"); + let base = ctx.parse("a.json", "[1, 2]"); + let left = ctx.parse("a.json", "[1, 3, 2]"); + let right = ctx.parse("a.json", "[1, 4, 2]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -395,9 +395,9 @@ mod tests { fn delete_delete() { let ctx = ctx(); - let base = ctx.parse_json("[1, 2]"); - let left = ctx.parse_json("[1]"); - let right = ctx.parse_json("[2]"); + let base = ctx.parse("a.json", "[1, 2]"); + let left = ctx.parse("a.json", "[1]"); + let right = ctx.parse("a.json", "[2]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -433,9 +433,9 @@ mod tests { fn delete_insert() { let ctx = ctx(); - let base = ctx.parse_json("[1, 2]"); - let left = ctx.parse_json("[1]"); - let right = ctx.parse_json("[1, 2, 3]"); + let base = ctx.parse("a.json", "[1, 2]"); + let left = ctx.parse("a.json", "[1]"); + let right = ctx.parse("a.json", "[1, 2, 3]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -471,9 +471,9 @@ mod tests { fn delete_modify() { let ctx = ctx(); - let base = ctx.parse_json("[1, {\"a\": 3}, 2]"); - let left = ctx.parse_json("[1, {\"a\": 4}, 2]"); - let right = ctx.parse_json("[1, 2]"); + let base = ctx.parse("a.json", "[1, {\"a\": 3}, 2]"); + let left = ctx.parse("a.json", "[1, {\"a\": 4}, 2]"); + let right = ctx.parse("a.json", "[1, 2]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -509,9 +509,9 @@ mod tests { fn commutative_conflict_end_separator() { let ctx = ctx(); - let base = ctx.parse_json("{\"x\": 0}"); - let left = ctx.parse_json("{\"a\": 1, \"x\": 0}"); - let right = ctx.parse_json("{\"b\": 2, \"x\": 0}"); + let base = ctx.parse("a.json", "{\"x\": 0}"); + let left = ctx.parse("a.json", "{\"a\": 1, \"x\": 0}"); + let right = ctx.parse("a.json", "{\"b\": 2, \"x\": 0}"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -536,9 +536,9 @@ mod tests { fn commutative_conflict_no_end_separator() { let ctx = ctx(); - let base = ctx.parse_json("{}"); - let left = ctx.parse_json("{\"a\": 1}"); - let right = ctx.parse_json("{\"b\": 2}"); + let base = ctx.parse("a.json", "{}"); + let left = ctx.parse("a.json", "{\"a\": 1}"); + let right = ctx.parse("a.json", "{\"b\": 2}"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -563,9 +563,9 @@ mod tests { fn commutative_conflict_double_delete() { let ctx = ctx(); - let base = ctx.parse_json("{\"a\": 1, \"b\": 2}"); - let left = ctx.parse_json("{\"a\": 1}"); - let right = ctx.parse_json("{\"b\": 2}"); + let base = ctx.parse("a.json", "{\"a\": 1, \"b\": 2}"); + let left = ctx.parse("a.json", "{\"a\": 1}"); + let right = ctx.parse("a.json", "{\"b\": 2}"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -590,9 +590,9 @@ mod tests { fn commutative_conflict_delete_modified() { let ctx = ctx(); - let base = ctx.parse_json("{\"a\": {\"x\": 1}, \"b\": 2}"); - let left = ctx.parse_json("{\"a\": {\"x\": 2}}"); - let right = ctx.parse_json("{\"b\": 2}"); + let base = ctx.parse("a.json", "{\"a\": {\"x\": 1}, \"b\": 2}"); + let left = ctx.parse("a.json", "{\"a\": {\"x\": 2}}"); + let right = ctx.parse("a.json", "{\"b\": 2}"); let (primary_matcher, auxiliary_matcher) = json_matchers(); @@ -636,9 +636,9 @@ mod tests { // both `left` and `right` add the `'s` to `&self`, so this would-be-conflict should be // resolved during the construction of the tree. NB: The `<'s>` is added just so that // `left` and `right` are not completely identical (which would've made the resolution trivial) - let base = ctx.parse_rust("fn foo(&self) {}"); - let left = ctx.parse_rust("fn foo(&'s self) {}"); - let right = ctx.parse_rust("fn foo<'s>(&'s self) {}"); + let base = ctx.parse("a.rs", "fn foo(&self) {}"); + let left = ctx.parse("a.rs", "fn foo(&'s self) {}"); + let right = ctx.parse("a.rs", "fn foo<'s>(&'s self) {}"); let (primary_matcher, auxiliary_matcher) = rust_matchers(); @@ -706,9 +706,9 @@ fn baz() { eprintln!(); }"; - let base = ctx.parse_rust(base); - let left = ctx.parse_rust(left); - let right = ctx.parse_rust(right); + let base = ctx.parse("a.rs", base); + let left = ctx.parse("a.rs", left); + let right = ctx.parse("a.rs", right); let primary_matcher = TreeMatcher { min_height: 1, diff --git a/src/merged_tree.rs b/src/merged_tree.rs index 7d4ab67..d7e582f 100644 --- a/src/merged_tree.rs +++ b/src/merged_tree.rs @@ -532,9 +532,9 @@ mod test { #[test] fn debug_print() { let ctx = ctx(); - let base = ctx.parse_json("[1, 1]"); - let left = ctx.parse_json("[1, 2]"); - let right = ctx.parse_json("[2, 1]"); + let base = ctx.parse("a.json", "[1, 1]"); + let left = ctx.parse("a.json", "[1, 2]"); + let right = ctx.parse("a.json", "[2, 1]"); let (primary_matcher, auxiliary_matcher) = json_matchers(); let (merged_tree, _) = three_way_merge( diff --git a/src/parsed_merge.rs b/src/parsed_merge.rs index 800e8ae..8a33e41 100644 --- a/src/parsed_merge.rs +++ b/src/parsed_merge.rs @@ -1222,8 +1222,8 @@ struct MyType { let left_rev = parsed.reconstruct_revision(Revision::Left); let right_rev = parsed.reconstruct_revision(Revision::Right); - let parsed_left = ctx.parse_rust(&left_rev); - let parsed_right = ctx.parse_rust(&right_rev); + let parsed_left = ctx.parse("a.rs", &left_rev); + let parsed_right = ctx.parse("a.rs", &right_rev); let matching = parsed.generate_matching( Revision::Left, @@ -1266,8 +1266,8 @@ struct MyType { let base_rev = parsed.reconstruct_revision(Revision::Base); let left_rev = parsed.reconstruct_revision(Revision::Left); - let parsed_base = ctx.parse_nix(&base_rev); - let parsed_left = ctx.parse_nix(&left_rev); + let parsed_base = ctx.parse("a.nix", &base_rev); + let parsed_left = ctx.parse("a.nix", &left_rev); let binding_set_base = parsed_base[0][2][1]; assert_eq!(binding_set_base.grammar_name, "binding_set"); diff --git a/src/signature.rs b/src/signature.rs index 1aedc0d..863166b 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -408,8 +408,8 @@ mod tests { fn equal_signatures() { let ctx = ctx(); - let document = ctx.parse_json("{\"a\":\"b\"}"); - let other_document = ctx.parse_json("{\"a\":\"c\"}"); + let document = ctx.parse("a.json", "{\"a\":\"b\"}"); + let other_document = ctx.parse("a.json", "{\"a\":\"c\"}"); let object = document[0]; let pair = object[1]; let other_pair = other_document[0][1]; @@ -435,8 +435,8 @@ mod tests { fn node_equality_and_hashing() { let ctx = ctx(); - let object = ctx.parse_json("{\"a\":\"b\"}")[0]; - let object_2 = ctx.parse_json("[{\"a\": \"b\"}]")[0][1]; + let object = ctx.parse("a.json", "{\"a\":\"b\"}")[0]; + let object_2 = ctx.parse("a.json", "[{\"a\": \"b\"}]")[0][1]; let class_mapping = ClassMapping::new(); let node_2 = class_mapping.map_to_leader(RevNode { @@ -493,8 +493,8 @@ mod tests { fn node_equality_and_hashing_care_about_languages() { let ctx = ctx(); - let tree_python = ctx.parse_python("foo()"); - let tree_java = ctx.parse_java("foo();"); + let tree_python = ctx.parse("a.py", "foo()"); + let tree_java = ctx.parse("a.java", "foo();"); let args_python = tree_python[0][0][1]; let args_java = tree_java[0][0][1]; diff --git a/src/test_utils.rs b/src/test_utils.rs index a9ba13d..57fd786 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -17,52 +17,12 @@ pub fn ctx<'a>() -> TestContext<'a> { } impl<'a> TestContext<'a> { - fn parse_internal(&'a self, extension: &str, source: &'a str) -> &'a AstNode<'a> { + pub fn parse(&'a self, filename: &str, source: &'a str) -> &'a AstNode<'a> { let lang_profile = - LangProfile::detect_from_filename(extension).expect("could not load language profile"); + LangProfile::detect_from_filename(filename).expect("could not load language profile"); AstNode::parse(source, lang_profile, &self.arena, &self.ref_arena) .expect("syntax error in source") } - - pub fn parse_rust(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.rs", source) - } - - pub fn parse_json(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.json", source) - } - - pub fn parse_html(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.html", source) - } - - pub fn parse_markdown(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.md", source) - } - - pub fn parse_java(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.java", source) - } - - pub fn parse_python(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.py", source) - } - - pub fn parse_go(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.go", source) - } - - pub fn parse_yaml(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.yaml", source) - } - - pub fn parse_toml(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.toml", source) - } - - pub fn parse_nix(&'a self, source: &'a str) -> &'a AstNode<'a> { - self.parse_internal("a.nix", source) - } } pub(crate) fn json_matchers() -> (TreeMatcher, TreeMatcher) { diff --git a/src/tree_builder.rs b/src/tree_builder.rs index 38fa027..01539ee 100644 --- a/src/tree_builder.rs +++ b/src/tree_builder.rs @@ -1036,7 +1036,7 @@ mod tests { fn recover_exact_tree() { let ctx = ctx(); - let tree = ctx.parse_json("[1, [2, 3]]"); + let tree = ctx.parse("a.json", "[1, [2, 3]]"); let class_mapping = ClassMapping::new(); let mut changeset = ChangeSet::new(); @@ -1068,7 +1068,7 @@ mod tests { fn contains() { let ctx = ctx(); - let tree = ctx.parse_json("[1, [2, 3]]"); + let tree = ctx.parse("a.json", "[1, [2, 3]]"); let class_mapping = ClassMapping::new(); let mut changeset = ChangeSet::new(); diff --git a/src/tree_matcher.rs b/src/tree_matcher.rs index 684be20..c058c79 100644 --- a/src/tree_matcher.rs +++ b/src/tree_matcher.rs @@ -504,8 +504,8 @@ mod tests { fn small_sample() { let ctx = ctx(); - let t1 = ctx.parse_rust("fn my_func() -> i32 { 1 + (3 + (5 - 1)) }"); - let t2 = ctx.parse_rust("fn other_func() { (3 + (5 - 1)) * 2 }"); + let t1 = ctx.parse("a.rs", "fn my_func() -> i32 { 1 + (3 + (5 - 1)) }"); + let t2 = ctx.parse("a.rs", "fn other_func() { (3 + (5 - 1)) * 2 }"); let matcher = TreeMatcher { min_height: 2, @@ -526,10 +526,11 @@ mod tests { fn example_from_the_paper() { let ctx = ctx(); - let t1 = ctx.parse_java( + let t1 = ctx.parse( + "a.java", "public class Test { public String foo(int i) { if (i == 0) return \"Foo!\"; } }", ); - let t2 = ctx.parse_java("public class Test { private String foo(int i) { if (i == 0) return \"Bar\"; else if (i == -1) return \"Foo!\"; } }"); + let t2 = ctx.parse("a.java", "public class Test { private String foo(int i) { if (i == 0) return \"Bar\"; else if (i == -1) return \"Foo!\"; } }"); let matcher = TreeMatcher { min_height: 2, @@ -550,10 +551,11 @@ mod tests { fn without_rted() { let ctx = ctx(); - let t1 = ctx.parse_java( + let t1 = ctx.parse( + "a.java", "public class Test { public String foo(int i) { if (i == 0) return \"Foo!\"; } }", ); - let t2 = ctx.parse_java("public class Test { private String foo(int i) { if (i == 0) return \"Bar\"; else if (i == -1) return \"Foo!\"; } }"); + let t2 = ctx.parse("a.java", "public class Test { private String foo(int i) { if (i == 0) return \"Bar\"; else if (i == -1) return \"Foo!\"; } }"); let matcher = TreeMatcher { min_height: 2, @@ -574,8 +576,8 @@ mod tests { fn matching_very_shallow_structures() { let ctx = ctx(); - let left = ctx.parse_json("[1, 2]"); - let right = ctx.parse_json("[0, 1, 2]"); + let left = ctx.parse("a.json", "[1, 2]"); + let right = ctx.parse("a.json", "[0, 1, 2]"); let matcher = TreeMatcher { min_height: 0, @@ -596,8 +598,8 @@ mod tests { fn matching_rust_types() { let ctx = ctx(); - let left = ctx.parse_rust("use std::collections::{HashMap};"); - let right = ctx.parse_rust("use std::collections::{HashMap, HashSet};"); + let left = ctx.parse("a.rs", "use std::collections::{HashMap};"); + let right = ctx.parse("a.rs", "use std::collections::{HashMap, HashSet};"); let matcher = TreeMatcher { min_height: 2, diff --git a/src/tree_matcher/priority_list.rs b/src/tree_matcher/priority_list.rs index 0c53a77..4454898 100644 --- a/src/tree_matcher/priority_list.rs +++ b/src/tree_matcher/priority_list.rs @@ -88,7 +88,7 @@ mod tests { let ctx = ctx(); let mut priority_list = PriorityList::new(); - let node = ctx.parse_rust("fn x() -> i32 { 1 + 2 }"); + let node = ctx.parse("a.rs", "fn x() -> i32 { 1 + 2 }"); priority_list.push(node); assert_eq!(priority_list.peek_max(), Some(4)); @@ -100,9 +100,9 @@ mod tests { let ctx = ctx(); let mut priority_list = PriorityList::new(); - let node1 = ctx.parse_rust("fn y() -> u8 { 1 + 2 }"); + let node1 = ctx.parse("a.rs", "fn y() -> u8 { 1 + 2 }"); priority_list.push(node1); - let node2 = ctx.parse_rust("fn z() { 3 * 5 }"); + let node2 = ctx.parse("a.rs", "fn z() { 3 * 5 }"); priority_list.push(node2); assert_eq!(priority_list.peek_max(), Some(4)); @@ -114,9 +114,9 @@ mod tests { let ctx = ctx(); let mut priority_list = PriorityList::new(); - let node1 = ctx.parse_rust("fn a() { 1 + 2 }"); + let node1 = ctx.parse("a.rs", "fn a() { 1 + 2 }"); priority_list.push(node1); - let node2 = ctx.parse_rust("fn b() { 3 * (5 + 1) }"); + let node2 = ctx.parse("a.rs", "fn b() { 3 * (5 + 1) }"); priority_list.push(node2); assert_eq!(priority_list.peek_max(), Some(6)); @@ -128,9 +128,9 @@ mod tests { let ctx = ctx(); let mut priority_list = PriorityList::new(); - let node1 = ctx.parse_rust("fn c() { 1 + (2 + 5) }"); + let node1 = ctx.parse("a.rs", "fn c() { 1 + (2 + 5) }"); priority_list.push(node1); - let node2 = ctx.parse_rust("fn d() { 3 * 9 }"); + let node2 = ctx.parse("a.rs", "fn d() { 3 * 9 }"); priority_list.push(node2); assert_eq!(priority_list.peek_max(), Some(6)); @@ -142,7 +142,7 @@ mod tests { let ctx = ctx(); let mut priority_list = PriorityList::new(); - let node1 = ctx.parse_rust("fn x() { 1 + (2 + 5) }"); + let node1 = ctx.parse("a.rs", "fn x() { 1 + (2 + 5) }"); let node1 = node1[0][3][1]; priority_list.open(node1); let child1 = node1[0]; diff --git a/src/visualizer.rs b/src/visualizer.rs index 78ad95f..be59f78 100644 --- a/src/visualizer.rs +++ b/src/visualizer.rs @@ -179,8 +179,8 @@ mod tests { let target_path = repo_dir.path().join("graph.dot"); let ctx = ctx(); - let parsed_left = ctx.parse_json("{\"foo\": 3}"); - let parsed_right = ctx.parse_json("{\"foo\": 4}"); + let parsed_left = ctx.parse("a.json", "{\"foo\": 3}"); + let parsed_right = ctx.parse("a.json", "{\"foo\": 4}"); let matching = DetailedMatching::default(); matching_to_graph(&target_path, parsed_left, parsed_right, &matching).unwrap();