Skip to content

Commit

Permalink
Move test measure function to the tests directory
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Aug 22, 2023
1 parent 24b8faf commit 014ade6
Show file tree
Hide file tree
Showing 166 changed files with 435 additions and 512 deletions.
85 changes: 3 additions & 82 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ async fn main() {
fs::write(test_filename, test_body.to_string()).unwrap();
}

let generic_measure_function = generate_generic_measure_function();
let generic_measure_token = quote!(#generic_measure_function);
writeln!(&mut base_mod_file, "{}", generic_measure_token).unwrap();

info!("formatting the source directory");
Command::new("cargo").arg("fmt").current_dir(repo_root).status().unwrap();
}
Expand Down Expand Up @@ -871,85 +867,10 @@ fn generate_scalar_definition(track_definition: &serde_json::Map<String, Value>)
}
}

fn generate_generic_measure_function() -> TokenStream {
quote!(
// WARNING: This enum is generated by the gentest script. Do not edit directly
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WritingMode {
Horizontal,
Vertical,
}

// WARNING: This function is generated by the gentest script. Do not edit directly
#[allow(dead_code)]
fn measure_standard_text(
known_dimensions: taffy::geometry::Size<Option<f32>>,
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
text_content: &str,
writing_mode: WritingMode,
_aspect_ratio: Option<f32>,
) -> taffy::geometry::Size<f32> {
use taffy::geometry::AbsoluteAxis;
use taffy::prelude::*;
const ZWS: char = '\u{200B}';
const H_WIDTH: f32 = 10.0;
const H_HEIGHT: f32 = 10.0;

if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}

let inline_axis = match writing_mode {
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
WritingMode::Vertical => AbsoluteAxis::Vertical,
};
let block_axis = inline_axis.other_axis();

let lines: Vec<&str> = text_content.split(ZWS).collect();
if lines.is_empty() {
return Size::ZERO;
}

let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
let inline_size =
known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
AvailableSpace::Definite(inline_size) => {
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
}
});
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
let mut line_count = 1;
let mut current_line_length = 0;
for line in &lines {
if current_line_length + line.len() > inline_line_length {
if current_line_length > 0 {
line_count += 1
};
current_line_length = line.len();
} else {
current_line_length += line.len();
};
}
(line_count as f32) * H_HEIGHT
});

match writing_mode {
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
WritingMode::Vertical => Size { width: block_size, height: inline_size },
}
}
)
}

fn generate_measure_function(text_content: &str, writing_mode: Option<&str>, aspect_ratio: Option<f32>) -> TokenStream {
let writing_mode_token = match writing_mode {
Some("vertical-rl" | "vertical-lr") => quote!(crate::generated::WritingMode::Vertical),
_ => quote!(crate::generated::WritingMode::Horizontal),
Some("vertical-rl" | "vertical-lr") => quote!(crate::WritingMode::Vertical),
_ => quote!(crate::WritingMode::Horizontal),
};

let aspect_ratio_token = match aspect_ratio {
Expand All @@ -960,7 +881,7 @@ fn generate_measure_function(text_content: &str, writing_mode: Option<&str>, asp
quote!(
taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
const TEXT : &str = #text_content;
crate::generated::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
crate::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
})
)
}
64 changes: 64 additions & 0 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,67 @@
// this allows us to both keep code generation scoped to a singe directory for fs events
// and to keep each test in a separate file
mod generated;

#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WritingMode {
Horizontal,
Vertical,
}

#[allow(dead_code)]
fn measure_standard_text(
known_dimensions: taffy::geometry::Size<Option<f32>>,
available_space: taffy::geometry::Size<taffy::style::AvailableSpace>,
text_content: &str,
writing_mode: WritingMode,
_aspect_ratio: Option<f32>,
) -> taffy::geometry::Size<f32> {
use taffy::geometry::AbsoluteAxis;
use taffy::prelude::*;
const ZWS: char = '\u{200B}';
const H_WIDTH: f32 = 10.0;
const H_HEIGHT: f32 = 10.0;
if let Size { width: Some(width), height: Some(height) } = known_dimensions {
return Size { width, height };
}
let inline_axis = match writing_mode {
WritingMode::Horizontal => AbsoluteAxis::Horizontal,
WritingMode::Vertical => AbsoluteAxis::Vertical,
};
let block_axis = inline_axis.other_axis();
let lines: Vec<&str> = text_content.split(ZWS).collect();
if lines.is_empty() {
return Size::ZERO;
}
let min_line_length: usize = lines.iter().map(|line| line.len()).max().unwrap_or(0);
let max_line_length: usize = lines.iter().map(|line| line.len()).sum();
let inline_size =
known_dimensions.get_abs(inline_axis).unwrap_or_else(|| match available_space.get_abs(inline_axis) {
AvailableSpace::MinContent => min_line_length as f32 * H_WIDTH,
AvailableSpace::MaxContent => max_line_length as f32 * H_WIDTH,
AvailableSpace::Definite(inline_size) => {
inline_size.min(max_line_length as f32 * H_WIDTH).max(min_line_length as f32 * H_WIDTH)
}
});
let block_size = known_dimensions.get_abs(block_axis).unwrap_or_else(|| {
let inline_line_length = (inline_size / H_WIDTH).floor() as usize;
let mut line_count = 1;
let mut current_line_length = 0;
for line in &lines {
if current_line_length + line.len() > inline_line_length {
if current_line_length > 0 {
line_count += 1
};
current_line_length = line.len();
} else {
current_line_length += line.len();
};
}
(line_count as f32) * H_HEIGHT
});
match writing_mode {
WritingMode::Horizontal => Size { width: inline_size, height: block_size },
WritingMode::Vertical => Size { width: block_size, height: inline_size },
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/generated/block/block_aspect_ratio_fill_max_width.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/generated/block/block_aspect_ratio_fill_min_width.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/generated/blockflex/blockflex_overflow_hidden.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/generated/blockgrid/blockgrid_block_in_grid_auto.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 014ade6

Please sign in to comment.