Crate rust_slim

source ·
Expand description

A (not yet complete) implementation of a SlimServer for acceptance testing.

This was implementating using the documentation found here

To add it to your project simply run:

cargo add rust_slim -F macros --dev

Then you need to create your fixtures. The recomended way of doing this is by using the #[fixture] macro. Here is an example:

use rust_slim::fixture;

#[derive(Default)]
pub struct Calculator {
    a: i64,
    b: i64,
}

#[fixture]
impl Calculator {
    pub fn set_a(&mut self, a: i64) {
        self.a = a
    }

    pub fn set_b(&mut self, b: i64) {
        self.b = b
    }

    pub fn sum(&self) -> i64 {
        self.a + self.b
    }

    pub fn mul(&self) -> i64 {
        self.a * self.b
    }

    pub fn sub(&self) -> i64 {
        self.a - self.b
    }

    pub fn div(&self) -> i64 {
        self.a / self.b
    }
}

All methods that are public will be able to be called by the slim server.

Than, we need to add an entrypoint to the slim server so we can run it. There are lot of ways of doing this. One is by creating an example in your project.

So create and example file called calculator.rs and add this:

use rust_slim::SlimServer;
use std::net::TcpListener;
use anyhow::Result;
use std::env::args;

fn main() -> Result<()> {
    let port = args().skip(1).next().unwrap_or("8085".to_string());
    let listener = TcpListener::bind(format!("0.0.0.0:{port}").to_string()).expect("Error");
    let (stream, _) = listener.accept()?;
    let mut server = SlimServer::new(stream.try_clone()?, stream);
    server.add_fixture::<Calculator>();
    server.run()?;
    Ok(())
}

Than, to run it, you simply call cargo run --example calculator. Now you need to configure your test runner (fitnesse, temoc) to call your server

Structs§

  • The SlimServer responsible to get the Slim commands and execute against the Fixtures.

Enums§

Traits§

  • ClassPath that will be used in the construction of the fixture. It must be pascal case and have its parts separated by a .. Eg: Fixtures.Calculator The #[fixture] macro will automatically implement it for the type in the impl block. By default, the macro will get the current module path and add the fixutre type. For example, a fixutre with the type Calculator inside the module examples::calculator::fixtures will be converted to a path like Examples.Calculator.Fixtures.Calculator. You can use a custom path by passing it to the macro as such
  • Trait used to construct the fixture. It is auto-implemented for fixtures that also implement Default.
  • Fixtures must implement this trait to be able to be executed by the slim server. The #[fixture] macro will automatically implement it for the type in the impl block.
  • Converts the result of a method into a result that the SlimServer can handle. This is mainly used so you can return whatever you want in a method and we can convert it inside the macro expansion of the [fixture] macro. If you are implementating the SlimFixture manually, you can ignore this. It has implementations for most basic types.

Functions§