feat: add Rust theory guides (terminology, algorithms, testing, debugging) and 2 new crates
This commit is contained in:
1 parent
2e002b6176
commit
015eae08c3
14 files changed
+357
-5
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "eng05-testing-types-and-mutation"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,13 @@
|
||||
# 05 — Rodzaje Testów: Jednostkowe, Integracyjne i Mutacyjne
|
||||
|
||||
Teoria: [notes/04-testing-pyramid-and-types.md](../../../notes/04-testing-pyramid-and-types.md)
|
||||
|
||||
## Zadania (`src/lib.rs`)
|
||||
1. **Unit (granice zabijające mutanty):** zaimplementuj `is_within_rate_limit(current: i32, limit: i32) -> Result<bool, RateLimitError>` — dla `current < 0` zwróć `Err(RateLimitError::NegativeCount)`, dla `limit <= 0` zwróć `Ok(false)`, inaczej `Ok(current <= limit)`.
|
||||
2. **Integration (prawdziwy system plików):** zaimplementuj `save_and_load_kv(path: &Path, pairs: &[(&str, &str)]) -> io::Result<Vec<(String, String)>>` — zapisz pary `KEY=VALUE\n` na dysk (`std::fs::write`) i odczytaj z powrotem (`std::fs::read_to_string`).
|
||||
3. **Mutation Testing (eksperyment ręczny):** po przejściu testów zmień `<=` na `<` w `is_within_rate_limit` (mutant!), uruchom `make test PKG=eng05-testing-types-and-mutation` i sprawdź, czy test graniczny `current == limit` zabił mutanta.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=eng05-testing-types-and-mutation
|
||||
```
|
||||
@@ -0,0 +1,63 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum RateLimitError {
|
||||
NegativeCount,
|
||||
}
|
||||
|
||||
pub fn is_within_rate_limit(current: i32, limit: i32) -> Result<bool, RateLimitError> {
|
||||
todo!(
|
||||
"Err(NegativeCount) dla current < 0; Ok(false) dla limit <= 0; inaczej Ok(current <= limit)"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn save_and_load_kv(path: &Path, pairs: &[(&str, &str)]) -> io::Result<Vec<(String, String)>> {
|
||||
todo!("Zapisz pary KEY=VALUE przez std::fs::write i odczytaj przez std::fs::read_to_string")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
// 1. Testy jednostkowe + warunki brzegowe zabijające mutanty (<= vs <, > 0 vs >= 0)
|
||||
#[test]
|
||||
fn rate_limit_boundaries_kill_mutants() {
|
||||
assert_eq!(is_within_rate_limit(9, 10), Ok(true));
|
||||
assert_eq!(is_within_rate_limit(10, 10), Ok(true)); // Zabija mutanta `<=` -> `<`
|
||||
assert_eq!(is_within_rate_limit(11, 10), Ok(false));
|
||||
assert_eq!(is_within_rate_limit(0, 0), Ok(false)); // Zabija mutanta `limit > 0` -> `>= 0`
|
||||
assert_eq!(is_within_rate_limit(0, -1), Ok(false));
|
||||
assert_eq!(
|
||||
is_within_rate_limit(-1, 10),
|
||||
Err(RateLimitError::NegativeCount)
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Test integracyjny na prawdziwym pliku tymczasowym z deterministycznym sprzątaniem
|
||||
#[test]
|
||||
fn saves_and_loads_kv_file_integration() {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let file_path = std::env::temp_dir().join(format!("rust-learning-kv-{nanos}.conf"));
|
||||
|
||||
let res = save_and_load_kv(&file_path, &[("env", "prod"), ("region", "eu-central-1")]);
|
||||
let exists = file_path.exists();
|
||||
let _ = fs::remove_file(&file_path);
|
||||
|
||||
assert!(exists);
|
||||
assert_eq!(
|
||||
res.unwrap(),
|
||||
vec![
|
||||
(String::from("env"), String::from("prod")),
|
||||
(String::from("region"), String::from("eu-central-1")),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user