Skip to content
Snippets Groups Projects
Commit 1f17a207 authored by Aleksander Einarsen's avatar Aleksander Einarsen
Browse files

#86 Changed cargo check to cargo build, and added cargo test and test files...

#86 Changed cargo check to cargo build, and added cargo test and test files for running code with tests
parent c41cfe67
Branches
No related tags found
No related merge requests found
......@@ -44,7 +44,9 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin
}
// Run go build
cmdString += " cargo check"
cmdString += " cargo build"
cmdString += " && cargo test"
//cmdSlice := strings.Fields(cmdString)
cmd := utils.MakeCommand(cmdString)
......
......@@ -27,6 +27,16 @@ func TestCompileStringToRust(t *testing.T) {
shouldCompile: true,
dependencies: []string{"rand", "colored"},
},
{
filename: "should_compile_and_run_tests",
shouldCompile: true,
dependencies: nil,
},
{
filename: "should_compile_with_faulty_test",
shouldCompile: false,
dependencies: nil,
},
}
for _, test := range tests {
......
fn divide(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
return Err(String::from("Cannot divide by zero"));
}
Ok(a / b)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_divide_positive_numbers() {
let result = divide(10.0, 2.0);
assert_eq!(result.unwrap(), 5.0);
}
#[test]
fn test_divide_negative_numbers() {
let result = divide(-10.0, 2.0);
assert_eq!(result.unwrap(), -5.0);
}
#[test]
fn test_divide_by_zero() {
let result = divide(10.0, 0.0);
assert!(result.is_err());
}
}
fn main() {
match divide(10.0, 2.0) {
Ok(result) => println!("10.0 divided by 2.0 is {}", result),
Err(e) => println!("Error: {}", e),
}
}
// Function to add two numbers
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_positive_numbers() {
let result = add(2, 3);
assert_eq!(result, 5);
}
#[test]
fn test_add_negative_numbers() {
let result = add(-2, -3);
assert_eq!(result, -5);
}
#[test]
fn test_add_faulty() {
// This test is intentionally faulty
let result = add(2, 2);
assert_eq!(result, 5); // This will fail because 2 + 2 is actually 4
}
}
fn main() {
println!("2 + 3 = {}", add(2, 3));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment