getopt2 0.1.0

Zero dependency strict command line argument parser
Documentation
/*
 * Copyright (c) Radim Kolar 2024.
 * SPDX-License-Identifier: MIT
 *
 * getopt2 library is licensed under MIT license:
 *   https://spdx.org/licenses/MIT.html
*/

use super::getopt;

use std::collections::HashMap;

#[test]
fn for_loop_can_consume_getopt_struct() {
   let getopt_for_consumption = getopt {
      arguments: vec!["a".to_string(), "b".to_string()],
      options: HashMap::new(),
      option_has_arg: HashMap::new(),
   };

   for arg in getopt_for_consumption {
      println!("Zkonzumovany argument: {}", arg);
   }

   /* ERROR: Structure gets consumed by for loop, we can't do second iteration

   for arg2 in getopt_for_consumption {
       println!("Zkonzumovany argument: {}", arg2);
   }
   */
}

#[test]
fn for_loop_wont_consume_immutable_ref() {
   let my_getopt = getopt {
      arguments: vec!["param1".to_string(), "file.txt".to_string(), "verbose".to_string()],
      options: HashMap::new(),
      option_has_arg: HashMap::new(),
   };

   // for loop over immutable reference to struct won't consume
   for arg in &my_getopt {
      println!("immutable ref: {}", arg);
   }
   // we can iterate second time
   for arg2 in &my_getopt {
      println!("immutable ref: {}", arg2);
   }

   // check if original my_getopt arguments still exists
   assert_eq!(my_getopt.arguments.len(), 3);
   assert_eq!(my_getopt.len(), 3);
}

#[test]
fn check_if_into_iter_returns_expected_values() {
   let my_getopt = getopt {
      arguments: vec!["param1".to_string(), "file.txt".to_string(), "verbose".to_string()],
      options: HashMap::new(),
      option_has_arg: HashMap::new(),
   };
   let cloned_args = my_getopt.arguments.clone();
   let mut expected = cloned_args.into_iter();
   let mut iterator = my_getopt.into_iter();

   loop {
      let next_item = iterator.next();
      let next_expected_item = expected.next();

      // Use assert_eq! to check if both iterators returned same values
      assert_eq!(next_item, next_expected_item, "Iterator returned unexpected value.");

      // If both values are None, iteration ended
      if next_item.is_none() {
         break;
      }
   }
}

#[test]
fn check_if_iter_returns_expected_values() {
   let my_getopt = getopt {
      arguments: vec!["param1".to_string(), "file.txt".to_string(), "verbose".to_string()],
      options: HashMap::new(),
      option_has_arg: HashMap::new(),
   };
   let cloned_args = my_getopt.arguments.clone();
   let mut expected = cloned_args.iter();
   let mut iterator = my_getopt.iter();

   loop {
      let next_item = iterator.next();
      let next_expected_item = expected.next();

      // Use assert_eq! to check if both iterators returned same values
      assert_eq!(next_item, next_expected_item, "Iterator returned unexpected value.");

      // If both values are None, iteration ended
      if next_item.is_none() {
         break;
      }
   }
}