feat(config): complete config module with unit tests

Swadhin Biswas committed 7 months ago
9b09809
1 parent 83eef77
commit 9b09809
1 file changed
+93 -0
Showing 1 changed file+93-0
▼src/config/mod.rs
+93-0
558
558
assert!(config.show_memory);
559
559
assert!(!config.show_disk);
560
560
assert!(!config.show_battery);
561
+ assert!(!config.show_song);
562
+ assert!(!config.show_users);
563
+ assert!(!config.memory_bar);
564
+ assert!(!config.disk_bar);
565
+ assert_eq!(config.bar_width, 15);
566
+ assert_eq!(config.gap, 3);
567
+ assert_eq!(config.image_backend, "ascii");
568
+ assert_eq!(config.block_range, (0, 15));
569
+ }
570
+
571
+ #[test]
572
+ fn test_color_config_distro() {
573
+ let cc = ColorConfig::Auto("distro".to_string());
574
+ assert!(cc.is_distro());
575
+ }
576
+
577
+ #[test]
578
+ fn test_color_config_numbers() {
579
+ let cc = ColorConfig::Numbers(vec![1, 2, 3]);
580
+ assert!(!cc.is_distro());
581
+ }
582
+
583
+ #[test]
584
+ fn test_config_parse_minimal_toml() {
585
+ let toml_str = r#"
586
+ no_color = true
587
+ separator = " => "
588
+ "#;
589
+ let config: Config = toml::from_str(toml_str).unwrap();
590
+ assert!(config.no_color);
591
+ assert_eq!(config.separator, " => ");
592
+ // Defaults should fill in
593
+ assert!(config.show_title);
594
+ assert_eq!(config.gap, 3);
595
+ }
596
+
597
+ #[test]
598
+ fn test_config_parse_colors_distro() {
599
+ let toml_str = r#"colors = "distro""#;
600
+ let config: Config = toml::from_str(toml_str).unwrap();
601
+ assert!(config.colors.is_distro());
602
+ }
603
+
604
+ #[test]
605
+ fn test_config_parse_colors_numbers() {
606
+ let toml_str = r#"colors = [4, 6, 1, 8]"#;
607
+ let config: Config = toml::from_str(toml_str).unwrap();
608
+ match config.colors {
609
+ ColorConfig::Numbers(nums) => assert_eq!(nums, vec![4, 6, 1, 8]),
610
+ _ => panic!("Expected Numbers variant"),
611
+ }
612
+ }
613
+
614
+ #[test]
615
+ fn test_config_parse_bar_settings() {
616
+ let toml_str = r#"
617
+ memory_bar = true
618
+ disk_bar = true
619
+ bar_width = 20
620
+ "#;
621
+ let config: Config = toml::from_str(toml_str).unwrap();
622
+ assert!(config.memory_bar);
623
+ assert!(config.disk_bar);
624
+ assert_eq!(config.bar_width, 20);
625
+ }
626
+
627
+ #[test]
628
+ fn test_config_parse_song_users() {
629
+ let toml_str = r#"
630
+ show_song = true
631
+ show_users = true
632
+ "#;
633
+ let config: Config = toml::from_str(toml_str).unwrap();
634
+ assert!(config.show_song);
635
+ assert!(config.show_users);
636
+ }
637
+
638
+ #[test]
639
+ fn test_example_config_is_valid_toml() {
640
+ let result = toml::from_str::<Config>(EXAMPLE_CONFIG);
641
+ assert!(result.is_ok(), "EXAMPLE_CONFIG is not valid: {:?}", result.err());
642
+ }
643
+
644
+ #[test]
645
+ fn test_config_serialize_roundtrip() {
646
+ let config = Config::default();
647
+ let toml_str = toml::to_string_pretty(&config).unwrap();
648
+ let parsed: Config = toml::from_str(&toml_str).unwrap();
649
+ assert_eq!(parsed.separator, config.separator);
650
+ assert_eq!(parsed.gap, config.gap);
651
+ assert_eq!(parsed.no_color, config.no_color);
652
+ }
653
+}
561
654