@@ -765,3 +765,141 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
765
765
_ => Err ( format_err ! ( "Invalid value for `--emit`" ) ) ,
766
766
}
767
767
}
768
+
769
+ #[ cfg( test) ]
770
+ #[ allow( dead_code) ]
771
+ mod test {
772
+ use super :: * ;
773
+ use rustfmt_config_proc_macro:: nightly_only_test;
774
+
775
+ fn get_config < O : CliOptions > ( path : Option < & Path > , options : Option < O > ) -> Config {
776
+ load_config ( path, options) . unwrap ( ) . 0
777
+ }
778
+
779
+ #[ nightly_only_test]
780
+ #[ test]
781
+ fn flag_sets_style_edition_override_correctly ( ) {
782
+ let mut options = GetOptsOptions :: default ( ) ;
783
+ options. style_edition = Some ( StyleEdition :: Edition2024 ) ;
784
+ let config = get_config ( None , Some ( options) ) ;
785
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
786
+ }
787
+
788
+ #[ nightly_only_test]
789
+ #[ test]
790
+ fn edition_sets_style_edition_override_correctly ( ) {
791
+ let mut options = GetOptsOptions :: default ( ) ;
792
+ options. edition = Some ( Edition :: Edition2024 ) ;
793
+ let config = get_config ( None , Some ( options) ) ;
794
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
795
+ }
796
+
797
+ #[ nightly_only_test]
798
+ #[ test]
799
+ fn version_sets_style_edition_override_correctly ( ) {
800
+ let mut options = GetOptsOptions :: default ( ) ;
801
+ options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "Two" . to_owned ( ) ) ] ) ;
802
+ let config = get_config ( None , Some ( options) ) ;
803
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
804
+ }
805
+
806
+ #[ nightly_only_test]
807
+ #[ test]
808
+ fn style_edition_flag_has_correct_precedence_over_edition ( ) {
809
+ let mut options = GetOptsOptions :: default ( ) ;
810
+ options. style_edition = Some ( StyleEdition :: Edition2021 ) ;
811
+ options. edition = Some ( Edition :: Edition2024 ) ;
812
+ let config = get_config ( None , Some ( options) ) ;
813
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
814
+ }
815
+
816
+ #[ nightly_only_test]
817
+ #[ test]
818
+ fn style_edition_flag_has_correct_precedence_over_version ( ) {
819
+ let mut options = GetOptsOptions :: default ( ) ;
820
+ options. style_edition = Some ( StyleEdition :: Edition2018 ) ;
821
+ options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "Two" . to_owned ( ) ) ] ) ;
822
+ let config = get_config ( None , Some ( options) ) ;
823
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2018 ) ;
824
+ }
825
+
826
+ #[ nightly_only_test]
827
+ #[ test]
828
+ fn style_edition_flag_has_correct_precedence_over_edition_version ( ) {
829
+ let mut options = GetOptsOptions :: default ( ) ;
830
+ options. style_edition = Some ( StyleEdition :: Edition2021 ) ;
831
+ options. edition = Some ( Edition :: Edition2018 ) ;
832
+ options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "Two" . to_owned ( ) ) ] ) ;
833
+ let config = get_config ( None , Some ( options) ) ;
834
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
835
+ }
836
+
837
+ #[ nightly_only_test]
838
+ #[ test]
839
+ fn style_edition_inline_has_correct_precedence_over_edition_version ( ) {
840
+ let mut options = GetOptsOptions :: default ( ) ;
841
+ options. edition = Some ( Edition :: Edition2018 ) ;
842
+ options. inline_config = HashMap :: from ( [
843
+ ( "version" . to_owned ( ) , "One" . to_owned ( ) ) ,
844
+ ( "style_edition" . to_owned ( ) , "2024" . to_owned ( ) ) ,
845
+ ] ) ;
846
+ let config = get_config ( None , Some ( options) ) ;
847
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
848
+ }
849
+
850
+ #[ nightly_only_test]
851
+ #[ test]
852
+ fn style_edition_config_file_trumps_edition_flag_version_inline ( ) {
853
+ let mut options = GetOptsOptions :: default ( ) ;
854
+ let config_file = Some ( Path :: new ( "tests/config/style-edition/just-style-edition" ) ) ;
855
+ options. edition = Some ( Edition :: Edition2018 ) ;
856
+ options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "One" . to_owned ( ) ) ] ) ;
857
+ let config = get_config ( config_file, Some ( options) ) ;
858
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
859
+ }
860
+
861
+ #[ nightly_only_test]
862
+ #[ test]
863
+ fn style_edition_config_file_trumps_edition_config_and_version_inline ( ) {
864
+ let mut options = GetOptsOptions :: default ( ) ;
865
+ let config_file = Some ( Path :: new (
866
+ "tests/config/style-edition/style-edition-and-edition" ,
867
+ ) ) ;
868
+ options. inline_config = HashMap :: from ( [ ( "version" . to_owned ( ) , "Two" . to_owned ( ) ) ] ) ;
869
+ let config = get_config ( config_file, Some ( options) ) ;
870
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
871
+ assert_eq ! ( config. edition( ) , Edition :: Edition2024 ) ;
872
+ }
873
+
874
+ #[ nightly_only_test]
875
+ #[ test]
876
+ fn version_config_trumps_edition_config_and_flag ( ) {
877
+ let mut options = GetOptsOptions :: default ( ) ;
878
+ let config_file = Some ( Path :: new ( "tests/config/style-edition/version-edition" ) ) ;
879
+ options. edition = Some ( Edition :: Edition2018 ) ;
880
+ let config = get_config ( config_file, Some ( options) ) ;
881
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2024 ) ;
882
+ }
883
+
884
+ #[ nightly_only_test]
885
+ #[ test]
886
+ fn style_edition_config_file_trumps_version_config ( ) {
887
+ let options = GetOptsOptions :: default ( ) ;
888
+ let config_file = Some ( Path :: new (
889
+ "tests/config/style-edition/version-style-edition" ,
890
+ ) ) ;
891
+ let config = get_config ( config_file, Some ( options) ) ;
892
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
893
+ }
894
+
895
+ #[ nightly_only_test]
896
+ #[ test]
897
+ fn style_edition_config_file_trumps_edition_version_config ( ) {
898
+ let options = GetOptsOptions :: default ( ) ;
899
+ let config_file = Some ( Path :: new (
900
+ "tests/config/style-edition/version-style-edition-and-edition" ,
901
+ ) ) ;
902
+ let config = get_config ( config_file, Some ( options) ) ;
903
+ assert_eq ! ( config. style_edition( ) , StyleEdition :: Edition2021 ) ;
904
+ }
905
+ }
0 commit comments