Skip to content

Commit

Permalink
apply clippy lints to tests
Browse files Browse the repository at this point in the history
Turns out Clippy has Opinions about clean code in tests too!
  • Loading branch information
MikkelPaulson committed Sep 9, 2024
1 parent 253af1a commit 8767d94
Show file tree
Hide file tree
Showing 31 changed files with 86 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Verify no mangled parens
run: if git grep ',)' '*.rs'; then exit 1; fi
- name: Run Clippy
run: cargo clippy --workspace -- --deny warnings
run: cargo clippy --workspace --tests -- --deny warnings

unit-tests:
name: Unit tests
Expand Down
20 changes: 12 additions & 8 deletions cli/src/rich/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ mod test {
selected: None,
query: "sh".into(),
};
assert_eq!(single.get_only_suggestion(), single.suggestions.get(0));
assert_eq!(single.get_only_suggestion(), single.suggestions.first());

let multiple = Autocomplete {
show_single_suggestion: true,
Expand Down Expand Up @@ -777,7 +777,7 @@ mod test {
autocomplete = autocomplete.up();
assert_eq!(
autocomplete.get_selected_suggestion(),
autocomplete.suggestions.get(0)
autocomplete.suggestions.first(),
);

autocomplete = autocomplete.up();
Expand Down Expand Up @@ -805,19 +805,19 @@ mod test {
autocomplete = autocomplete.down();
assert_eq!(
autocomplete.get_selected_suggestion(),
autocomplete.suggestions.get(0)
autocomplete.suggestions.first(),
);

autocomplete = autocomplete.down();
assert_eq!(
autocomplete.get_selected_suggestion(),
autocomplete.suggestions.get(1)
autocomplete.suggestions.get(1),
);

autocomplete = autocomplete.down();
assert_eq!(
autocomplete.get_selected_suggestion(),
autocomplete.suggestions.get(0)
autocomplete.suggestions.first(),
);
}

Expand Down Expand Up @@ -971,7 +971,9 @@ mod test {
assert_eq!(3, input.index);
assert_eq!(0, input.cursor);

input.history.last_mut().map(|s| s.push_str("foo"));
if let Some(s) = input.history.last_mut() {
s.push_str("foo");
}
input.cursor = 3;
assert_eq!(vec!["foo", "bar", "foo", "foo"], input.history);

Expand Down Expand Up @@ -1015,8 +1017,10 @@ mod test {

#[test]
fn key_esc_test() {
let mut input = Input::default();
input.search_query = Some(String::new());
let mut input = Input {
search_query: Some(String::new()),
..Default::default()
};

input.key(Key::Esc, false);
assert_eq!(None, input.search_query);
Expand Down
2 changes: 1 addition & 1 deletion core/src/app/command/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ mod tests {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}

fn literal(
Expand Down
2 changes: 1 addition & 1 deletion core/src/app/command/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
2 changes: 1 addition & 1 deletion core/src/app/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
2 changes: 1 addition & 1 deletion core/src/app/command/runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn assert_autocomplete(
actual_suggestions: Vec<AutocompleteSuggestion>,
) {
let mut expected: Vec<_> = expected_suggestions
.into_iter()
.iter()
.map(|(a, b)| ((*a).into(), (*b).into()))
.collect();
expected.sort();
Expand Down
4 changes: 2 additions & 2 deletions core/src/app/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod test {
#[test]
fn debug_test() {
let mut app_meta = app_meta();
app_meta.demographics = Demographics::new(HashMap::new().into());
app_meta.demographics = Demographics::new(HashMap::new());

assert_eq!(
"AppMeta { command_aliases: {}, demographics: Demographics { groups: GroupMapWrapper({}) }, repository: Repository { data_store_enabled: false, recent: [] } }",
Expand All @@ -59,6 +59,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
2 changes: 1 addition & 1 deletion core/src/reference/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
3 changes: 1 addition & 2 deletions core/src/storage/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ mod test {
Some("Gandalf the White"),
block_on(ds.get_all_the_things())
.unwrap()
.iter()
.next()
.first()
.unwrap()
.name()
.value()
Expand Down
8 changes: 4 additions & 4 deletions core/src/storage/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ data_store.snapshot() = {:?}",
#[test]
fn change_test_edit_by_name_from_recent_data_store_failed() {
let mut repo = repo();
repo.data_store = Box::new(NullDataStore::default());
repo.data_store = Box::new(NullDataStore);
let change = Change::Edit {
name: "Odysseus".into(),
uuid: None,
Expand Down Expand Up @@ -1956,14 +1956,14 @@ data_store.snapshot() = {:?}",
fn data_store_enabled_test_success() {
let mut repo = repo();
block_on(repo.init());
assert_eq!(true, repo.data_store_enabled());
assert!(repo.data_store_enabled());
}

#[test]
fn data_store_enabled_test_failure() {
let mut repo = null_repo();
block_on(repo.init());
assert_eq!(false, repo.data_store_enabled());
assert!(!repo.data_store_enabled());
}

fn thing(uuid: Uuid, data: impl Into<ThingData>) -> Thing {
Expand All @@ -1989,7 +1989,7 @@ data_store.snapshot() = {:?}",
}

fn null_repo() -> Repository {
Repository::new(NullDataStore::default())
Repository::new(NullDataStore)
}

fn populate_repo(repo: &mut Repository) {
Expand Down
20 changes: 10 additions & 10 deletions core/src/time/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,39 +262,39 @@ mod test {
);

assert_autocomplete(
&[("+1d".into(), "advance time by 1 day".into())][..],
&[("+1d", "advance time by 1 day")][..],
block_on(TimeCommand::autocomplete("+1d", &app_meta)),
);
assert_autocomplete(
&[("+1D".into(), "advance time by 1 day".into())][..],
&[("+1D", "advance time by 1 day")][..],
block_on(TimeCommand::autocomplete("+1D", &app_meta)),
);
assert_autocomplete(
&[("+1h".into(), "advance time by 1 hour".into())][..],
&[("+1h", "advance time by 1 hour")][..],
block_on(TimeCommand::autocomplete("+1h", &app_meta)),
);
assert_autocomplete(
&[("+1H".into(), "advance time by 1 hour".into())][..],
&[("+1H", "advance time by 1 hour")][..],
block_on(TimeCommand::autocomplete("+1H", &app_meta)),
);
assert_autocomplete(
&[("+1m".into(), "advance time by 1 minute".into())][..],
&[("+1m", "advance time by 1 minute")][..],
block_on(TimeCommand::autocomplete("+1m", &app_meta)),
);
assert_autocomplete(
&[("+1M".into(), "advance time by 1 minute".into())][..],
&[("+1M", "advance time by 1 minute")][..],
block_on(TimeCommand::autocomplete("+1M", &app_meta)),
);
assert_autocomplete(
&[("+1s".into(), "advance time by 1 second".into())][..],
&[("+1s", "advance time by 1 second")][..],
block_on(TimeCommand::autocomplete("+1s", &app_meta)),
);
assert_autocomplete(
&[("+1S".into(), "advance time by 1 second".into())][..],
&[("+1S", "advance time by 1 second")][..],
block_on(TimeCommand::autocomplete("+1S", &app_meta)),
);
assert_autocomplete(
&[("+1r".into(), "advance time by 1 round".into())][..],
&[("+1r", "advance time by 1 round")][..],
block_on(TimeCommand::autocomplete("+1r", &app_meta)),
);
assert_autocomplete(
Expand Down Expand Up @@ -343,6 +343,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
2 changes: 1 addition & 1 deletion core/src/world/command/autocomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
3 changes: 2 additions & 1 deletion core/src/world/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ mod test {
..Default::default()
}
.into(),
#[allow(clippy::single_range_in_vec_init)]
unknown_words: vec![10..14],
word_count: 2,
},
Expand Down Expand Up @@ -742,6 +743,6 @@ mod test {
fn event_dispatcher(_event: Event) {}

fn app_meta() -> AppMeta {
AppMeta::new(NullDataStore::default(), &event_dispatcher)
AppMeta::new(NullDataStore, &event_dispatcher)
}
}
8 changes: 4 additions & 4 deletions core/src/world/npc/ethnicity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ mod test {
(0..20)
.map(|_| gen_name(
&mut rng,
&syllable_count_dist,
&start_dist,
&mid_dist,
&end_dist
syllable_count_dist,
start_dist,
mid_dist,
end_dist
))
.collect::<Vec<_>>(),
);
Expand Down
36 changes: 21 additions & 15 deletions core/src/world/npc/species/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ mod test {

#[test]
fn regenerate_test_default() {
let mut npc = NpcData::default();
npc.species = Field::new_generated(Species::Human);
let mut npc = NpcData {
species: Field::new_generated(Species::Human),
..Default::default()
};
let mut rng = SmallRng::seed_from_u64(0);

regenerate(&mut rng, &mut npc);
Expand All @@ -169,16 +171,18 @@ mod test {

#[test]
fn regenerate_test_locked() {
let mut npc = NpcData::default();
npc.species = Species::Human.into();
npc.age = Age::Adult.into();
npc.age_years = u16::MAX.into();
npc.gender = Gender::Neuter.into();
npc.size = Size::Tiny {
height: u16::MAX,
weight: u16::MAX,
}
.into();
let mut npc = NpcData {
species: Species::Human.into(),
age: Age::Adult.into(),
age_years: u16::MAX.into(),
gender: Gender::Neuter.into(),
size: Size::Tiny {
height: u16::MAX,
weight: u16::MAX,
}
.into(),
..Default::default()
};

let mut rng = SmallRng::seed_from_u64(0);

Expand All @@ -198,9 +202,11 @@ mod test {

#[test]
fn regenerate_test_age_years_provided() {
let mut npc = NpcData::default();
npc.species = Species::Human.into();
npc.age_years = u16::MAX.into();
let mut npc = NpcData {
species: Species::Human.into(),
age_years: u16::MAX.into(),
..Default::default()
};

let mut rng = SmallRng::seed_from_u64(0);

Expand Down
2 changes: 1 addition & 1 deletion core/src/world/place/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ mod test {
Place {
uuid: uuid::Uuid::nil(),
data: PlaceData {
location_uuid: Uuid::from(uuid::Uuid::nil()).into(),
location_uuid: uuid::Uuid::nil().into(),
subtype: "inn".parse::<PlaceType>().ok().into(),

name: "Oaken Mermaid Inn".into(),
Expand Down
2 changes: 1 addition & 1 deletion core/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn sync_app() -> SyncApp {

#[allow(dead_code)]
pub fn sync_app_with_invalid_data_store() -> SyncApp {
sync_app_with_data_store(NullDataStore::default())
sync_app_with_data_store(NullDataStore)
}

#[allow(dead_code)]
Expand Down
File renamed without changes.
5 changes: 0 additions & 5 deletions core/tests/integration/app/app/mod.rs

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn all_commands_are_valid() {
.filter(|s| !s.contains('['))
{
// Basically, we just want to make sure all commands run successfully.
app.command(command).expect(&format!("{}", command));
app.command(command).expect(command);
}
}
}
6 changes: 5 additions & 1 deletion core/tests/integration/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
mod app;
mod about;
mod changelog;
mod debug;
mod help;
mod roll;
mod tutorial;

use crate::common::{get_name, sync_app};
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion core/tests/integration/storage/change/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn npc_can_be_deleted_from_data_store() {

#[test]
fn delete_works_with_unusable_data_store() {
let mut app = sync_app_with_data_store(NullDataStore::default());
let mut app = sync_app_with_data_store(NullDataStore);

app.command("npc named Potato Johnson").unwrap();

Expand Down
Loading

0 comments on commit 8767d94

Please sign in to comment.