Skip to content

Commit 9fa00c7

Browse files
committed
Refactor tests to use matches!
- Fix cargo clippy warnings to use matches! and .is_ok() in tests
1 parent 544a6ba commit 9fa00c7

File tree

2 files changed

+74
-134
lines changed

2 files changed

+74
-134
lines changed

src/ser.rs

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,7 @@ mod tests {
601601

602602
#[test]
603603
fn test_serialize_bool() {
604-
assert!(match to_vec(&true) {
605-
Err(Error::UnsupportedType) => true,
606-
_ => false,
607-
});
604+
assert!(matches!(to_vec(&true), Err(Error::UnsupportedType)));
608605
}
609606

610607
#[test]
@@ -686,19 +683,13 @@ mod tests {
686683
#[test]
687684
fn test_serialize_f32() {
688685
let value: f32 = 2.0;
689-
assert!(match to_vec(&value) {
690-
Err(Error::UnsupportedType) => true,
691-
_ => false,
692-
});
686+
assert!(matches!(to_vec(&value), Err(Error::UnsupportedType)));
693687
}
694688

695689
#[test]
696690
fn test_serialize_f64() {
697691
let value: f64 = 2.0;
698-
assert!(match to_vec(&value) {
699-
Err(Error::UnsupportedType) => true,
700-
_ => false,
701-
});
692+
assert!(matches!(to_vec(&value), Err(Error::UnsupportedType)));
702693
}
703694

704695
#[test]
@@ -731,19 +722,13 @@ mod tests {
731722
#[test]
732723
fn test_serialize_unit() {
733724
let value = ();
734-
assert!(match to_vec(&value) {
735-
Err(Error::UnsupportedType) => true,
736-
_ => false,
737-
});
725+
assert!(matches!(to_vec(&value), Err(Error::UnsupportedType)));
738726
}
739727

740728
#[test]
741729
fn test_serialize_none() {
742730
let value: Option<i64> = None;
743-
assert!(match to_vec(&value) {
744-
Err(Error::UnsupportedType) => true,
745-
_ => false,
746-
});
731+
assert!(matches!(to_vec(&value), Err(Error::UnsupportedType)));
747732
}
748733

749734
#[test]
@@ -757,38 +742,31 @@ mod tests {
757742
use serde::Serializer;
758743

759744
let mut writer = Vec::new();
760-
assert!(
761-
match super::Serializer::new(&mut writer).serialize_unit_struct("Nothing") {
762-
Err(Error::UnsupportedType) => true,
763-
_ => false,
764-
}
765-
);
745+
assert!(matches!(
746+
super::Serializer::new(&mut writer).serialize_unit_struct("Nothing"),
747+
Err(Error::UnsupportedType)
748+
));
766749
}
767750

768751
#[test]
769752
fn test_serialize_unit_variant() {
770753
use serde::Serializer;
771754

772755
let mut writer = Vec::new();
773-
assert!(match super::Serializer::new(&mut writer)
774-
.serialize_unit_variant("Nothing", 0, "Case")
775-
{
776-
Err(Error::UnsupportedType) => true,
777-
_ => false,
778-
});
756+
assert!(matches!(
757+
super::Serializer::new(&mut writer).serialize_unit_variant("Nothing", 0, "Case"),
758+
Err(Error::UnsupportedType)
759+
));
779760
}
780761

781762
#[test]
782763
fn test_serialize_newtype_struct() {
783764
use serde::Serializer;
784765

785766
let mut writer = Vec::new();
786-
assert!(
787-
match super::Serializer::new(&mut writer).serialize_newtype_struct("Nothing", &2) {
788-
Err(_) => false,
789-
Ok(_) => true,
790-
}
791-
);
767+
assert!(super::Serializer::new(&mut writer)
768+
.serialize_newtype_struct("Nothing", &2)
769+
.is_ok());
792770
assert_eq!(String::from_utf8(writer).unwrap(), "i2e");
793771
}
794772

@@ -797,12 +775,10 @@ mod tests {
797775
use serde::Serializer;
798776

799777
let mut writer = Vec::new();
800-
assert!(match super::Serializer::new(&mut writer)
801-
.serialize_unit_variant("Nothing", 0, "Case")
802-
{
803-
Err(Error::UnsupportedType) => true,
804-
_ => false,
805-
});
778+
assert!(matches!(
779+
super::Serializer::new(&mut writer).serialize_unit_variant("Nothing", 0, "Case"),
780+
Err(Error::UnsupportedType)
781+
));
806782
}
807783

808784
#[test]
@@ -825,61 +801,53 @@ mod tests {
825801
use serde::Serializer;
826802

827803
let mut writer = Vec::new();
828-
assert!(
829-
match super::Serializer::new(&mut writer).serialize_tuple(0) {
830-
Err(Error::UnsupportedType) => true,
831-
_ => false,
832-
}
833-
);
804+
assert!(matches!(
805+
super::Serializer::new(&mut writer).serialize_tuple(0),
806+
Err(Error::UnsupportedType)
807+
));
834808
}
835809

836810
#[test]
837811
fn test_serialize_tuple_struct() {
838812
use serde::Serializer;
839813

840814
let mut writer = Vec::new();
841-
assert!(
842-
match super::Serializer::new(&mut writer).serialize_tuple_struct("Tuple Struct", 2) {
843-
Err(Error::UnsupportedType) => true,
844-
_ => false,
845-
}
846-
);
815+
assert!(matches!(
816+
super::Serializer::new(&mut writer).serialize_tuple_struct("Tuple Struct", 2),
817+
Err(Error::UnsupportedType)
818+
));
847819
}
848820

849821
#[test]
850822
fn test_serialize_tuple_variant() {
851823
use serde::Serializer;
852824

853825
let mut writer = Vec::new();
854-
assert!(
855-
match super::Serializer::new(&mut writer).serialize_tuple_variant(
826+
assert!(matches!(
827+
super::Serializer::new(&mut writer).serialize_tuple_variant(
856828
"Tuple Variant",
857829
2,
858830
"Case",
859831
1
860-
) {
861-
Err(Error::UnsupportedType) => true,
862-
_ => false,
863-
}
864-
);
832+
),
833+
Err(Error::UnsupportedType)
834+
));
865835
}
866836

867837
#[test]
868838
fn test_serialize_struct_variant() {
869839
use serde::Serializer;
870840

871841
let mut writer = Vec::new();
872-
assert!(
873-
match super::Serializer::new(&mut writer).serialize_struct_variant(
842+
assert!(matches!(
843+
super::Serializer::new(&mut writer).serialize_struct_variant(
874844
"Struct Variant",
875845
2,
876846
"Case",
877847
1
878-
) {
879-
Err(Error::UnsupportedType) => true,
880-
_ => false,
881-
}
882-
);
848+
),
849+
Err(Error::UnsupportedType)
850+
));
883851
}
884852

885853
#[test]

src/value/ser.rs

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,7 @@ mod tests {
485485

486486
#[test]
487487
fn test_serialize_bool() {
488-
assert!(match to_value(&true) {
489-
Err(Error::UnsupportedType) => true,
490-
_ => false,
491-
});
488+
assert!(matches!(to_value(&true), Err(Error::UnsupportedType)));
492489
}
493490

494491
#[test]
@@ -573,19 +570,13 @@ mod tests {
573570
#[test]
574571
fn test_serialize_f32() {
575572
let value: f32 = 2.0;
576-
assert!(match to_value(&value) {
577-
Err(Error::UnsupportedType) => true,
578-
_ => false,
579-
});
573+
assert!(matches!(to_value(&value), Err(Error::UnsupportedType)));
580574
}
581575

582576
#[test]
583577
fn test_serialize_f64() {
584578
let value: f64 = 2.0;
585-
assert!(match to_value(&value) {
586-
Err(Error::UnsupportedType) => true,
587-
_ => false,
588-
});
579+
assert!(matches!(to_value(&value), Err(Error::UnsupportedType)));
589580
}
590581

591582
#[test]
@@ -624,19 +615,13 @@ mod tests {
624615
#[test]
625616
fn test_serialize_unit() {
626617
let value = ();
627-
assert!(match to_value(&value) {
628-
Err(Error::UnsupportedType) => true,
629-
_ => false,
630-
});
618+
assert!(matches!(to_value(&value), Err(Error::UnsupportedType)));
631619
}
632620

633621
#[test]
634622
fn test_serialize_none() {
635623
let value: Option<i64> = None;
636-
assert!(match to_value(&value) {
637-
Err(Error::UnsupportedType) => true,
638-
_ => false,
639-
});
624+
assert!(matches!(to_value(&value), Err(Error::UnsupportedType)));
640625
}
641626

642627
#[test]
@@ -649,46 +634,39 @@ mod tests {
649634
fn test_serialize_unit_struct() {
650635
use serde::Serializer;
651636

652-
assert!(match super::Serializer.serialize_unit_struct("Nothing") {
653-
Err(Error::UnsupportedType) => true,
654-
_ => false,
655-
});
637+
assert!(matches!(
638+
super::Serializer.serialize_unit_struct("Nothing"),
639+
Err(Error::UnsupportedType)
640+
));
656641
}
657642

658643
#[test]
659644
fn test_serialize_unit_variant() {
660645
use serde::Serializer;
661646

662-
assert!(
663-
match super::Serializer.serialize_unit_variant("Nothing", 0, "Case") {
664-
Err(Error::UnsupportedType) => true,
665-
_ => false,
666-
}
667-
);
647+
assert!(matches!(
648+
super::Serializer.serialize_unit_variant("Nothing", 0, "Case"),
649+
Err(Error::UnsupportedType)
650+
));
668651
}
669652

670653
#[test]
671654
fn test_serialize_newtype_struct() {
672655
use serde::Serializer;
673656

674-
assert!(
675-
match super::Serializer.serialize_newtype_struct("Nothing", &2) {
676-
Err(_) => false,
677-
Ok(_) => true,
678-
}
679-
);
657+
assert!(super::Serializer
658+
.serialize_newtype_struct("Nothing", &2)
659+
.is_ok());
680660
}
681661

682662
#[test]
683663
fn test_serialize_newtype_variant() {
684664
use serde::Serializer;
685665

686-
assert!(
687-
match super::Serializer.serialize_unit_variant("Nothing", 0, "Case") {
688-
Err(Error::UnsupportedType) => true,
689-
_ => false,
690-
}
691-
);
666+
assert!(matches!(
667+
super::Serializer.serialize_unit_variant("Nothing", 0, "Case"),
668+
Err(Error::UnsupportedType)
669+
));
692670
}
693671

694672
#[test]
@@ -714,46 +692,40 @@ mod tests {
714692
fn test_serialize_tuple() {
715693
use serde::Serializer;
716694

717-
assert!(match super::Serializer.serialize_tuple(0) {
718-
Err(Error::UnsupportedType) => true,
719-
_ => false,
720-
});
695+
assert!(matches!(
696+
super::Serializer.serialize_tuple(0),
697+
Err(Error::UnsupportedType)
698+
));
721699
}
722700

723701
#[test]
724702
fn test_serialize_tuple_struct() {
725703
use serde::Serializer;
726704

727-
assert!(
728-
match super::Serializer.serialize_tuple_struct("Tuple Struct", 2) {
729-
Err(Error::UnsupportedType) => true,
730-
_ => false,
731-
}
732-
);
705+
assert!(matches!(
706+
super::Serializer.serialize_tuple_struct("Tuple Struct", 2),
707+
Err(Error::UnsupportedType)
708+
));
733709
}
734710

735711
#[test]
736712
fn test_serialize_tuple_variant() {
737713
use serde::Serializer;
738714

739-
assert!(
740-
match super::Serializer.serialize_tuple_variant("Tuple Variant", 2, "Case", 1) {
741-
Err(Error::UnsupportedType) => true,
742-
_ => false,
743-
}
744-
);
715+
assert!(matches!(
716+
super::Serializer.serialize_tuple_variant("Tuple Variant", 2, "Case", 1),
717+
Err(Error::UnsupportedType)
718+
));
745719
}
746720

747721
#[test]
748722
fn test_serialize_struct_variant() {
749723
use serde::Serializer;
750724

751-
assert!(
752-
match super::Serializer.serialize_struct_variant("Struct Variant", 2, "Case", 1) {
753-
Err(Error::UnsupportedType) => true,
754-
_ => false,
755-
}
756-
);
725+
assert!(matches!(
726+
super::Serializer.serialize_struct_variant("Struct Variant", 2, "Case", 1),
727+
Err(Error::UnsupportedType)
728+
));
757729
}
758730

759731
#[test]

0 commit comments

Comments
 (0)