@@ -217,3 +217,71 @@ async fn run_bc(expr: &str) -> Result<String, Box<dyn std::error::Error + Send +
217217 . into ( ) )
218218 }
219219}
220+
221+ #[ cfg( test) ]
222+ mod tests {
223+ use super :: * ;
224+
225+ #[ test]
226+ fn base64_roundtrip ( ) {
227+ let text = "Hello 🦀 Telegram Bot! 123" ;
228+ let encoded = general_purpose:: STANDARD . encode ( text. as_bytes ( ) ) ;
229+ let decoded_bytes = general_purpose:: STANDARD . decode ( & encoded) . unwrap ( ) ;
230+ let decoded = String :: from_utf8 ( decoded_bytes) . unwrap ( ) ;
231+ assert_eq ! ( decoded, text) ;
232+ }
233+
234+ #[ test]
235+ fn url_decode_works ( ) {
236+ let encoded = "hello%20world%21%40" ;
237+ let decoded = percent_decode_str ( encoded) . decode_utf8_lossy ( ) . into_owned ( ) ;
238+ assert_eq ! ( decoded, "hello world!@" ) ;
239+ }
240+
241+ #[ test]
242+ fn rng_always_in_range ( ) {
243+ let min = 10u32 ;
244+ let max = 20u32 ;
245+ for _ in 0 ..50 {
246+ let n = rand:: thread_rng ( ) . gen_range ( min..=max) ;
247+ assert ! ( n >= min && n <= max) ;
248+ }
249+ }
250+
251+ #[ test]
252+ fn password_correct_length_and_charset ( ) {
253+ let len = 15u32 ;
254+ let chars: Vec < char > =
255+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-="
256+ . chars ( )
257+ . collect ( ) ;
258+
259+ let pw = {
260+ let mut rng = rand:: thread_rng ( ) ;
261+ ( 0 ..len)
262+ . map ( |_| chars[ rng. gen_range ( 0 ..chars. len ( ) ) ] )
263+ . collect :: < String > ( )
264+ } ;
265+
266+ assert_eq ! ( pw. len( ) , len as usize ) ;
267+ assert ! ( pw. chars( ) . all( |c| chars. contains( & c) ) ) ;
268+ }
269+
270+ #[ tokio:: test]
271+ async fn bc_calculator_basic ( ) {
272+ let res = run_bc ( "2 + 2 * 3" ) . await . unwrap ( ) ;
273+ assert_eq ! ( res. trim( ) , "8" ) ;
274+ }
275+
276+ #[ tokio:: test]
277+ async fn bc_calculator_with_sqrt ( ) {
278+ let res = run_bc ( "scale=0; sqrt(16)" ) . await . unwrap ( ) ;
279+ assert_eq ! ( res. trim( ) , "4" ) ;
280+ }
281+
282+ #[ tokio:: test]
283+ async fn bc_error_handling ( ) {
284+ let res = run_bc ( "syntax error!" ) . await ;
285+ assert ! ( res. is_err( ) ) ; // bc should return non-zero
286+ }
287+ }
0 commit comments