Skip to content

Commit

Permalink
Add tests to text_format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris030 committed Jul 31, 2023
1 parent 6a3bc24 commit 20f249e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions roblib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ roland = []
camloc = ["dep:camloc-server"]

gpio-backend = ["dep:rppal"]

[dev-dependencies]
rand = "0.8.5"
54 changes: 54 additions & 0 deletions roblib/src/text_format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
pub mod de;
pub mod error;
pub mod ser;

#[cfg(test)]
mod tests {
use crate::{
cmd::{self, Concrete},
event,
};
use rand::random;

#[test]
fn ser_matches_de() -> anyhow::Result<()> {
for _ in 0..100 {
let cs = [
Concrete::MoveRobot(cmd::MoveRobot(random(), random())),
Concrete::MoveRobotByAngle(cmd::MoveRobotByAngle(random(), random())),
Concrete::StopRobot(cmd::StopRobot),
Concrete::Led(cmd::Led(random(), random(), random())),
Concrete::RolandServo(cmd::RolandServo(random())),
Concrete::Buzzer(cmd::Buzzer(random())),
Concrete::TrackSensor(cmd::TrackSensor),
Concrete::UltraSensor(cmd::UltraSensor),
Concrete::PinMode(cmd::PinMode(
random(),
if random::<bool>() {
crate::gpio::Mode::Input
} else {
crate::gpio::Mode::Output
},
)),
Concrete::ReadPin(cmd::ReadPin(random())),
Concrete::WritePin(cmd::WritePin(random(), random())),
Concrete::Pwm(cmd::Pwm(random(), random(), random())),
Concrete::Servo(cmd::Servo(random(), random())),
Concrete::GetPosition(cmd::GetPosition),
Concrete::Subscribe(cmd::Subscribe(event::GpioPin(random()).into())),
Concrete::Unsubscribe(cmd::Unsubscribe(event::GpioPin(random()).into())),
Concrete::Nop(cmd::Nop),
Concrete::GetUptime(cmd::GetUptime),
Concrete::Abort(cmd::Abort),
];

for c in cs {
let txt1 = super::ser::to_string(&c)?;
let concrete = super::de::from_str::<Concrete>(&txt1)?;
let txt2 = super::ser::to_string(&concrete)?;
if txt1 != txt2 {
println!("❌ {txt1} | {txt2}");
}
}
}

Ok(())
}
}

1 comment on commit 20f249e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Artifacts

View all

base roland
aarch64-unknown-linux-gnu Download Download
aarch64-unknown-linux-musl Download Download
armv7-unknown-linux-gnueabihf Download Download
armv7-unknown-linux-musleabihf Download Download
x86_64-pc-windows-msvc Download Download
x86_64-unknown-linux-gnu Download Download
x86_64-unknown-linux-musl Download Download

Please sign in to comment.