From 83803be3e809de239b6745ebbe0a1cdba4331d5b Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 2 Aug 2022 15:45:09 +0200 Subject: [PATCH] Write tests and fix things here and there --- src/data/schema/art.rs | 30 ++++++++++++- src/data/schema/card.rs | 2 +- src/data/schema/keyword.rs | 88 +++++++++++++++++++++++++++++++++++++- src/data/schema/rarity.rs | 35 +++++++++++++-- src/data/schema/region.rs | 32 +++++++++++++- src/data/schema/set.rs | 26 ++++++++++- src/data/schema/speed.rs | 32 ++++++++++++-- src/data/schema/type.rs | 39 ++++++++++++++++- 8 files changed, 271 insertions(+), 13 deletions(-) diff --git a/src/data/schema/art.rs b/src/data/schema/art.rs index a3f87f4..92815b9 100644 --- a/src/data/schema/art.rs +++ b/src/data/schema/art.rs @@ -1,7 +1,7 @@ //! Module defining [CardArt]. /// An art asset associated with a [Card]. -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct CardArt { /// URL to the `.png` image of the rendered card. /// @@ -51,4 +51,32 @@ impl CardArt { .replace("https://dd.b.pvp.net/latest/set1", "https://poro.steffo.eu/set1-en_us") .replace(".png", ".jpg") } +} + + +#[cfg(test)] +mod tests { + use super::CardArt; + + #[test] + fn deserialize() { + assert_eq!( + serde_json::de::from_str::<'static, CardArt>(r#"{"gameAbsolutePath": https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001.png, "fullAbsolutePath": "https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001-full.png"}"#).unwrap(), + CardArt { + card_png: "https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001.png", + full_png: "https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001-full.png", + } + ); + } + + #[test] + fn png_to_jpg() { + let art = CardArt { + card_png: "https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001.png", + full_png: "https://dd.b.pvp.net/latest/set1/en_us/img/cards/01DE001-full.png", + }; + + assert_eq!(art.card_jpg(), "https://poro.steffo.eu/set1-en_us/en_us/img/cards/01DE001.jpg"); + assert_eq!(art.full_jpg(), "https://poro.steffo.eu/set1-en_us/en_us/img/cards/01DE001-full.jpg"); + } } \ No newline at end of file diff --git a/src/data/schema/card.rs b/src/data/schema/card.rs index 02371e5..2ec263d 100644 --- a/src/data/schema/card.rs +++ b/src/data/schema/card.rs @@ -10,7 +10,7 @@ use super::rarity::CardRarity; use super::speed::SpellSpeed; /// A single Legends of Runeterra card as represented in the data files from [Data Dragon](https://developer.riotgames.com/docs/lor). -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all="camelCase")] pub struct Card { /// Unique seven-character identifier of the card. diff --git a/src/data/schema/keyword.rs b/src/data/schema/keyword.rs index 3e9340f..527147d 100644 --- a/src/data/schema/keyword.rs +++ b/src/data/schema/keyword.rs @@ -4,7 +4,7 @@ /// /// Since more keywords will probably be added in the future, this enum is [non_exaustive](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute). #[non_exhaustive] -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum CardKeyword { /// Overwhelm on spells. /// @@ -331,3 +331,89 @@ pub enum CardKeyword { #[serde(other)] Unsupported, } + + +#[cfg(test)] +mod tests { + use super::CardKeyword; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!(serde_json::de::from_str::<'static, CardKeyword>($src).unwrap(), $res); + } + } + } + + test_deserialization!(deserialize_spelloverwhelm, "SpellOverwhelm", CardKeyword::SpellOverwhelm); + test_deserialization!(deserialize_burst, "Burst", CardKeyword::Burst); + test_deserialization!(deserialize_countdown, "Countdown", CardKeyword::Countdown); + test_deserialization!(deserialize_onplay, "PlaySkillMark", CardKeyword::OnPlay); + test_deserialization!(deserialize_landmark, "LandmarkVisualOnly", CardKeyword::Landmark); + test_deserialization!(deserialize_shurima, "Shurima", CardKeyword::Shurima); + test_deserialization!(deserialize_attach, "Attach", CardKeyword::Attach); + test_deserialization!(deserialize_noxus, "Noxus", CardKeyword::Noxus); + test_deserialization!(deserialize_fleeting, "Fleeting", CardKeyword::Fleeting); + test_deserialization!(deserialize_clobbernoemptyslotrequirement, "ClobberNoEmptySlotRequirement", CardKeyword::ClobberNoEmptySlotRequirement); + test_deserialization!(deserialize_nab, "Nab", CardKeyword::Nab); + test_deserialization!(deserialize_focus, "Focus", CardKeyword::Focus); + test_deserialization!(deserialize_enlightened, "Enlightened", CardKeyword::Enlightened); + test_deserialization!(deserialize_invoke, "Invoke", CardKeyword::Invoke); + test_deserialization!(deserialize_boon, "Boon", CardKeyword::Boon); + test_deserialization!(deserialize_trap, "Autoplay", CardKeyword::Trap); + test_deserialization!(deserialize_drain, "Drain", CardKeyword::Drain); + test_deserialization!(deserialize_lastbreath, "LastBreath", CardKeyword::LastBreath); + test_deserialization!(deserialize_demacia, "Demacia", CardKeyword::Demacia); + test_deserialization!(deserialize_bandlecity, "BandleCity", CardKeyword::BandleCity); + test_deserialization!(deserialize_fast, "Fast", CardKeyword::Fast); + test_deserialization!(deserialize_bilgewater, "Bilgewater", CardKeyword::Bilgewater); + test_deserialization!(deserialize_runeterra, "Runeterra", CardKeyword::Runeterra); + test_deserialization!(deserialize_recall, "Recall", CardKeyword::Recall); + test_deserialization!(deserialize_weakest, "Weakest", CardKeyword::Weakest); + test_deserialization!(deserialize_support, "Support", CardKeyword::Support); + test_deserialization!(deserialize_slow, "Slow", CardKeyword::Slow); + test_deserialization!(deserialize_obliterate, "Obliterate", CardKeyword::Obliterate); + test_deserialization!(deserialize_imbue, "Imbue", CardKeyword::Imbue); + test_deserialization!(deserialize_targon, "MtTargon", CardKeyword::Targon); + test_deserialization!(deserialize_shadowisles, "ShadowIsles", CardKeyword::ShadowIsles); + test_deserialization!(deserialize_auravisualfakekeyword, "AuraVisualFakeKeyword", CardKeyword::AuraVisualFakeKeyword); + test_deserialization!(deserialize_ionia, "Ionia", CardKeyword::Ionia); + test_deserialization!(deserialize_nightfall, "Nightfall", CardKeyword::Nightfall); + test_deserialization!(deserialize_piltoverzaun, "PiltoverZaun", CardKeyword::PiltoverZaun); + test_deserialization!(deserialize_attune, "Attune", CardKeyword::Attune); + test_deserialization!(deserialize_daybreak, "Daybreak", CardKeyword::Daybreak); + test_deserialization!(deserialize_silenceindividualkeyword, "SilenceIndividualKeyword", CardKeyword::SilenceIndividualKeyword); + test_deserialization!(deserialize_skill, "Skill", CardKeyword::Skill); + test_deserialization!(deserialize_plunder, "Plunder", CardKeyword::Plunder); + test_deserialization!(deserialize_doubleattack, "DoubleAttack", CardKeyword::DoubleAttack); + test_deserialization!(deserialize_vulnerable, "Vulnerable", CardKeyword::Vulnerable); + test_deserialization!(deserialize_elusive, "Elusive", CardKeyword::Elusive); + test_deserialization!(deserialize_stun, "Stun", CardKeyword::Stun); + test_deserialization!(deserialize_fated, "Fated", CardKeyword::Fated); + test_deserialization!(deserialize_blockelusive, "BlockElusive", CardKeyword::BlockElusive); + test_deserialization!(deserialize_fury, "Fury", CardKeyword::Fury); + test_deserialization!(deserialize_barrier, "Barrier", CardKeyword::Barrier); + test_deserialization!(deserialize_immobile, "Immobile", CardKeyword::Immobile); + test_deserialization!(deserialize_hallowed, "Hallowed", CardKeyword::Hallowed); + test_deserialization!(deserialize_evolve, "Evolve", CardKeyword::Evolve); + test_deserialization!(deserialize_frostbite, "Frostbite", CardKeyword::Frostbite); + test_deserialization!(deserialize_overwhelm, "Overwhelm", CardKeyword::Overwhelm); + test_deserialization!(deserialize_quickattack, "QuickStrike", CardKeyword::QuickAttack); + test_deserialization!(deserialize_tough, "Tough", CardKeyword::Tough); + test_deserialization!(deserialize_regeneration, "Regeneration", CardKeyword::Regeneration); + test_deserialization!(deserialize_silenced, "Silenced", CardKeyword::Silenced); + test_deserialization!(deserialize_spellshield, "SpellShield", CardKeyword::SpellShield); + test_deserialization!(deserialize_lifesteal, "Lifesteal", CardKeyword::Lifesteal); + test_deserialization!(deserialize_augment, "Augment", CardKeyword::Augment); + test_deserialization!(deserialize_impact, "Impact", CardKeyword::Impact); + test_deserialization!(deserialize_scout, "Scout", CardKeyword::Scout); + test_deserialization!(deserialize_ephemeral, Ephemeral, CardKeyword::Ephemeral); + test_deserialization!(deserialize_lurk, Lurker, CardKeyword::Lurk); + test_deserialization!(deserialize_formidable, Formidable, CardKeyword::Formidable); + test_deserialization!(deserialize_challenger, Challenger, CardKeyword::Challenger); + test_deserialization!(deserialize_fearsome, Fearsome, CardKeyword::Fearsome); + test_deserialization!(deserialize_cantblock, CantBlock, CardKeyword::CantBlock); + test_deserialization!(deserialize_deep, Deep, CardKeyword::Deep); + test_deserialization!(deserialize_unsupported, Unsupported, CardKeyword::Unsupported); +} diff --git a/src/data/schema/rarity.rs b/src/data/schema/rarity.rs index 8b3570f..1b8b9be 100644 --- a/src/data/schema/rarity.rs +++ b/src/data/schema/rarity.rs @@ -1,21 +1,48 @@ //! Module defining [CardRarity]. /// A possible card rarity. -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum CardRarity { /// The card has no rarity, as it probably is not collectible. + #[serde(alias = "")] #[serde(alias = "NONE")] None, /// A common card. - #[serde(alias = "COMMON")] + #[serde(rename = "COMMON")] Common, /// A rare card. - #[serde(alias = "RARE")] + #[serde(rename = "RARE")] Rare, /// An epic card. - #[serde(alias = "EPIC")] + #[serde(rename = "EPIC")] Epic, /// A champion. #[serde(alias = "CHAMPION")] Champion, } + + +#[cfg(test)] +mod tests { + use super::CardRarity; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!(serde_json::de::from_str::<'static, CardRarity>($src).unwrap(), $res); + } + } + } + + test_deserialization!(deserialize_none, "None", CardRarity::None); + test_deserialization!(deserialize_common, "COMMON", CardRarity::Common); + test_deserialization!(deserialize_rare, "RARE", CardRarity::Rare); + test_deserialization!(deserialize_epic, "EPIC", CardRarity::Epic); + test_deserialization!(deserialize_champion, "Champion", CardRarity::Champion); + + #[test] + fn deserialize_fallback() { + assert!(serde_json::de::from_str::<'static, CardRarity>("Xyzzy").is_err()); + } +} diff --git a/src/data/schema/region.rs b/src/data/schema/region.rs index 015d674..62399a2 100644 --- a/src/data/schema/region.rs +++ b/src/data/schema/region.rs @@ -4,7 +4,7 @@ /// /// Since more regions might be added in the future, especially Origin ones, this enum is [non_exaustive](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute). #[non_exhaustive] -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum CardRegion { /// Noxus. Noxus, @@ -39,3 +39,33 @@ pub enum CardRegion { #[serde(other)] Unsupported, } + + +#[cfg(test)] +mod tests { + use super::CardRegion; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!(serde_json::de::from_str::<'static, CardRegion>($src).unwrap(), $res); + } + } + } + + test_deserialization!(deserialize_noxus, "Noxus", CardRegion::Noxus); + test_deserialization!(deserialize_demacia, "Demacia", CardRegion::Demacia); + test_deserialization!(deserialize_freljord, "Freljord", CardRegion::Freljord); + test_deserialization!(deserialize_shadowisles, "ShadowIsles", CardRegion::ShadowIsles); + test_deserialization!(deserialize_targon, "Targon", CardRegion::Targon); + test_deserialization!(deserialize_ionia, "Ionia", CardRegion::Ionia); + test_deserialization!(deserialize_shurima, "Shurima", CardRegion::Shurima); + test_deserialization!(deserialize_piltoverzaun, "PiltoverZaun", CardRegion::PiltoverZaun); + test_deserialization!(deserialize_bandlecity, "BandleCity", CardRegion::BandleCity); + test_deserialization!(deserialize_runeterra, "Runeterra", CardRegion::Runeterra); + test_deserialization!(deserialize_jhin, "Jhin", CardRegion::Jhin); + test_deserialization!(deserialize_evelynn, "Evelynn", CardRegion::Evelynn); + test_deserialization!(deserialize_bard, "Bard", CardRegion::Bard); + test_deserialization!(deserialize_fallback, "Xyzzy", CardRegion::Unsupported); +} diff --git a/src/data/schema/set.rs b/src/data/schema/set.rs index 301829e..ae552df 100644 --- a/src/data/schema/set.rs +++ b/src/data/schema/set.rs @@ -4,7 +4,7 @@ /// /// Since more sets will definitely be added in the future, this enum is [non_exaustive](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute). #[non_exhaustive] -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum CardSet { /// Foundations, or "base". #[serde(rename = "Set1")] @@ -38,3 +38,27 @@ pub enum CardSet { #[serde(other)] Unsupported, } + + +#[cfg(test)] +mod tests { + use super::CardSet; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!(serde_json::de::from_str::<'static, CardSet>($src).unwrap(), $res); + } + } + } + + test_deserialization!(deserialize_set1, "Set1", CardSet::Foundations); + test_deserialization!(deserialize_set2, "Set2", CardSet::RisingTides); + test_deserialization!(deserialize_set3, "Set3", CardSet::CallOfTheMountain); + test_deserialization!(deserialize_set4, "Set4", CardSet::EmpiresOfTheAscended); + test_deserialization!(deserialize_set5, "Set5", CardSet::BeyondTheBandlewood); + test_deserialization!(deserialize_set6, "Set6", CardSet::Worldwalker); + test_deserialization!(deserialize_setevent, "SetEvent", CardSet::Events); + test_deserialization!(deserialize_fallback, "Xyzzy", CardSet::Unsupported); +} diff --git a/src/data/schema/speed.rs b/src/data/schema/speed.rs index 91dd3ef..c4c3351 100644 --- a/src/data/schema/speed.rs +++ b/src/data/schema/speed.rs @@ -1,10 +1,11 @@ //! Module defining [SpellSpeed]. + /// A possible spell speed. -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum SpellSpeed { /// Non-spell cards have this speed. - #[serde(alias = "")] + #[serde(rename = "")] None, /// A Slow spell. Slow, @@ -12,4 +13,29 @@ pub enum SpellSpeed { Fast, /// Either a Burst or a Focus spell; to disambiguate between the two, check for the `Focus` keyword. Burst, -} \ No newline at end of file +} + + +#[cfg(test)] +mod tests { + use super::SpellSpeed; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!(serde_json::de::from_str::<'static, SpellSpeed>($src).unwrap(), $res); + } + } + } + + test_deserialization!(deserialize_none, "", SpellSpeed::None); + test_deserialization!(deserialize_slow, "Slow", SpellSpeed::Slow); + test_deserialization!(deserialize_fast, "Fast", SpellSpeed::Fast); + test_deserialization!(deserialize_burst, "Burst", SpellSpeed::Burst); + + #[test] + fn deserialize_fallback() { + assert!(serde_json::de::from_str::<'static, SpellSpeed>("Xyzzy").is_err()); + } +} diff --git a/src/data/schema/type.rs b/src/data/schema/type.rs index c9e4d46..8d7f4ef 100644 --- a/src/data/schema/type.rs +++ b/src/data/schema/type.rs @@ -1,10 +1,11 @@ //! Module defining [Type]. + /// A possible card type. /// /// Since more types might be added in the future, as it happened with landmarks, this enum is [non_exaustive](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute). #[non_exhaustive] -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum CardType { /// A spell. Spell, @@ -21,4 +22,40 @@ pub enum CardType { /// Unsupported card type. #[serde(other)] Unsupported, +} + + +#[cfg(test)] +mod tests { + use super::CardType; + + #[test] + fn deserialize_spell() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Spell").unwrap(), CardType::Spell); + } + + #[test] + fn deserialize_unit() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Unit").unwrap(), CardType::Unit); + } + + #[test] + fn deserialize_ability() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Ability").unwrap(), CardType::Ability); + } + + #[test] + fn deserialize_landmark() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Landmark").unwrap(), CardType::Landmark); + } + + #[test] + fn deserialize_trap() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Trap").unwrap(), CardType::Trap); + } + + #[test] + fn deserialize_fallback() { + assert_eq!(serde_json::de::from_str::<'static, CardType>("Xyzzy").unwrap(), CardType::Unsupported); + } } \ No newline at end of file