#![cfg(any(feature = "ssr", feature = "hydrate", feature = "csr"))]
#[cfg(all(feature = "leptos-0-8", not(feature = "leptos")))]
extern crate leptos_0_8 as leptos;
#[cfg(feature = "leptos")]
extern crate leptos_0_9 as leptos;
#[path = "support/corpus.rs"]
mod support;
use std::sync::{Arc, OnceLock};
use mf2::leptos::{Setup, install};
use mf2::native;
use mf2::{Catalog, Corpus, Dir};
use support::{files, welcome};
fn corpus() -> &'static Corpus {
static CORPUS: OnceLock<&'static Corpus> = OnceLock::new();
CORPUS.get_or_init(|| support::corpus(true).0)
}
fn web_catalog() -> Arc<Catalog> {
let (corpus, raw) = support::corpus_of(
&[(
"en",
Dir::Ltr,
[
"Welcome (web)",
"Hello, {$name} (web)",
"{$n :integer} files (web)",
],
)],
true,
);
install(Setup::new(
&support::REGISTRY,
&mf2::host_std::HOST,
corpus.manifest_hash(),
"en",
&[("en", Dir::Ltr)],
));
Arc::new(Catalog::new(raw[0].1.clone(), corpus.manifest_hash()).expect("the catalog reads"))
}
fn with_web<R>(body: impl FnOnce() -> R) -> R {
let catalog = web_catalog();
#[cfg(feature = "ssr")]
{
use leptos::prelude::Owner;
let owner = Owner::new();
owner.with(|| {
mf2::leptos::RequestI18n::new(catalog).provide();
body()
})
}
#[cfg(not(feature = "ssr"))]
{
mf2::leptos::set_active(catalog);
body()
}
}
#[test]
fn the_request_s_or_the_page_s_catalog_comes_first() {
let shown = with_web(|| {
native::with_locale(corpus(), "fr", || {
(
welcome().to_string(),
format!("{}", files(3)),
welcome().to_cow().into_owned(),
)
})
.expect("fr is supported")
});
assert_eq!(shown.0, "Welcome (web)");
assert_eq!(shown.1, "3 files (web)");
assert_eq!(shown.2, "Welcome (web)");
}
#[test]
fn without_it_the_native_thread_s_language_answers() {
std::thread::spawn(|| {
native::with_locale(corpus(), "fr", || {
assert_eq!(welcome().to_string(), "Bienvenue");
assert_eq!(format!("{}", files(3)), "3 fichiers");
assert!(matches!(
welcome().to_cow(),
std::borrow::Cow::Borrowed("Bienvenue")
));
})
.expect("fr is supported");
})
.join()
.expect("the store answers");
}
#[test]
fn with_neither_the_web_s_rule_holds() {
std::thread::spawn(|| {
assert_eq!(welcome().to_string(), "");
assert_eq!(format!("[{}]", files(3)), "[]");
})
.join()
.expect("never a panic beside a Leptos mode");
}