Skip to content

Commit 6f82ea9

Browse files
tests: augment heuristics tests
1 parent b9db337 commit 6f82ea9

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
@@ -681,4 +681,254 @@ ignore = []
681681
assert_eq!(config.unstable_features(), true);
682682
}
683683
}
684+
685+
#[cfg(test)]
686+
mod width_heuristics {
687+
use super::*;
688+
689+
#[test]
690+
fn test_scaled_sets_correct_widths() {
691+
let toml = r#"
692+
width_heuristics = "Scaled"
693+
max_width = 200
694+
"#;
695+
let config = Config::from_toml(toml, Path::new("")).unwrap();
696+
assert_eq!(config.array_width(), 120);
697+
assert_eq!(config.attr_fn_like_width(), 140);
698+
assert_eq!(config.chain_width(), 120);
699+
assert_eq!(config.fn_call_width(), 120);
700+
assert_eq!(config.single_line_if_else_max_width(), 100);
701+
assert_eq!(config.struct_lit_width(), 36);
702+
assert_eq!(config.struct_variant_width(), 70);
703+
}
704+
705+
#[test]
706+
fn test_max_sets_correct_widths() {
707+
let toml = r#"
708+
width_heuristics = "Max"
709+
max_width = 120
710+
"#;
711+
let config = Config::from_toml(toml, Path::new("")).unwrap();
712+
assert_eq!(config.array_width(), 120);
713+
assert_eq!(config.attr_fn_like_width(), 120);
714+
assert_eq!(config.chain_width(), 120);
715+
assert_eq!(config.fn_call_width(), 120);
716+
assert_eq!(config.single_line_if_else_max_width(), 120);
717+
assert_eq!(config.struct_lit_width(), 120);
718+
assert_eq!(config.struct_variant_width(), 120);
719+
}
720+
721+
#[test]
722+
fn test_off_sets_correct_widths() {
723+
let toml = r#"
724+
width_heuristics = "Off"
725+
max_width = 100
726+
"#;
727+
let config = Config::from_toml(toml, Path::new("")).unwrap();
728+
assert_eq!(config.array_width(), usize::max_value());
729+
assert_eq!(config.attr_fn_like_width(), usize::max_value());
730+
assert_eq!(config.chain_width(), usize::max_value());
731+
assert_eq!(config.fn_call_width(), usize::max_value());
732+
assert_eq!(config.single_line_if_else_max_width(), 0);
733+
assert_eq!(config.struct_lit_width(), 0);
734+
assert_eq!(config.struct_variant_width(), 0);
735+
}
736+
737+
#[test]
738+
fn test_override_works_with_scaled() {
739+
let toml = r#"
740+
width_heuristics = "Scaled"
741+
array_width = 20
742+
attr_fn_like_width = 40
743+
chain_width = 20
744+
fn_call_width = 90
745+
single_line_if_else_max_width = 40
746+
struct_lit_width = 30
747+
struct_variant_width = 34
748+
"#;
749+
let config = Config::from_toml(toml, Path::new("")).unwrap();
750+
assert_eq!(config.array_width(), 20);
751+
assert_eq!(config.attr_fn_like_width(), 40);
752+
assert_eq!(config.chain_width(), 20);
753+
assert_eq!(config.fn_call_width(), 90);
754+
assert_eq!(config.single_line_if_else_max_width(), 40);
755+
assert_eq!(config.struct_lit_width(), 30);
756+
assert_eq!(config.struct_variant_width(), 34);
757+
}
758+
759+
#[test]
760+
fn test_override_with_max() {
761+
let toml = r#"
762+
width_heuristics = "Max"
763+
array_width = 20
764+
attr_fn_like_width = 40
765+
chain_width = 20
766+
fn_call_width = 90
767+
single_line_if_else_max_width = 40
768+
struct_lit_width = 30
769+
struct_variant_width = 34
770+
"#;
771+
let config = Config::from_toml(toml, Path::new("")).unwrap();
772+
assert_eq!(config.array_width(), 20);
773+
assert_eq!(config.attr_fn_like_width(), 40);
774+
assert_eq!(config.chain_width(), 20);
775+
assert_eq!(config.fn_call_width(), 90);
776+
assert_eq!(config.single_line_if_else_max_width(), 40);
777+
assert_eq!(config.struct_lit_width(), 30);
778+
assert_eq!(config.struct_variant_width(), 34);
779+
}
780+
781+
#[test]
782+
fn test_override_with_off() {
783+
let toml = r#"
784+
width_heuristics = "Off"
785+
array_width = 20
786+
attr_fn_like_width = 40
787+
chain_width = 20
788+
fn_call_width = 90
789+
single_line_if_else_max_width = 40
790+
struct_lit_width = 30
791+
struct_variant_width = 34
792+
"#;
793+
let config = Config::from_toml(toml, Path::new("")).unwrap();
794+
assert_eq!(config.array_width(), 20);
795+
assert_eq!(config.attr_fn_like_width(), 40);
796+
assert_eq!(config.chain_width(), 20);
797+
assert_eq!(config.fn_call_width(), 90);
798+
assert_eq!(config.single_line_if_else_max_width(), 40);
799+
assert_eq!(config.struct_lit_width(), 30);
800+
assert_eq!(config.struct_variant_width(), 34);
801+
}
802+
803+
#[test]
804+
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
805+
fn test_panics_when_fn_call_width_config_exceeds_max_width() {
806+
let toml = r#"
807+
max_width = 80
808+
fn_call_width = 90
809+
"#;
810+
Config::from_toml(toml, Path::new("")).unwrap();
811+
}
812+
813+
#[test]
814+
#[should_panic(
815+
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
816+
)]
817+
fn test_panics_when_attr_fn_like_width_config_exceeds_max_width() {
818+
let toml = r#"
819+
max_width = 80
820+
attr_fn_like_width = 90
821+
"#;
822+
Config::from_toml(toml, Path::new("")).unwrap();
823+
}
824+
825+
#[test]
826+
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
827+
fn test_panics_when_struct_lit_config_exceeds_max_width() {
828+
let toml = r#"
829+
max_width = 80
830+
struct_lit_width = 90
831+
"#;
832+
Config::from_toml(toml, Path::new("")).unwrap();
833+
}
834+
835+
#[test]
836+
#[should_panic(
837+
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
838+
)]
839+
fn test_panics_when_struct_variant_width_config_exceeds_max_width() {
840+
let toml = r#"
841+
max_width = 80
842+
struct_variant_width = 90
843+
"#;
844+
Config::from_toml(toml, Path::new("")).unwrap();
845+
}
846+
847+
#[test]
848+
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
849+
fn test_panics_when_array_width_config_exceeds_max_width() {
850+
let toml = r#"
851+
max_width = 80
852+
array_width = 90
853+
"#;
854+
Config::from_toml(toml, Path::new("")).unwrap();
855+
}
856+
857+
#[test]
858+
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
859+
fn test_panics_when_chain_width_config_exceeds_max_width() {
860+
let toml = r#"
861+
max_width = 80
862+
chain_width = 90
863+
"#;
864+
Config::from_toml(toml, Path::new("")).unwrap();
865+
}
866+
867+
#[test]
868+
#[should_panic(
869+
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
870+
)]
871+
fn test_panics_when_single_line_if_else_max_width_config_exceeds_max_width() {
872+
let toml = r#"
873+
max_width = 80
874+
single_line_if_else_max_width = 90
875+
"#;
876+
Config::from_toml(toml, Path::new("")).unwrap();
877+
}
878+
879+
#[test]
880+
#[should_panic(expected = "`fn_call_width` cannot have a value that exceeds `max_width")]
881+
fn test_panics_when_fn_call_width_override_exceeds_max_width() {
882+
let mut config = Config::default();
883+
config.override_value("fn_call_width", "101");
884+
}
885+
886+
#[test]
887+
#[should_panic(
888+
expected = "`attr_fn_like_width` cannot have a value that exceeds `max_width"
889+
)]
890+
fn test_panics_when_attr_fn_like_width_override_exceeds_max_width() {
891+
let mut config = Config::default();
892+
config.override_value("attr_fn_like_width", "101");
893+
}
894+
895+
#[test]
896+
#[should_panic(expected = "`struct_lit_width` cannot have a value that exceeds `max_width")]
897+
fn test_panics_when_struct_lit_override_exceeds_max_width() {
898+
let mut config = Config::default();
899+
config.override_value("struct_lit_width", "101");
900+
}
901+
902+
#[test]
903+
#[should_panic(
904+
expected = "`struct_variant_width` cannot have a value that exceeds `max_width"
905+
)]
906+
fn test_panics_when_struct_variant_width_override_exceeds_max_width() {
907+
let mut config = Config::default();
908+
config.override_value("struct_variant_width", "101");
909+
}
910+
911+
#[test]
912+
#[should_panic(expected = "`array_width` cannot have a value that exceeds `max_width")]
913+
fn test_panics_when_array_width_override_exceeds_max_width() {
914+
let mut config = Config::default();
915+
config.override_value("array_width", "101");
916+
}
917+
918+
#[test]
919+
#[should_panic(expected = "`chain_width` cannot have a value that exceeds `max_width")]
920+
fn test_panics_when_chain_width_override_exceeds_max_width() {
921+
let mut config = Config::default();
922+
config.override_value("chain_width", "101");
923+
}
924+
925+
#[test]
926+
#[should_panic(
927+
expected = "`single_line_if_else_max_width` cannot have a value that exceeds `max_width"
928+
)]
929+
fn test_panics_when_single_line_if_else_max_width_override_exceeds_max_width() {
930+
let mut config = Config::default();
931+
config.override_value("single_line_if_else_max_width", "101");
932+
}
933+
}
684934
}
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)