Skip to content

Commit

Permalink
ci: add tests to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Bloeckchengrafik committed Mar 13, 2024
1 parent a030591 commit 79c8c1c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Cargo Test

on:
push:
pull_request:

env:
CARGO_TERM_COLOR: always

jobs:
build_and_test:
name: Rust project - latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: rustup update stable && rustup default stable
- run: cargo test --verbose -- --nocapture

4 changes: 2 additions & 2 deletions crates/ftswarm/src/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;
pub struct WhoamiResponse {
pub hostname: String,
pub id: String,
pub serial: Option<String>,
pub serial: Option<i32>,
}

impl TryFrom<String> for WhoamiResponse {
Expand All @@ -16,7 +16,7 @@ impl TryFrom<String> for WhoamiResponse {
let id = parts.next().ok_or("No ID found")?;
let hostname = parts.next().ok_or("No hostname found")?;

let serial = parts.next().map(|s| s.to_string());
let serial = id.replace("ftSwarm", "").parse().ok();

Ok(WhoamiResponse {
hostname: hostname.to_string(),
Expand Down
5 changes: 4 additions & 1 deletion crates/ftswarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mod message_queue;
pub mod swarm_object;
mod direct;

#[cfg(test)]
mod tests;

/// A macro to create a struct with static string aliases
///
/// # Example
Expand Down Expand Up @@ -51,7 +54,7 @@ macro_rules! aliases {
}
) => {
#[derive(Debug)]
struct $enum_name {
pub struct $enum_name {
}

impl $enum_name {
Expand Down
35 changes: 35 additions & 0 deletions crates/ftswarm/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use ftswarm_serial::FixedSerialPort;

use crate::{aliases, FtSwarm};

aliases! {
Outputs {
LED1 = "led1",
LED2 = "led2",
}
}

aliases! {
Inputs {
SWITCH = "switch",
}
}

#[test]
fn test_aliases() {
assert_eq!(Outputs::LED1, "led1");
assert_eq!(Outputs::LED2, "led2");
assert_eq!(Inputs::SWITCH, "switch");
}

#[tokio::test]
async fn test_lowlevel() {
let static_serial = FixedSerialPort::new();
static_serial.add_response("ftSwarm100/example");

let swarm = FtSwarm::new(static_serial);
let whoami = swarm.whoami().await.unwrap();
assert_eq!(whoami.hostname, "example");
assert_eq!(whoami.id, "ftSwarm100");
assert_eq!(whoami.serial, Some(100));
}
5 changes: 2 additions & 3 deletions crates/ftswarm_serial/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ impl FixedSerialPort {
}
}

#[allow(dead_code)]
fn add_response(&self, response: String) {
pub fn add_response(&self, response: &str) {
let mut commands = self.commands.lock().unwrap();
commands.push(response);
commands.push(response.to_string());
}

fn pop_command(&self) -> Option<String> {
Expand Down

0 comments on commit 79c8c1c

Please sign in to comment.