Mirki #programowanie #chwalesie po blisko 20 latach programowania moja pierwsza publicznie wydana biblioteka, wprawdzie malutka, ale zawsze to coś :) #rustlang więc może trochę niszowo, ale to mój obecny numer 1 :)
To klasyczny "test case", czyli zamiast pisać tak:
mod fancy_addition { use super::*; fn fancy_addition(x: Option, y: Option) -> i8 { x.unwrap_or(0) + y.unwrap_or(0) } #[test] fn treats_none_as_0() { let expected = 0; let actual = fancy_addition(None, None); assert_eq!(expected, actual); } #[test] fn some_2_some_3() { let expected = 5; let actual = fancy_addition(Some(2), Some(3)); assert_eq!(expected, actual); } #[test] fn some_2_3_some_4() { let expected = 2 + 3 + 4; let actual = fancy_addition(Some(2 + 3), Some(4)); assert_eq!(expected, actual); } } Pisze się tak:
#[test_case( 2, 4 :: "when both operands are possitive")] #[test_case( 4, 2 :: "when operands are swapped")] #[test_case(-2, -4 :: "when both operands are negative")] fn multiplication_tests(x: i8, y: i8) { let actual = (x * y).abs(); assert_eq!(8, actual) } Lub jeszcze lepiej tak:
#[test_case( 2 => 2 :: "returns given number for positive input")] #[test_case(-2 => 2 :: "returns opposite number for non-positive input")] #[test_case( 0 => 0 :: "returns 0 for 0")] fn abs_tests(x: i8) -> i8 { if x > 0 { x } else { -x } } Wszelkie komentarze mile widziane!
@sasik520: Witam Pana, sam nie zamierzam korzystać z Pana twórczości, jednakże chciałbym Panu pogratulować wytrwałości oraz skończonej biblioteki. Pozdrawiam Pana i życzę miłego dnia.
https://crates.io/crates/test-case-derive
Po co to?
To klasyczny "test case", czyli zamiast pisać tak:
mod fancy_addition {
use super::*;
fn fancy_addition(x: Option, y: Option) -> i8 {
x.unwrap_or(0) + y.unwrap_or(0)
}
#[test]
fn treats_none_as_0() {
let expected = 0;
let actual = fancy_addition(None, None);
assert_eq!(expected, actual);
}
#[test]
fn some_2_some_3() {
let expected = 5;
let actual = fancy_addition(Some(2), Some(3));
assert_eq!(expected, actual);
}
#[test]
fn some_2_3_some_4() {
let expected = 2 + 3 + 4;
let actual = fancy_addition(Some(2 + 3), Some(4));
assert_eq!(expected, actual);
}
}
Pisze się tak:
#[test_case( 2, 4 :: "when both operands are possitive")]
#[test_case( 4, 2 :: "when operands are swapped")]
#[test_case(-2, -4 :: "when both operands are negative")]
fn multiplication_tests(x: i8, y: i8) {
let actual = (x * y).abs();
assert_eq!(8, actual)
}
Lub jeszcze lepiej tak:
#[test_case( 2 => 2 :: "returns given number for positive input")]
#[test_case(-2 => 2 :: "returns opposite number for non-positive input")]
#[test_case( 0 => 0 :: "returns 0 for 0")]
fn abs_tests(x: i8) -> i8 {
if x > 0 { x } else { -x }
}
Wszelkie komentarze mile widziane!
Witam Pana, sam nie zamierzam korzystać z Pana twórczości, jednakże chciałbym Panu pogratulować wytrwałości oraz skończonej biblioteki.
Pozdrawiam Pana i życzę miłego dnia.