Files
windmill/backend/windmill-macros/tests/annotations.rs
pyranota c3ba832444 Annotations refactor (#4512)
* Refactor annotation system

* Move tests to different location

* Limit annotations per line (15)

* Write initial benchmark

* Optimize

* More optimizations

* Rewrite with 3x speed!

* Remove benchmarks

They were in wrong place and for development

* Polishing

* Remove unused leftover

* Integrate new annotation system

* Add comment explanation

* Remove `hashe-based matching`

Turned to be it is impure and non-deterministic method

* Add `tracing::error` for `// native` without `deno_core`

If you build backend without `deno_core` feature flag, and add //native
annotation to ts script, it would just silently exit.

This commit prints error to logs

* add more tests

* Update annotations.rs

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
2024-10-17 09:14:31 +02:00

170 lines
4.5 KiB
Rust

#[cfg(test)]
mod annotations_tests {
extern crate windmill_macros;
use itertools::Itertools;
use windmill_macros::annotations;
// Previous implementation.
// We have to make sure that new one works the same as old one
fn old(inner_content: &str) -> Annotations {
let annotations = inner_content
.lines()
.take_while(|x| x.starts_with("#"))
.map(|x| x.to_string().replace("#", "").trim().to_string())
.collect_vec();
let ann1: bool = annotations.contains(&"ann1".to_string());
let ann2: bool = annotations.contains(&"ann2".to_string());
let ann3: bool = annotations.contains(&"ann3".to_string());
let ann4: bool = annotations.contains(&"ann4".to_string());
let ann5: bool = annotations.contains(&"ann5".to_string());
Annotations { ann1, ann2, ann3, ann4, ann5 }
}
#[annotations("#")]
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct Annotations {
pub ann1: bool,
pub ann2: bool,
pub ann3: bool,
pub ann4: bool,
pub ann5: bool,
}
#[annotations("//")]
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct SlashedAnnotations {
pub ann1: bool,
pub ann2: bool,
pub ann3: bool,
pub ann4: bool,
}
#[annotations("--")]
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct MinusedAnnotations {
pub ann1: bool,
pub ann2: bool,
}
// e.g. rust, TS and JS
#[test]
fn slashed_annotations() {
let cont = "// ann1
// ann2
//ann3";
assert_eq!(
SlashedAnnotations { ann1: true, ann2: true, ann3: true, ann4: false },
SlashedAnnotations::parse(cont)
);
}
// e.g. Haskell, SQL
#[test]
fn minused_annotations() {
let cont = "-- ann1
-- ann2";
assert_eq!(
MinusedAnnotations { ann1: true, ann2: true },
MinusedAnnotations::parse(cont)
);
}
#[test]
fn simple_integration() {
let cont = "# ann1";
let expected = Annotations { ann1: true, ..Default::default() };
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
#[test]
fn multiline_integration() {
let cont = "# ann2
# ann3
# ann4
# ann5
";
let expected = Annotations {
ann1: false,
ann2: true,
ann3: true,
ann4: true,
ann5: true,
//
};
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
#[test]
fn spacing_integration() {
// First line is ignored and not used
{
let cont = "
# ann2";
let expected = Annotations { ..Default::default() };
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
// Wrong spacing for ann3
{
let cont = "# ann2
# ann3";
let expected = Annotations { ann2: true, ..Default::default() };
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
// Drunk but valid spacing
{
let cont = "#ann1
# ann2";
let expected = Annotations { ann2: true, ann1: true, ..Default::default() };
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
}
#[test]
fn comments_inbetween_integration() {
let cont = "# ann2
# Just comment, has nothing to do with annotations
# Another comment: ann1 ann2 ann3
# ann4 is not valid annotation
# Actual annotation next line:
# ann5
# Should be ignored
# ann3
";
let expected = Annotations { ann2: true, ann5: true, ..Default::default() };
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
#[test]
fn hash_collision() {
// TODO
}
#[test]
fn non_matching_integration() {
{
let cont = r#" "ann1", ann2 "#;
let expected = Annotations::default();
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
// Empty
{
let cont = "";
let expected = Annotations::default();
assert_eq!(expected, old(cont));
assert_eq!(expected, Annotations::parse(cont));
}
}
}