Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix re_types_builder tests for doc generation #8370

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 146 additions & 152 deletions crates/build/re_types_builder/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ mod doclink_translation {
})
}

fn tokenize(input: &str) -> Vec<&str> {
pub(super) fn tokenize(input: &str) -> Vec<&str> {
tokenize_with(input, &['[', ']', '`', '.'])
}

Expand All @@ -458,166 +458,157 @@ mod doclink_translation {
}
tokens
}
}

#[cfg(test)]
mod tests {
use crate::{Attributes, Docs, Object, Objects};

use super::*;

fn test_objects() -> Objects {
Objects {
objects: std::iter::once((
"rerun.views.Spatial2DView".to_owned(),
Object {
virtpath: "path".to_owned(),
filepath: "path".into(),
fqname: "rerun.views.Spatial2DView".to_owned(),
pkg_name: "test".to_owned(),
name: "Spatial2DView".to_owned(),
docs: Docs::default(),
kind: ObjectKind::View,
attrs: Attributes::default(),
fields: Vec::new(),
class: crate::ObjectClass::Struct,
datatype: None,
},
))
.collect(),
}
#[cfg(test)]
mod tests {
use crate::{
codegen::Target,
docs::doclink_translation::{tokenize, translate_doc_line},
Attributes, Docs, Object, ObjectKind, Objects,
};

fn test_objects() -> Objects {
Objects {
objects: std::iter::once((
"rerun.views.Spatial2DView".to_owned(),
Object {
virtpath: "path".to_owned(),
filepath: "path".into(),
fqname: "rerun.views.Spatial2DView".to_owned(),
pkg_name: "test".to_owned(),
name: "Spatial2DView".to_owned(),
docs: Docs::default(),
kind: ObjectKind::View,
attrs: Attributes::default(),
fields: Vec::new(),
class: crate::ObjectClass::Struct,
datatype: None,
},
))
.collect(),
}
}

#[test]
fn test_tokenize() {
assert_eq!(tokenize("This is a comment"), vec!["This is a comment"]);
assert_eq!(
tokenize("A vector `[1, 2, 3]` and a doclink [archetype.Image]."),
vec![
"A vector ",
"`",
"[",
"1, 2, 3",
"]",
"`",
" and a doclink ",
"[",
"archetype",
".",
"Image",
"]",
"."
]
);
}
#[test]
fn test_tokenize() {
assert_eq!(tokenize("This is a comment"), vec!["This is a comment"]);
assert_eq!(
tokenize("A vector `[1, 2, 3]` and a doclink [archetype.Image]."),
vec![
"A vector ",
"`",
"[",
"1, 2, 3",
"]",
"`",
" and a doclink ",
"[",
"archetype",
".",
"Image",
"]",
"."
]
);
}

#[test]
fn test_translate_doclinks() {
let objects = test_objects();
let (_report, reporter) = crate::report::init();

let input =
"A vector `[1, 2, 3]` and a doclink [views.Spatial2DView] and a [url](www.rerun.io).";

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Cpp
),
"A vector `[1, 2, 3]` and a doclink `views::Spatial2DView` and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Python
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView`][rerun.blueprint.views.Spatial2DView] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Rust
),
"A vector `[1, 2, 3]` and a doclink [`views::Spatial2DView`][crate::blueprint::views::Spatial2DView] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::WebDocsMarkdown
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView`](https://rerun.io/docs/reference/types/views/spatial2d_view) and a [url](www.rerun.io)."
);
}
#[test]
fn test_translate_doclinks() {
let objects = test_objects();
let (_report, reporter) = crate::report::init();

#[test]
fn test_translate_doclinks_with_field() {
let objects = test_objects();
let (_report, reporter) = crate::report::init();

let input =
"A vector `[1, 2, 3]` and a doclink [views.Spatial2DView.position] and a [url](www.rerun.io).";

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Cpp
),
"A vector `[1, 2, 3]` and a doclink `views::Spatial2DView::position` and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Python
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView.position`][rerun.blueprint.views.Spatial2DView.position] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Rust
),
"A vector `[1, 2, 3]` and a doclink [`views::Spatial2DView::position`][crate::blueprint::views::Spatial2DView::position] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::WebDocsMarkdown
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView#position`](https://rerun.io/docs/reference/types/views/spatial2d_view) and a [url](www.rerun.io)."
);
}
let input =
"A vector `[1, 2, 3]` and a doclink [views.Spatial2DView] and a [url](www.rerun.io).";

assert_eq!(
translate_doc_line(&reporter, &objects, input, Target::Cpp),
"A vector `[1, 2, 3]` and a doclink `views::Spatial2DView` and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Python
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView`][rerun.blueprint.views.Spatial2DView] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Rust
),
"A vector `[1, 2, 3]` and a doclink [`views::Spatial2DView`][crate::blueprint::views::Spatial2DView] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::WebDocsMarkdown
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView`](https://rerun.io/docs/reference/types/views/spatial2d_view) and a [url](www.rerun.io)."
);
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_translate_doclinks_with_field() {
let objects = test_objects();
let (_report, reporter) = crate::report::init();

let input =
"A vector `[1, 2, 3]` and a doclink [views.Spatial2DView.position] and a [url](www.rerun.io).";

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Cpp
),
"A vector `[1, 2, 3]` and a doclink `views::Spatial2DView::position` and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Python
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView.position`][rerun.blueprint.views.Spatial2DView.position] and a [url](www.rerun.io)."
);

assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::Rust
),
"A vector `[1, 2, 3]` and a doclink [`views::Spatial2DView::position`][crate::blueprint::views::Spatial2DView::position] and a [url](www.rerun.io)."
);

use super::*;
assert_eq!(
translate_doc_line(
&reporter,
&objects,
input,
Target::WebDocsMarkdown
),
"A vector `[1, 2, 3]` and a doclink [`views.Spatial2DView#position`](https://rerun.io/docs/reference/types/views/spatial2d_view) and a [url](www.rerun.io)."
);
}

#[test]
fn test_docs() {
let objects = Objects::default();
let objects = test_objects();
let (_report, reporter) = crate::report::init();

let docs = Docs::from_lines(
Expand Down Expand Up @@ -645,7 +636,7 @@ mod tests {
assert_eq!(
docs.lines_for(&reporter, &objects, Target::Python),
vec![
"Doclink to [`views.Spatial2DView`][rerun.views.Spatial2DView].",
"Doclink to [`views.Spatial2DView`][rerun.blueprint.views.Spatial2DView].",
"",
"The second line.",
"",
Expand All @@ -670,7 +661,10 @@ mod tests {

assert_eq!(
docs.first_line(&reporter, &objects, Target::Rust),
Some("Doclink to [`views::Spatial2DView`][crate::views::Spatial2DView].".to_owned())
Some(
"Doclink to [`views::Spatial2DView`][crate::blueprint::views::Spatial2DView]."
.to_owned()
)
);
}
}
Loading