Skip to content

Commit

Permalink
codewide: ditch assert!(matches!(...))
Browse files Browse the repository at this point in the history
As assert_matches!(...) provide superior experience (providing helpful
debugging messages with how the mismatched type looked like), all
assert!(matches!(...)) occurences across the code were changed to
assert_matches!(...).
  • Loading branch information
wprzytula committed May 21, 2024
1 parent 165e8a8 commit 8065379
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 101 deletions.
6 changes: 4 additions & 2 deletions scylla-cql/src/frame/value_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use super::value::{
CqlDate, CqlDuration, CqlTime, CqlTimestamp, LegacyBatchValues, LegacySerializedValues,
MaybeUnset, SerializeValuesError, Unset, Value, ValueList, ValueTooBig,
};
#[cfg(test)]
use assert_matches::assert_matches;
use bytes::BufMut;
use std::collections::hash_map::DefaultHasher;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
Expand Down Expand Up @@ -1262,7 +1264,7 @@ fn serialized_values_value_list() {
ser_values.add_value(&"qwertyuiop").unwrap();

let ser_ser_values: Cow<LegacySerializedValues> = ser_values.serialized().unwrap();
assert!(matches!(ser_ser_values, Cow::Borrowed(_)));
assert_matches!(ser_ser_values, Cow::Borrowed(_));

assert_eq!(&ser_values, ser_ser_values.as_ref());
}
Expand All @@ -1272,7 +1274,7 @@ fn cow_serialized_values_value_list() {
let cow_ser_values: Cow<LegacySerializedValues> = Cow::Owned(LegacySerializedValues::new());

let serialized: Cow<LegacySerializedValues> = cow_ser_values.serialized().unwrap();
assert!(matches!(serialized, Cow::Borrowed(_)));
assert_matches!(serialized, Cow::Borrowed(_));

assert_eq!(cow_ser_values.as_ref(), serialized.as_ref());
}
Expand Down
43 changes: 19 additions & 24 deletions scylla-cql/src/types/serialize/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ mod tests {
};

use super::SerializedValues;
use assert_matches::assert_matches;
use scylla_macros::SerializeRow;

fn col_spec(name: &str, typ: ColumnType) -> ColumnSpec {
Expand Down Expand Up @@ -1044,13 +1045,13 @@ mod tests {
let err = do_serialize_err(v, &spec);
let err = get_typeck_err(&err);
assert_eq!(err.rust_name, std::any::type_name::<()>());
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::WrongColumnCount {
actual: 0,
asked_for: 1,
}
));
);

// Non-unit tuple
// Count mismatch
Expand All @@ -1059,13 +1060,13 @@ mod tests {
let err = do_serialize_err(v, &spec);
let err = get_typeck_err(&err);
assert_eq!(err.rust_name, std::any::type_name::<(&str,)>());
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::WrongColumnCount {
actual: 1,
asked_for: 2,
}
));
);

// Serialization of one of the element fails
let v = ("Ala ma kota", 123_i32);
Expand All @@ -1086,13 +1087,13 @@ mod tests {
let err = do_serialize_err(v, &spec);
let err = get_typeck_err(&err);
assert_eq!(err.rust_name, std::any::type_name::<Vec<&str>>());
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::WrongColumnCount {
actual: 1,
asked_for: 2,
}
));
);

// Serialization of one of the element fails
let v = vec!["Ala ma kota", "Kot ma pchły"];
Expand Down Expand Up @@ -1214,10 +1215,10 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinTypeCheckError>().unwrap();
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::ValueMissingForColumn { .. }
));
);

let spec_duplicate_column = [
col("a", ColumnType::Text),
Expand All @@ -1232,10 +1233,7 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinTypeCheckError>().unwrap();
assert!(matches!(
err.kind,
BuiltinTypeCheckErrorKind::NoColumnWithName { .. }
));
assert_matches!(err.kind, BuiltinTypeCheckErrorKind::NoColumnWithName { .. });

let spec_wrong_type = [
col("a", ColumnType::Text),
Expand All @@ -1248,10 +1246,10 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut row_writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinSerializationError>().unwrap();
assert!(matches!(
assert_matches!(
err.kind,
BuiltinSerializationErrorKind::ColumnSerializationFailed { .. }
));
);
}

#[derive(SerializeRow)]
Expand Down Expand Up @@ -1325,10 +1323,10 @@ mod tests {
let ctx = RowSerializationContext { columns: &spec };
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinTypeCheckError>().unwrap();
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::ColumnNameMismatch { .. }
));
);

let spec_without_c = [
col("a", ColumnType::Text),
Expand All @@ -1341,10 +1339,10 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinTypeCheckError>().unwrap();
assert!(matches!(
assert_matches!(
err.kind,
BuiltinTypeCheckErrorKind::ValueMissingForColumn { .. }
));
);

let spec_duplicate_column = [
col("a", ColumnType::Text),
Expand All @@ -1359,10 +1357,7 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinTypeCheckError>().unwrap();
assert!(matches!(
err.kind,
BuiltinTypeCheckErrorKind::NoColumnWithName { .. }
));
assert_matches!(err.kind, BuiltinTypeCheckErrorKind::NoColumnWithName { .. });

let spec_wrong_type = [
col("a", ColumnType::Text),
Expand All @@ -1375,10 +1370,10 @@ mod tests {
};
let err = <_ as SerializeRow>::serialize(&row, &ctx, &mut writer).unwrap_err();
let err = err.0.downcast_ref::<BuiltinSerializationError>().unwrap();
assert!(matches!(
assert_matches!(
err.kind,
BuiltinSerializationErrorKind::ColumnSerializationFailed { .. }
));
);
}

#[test]
Expand Down
Loading

0 comments on commit 8065379

Please sign in to comment.