#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
use std::path::PathBuf;
use zerodds_idl::config::ParserConfig;
use zerodds_idl_python::{PythonGenOptions, generate_python_module};
fn emit_py(src: &str) -> String {
let ast = zerodds_idl::parse(src, &ParserConfig::default()).expect("parse");
generate_python_module(&ast, &PythonGenOptions::default()).expect("gen")
}
fn out_dir() -> PathBuf {
let here = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let root = here
.parent()
.and_then(|p| p.parent())
.expect("workspace root");
let dir = root.join("crates/py/python/tests/_generated");
std::fs::create_dir_all(&dir).expect("mkdir _generated");
dir
}
fn write_module(name: &str, src: &str) {
let py = emit_py(src);
let path = out_dir().join(format!("{name}.py"));
std::fs::write(&path, py).expect("write generated module");
}
#[test]
fn generate_python_modules_for_pytest() {
write_module(
"optionals_gen",
"module conf { struct Optionals { \
long required; @optional long maybe; @optional string note; \
}; };",
);
write_module(
"consts_gen",
"const long MAX_ITEMS = 10; \
const double RATE = 2.5; \
const boolean ENABLED = TRUE; \
module conf { const long N = 4; };",
);
write_module(
"combo_gen",
"module combo { \
enum Mode { MODE_IDLE, MODE_ACTIVE, MODE_FAULT }; \
typedef double CurrentInAmpsType; \
struct Sample { long seq; double value; }; \
union Reading switch (Mode) { \
case MODE_IDLE: long idleTicks; \
case MODE_ACTIVE: double activeRate; \
default: string faultCode; \
}; \
struct Telemetry { \
long unitId; \
Mode mode; \
CurrentInAmpsType batteryCurrent; \
sequence<Sample> history; \
Reading reading; \
@optional double calibration; \
long window[4]; \
}; \
};",
);
write_module("chars_gen", "struct Chars { char c; wstring w; };");
write_module(
"arrays_gen",
"module conf { \
struct Point { long x; long y; }; \
struct Arrays { long grid[2][2]; Point shape[2]; }; \
};",
);
write_module(
"bounds_gen",
"module b { struct Bounded { \
sequence<long, 3> nums; \
string<5> name; \
wstring<3> wname; \
map<string, long, 2> counters; \
sequence<long> unbounded; \
string fullstr; \
}; };",
);
write_module(
"deepnest_gen",
"module d { \
struct L3 { long v; }; \
struct L2 { L3 inner; }; \
struct L1 { L2 inner; }; \
struct HasSeq { sequence<long> xs; }; \
struct Deep { \
L1 chain; \
sequence<sequence<L3>> grid; \
map<string, HasSeq> table; \
}; \
};",
);
write_module(
"optagg_gen",
"module o { \
struct Inner { long v; }; \
struct OptAgg { \
@optional Inner nested; \
@optional sequence<long> nums; \
@optional map<string, long> table; \
@optional string note; \
}; \
};",
);
write_module("unicode_gen", "struct Uni { string s; wstring w; };");
write_module(
"extremes_gen",
"struct Extremes { \
int8 i8; uint8 u8; \
int16 i16; uint16 u16; \
int32 i32; uint32 u32; \
int64 i64; uint64 u64; \
float f; double dbl; \
};",
);
write_module(
"keyed_gen",
"struct Keyed { @key long id; string label; long long payload; };",
);
}