Add a uniq test module for yunq in rust.

This commit is contained in:
Drew Galbraith 2024-08-29 21:27:27 -07:00
parent c1db6cb11f
commit 4ee8dc924c
13 changed files with 132 additions and 10 deletions

View file

@ -0,0 +1,14 @@
[package]
name = "yunq-test"
version = "0.1.0"
edition = "2021"
[dependencies]
mammoth = { path = "../mammoth", default-features = false}
yunq = {path = "../yunq", default-features = false}
yunq-derive = {path = "../yunq-derive"}
[build-dependencies]
yunqc = {path = "../../../yunq/rust"}
[dev-dependencies]

View file

@ -0,0 +1,14 @@
use std::fs;
fn main() {
let input_file = "../../../lib/yunq/test/example/example.yunq";
println!("cargo::rerun-if-changed={input_file}");
let input = fs::read_to_string(input_file).expect("Failed to read input file");
let code = yunqc::codegen(&input).expect("Failed to generate yunq code.");
let out = std::env::var("OUT_DIR").unwrap() + "/yunq.rs";
fs::write(out, code).expect("Failed to write generated code.");
}

View file

@ -0,0 +1,62 @@
#![no_std]
include!(concat!(env!("OUT_DIR"), "/yunq.rs"));
#[cfg(test)]
mod tests {
use super::*;
extern crate std;
use std::vec;
#[test]
fn basic_serialization() -> Result<(), ZError> {
let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
basic.serialize(&mut buf, 0, &mut caps)?;
let parsed = Basic::parse(&buf, 0, &caps)?;
assert!(parsed == basic);
Ok(())
}
#[test]
fn basic_serialization_as_request() -> Result<(), ZError> {
let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
let req_id = 12;
basic.serialize_as_request(req_id, &mut buf, &mut caps)?;
assert!(buf.at::<u64>(8)? == req_id);
let parsed = Basic::parse_from_request(&buf, &caps)?;
assert!(parsed == basic);
Ok(())
}
#[test]
fn capability_serialization() -> Result<(), ZError> {
let cap_id = 100;
let cap = Cap { cap: cap_id };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
cap.serialize(&mut buf, 0, &mut caps)?;
assert!(caps.len() == 1);
assert!(caps[0] == cap_id);
let parsed = Cap::parse(&buf, 0, &caps)?;
assert!(parsed == cap);
Ok(())
}
}