Skip to content

Commit a56ef14

Browse files
committed
test macro
1 parent 2015550 commit a56ef14

File tree

8 files changed

+248
-6
lines changed

8 files changed

+248
-6
lines changed

Cargo.lock

+86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[workspace]
2+
members = ["mono-proc"]
3+
14
[package]
25
name = "monoOS"
36
version = "0.1.0"
@@ -6,11 +9,13 @@ description = "A hobbyist monolithic operating system, targeting x86-64."
69
readme = "README.md"
710
license = "MIT"
811
authors = ["binds <[email protected]>"]
12+
default-run = "monoos"
913

1014
[dependencies]
1115
complete-pic = { version = "0.3.1", default-features = false, features = ["8259pic"] }
1216
limine = "0.1.11"
1317
log = "0.4.19"
18+
mono-proc = { version = "0.1.0", path = "mono-proc" }
1419
spin = { version = "0.9.8", default-features = false, features = ["spin_mutex", "once", "lazy"] }
1520
uart_16550 = "0.3.0"
1621
x86_64 = "0.14.10"

Makefile

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ override ESP := $(BUILD_ROOT)/EFI/BOOT
3737
override KERNEL_BIN := $(BUILD_ROOT)/monoos.elf
3838

3939
# Targets
40-
.PHONY: all iso run miri clean
40+
.PHONY: all iso run test miri clean
4141
all: iso
4242

4343
gen_build_dirs:
@@ -54,7 +54,10 @@ ovmf:
5454
kernel_build:
5555
@cargo build $(CARGO_ARGS)
5656

57-
$(ISO): gen_build_dirs limine ovmf kernel_build
57+
kernel_test:
58+
@cargo test $(CARGO_ARGS)
59+
60+
$(ISO): gen_build_dirs limine ovmf
5861
@cp target/x86_64-unknown-none/$(PROFILE)/monoos $(KERNEL_BIN)
5962
@cp limine.cfg $(LIMINE_DIR)/limine-uefi-cd.bin $(BUILD_ROOT)
6063
@cp $(LIMINE_DIR)/BOOTX64.EFI $(ESP)
@@ -63,7 +66,11 @@ $(ISO): gen_build_dirs limine ovmf kernel_build
6366
# Convenience target for $(ISO)
6467
iso: $(ISO)
6568

66-
run: iso
69+
run: kernel_build iso
70+
@qemu-system-x86_64 $(QEMU_ARGS)
71+
72+
test: kernel_test iso
73+
override QEMU_ARGS += -display none
6774
@qemu-system-x86_64 $(QEMU_ARGS)
6875

6976
miri:

mono-proc/Cargo.lock

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mono-proc/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "mono-proc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
proc-macro-error = "1.0.4"
11+
proc-macro2 = "1.0.66"
12+
quote = "1.0.33"
13+
syn = { version = "2.0.29", features = ["full"] }

mono-proc/src/lib.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use proc_macro::TokenStream;
2+
use proc_macro_error::proc_macro_error;
3+
use syn::ItemFn;
4+
5+
/// Attribute that tests are marked with.
6+
#[proc_macro_error]
7+
#[proc_macro_attribute]
8+
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
9+
let attr = attr.to_string();
10+
let quiet = if attr.contains("quiet") { true } else { false };
11+
12+
let input = syn::parse_macro_input!(item as ItemFn);
13+
let name = &input.sig.ident;
14+
let body = &input.block;
15+
16+
let test_marker = quote::format_ident!("{name}_test_marker");
17+
let result = quote::quote! {
18+
#[test_case]
19+
static #test_marker: crate::tests::Test = crate::tests::Test {
20+
path: concat!(module_path!(), "::", stringify!(#name)),
21+
func: #name,
22+
quiet: #quiet
23+
};
24+
25+
fn #name() {
26+
#body
27+
}
28+
};
29+
30+
result.into()
31+
}

src/main.rs

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ mod cpu;
2121
mod drivers;
2222
mod logger;
2323
mod mem;
24+
25+
#[cfg(test)]
2426
mod tests;
2527

2628
use core::{panic::PanicInfo, sync::atomic::Ordering};
@@ -65,6 +67,9 @@ extern "C" fn kmain() -> ! {
6567
// drivers::graphics::init(framebuffer);
6668
// log::info!("initialized graphics driver");
6769

70+
#[cfg(test)]
71+
test_main();
72+
6873
hlt()
6974
}
7075

src/tests.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub struct Test {
2-
pub name: &'static str,
2+
pub path: &'static str,
33
pub func: fn(),
44
pub quiet: bool,
55
}
@@ -14,16 +14,24 @@ pub fn test_runner(tests: &[&Test]) {
1414
(test.func)();
1515

1616
if !test.quiet {
17-
log::info!("test {} ... ok", test.name);
17+
log::info!("test {} ... ok", test.path);
1818
} else {
1919
suppressed += 1;
2020
}
2121
}
2222

2323
log::info!("");
2424
log::info!(
25-
"tests completed. {} successful; {} suppressed",
25+
"tests completed. {} successful; {} suppressed",
2626
successful,
2727
suppressed
2828
);
2929
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
#[mono_proc::test]
34+
fn testtest() {
35+
assert!(true);
36+
}
37+
}

0 commit comments

Comments
 (0)