Skip to content

Commit 03ad28b

Browse files
committed
Use claim to simplify assertions
1 parent d4c3f8d commit 03ad28b

File tree

18 files changed

+120
-120
lines changed

18 files changed

+120
-120
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ toml = "0.5"
8888
url = "2.1"
8989

9090
[dev-dependencies]
91+
claim = "0.4.0"
9192
conduit-test = "0.9.0-alpha.3"
9293
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
9394
hyper-tls = "0.4"

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,5 @@ fn parse_traffic_patterns_splits_on_comma_and_looks_for_equal_sign() {
187187
let patterns_2 = parse_traffic_patterns(pattern_string_2).collect::<Vec<_>>();
188188
assert_eq!(vec![("Baz", "QUX")], patterns_2);
189189

190-
assert!(parse_traffic_patterns(pattern_string_3).next().is_none());
190+
assert_none!(parse_traffic_patterns(pattern_string_3).next());
191191
}

src/email.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ mod tests {
141141
"test",
142142
"test",
143143
);
144-
assert!(result.is_err());
144+
assert_err!(result);
145145
}
146146

147147
#[test]
148148
fn sending_to_valid_email_succeeds() {
149149
let result = send_email("[email protected]", "test", "test");
150-
assert!(result.is_ok());
150+
assert_ok!(result);
151151
}
152152
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#![warn(clippy::all, rust_2018_idioms)]
88
#![warn(missing_debug_implementations, missing_copy_implementations)]
99

10+
#[cfg(test)]
11+
#[macro_use]
12+
extern crate claim;
1013
#[macro_use]
1114
extern crate derive_deref;
1215
#[macro_use]

src/models/krate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ mod tests {
564564
repository: None,
565565
max_upload_size: None,
566566
};
567-
assert!(krate.validate().is_err());
567+
assert_err!(krate.validate());
568568
}
569569

570570
#[test]

src/models/token.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ mod tests {
133133
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
134134
};
135135
let json = serde_json::to_string(&tok).unwrap();
136-
assert!(json
136+
assert_some!(json
137137
.as_str()
138-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
139-
.is_some());
140-
assert!(json
138+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#));
139+
assert_some!(json
141140
.as_str()
142-
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
143-
.is_some());
141+
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#));
144142
}
145143

146144
#[test]
@@ -154,13 +152,11 @@ mod tests {
154152
last_used_at: Some(NaiveDate::from_ymd(2017, 1, 6).and_hms(14, 23, 12)),
155153
};
156154
let json = serde_json::to_string(&tok).unwrap();
157-
assert!(json
155+
assert_some!(json
158156
.as_str()
159-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#)
160-
.is_some());
161-
assert!(json
157+
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#));
158+
assert_some!(json
162159
.as_str()
163-
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#)
164-
.is_some());
160+
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#));
165161
}
166162
}

src/tests/all.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

3+
#[macro_use]
4+
extern crate claim;
35
#[macro_use]
46
extern crate diesel;
57
#[macro_use]
@@ -33,15 +35,6 @@ use conduit_test::MockRequest;
3335
use diesel::prelude::*;
3436
use reqwest::{blocking::Client, Proxy};
3537

36-
macro_rules! t {
37-
($e:expr) => {
38-
match $e {
39-
Ok(e) => e,
40-
Err(m) => panic!("{} failed with: {}", stringify!($e), m),
41-
}
42-
};
43-
}
44-
4538
mod account_lock;
4639
mod authentication;
4740
mod badge;
@@ -164,7 +157,7 @@ fn build_app(
164157
};
165158

166159
let app = App::new(config, client);
167-
t!(t!(app.primary_database.get()).begin_test_transaction());
160+
assert_ok!(assert_ok!(app.primary_database.get()).begin_test_transaction());
168161
let app = Arc::new(app);
169162
let handler = cargo_registry::build_handler(Arc::clone(&app));
170163
(app, handler)

src/tests/builders/publish.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ lazy_static! {
1414
{
1515
let mut ar =
1616
tar::Builder::new(GzEncoder::new(&mut empty_tarball, Compression::default()));
17-
t!(ar.finish());
17+
assert_ok!(ar.finish());
1818
}
1919
empty_tarball
2020
};
@@ -88,12 +88,12 @@ impl PublishBuilder {
8888
let mut ar = tar::Builder::new(GzEncoder::new(&mut tarball, Compression::default()));
8989
for &mut (name, ref mut data, size) in files {
9090
let mut header = tar::Header::new_gnu();
91-
t!(header.set_path(name));
91+
assert_ok!(header.set_path(name));
9292
header.set_size(size);
9393
header.set_cksum();
94-
t!(ar.append(&header, data));
94+
assert_ok!(ar.append(&header, data));
9595
}
96-
t!(ar.finish());
96+
assert_ok!(ar.finish());
9797
}
9898

9999
self.tarball = tarball;

src/tests/category.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ fn show() {
4444

4545
// Create a category and a subcategory
4646
app.db(|conn| {
47-
t!(new_category("Foo Bar", "foo-bar", "Foo Bar crates").create_or_update(conn));
48-
t!(new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates").create_or_update(conn));
47+
assert_ok!(new_category("Foo Bar", "foo-bar", "Foo Bar crates").create_or_update(conn));
48+
assert_ok!(
49+
new_category("Foo Bar::Baz", "foo-bar::baz", "Baz crates").create_or_update(conn)
50+
);
4951
});
5052

5153
// The category and its subcategories should be in the json
@@ -69,8 +71,10 @@ fn update_crate() {
6971
let user = user.as_model();
7072

7173
app.db(|conn| {
72-
t!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(conn));
73-
t!(new_category("Category 2", "category-2", "Category 2 crates").create_or_update(conn));
74+
assert_ok!(new_category("cat1", "cat1", "Category 1 crates").create_or_update(conn));
75+
assert_ok!(
76+
new_category("Category 2", "category-2", "Category 2 crates").create_or_update(conn)
77+
);
7478
let krate = CrateBuilder::new("foo_crate", user.id).expect_build(&conn);
7579

7680
// Updating with no categories has no effect
@@ -122,7 +126,7 @@ fn update_crate() {
122126
assert_eq!(count(&anon, "category-2"), 0);
123127

124128
// Add a category and its subcategory
125-
t!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(conn));
129+
assert_ok!(new_category("cat1::bar", "cat1::bar", "bar crates").create_or_update(conn));
126130
Category::update_crate(&conn, &krate, &["cat1", "cat1::bar"]).unwrap();
127131

128132
assert_eq!(count(&anon, "cat1"), 1);

0 commit comments

Comments
 (0)