|
1 | 1 | #![cfg(test)] |
| 2 | +use std::sync::Mutex; |
| 3 | + |
2 | 4 | use petgraph::dot::{Config, Dot}; |
3 | 5 | use proc_macro2::TokenStream; |
4 | 6 |
|
5 | | -use crate::{AscentMacroKind, ascent_impl}; |
| 7 | +use crate::{AscentMacroKind, ascent_impl, ascent_source_impl}; |
6 | 8 |
|
7 | | -#[test] |
8 | | -fn test_macro0() { |
9 | | - let inp = quote! { |
10 | | - struct Polonius<T: FactTypes>; |
11 | | - relation subset(T::Origin, T::Origin, T::Point);// = ctx.subset_base.clone(); |
12 | | - relation cfg_edge(T::Point, T::Point); |
13 | | - relation origin_live_on_entry(T::Origin, T::Point); |
14 | | - relation origin_contains_loan_on_entry(T::Origin, T::Loan, T::Point); |
15 | | - relation loan_live_at(T::Loan, T::Point); |
16 | | - relation loan_invalidated_at(T::Loan, T::Point); |
17 | | - relation errors(T::Loan, T::Point); |
18 | | - relation placeholder_origin(T::Origin); |
19 | | - relation subset_error(T::Origin, T::Origin, T::Point); |
20 | | - relation loan_killed_at(T::Loan, T::Point);// = loan_killed_at.iter().cloned().collect(); |
21 | | - relation known_placeholder_subset(T::Origin, T::Origin);// = known_placeholder_subset.iter().cloned().collect(); |
22 | | - |
23 | | - subset(origin1, origin3, point) <-- |
24 | | - subset(origin1, origin2, point), |
25 | | - subset(origin2, origin3, point), |
26 | | - if origin1 != origin3; |
27 | | - |
28 | | - subset(origin1, origin2, point2) <-- |
29 | | - subset(origin1, origin2, point1), |
30 | | - cfg_edge(point1, point2), |
31 | | - origin_live_on_entry(origin1, point2), |
32 | | - origin_live_on_entry(origin2, point2); |
33 | | - |
34 | | - origin_contains_loan_on_entry(origin2, loan, point) <-- |
35 | | - origin_contains_loan_on_entry(origin1, loan, point), |
36 | | - subset(origin1, origin2, point); |
37 | | - |
38 | | - origin_contains_loan_on_entry(origin, loan, point2) <-- |
39 | | - origin_contains_loan_on_entry(origin, loan, point1), |
40 | | - cfg_edge(point1, point2), |
41 | | - !loan_killed_at(loan, point1), |
42 | | - origin_live_on_entry(origin, point2); |
43 | | - |
44 | | - loan_live_at(loan, point) <-- |
45 | | - origin_contains_loan_on_entry(origin, loan, point), |
46 | | - origin_live_on_entry(origin, point); |
47 | | - |
48 | | - errors(loan, point) <-- |
49 | | - loan_invalidated_at(loan, point), |
50 | | - loan_live_at(loan, point); |
51 | | - |
52 | | - subset_error(origin1, origin2, point) <-- |
53 | | - subset(origin1, origin2, point), |
54 | | - placeholder_origin(origin1), |
55 | | - placeholder_origin(origin2), |
56 | | - !known_placeholder_subset(origin1, origin2), |
57 | | - if origin1 != origin2; |
| 9 | +enum TestAscentMacroKind { |
| 10 | + Ascent(AscentMacroKind), |
| 11 | + AscentSource, |
| 12 | +} |
| 13 | +impl From<AscentMacroKind> for TestAscentMacroKind { |
| 14 | + fn from(value: AscentMacroKind) -> Self { Self::Ascent(value) } |
| 15 | +} |
| 16 | + |
| 17 | +fn write_to_scratchpad_base(tokens: TokenStream, prefix: TokenStream, kind: TestAscentMacroKind) -> TokenStream { |
| 18 | + static LOCK: Mutex<()> = Mutex::new(()); |
| 19 | + let code = match kind { |
| 20 | + TestAscentMacroKind::Ascent(ascent_macro_kind) => ascent_impl(tokens, ascent_macro_kind).unwrap(), |
| 21 | + TestAscentMacroKind::AscentSource => ascent_source_impl(tokens).unwrap(), |
58 | 22 | }; |
59 | | - // write_ascent_run_to_scratchpad(inp); |
60 | | - write_ascent_run_par_to_scratchpad(inp); |
| 23 | + let template = std::fs::read_to_string("src/scratchpad_template.rs").unwrap(); |
| 24 | + let code_in_template = template.replace("todo!(\"here\");", &code.to_string()); |
| 25 | + let _lock = LOCK.lock().unwrap(); |
| 26 | + std::fs::write("examples/scratchpad.rs", prefix.to_string()).unwrap(); |
| 27 | + std::fs::write("examples/scratchpad.rs", code_in_template).unwrap(); |
| 28 | + std::process::Command::new("rustfmt").args(["examples/scratchpad.rs"]).spawn().unwrap().wait().unwrap(); |
| 29 | + code |
| 30 | +} |
| 31 | + |
| 32 | +fn write_to_scratchpad(tokens: TokenStream) -> TokenStream { |
| 33 | + write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: false, is_parallel: false }.into()) |
| 34 | +} |
| 35 | + |
| 36 | +fn write_ascent_source_to_scratchpad(tokens: TokenStream) -> TokenStream { |
| 37 | + write_to_scratchpad_base(tokens, quote! {}, TestAscentMacroKind::AscentSource) |
| 38 | +} |
| 39 | + |
| 40 | +fn write_with_prefix_to_scratchpad(tokens: TokenStream, prefix: TokenStream) -> TokenStream { |
| 41 | + write_to_scratchpad_base(tokens, prefix, AscentMacroKind { is_ascent_run: false, is_parallel: false }.into()) |
| 42 | +} |
| 43 | + |
| 44 | +fn write_par_to_scratchpad(tokens: TokenStream) -> TokenStream { |
| 45 | + write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: false, is_parallel: true }.into()) |
| 46 | +} |
| 47 | + |
| 48 | +#[allow(unused)] |
| 49 | +fn write_ascent_run_to_scratchpad(tokens: TokenStream) -> TokenStream { |
| 50 | + write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: true, is_parallel: false }.into()) |
61 | 51 | } |
| 52 | + |
| 53 | +#[allow(unused)] |
| 54 | +fn write_ascent_run_par_to_scratchpad(tokens: TokenStream) -> TokenStream { |
| 55 | + write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: true, is_parallel: true }.into()) |
| 56 | +} |
| 57 | + |
| 58 | + |
62 | 59 | #[test] |
63 | 60 | fn test_macro_generic_tc() { |
64 | 61 | let inp = quote! { |
@@ -410,38 +407,6 @@ fn exp_items_in_fn() { |
410 | 407 | println!("point is {:?}, with size {}", p, p.size()); |
411 | 408 | } |
412 | 409 |
|
413 | | -fn write_to_scratchpad_base( |
414 | | - tokens: TokenStream, prefix: TokenStream, is_ascent_run: bool, is_parallel: bool, |
415 | | -) -> TokenStream { |
416 | | - let code = ascent_impl(tokens, AscentMacroKind { is_ascent_run, is_parallel }); |
417 | | - let code = code.unwrap(); |
418 | | - let template = std::fs::read_to_string("src/scratchpad_template.rs").unwrap(); |
419 | | - let code_in_template = template.replace("todo!(\"here\");", &code.to_string()); |
420 | | - std::fs::write("src/scratchpad.rs", prefix.to_string()).unwrap(); |
421 | | - std::fs::write("src/scratchpad.rs", code_in_template).unwrap(); |
422 | | - std::process::Command::new("rustfmt").args(["src/scratchpad.rs"]).spawn().unwrap().wait().unwrap(); |
423 | | - code |
424 | | -} |
425 | | - |
426 | | -fn write_to_scratchpad(tokens: TokenStream) -> TokenStream { write_to_scratchpad_base(tokens, quote! {}, false, false) } |
427 | | - |
428 | | -fn write_with_prefix_to_scratchpad(tokens: TokenStream, prefix: TokenStream) -> TokenStream { |
429 | | - write_to_scratchpad_base(tokens, prefix, false, false) |
430 | | -} |
431 | | - |
432 | | -fn write_par_to_scratchpad(tokens: TokenStream) -> TokenStream { |
433 | | - write_to_scratchpad_base(tokens, quote! {}, false, true) |
434 | | -} |
435 | | - |
436 | | -#[allow(unused)] |
437 | | -fn write_ascent_run_to_scratchpad(tokens: TokenStream) -> TokenStream { |
438 | | - write_to_scratchpad_base(tokens, quote! {}, true, false) |
439 | | -} |
440 | | - |
441 | | -fn write_ascent_run_par_to_scratchpad(tokens: TokenStream) -> TokenStream { |
442 | | - write_to_scratchpad_base(tokens, quote! {}, true, true) |
443 | | -} |
444 | | - |
445 | 410 | #[test] |
446 | 411 | fn test_macro_lambda_calc() { |
447 | 412 | let prefix = quote! { |
@@ -543,3 +508,23 @@ fn test_macro_in_macro() { |
543 | 508 |
|
544 | 509 | write_to_scratchpad(inp); |
545 | 510 | } |
| 511 | + |
| 512 | +#[test] |
| 513 | +fn test_include_source() { |
| 514 | + let inp = quote! { |
| 515 | + // relation before(); |
| 516 | + include_source!(my_ascent_source); |
| 517 | + relation after(); |
| 518 | + }; |
| 519 | + |
| 520 | + write_to_scratchpad(inp); |
| 521 | +} |
| 522 | + |
| 523 | +#[test] |
| 524 | +fn test_ascent_source() { |
| 525 | + let inp = quote! { test_ascent_source: |
| 526 | + relation in_ascent_source(i32); |
| 527 | + in_ascent_source(42); |
| 528 | + }; |
| 529 | + write_ascent_source_to_scratchpad(inp); |
| 530 | +} |
0 commit comments