Skip to content

Commit bdce45b

Browse files
tests: augment heuristics tests
1 parent b7bfb37 commit bdce45b

File tree

5 files changed

+351
-0
lines changed

5 files changed

+351
-0
lines changed

rustfmt-core/rustfmt-config/src/lib.rs

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,254 @@ ignore = []
618618
// assert_eq!(config.unstable_features(), true);
619619
// ::std::env::set_var("CFG_RELEASE_CHANNEL", v);
620620
// }
621+
622+
#[cfg(test)]
623+
mod width_heuristics {
624+
use super::*;
625+
626+
#[test]
627+
fn test_scaled_sets_correct_widths() {
628+
let toml = r#"
629+
width_heuristics = "Scaled"
630+
max_width = 200
631+
"#;
632+
let config = Config::from_toml(toml, Path::new("")).unwrap();
633+
assert_eq!(config.array_width(), 120);
634+
assert_eq!(config.attr_fn_like_width(), 140);
635+
assert_eq!(config.chain_width(), 120);
636+
assert_eq!(config.fn_call_width(), 120);
637+
assert_eq!(config.single_line_if_else_max_width(), 100);
638+
assert_eq!(config.struct_lit_width(), 36);
639+
assert_eq!(config.struct_variant_width(), 70);
640+
}
641+
642+
#[test]
643+
fn test_max_sets_correct_widths() {
644+
let toml = r#"
645+
width_heuristics = "Max"
646+
max_width = 120
647+
"#;
648+
let config = Config::from_toml(toml, Path::new("")).unwrap();
649+
assert_eq!(config.array_width(), 120);
650+
assert_eq!(config.attr_fn_like_width(), 120);
651+
assert_eq!(config.chain_width(), 120);
652+
assert_eq!(config.fn_call_width(), 120);
653+
assert_eq!(config.single_line_if_else_max_width(), 120);
654+
assert_eq!(config.struct_lit_width(), 120);
655+
assert_eq!(config.struct_variant_width(), 120);
656+
}
657+
658+
#[test]
659+
fn test_off_sets_correct_widths() {
660+
let toml = r#"
661+
width_heuristics = "Off"
662+
max_width = 100
663+
"#;
664+
let config = Config::from_toml(toml, Path::new("")).unwrap();
665+
assert_eq!(config.array_width(), usize::max_value());
666+
assert_eq!(config.attr_fn_like_width(), usize::max_value());
667+
assert_eq!(config.chain_width(), usize::max_value());
668+
assert_eq!(config.fn_call_width(), usize::max_value());
669+
assert_eq!(config.single_line_if_else_max_width(), 0);
670+
assert_eq!(config.struct_lit_width(), 0);
671+
assert_eq!(config.struct_variant_width(), 0);
672+
}
673+
674+
#[test]
675+
fn test_override_works_with_scaled() {
676+
let toml = r#"
677+
width_heuristics = "Scaled"
678+
array_width = 20
679+
attr_fn_like_width = 40
680+
chain_width = 20
681+
fn_call_width = 90
682+
single_line_if_else_max_width = 40
683+
struct_lit_width = 30
684+
struct_variant_width = 34
685+
"#;
686+
let config = Config::from_toml(toml, Path::new("")).unwrap();
687+
assert_eq!(config.array_width(), 20);
688+
assert_eq!(config.attr_fn_like_width(), 40);
689+
assert_eq!(config.chain_width(), 20);
690+
assert_eq!(config.fn_call_width(), 90);
691+
assert_eq!(config.single_line_if_else_max_width(), 40);
692+
assert_eq!(config.struct_lit_width(), 30);
693+
assert_eq!(config.struct_variant_width(), 34);
694+
}
695+
696+
#[test]
697+
fn test_override_with_max() {
698+
let toml = r#"
699+
width_heuristics = "Max"
700+
array_width = 20
701+
attr_fn_like_width = 40
702+
chain_width = 20
703+
fn_call_width = 90
704+
single_line_if_else_max_width = 40
705+
struct_lit_width = 30
706+
struct_variant_width = 34
707+
"#;
708+
let config = Config::from_toml(toml, Path::new("")).unwrap();
709+
assert_eq!(config.array_width(), 20);
710+
assert_eq!(config.attr_fn_like_width(), 40);
711+
assert_eq!(config.chain_width(), 20);
712+
assert_eq!(config.fn_call_width(), 90);
713+
assert_eq!(config.single_line_if_else_max_width(), 40);
714+
assert_eq!(config.struct_lit_width(), 30);
715+
assert_eq!(config.struct_variant_width(), 34);
716+
}
717+
718+
#[test]
719+
fn test_override_with_off() {
720+
let toml = r#"
721+
width_heuristics = "Off"
722+
array_width = 20
723+
attr_fn_like_width = 40
724+
chain_width = 20
725+
fn_call_width = 90
726+
single_line_if_else_max_width = 40
727+
struct_lit_width = 30
728+
struct_variant_width = 34
729+
"#;
730+
let config = Config::from_toml(toml, Path::new("")).unwrap();
731+
assert_eq!(config.array_width(), 20);
732+
assert_eq!(config.attr_fn_like_width(), 40);
733+
assert_eq!(config.chain_width(), 20);
734+
assert_eq!(config.fn_call_width(), 90);
735+
assert_eq!(config.single_line_if_else_max_width(), 40);
736+
assert_eq!(config.struct_lit_width(), 30);
737+
assert_eq!(config.struct_variant_width(), 34);
738+
}
739+
740+
#[test]
741+
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
742+
fn test_panics_when_fn_call_width_config_exceeds_max_width() {
743+
let toml = r#"
744+
max_width = 80
745+
fn_call_width = 90
746+
"#;
747+
Config::from_toml(toml, Path::new("")).unwrap();
748+
}
749+
750+
#[test]
751+
#[should_panic(
752+
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
753+
)]
754+
fn test_panics_when_attr_fn_like_width_config_exceeds_max_width() {
755+
let toml = r#"
756+
max_width = 80
757+
attr_fn_like_width = 90
758+
"#;
759+
Config::from_toml(toml, Path::new("")).unwrap();
760+
}
761+
762+
#[test]
763+
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
764+
fn test_panics_when_struct_lit_config_exceeds_max_width() {
765+
let toml = r#"
766+
max_width = 80
767+
struct_lit_width = 90
768+
"#;
769+
Config::from_toml(toml, Path::new("")).unwrap();
770+
}
771+
772+
#[test]
773+
#[should_panic(
774+
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
775+
)]
776+
fn test_panics_when_struct_variant_width_config_exceeds_max_width() {
777+
let toml = r#"
778+
max_width = 80
779+
struct_variant_width = 90
780+
"#;
781+
Config::from_toml(toml, Path::new("")).unwrap();
782+
}
783+
784+
#[test]
785+
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
786+
fn test_panics_when_array_width_config_exceeds_max_width() {
787+
let toml = r#"
788+
max_width = 80
789+
array_width = 90
790+
"#;
791+
Config::from_toml(toml, Path::new("")).unwrap();
792+
}
793+
794+
#[test]
795+
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
796+
fn test_panics_when_chain_width_config_exceeds_max_width() {
797+
let toml = r#"
798+
max_width = 80
799+
chain_width = 90
800+
"#;
801+
Config::from_toml(toml, Path::new("")).unwrap();
802+
}
803+
804+
#[test]
805+
#[should_panic(
806+
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
807+
)]
808+
fn test_panics_when_single_line_if_else_max_width_config_exceeds_max_width() {
809+
let toml = r#"
810+
max_width = 80
811+
single_line_if_else_max_width = 90
812+
"#;
813+
Config::from_toml(toml, Path::new("")).unwrap();
814+
}
815+
816+
#[test]
817+
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
818+
fn test_panics_when_fn_call_width_override_exceeds_max_width() {
819+
let mut config = Config::default();
820+
config.override_value("fn_call_width", "101");
821+
}
822+
823+
#[test]
824+
#[should_panic(
825+
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
826+
)]
827+
fn test_panics_when_attr_fn_like_width_override_exceeds_max_width() {
828+
let mut config = Config::default();
829+
config.override_value("attr_fn_like_width", "101");
830+
}
831+
832+
#[test]
833+
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
834+
fn test_panics_when_struct_lit_override_exceeds_max_width() {
835+
let mut config = Config::default();
836+
config.override_value("struct_lit_width", "101");
837+
}
838+
839+
#[test]
840+
#[should_panic(
841+
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
842+
)]
843+
fn test_panics_when_struct_variant_width_override_exceeds_max_width() {
844+
let mut config = Config::default();
845+
config.override_value("struct_variant_width", "101");
846+
}
847+
848+
#[test]
849+
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
850+
fn test_panics_when_array_width_override_exceeds_max_width() {
851+
let mut config = Config::default();
852+
config.override_value("array_width", "101");
853+
}
854+
855+
#[test]
856+
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
857+
fn test_panics_when_chain_width_override_exceeds_max_width() {
858+
let mut config = Config::default();
859+
config.override_value("chain_width", "101");
860+
}
861+
862+
#[test]
863+
#[should_panic(
864+
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
865+
)]
866+
fn test_panics_when_single_line_if_else_max_width_override_exceeds_max_width() {
867+
let mut config = Config::default();
868+
config.override_value("single_line_if_else_max_width", "101");
869+
}
870+
}
621871
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-width_heuristics: Off
2+
3+
enum Lorem {
4+
Ipsum,
5+
Dolor(bool),
6+
Sit {
7+
amet: Consectetur,
8+
adipiscing: Elit,
9+
},
10+
}
11+
12+
fn main() {
13+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
14+
15+
let lorem = Lorem {
16+
ipsum: dolor,
17+
sit: amet,
18+
};
19+
20+
let lorem = if ipsum {
21+
dolor
22+
} else {
23+
sit
24+
};
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-width_heuristics: Scaled
2+
3+
enum Lorem {
4+
Ipsum,
5+
Dolor(bool),
6+
Sit {
7+
amet: Consectetur,
8+
adipiscing: Elit,
9+
},
10+
}
11+
12+
fn main() {
13+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
14+
15+
let lorem = Lorem {
16+
ipsum: dolor,
17+
sit: amet,
18+
};
19+
20+
let lorem = if ipsum {
21+
dolor
22+
} else {
23+
sit
24+
};
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-width_heuristics: Off
2+
3+
enum Lorem {
4+
Ipsum,
5+
Dolor(bool),
6+
Sit {
7+
amet: Consectetur,
8+
adipiscing: Elit,
9+
},
10+
}
11+
12+
fn main() {
13+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
14+
15+
let lorem = Lorem {
16+
ipsum: dolor,
17+
sit: amet,
18+
};
19+
20+
let lorem = if ipsum {
21+
dolor
22+
} else {
23+
sit
24+
};
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// rustfmt-width_heuristics: Scaled
2+
3+
enum Lorem {
4+
Ipsum,
5+
Dolor(bool),
6+
Sit { amet: Consectetur, adipiscing: Elit },
7+
}
8+
9+
fn main() {
10+
lorem(
11+
"lorem",
12+
"ipsum",
13+
"dolor",
14+
"sit",
15+
"amet",
16+
"consectetur",
17+
"adipiscing",
18+
);
19+
20+
let lorem = Lorem {
21+
ipsum: dolor,
22+
sit: amet,
23+
};
24+
25+
let lorem = if ipsum { dolor } else { sit };
26+
}

0 commit comments

Comments
 (0)