Skip to content

Commit

Permalink
test: add fixtures to test function codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Jan 20, 2024
1 parent 01d7e83 commit 06ac6e5
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 31 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions frb_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ notify = "6.1.1"
notify-debouncer-mini = "0.4.1"

[dev-dependencies]
pretty_assertions = "1.4.0"
semver = "1.0.12"

[package.metadata.binstall]
Expand Down
6 changes: 6 additions & 0 deletions frb_codegen/src/library/codegen/generator/api_dart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ mod tests {
body("library/codegen/generator/api_dart/mod/simple")
}

#[test]
#[serial]
fn test_functions() -> anyhow::Result<()> {
body("library/codegen/generator/api_dart/mod/functions")
}

fn body(fixture_name: &str) -> anyhow::Result<()> {
configure_opinionated_test_logging();
let test_fixture_dir = get_test_fixture_dir(fixture_name);
Expand Down
34 changes: 21 additions & 13 deletions frb_codegen/src/library/commands/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use crate::codegen::dumper::Dumper;
use crate::codegen::ConfigDumpContent;
use crate::command_args;
use crate::library::commands::command_runner::execute_command;

use anyhow::{bail, Context, Result};
use itertools::Itertools;
use lazy_static::lazy_static;
use log::{debug, info, warn};
use regex::Regex;

use std::borrow::Cow;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::{env, fs};

#[derive(Default)]
Expand Down Expand Up @@ -41,7 +44,10 @@ impl CachedCargoExpand {

let expanded = match self.cache.entry(rust_crate_dir.to_owned()) {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(run_cargo_expand(rust_crate_dir, dumper, true)?),
Vacant(entry) => entry.insert(
unwrap_frb_attrs_in_doc(&run_cargo_expand(rust_crate_dir, dumper, true)?)
.into_owned(),
),
};

extract_module(expanded, module)
Expand Down Expand Up @@ -93,7 +99,7 @@ fn run_cargo_expand(
"--theme=none",
"--ugly",
"--config",
"build.rustflags=\"--cfg frb_expand\""
r#"build.rustflags="--cfg frb_expand""#
);

let output = execute_command("cargo", &args, Some(rust_crate_dir), None)
Expand All @@ -114,20 +120,22 @@ fn run_cargo_expand(
// frb-coverage:ignore-end
}

let mut stdout_lines = stdout.lines();
stdout_lines.next();
let ans = stdout_lines.join("\n");
static PATTERN: OnceLock<Regex> = OnceLock::new();
let pattern = PATTERN.get_or_init(|| {
Regex::new(r####"#\[doc =[\s\n]*r###"frb_marker: ([\s\S]*?)"###]"####).unwrap()
});
let ans = pattern.replace_all(&ans, "$1").into_owned();

let ans = stdout.lines().skip(1).join("\n");
dumper.dump_str(ConfigDumpContent::Source, "cargo_expand.rs", &ans)?;

Ok(ans)
}

/// Turns `#[doc = "frb_marker: .."]` back into `#[frb(..)]`, usually produced
/// as a side-effect of cargo-expand.
// NOTE: The amount of pounds must match exactly with the implementation in frb_macros
fn unwrap_frb_attrs_in_doc(code: &str) -> Cow<str> {
lazy_static! {
static ref PATTERN: Regex =
Regex::new(r####"#\[doc =[\s\n]*r###"frb_marker: ([\s\S]*?)"###]"####).unwrap();
}
PATTERN.replace_all(code, "$1")
}

fn install_cargo_expand() -> Result<()> {
execute_command(
"cargo",
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/commands/command_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ macro_rules! command_run {
}};
}

/// Formats a list of [`PathBuf`]s using the syntax detailed in [`run`].
/// Formats a list of [`PathBuf`]s using the syntax detailed in [`command_run`].
#[doc(hidden)]
#[macro_export]
macro_rules! command_args {
Expand Down
30 changes: 23 additions & 7 deletions frb_codegen/src/library/utils/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,43 @@ pub(crate) fn json_golden_test(
let actual: Value = serde_json::from_str(&actual_str)?;
debug!("json_golden_test sanitizers={sanitizers:?} actual:\n{actual_str}");

raw_golden_test(actual, &actual_str, matcher_path, |x| {
Ok(serde_json::from_str(&x)?)
})
raw_golden_test(
actual,
&actual_str,
matcher_path,
|x| Ok(serde_json::from_str(&x)?),
None,
)
}

pub(crate) fn text_golden_test(actual: String, matcher_path: &Path) -> anyhow::Result<()> {
raw_golden_test(actual.clone(), &actual, matcher_path, |x| {
raw_golden_test(
actual.clone(),
&actual,
matcher_path,
// Otherwise tests in macos/linux passes but fails on windows
Ok(x.replace("\r\n", "\n"))
})
|x| Ok(x.replace("\r\n", "\n")),
Some(|expect, actual| {
#[cfg(test)]
use pretty_assertions::assert_str_eq as assert_eq;
assert_eq!(expect, actual);
}),
)
}

fn raw_golden_test<T, F>(
actual: T,
actual_str: &str,
matcher_path: &Path,
deserializer: F,
asserter: Option<fn(&T, &T)>,
) -> anyhow::Result<()>
where
T: Eq + Debug,
F: Fn(String) -> anyhow::Result<T>,
{
#[cfg(test)]
use pretty_assertions::assert_eq;
// This is *test* utils, not a part of real codegen, so no need to consider coverage
// frb-coverage:ignore-start
let expect = deserializer(if matcher_path.exists() {
Expand All @@ -64,7 +79,8 @@ where
fs::write(matcher_path, actual_str)?;
}
} else {
assert_eq!(actual, expect);
let asserter = asserter.unwrap_or(|expect, actual| assert_eq!(expect, actual));
asserter(&expect, &actual);
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "example"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
flutter_rust_bridge_macros.path = "../../../../../../../../frb_macros"

[workspace]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ {VERSION}.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import 'frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';


Future<void> fnWithDefaultArg({int foo = 1, dynamic hint}) => RustLib.instance.api.fnWithDefaultArg(foo: foo, hint: hint);



Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rust_input: src/api.rs
dart_output: .
c_output: frb_generated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: fake_dart_package
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[flutter_rust_bridge_macros::frb]
pub fn fn_with_default_arg(#[frb(default = 1)] foo: i32) {
drop(foo);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod api;
20 changes: 11 additions & 9 deletions frb_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use proc_macro::*;
// frb-coverage:ignore-start
#[proc_macro_attribute]
pub fn frb(attribute: TokenStream, item: TokenStream) -> TokenStream {
let mut attribute = format_frb_attribute(format!("#[frb({attribute})]"));
let mut output = format_frb_attribute(format!("#[frb({attribute})]"));
let item = strip_frb_attr(item);
attribute.extend(item);
attribute
output.extend(item);
output
}

fn strip_frb_attr(item: TokenStream) -> TokenStream {
Expand All @@ -30,14 +30,16 @@ fn strip_frb_attr(item: TokenStream) -> TokenStream {
Some(format_frb_attribute(format!("#[{}]", group.stream())))
}
(_, T::Group(group)) => Some(
pound
.take()
.into_iter()
.chain(Some(T::Group(Group::new(
[
pound.take(),
Some(T::Group(Group::new(
group.delimiter(),
strip_frb_attr(group.stream()),
))))
.collect(),
))),
]
.into_iter()
.flatten()
.collect(),
),
_ => Some(tok.into()),
}
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ _port_forward_ubuntu:
ssh -L 8181:localhost:8181 ubuntu -N

[no-cd]
expand *args:
_expand *args:
cargo expand --config 'build.rustflags="--cfg frb_expand"' {{args}}

0 comments on commit 06ac6e5

Please sign in to comment.