From b92dd0a4c59287b661759d3cdcd6f62e1afcd815 Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Fri, 25 Mar 2022 22:28:51 +0100 Subject: feat: Add test to generate function Add testing for `generate` rust function. That function is pretty much the whole bussiness logic of the application and as far as I can see the only one worth testing. --- tests/web.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/web.rs b/tests/web.rs index de5c1da..607f16d 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -3,11 +3,62 @@ #![cfg(target_arch = "wasm32")] extern crate wasm_bindgen_test; + use wasm_bindgen_test::*; +use genpass::generate; wasm_bindgen_test_configure!(run_in_browser); +const ATTEMPTS: usize = 5; + +#[wasm_bindgen_test] +fn generate_length() { + for x in (0..=100).step_by(5) { + let password = generate(x, true, true, true, true); + assert_eq!(x, password.len()) + } +} + +#[wasm_bindgen_test] +fn generate_not_upper_letters() { + let lower_charset = "abcdefghijklmnopqrstuvwxyz"; + for _ in 0..ATTEMPTS { + let password = generate(100, false, true, true, true); + for char in String::from(lower_charset).chars().into_iter() { + assert!(!password.contains(char)) + } + } +} + +#[wasm_bindgen_test] +fn generate_not_lower_letters() { + let upper_charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + for x in 0..ATTEMPTS { + let password = generate(x, true, false, true, true); + for char in String::from(upper_charset).chars().into_iter() { + assert!(!password.contains(char)) + } + } +} + +#[wasm_bindgen_test] +fn generate_not_number_letters() { + let upper_charset = "1234567890"; + for x in 0..ATTEMPTS { + let password = generate(x, true, true, false, true); + for char in String::from(upper_charset).chars().into_iter() { + assert!(!password.contains(char)) + } + } +} + #[wasm_bindgen_test] -fn pass() { - assert_eq!(1 + 1, 2); +fn generate_not_special_letters() { + let upper_charset = "!@#$%^&*()+{}[]"; + for x in 0..ATTEMPTS { + let password = generate(x, true, true, true, false); + for char in String::from(upper_charset).chars().into_iter() { + assert!(!password.contains(char)) + } + } } -- cgit v1.2.3