Skip to content

Commit

Permalink
test: cel spec
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Feb 19, 2024
1 parent e0d8f85 commit 7732e95
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "conformance_tests/cel-spec"]
path = conformance_tests/cel-spec
url = [email protected]:google/cel-spec.git
[submodule "conformance_tests/googleapis"]
path = conformance_tests/googleapis
url = [email protected]:googleapis/googleapis.git
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["parser", "program", "web"]
members = ["parser", "program", "web", "cel_spec"]
resolver = "1"
17 changes: 17 additions & 0 deletions cel_spec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "cel_spec"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
prost = "0.12"
prost-types = "0.12"
prost-reflect = { version = "0.13.0", features = ["text-format"]}
quote = "1.0.35"
program = { path = "../program" }

[build_dependencies]
prost-build = "0.12.3"
13 changes: 13 additions & 0 deletions cel_spec/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::{env, io::Result, path::PathBuf};

fn main() -> Result<()> {
let out = env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
let out = PathBuf::from(out).join("cel.bin");
let mut config = prost_build::Config::new();
config.disable_comments(&["."]);
config.file_descriptor_set_path(out);
config.compile_protos(
&["cel-spec/proto/test/v1/simple.proto"],
&["cel-spec/proto/", "googleapis/"],
)
}
44 changes: 44 additions & 0 deletions cel_spec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use proc_macro::TokenStream;
use quote::{format_ident, quote, ToTokens};

const BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/cel.bin"));

#[proc_macro]
pub fn cel_test(attr: TokenStream) -> TokenStream {
println!("attr: \"{}\"", attr.to_string());
use prost_reflect::{DescriptorPool, DynamicMessage};

let pool = DescriptorPool::decode(BYTES.as_ref()).unwrap();
let message_descriptor = pool.get_message_by_name("google.api.expr.test.v1.SimpleTestFile").unwrap();
let bytes = include_str!("../cel-spec/tests/simple/testdata/basic.textproto");
let suite = DynamicMessage::parse_text_format(message_descriptor, &bytes).unwrap();

let mut ast = String::new();
for section in suite.get_field_by_name("section").unwrap().as_list().unwrap() {
let section = section.as_message().unwrap();
let sname = section.get_field_by_name("name").unwrap();

ast.push_str(format!("pub mod {}{{", sname.as_str().unwrap()).as_str());

for case in section.get_field_by_name("test").unwrap().as_list().unwrap() {
let case = case.as_message().unwrap();
let name = case.get_field_by_name("name").unwrap();
let expr = case.get_field_by_name("expr").unwrap();

let name = format_ident!("{}", name.as_str().unwrap());
let expr = expr.as_str().unwrap();
ast.push_str(
&quote!{
#[test]
fn #name() {
assert!(program::Program::new(#expr).expect("failed to compile").execute(program::context::Context::default()));
}
}.to_string()
)
}

ast.push_str("}");
break;
}
ast.parse().unwrap()
}
6 changes: 6 additions & 0 deletions cel_spec/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

extern crate cel_spec;

cel_spec::cel_test!(suite = "basic");


1 change: 1 addition & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
parser = {path = "../parser"}
ordered-float = "4.2.0"
ordered_hash_map = "0.4.0"
prost-reflect = "0.13.0"
3 changes: 0 additions & 3 deletions web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ crate-type = ["cdylib"]
[dependencies]
program = {path = "../program"}
wasm-bindgen = "0.2"

[profile.release]
panic = "unwind"

0 comments on commit 7732e95

Please sign in to comment.