Skip to content

Commit

Permalink
test(language/src): add more snippet tests for kotlin, java, javascri…
Browse files Browse the repository at this point in the history
…pt, and python
  • Loading branch information
Alex-ley-scrub committed Nov 15, 2024
1 parent d256208 commit a152297
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
26 changes: 26 additions & 0 deletions crates/language/src/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,30 @@ mod tests {
let nodes = nodes_from_indices(&snippets);
assert!(!nodes.is_empty());
}

#[test]
fn list_snippet() {
let snippet = r#"List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");"#;
let lang = Java::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn array_snippet() {
let snippet = r#"int[] numbers = {1, 2, 3, 4, 5};"#;
let lang = Java::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}
}
26 changes: 26 additions & 0 deletions crates/language/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,30 @@ mod tests {
let nodes = nodes_from_indices(&snippets);
assert!(!nodes.is_empty());
}

#[test]
fn assignment_snippet() {
let snippet = r#"const key = $value"#;
let lang = JavaScript::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn array_snippet() {
let snippet = r#"const fruits = ["Apple", "Banana", "Cherry"]"#;
let lang = JavaScript::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("{:?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}
}
102 changes: 99 additions & 3 deletions crates/language/src/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ impl Language for Kotlin {
&[
("", ""),
("import ", ""),
("var GRIT_VAR = ", ""),
("val GRIT_VAR = ", ""),
("const val GRIT_VAR = ", ""),
("class GRIT_CLASS { ", " }"),
("class GRIT_CLASS { ", " fun GRIT_FUNCTION() {} }"),
("GRIT_FN(", ") {}"),
("fun GRIT_FN(", ") {}"),
("fun GRIT_FN(GRIT_ARG:", ") {}"),
("fun GRIT_FN(GRIT_ARG", ") {}"),
]
}
}
Expand All @@ -96,12 +98,106 @@ mod tests {
use super::*;

#[test]
fn pair_snippet() {
let snippet = "import kotlin.math.PI";
fn import_snippet() {
let snippet = r#"import kotlin.math.$var"#;
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("{:?}", nodes);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn assignment_snippet() {
let snippet = r#"val key = $value"#;
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn kv_snippet() {
let snippet = "$key: $value";
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn fun_snippet() {
let snippet = "fun add(a: Int, b: Int): Int = a + b";
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn array_snippet() {
let snippet = "val numbers = arrayOf(1, 2, 3, 4, 5)";
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn list_snippet() {
let snippet = r#"val fruits = listOf("Apple", "Banana", "Cherry")"#;
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn kotlin_map_snippet() {
let snippet = r#"val immutableMap = mapOf("key1" to "value1", "key2" to "value2")"#;
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn kotlin_string_literal() {
let snippet = r#"val greeting = "Hello, World!""#;
let lang = Kotlin::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}
}
39 changes: 39 additions & 0 deletions crates/language/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,43 @@ mod tests {
println!("nodes: {:#?}", nodes);
assert!(!nodes.is_empty());
}

#[test]
fn assignment_snippet() {
let snippet = r#"key = "value""#;
let lang = Python::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn def_snippet() {
let snippet: &str = "def add(a: int, b: int) -> int: a + b;";
let lang = Python::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}

#[test]
fn lambda_snippet() {
let snippet: &str = "add = lambda a, b: a + b;";
let lang = Python::new(None);
let snippets = lang.parse_snippet_contexts(snippet);
let nodes = nodes_from_indices(&snippets);
println!("nodes: {:#?}", nodes);
nodes.iter().for_each(|n| {
n.print_node_tree();
});
assert!(!nodes.is_empty());
}
}

0 comments on commit a152297

Please sign in to comment.