Skip to content

Commit

Permalink
Sort test fixtures and generated tests into folders (#514)
Browse files Browse the repository at this point in the history
* Moved gridflex files into gridflex folder

* Moved blockflex files into blockflex folder

* Moved blockgrid files into blockgrid folder

* Moved leaf files into leaf folder

* Moved block files into block folder

* Moved grid files into grid folder

* Moved flex files into flex folder

* Updated script and link tags in test_fixtures

* Updated scripts to respect new folder structure

* Moved xgrid files into the grid folder

* Generated tests with cargo gentest

* Moved generic_measure_function after mod includes
  • Loading branch information
aemreaydin authored Aug 22, 2023
1 parent 47aa8d4 commit 24b8faf
Show file tree
Hide file tree
Showing 2,672 changed files with 16,741 additions and 16,717 deletions.
1 change: 1 addition & 0 deletions scripts/format-fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"

[dependencies]
regex = "1.8.1"
walkdir = "2.3.3"
6 changes: 3 additions & 3 deletions scripts/format-fixtures/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use std::{
};

use regex::{Captures, Regex};
use walkdir::WalkDir;

fn main() {
let fixtures_root = std::env::var("FIXTURE_DIR").map(PathBuf::from).unwrap_or_else(|_| {
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let repo_root = root_dir.parent().and_then(Path::parent).unwrap();
repo_root.join("test_fixtures")
});
let fixtures = fs::read_dir(fixtures_root).unwrap();

println!("reading test fixtures from disk");
let fixtures: Vec<_> = fixtures
let fixtures: Vec<_> = WalkDir::new(fixtures_root)
.into_iter()
.filter_map(|a| a.ok())
.filter(|f| f.path().is_file() && f.path().extension().map(|p| p == "html").unwrap_or(false))
.map(|f| {
let fixture_path = f.path();
let fixture_path = f.path().to_path_buf();
let name = fixture_path.file_stem().unwrap().to_str().unwrap().to_string();
(name, fixture_path)
})
Expand Down
1 change: 1 addition & 0 deletions scripts/gentest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ quote = "1.0.2"
serde_json = "1"
syn = "2.0.4"
tokio = { version = "1.18", features = ["full"] }
walkdir = "2.3.3"
79 changes: 45 additions & 34 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand All @@ -9,6 +10,7 @@ use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use serde_json::Value;
use syn::Ident;
use walkdir::WalkDir;

macro_rules! dim_quoted_renamed {
($obj:ident, $in_name:ident, $out_name:ident, $value_mapper:ident, $default:expr) => {
Expand Down Expand Up @@ -58,16 +60,15 @@ async fn main() {
let repo_root = root_dir.parent().and_then(Path::parent).unwrap();

let fixtures_root = repo_root.join("test_fixtures");
let fixtures = fs::read_dir(fixtures_root).unwrap();

info!("reading test fixtures from disk");
let mut fixtures: Vec<_> = fixtures
let mut fixtures: Vec<_> = WalkDir::new(fixtures_root.clone())
.into_iter()
.filter_map(|a| a.ok())
.filter(|f| !f.file_name().to_string_lossy().starts_with('x')) // ignore tests beginning with x
.filter(|f| f.path().is_file() && f.path().extension().map(|p| p == "html").unwrap_or(false))
.map(|f| {
let fixture_path = f.path();
let fixture_path = f.path().to_path_buf();
let name = fixture_path.file_stem().unwrap().to_str().unwrap().to_string();
(name, fixture_path)
})
Expand All @@ -94,8 +95,8 @@ async fn main() {
asserts_non_zero_width_scrollbars(client.clone()).await;

let mut test_descs = vec![];
for (name, fixture_path) in fixtures {
test_descs.push(test_root_element(client.clone(), name, fixture_path).await);
for (name, fixture_path) in fixtures.iter() {
test_descs.push(test_root_element(client.clone(), name.clone(), fixture_path).await);
}

info!("killing webdriver instance...");
Expand All @@ -105,41 +106,51 @@ async fn main() {

let test_descs: Vec<_> = test_descs
.iter()
.map(|(name, description)| {
.map(|(name, fixture_path, description)| {
debug!("generating test contents for {}", &name);
(name.clone(), generate_test(name, description))
(name.clone(), fixture_path, generate_test(name, description))
})
.collect();

let mod_statemnts = test_descs
.iter()
.map(|(name, _)| {
let name_ident = Ident::new(name, Span::call_site());
if name.starts_with("grid") {
quote!(#[cfg(feature = "grid")] mod #name_ident;)
} else {
quote!(mod #name_ident;)
}
})
.fold(quote!(), |a, b| quote!(#a #b));
let generic_measure_function = generate_generic_measure_function();

let test_mod_file = quote!(
#generic_measure_function
#mod_statemnts
);

info!("writing generated test file to disk...");
let tests_base_path = repo_root.join("tests").join("generated");
fs::remove_dir_all(&tests_base_path).unwrap();
fs::create_dir(&tests_base_path).unwrap();
for (name, test_body) in test_descs {
let mut test_filename = tests_base_path.join(&name);

// Open base mod.rs file for appending
let mut base_mod_file = OpenOptions::new().create(true).append(true).open(tests_base_path.join("mod.rs")).unwrap();

for (name, fixture_path, test_body) in test_descs {
// Create test directory if it doesn't exist
let test_path_stripped = fixture_path.parent().unwrap().strip_prefix(&fixtures_root).unwrap();
let test_path = tests_base_path.join(test_path_stripped);
if !test_path.exists() {
fs::create_dir(&test_path).unwrap();

let ident = Ident::new(test_path_stripped.to_str().unwrap(), Span::call_site());
let token = quote!(mod #ident;);
writeln!(&mut base_mod_file, "{}", token).unwrap();
}
// Open mod file in current folder
let mod_path = test_path.join("mod.rs");
let mut mod_file = OpenOptions::new().create(true).append(true).open(mod_path).unwrap();

let name_ident = Ident::new(&name, Span::call_site());
let token = if name.starts_with("grid") {
quote!(#[cfg(feature = "grid")] mod #name_ident;)
} else {
quote!(mod #name_ident;)
};
writeln!(&mut mod_file, "{}", token).unwrap();
let mut test_filename = test_path.join(&name);
test_filename.set_extension("rs");
debug!("writing {} to disk...", &name);
fs::write(test_filename, test_body.to_string()).unwrap();
}
fs::write(tests_base_path.join("mod.rs"), test_mod_file.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 All @@ -166,7 +177,7 @@ async fn asserts_non_zero_width_scrollbars(client: Client) {
}
}

async fn test_root_element(client: Client, name: String, fixture_path: impl AsRef<Path>) -> (String, Value) {
async fn test_root_element(client: Client, name: String, fixture_path: impl AsRef<Path>) -> (String, PathBuf, Value) {
let fixture_path = fixture_path.as_ref();

let url = format!("file://{}", fixture_path.display());
Expand All @@ -178,7 +189,7 @@ async fn test_root_element(client: Client, name: String, fixture_path: impl AsRe
.unwrap();
let description_string = description.as_str().unwrap();
let description = serde_json::from_str(description_string).unwrap();
(name, description)
(name.to_string(), fixture_path.to_path_buf(), description)
}

fn generate_test(name: impl AsRef<str>, description: &Value) -> TokenStream {
Expand Down Expand Up @@ -937,8 +948,8 @@ fn generate_generic_measure_function() -> TokenStream {

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!(super::WritingMode::Vertical),
_ => quote!(super::WritingMode::Horizontal),
Some("vertical-rl" | "vertical-lr") => quote!(crate::generated::WritingMode::Vertical),
_ => quote!(crate::generated::WritingMode::Horizontal),
};

let aspect_ratio_token = match aspect_ratio {
Expand All @@ -949,7 +960,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;
super::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
crate::generated::measure_standard_text(known_dimensions, available_space, TEXT, #writing_mode_token, #aspect_ratio_token)
})
)
}
3 changes: 3 additions & 0 deletions scripts/import-yoga-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ name = "import-yoga-test"
version = "0.1.0"
authors = ["Nico Burns"]
edition = "2021"

[dependencies]
walkdir = "2.3.3"
5 changes: 3 additions & 2 deletions scripts/import-yoga-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

struct YogaFixture {
#[allow(dead_code)]
Expand All @@ -16,8 +17,8 @@ fn main() {
let taffy_fixtures_dir = taffy_repo_root.join("test_fixtures");

// Get Taffy fixture names
let taffy_fixture_names = fs::read_dir(&taffy_fixtures_dir)
.unwrap()
let taffy_fixture_names = WalkDir::new(&taffy_fixtures_dir)
.into_iter()
.filter_map(|a| a.ok())
.map(|f| {
let file_name = f.file_name();
Expand Down

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_height.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_height_from_inset.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_max_height.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_max_width.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_min_height.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_min_width.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_width.html

This file was deleted.

17 changes: 0 additions & 17 deletions test_fixtures/absolute_aspect_ratio_fill_width_from_inset.html

This file was deleted.

Loading

0 comments on commit 24b8faf

Please sign in to comment.