Skip to content

Commit

Permalink
Add WASM integration tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pemistahl committed Apr 6, 2022
1 parent 65c2301 commit d5cb60a
Show file tree
Hide file tree
Showing 5 changed files with 549 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -206,7 +206,7 @@ of the rule-based engine is quite good, however, filtering based on your own kno
If you want to reproduce the accuracy results above, you can generate the test reports yourself for both classifiers
and all languages by doing:

cargo run --release --example accuracy_reports
cargo run --release --bin accuracy_reports --features accuracy-reports

It is important to use the `--release` flag here because loading the language models in debug mode takes too much time.
For each detector and language, a test report file is then written into
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -290,6 +290,9 @@ pub use isocode::{IsoCode639_1, IsoCode639_3};
pub use language::Language;
pub use writer::{LanguageModelFilesWriter, TestDataFilesWriter};

#[cfg(target_family = "wasm")]
pub use wasm::LanguageDetectorBuilder as WasmLanguageDetectorBuilder;

#[cfg(test)]
use regex::Regex;

Expand Down
264 changes: 264 additions & 0 deletions tests/node.rs
@@ -0,0 +1,264 @@
/*
* Copyright © 2020-today Peter M. Stahl pemistahl@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use lingua::{IsoCode639_1, IsoCode639_3, Language, WasmLanguageDetectorBuilder};
use maplit::hashset;
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_all_languages() {
WasmLanguageDetectorBuilder::fromAllLanguages();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_spoken_languages() {
WasmLanguageDetectorBuilder::fromAllSpokenLanguages();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_languages_with_arabic_script() {
WasmLanguageDetectorBuilder::fromAllLanguagesWithArabicScript();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_languages_with_cyrillic_script() {
WasmLanguageDetectorBuilder::fromAllLanguagesWithCyrillicScript();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_languages_with_devanagari_script() {
WasmLanguageDetectorBuilder::fromAllLanguagesWithDevanagariScript();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_languages_with_latin_script() {
WasmLanguageDetectorBuilder::fromAllLanguagesWithLatinScript();
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_blacklist() {
let languages = Box::new([
JsValue::from(Language::Turkish.to_string()),
JsValue::from(Language::Romanian.to_string()),
]);
let result = WasmLanguageDetectorBuilder::fromAllLanguagesWithout(languages);
assert!(result.is_ok());
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_long_blacklist() {
let languages = Box::new([
JsValue::from(Language::Afrikaans.to_string()),
JsValue::from(Language::Albanian.to_string()),
JsValue::from(Language::Arabic.to_string()),
JsValue::from(Language::Armenian.to_string()),
JsValue::from(Language::Azerbaijani.to_string()),
JsValue::from(Language::Basque.to_string()),
JsValue::from(Language::Belarusian.to_string()),
JsValue::from(Language::Bengali.to_string()),
JsValue::from(Language::Bokmal.to_string()),
JsValue::from(Language::Bosnian.to_string()),
JsValue::from(Language::Bulgarian.to_string()),
JsValue::from(Language::Catalan.to_string()),
JsValue::from(Language::Chinese.to_string()),
JsValue::from(Language::Croatian.to_string()),
JsValue::from(Language::Czech.to_string()),
JsValue::from(Language::Danish.to_string()),
JsValue::from(Language::Dutch.to_string()),
JsValue::from(Language::English.to_string()),
JsValue::from(Language::Esperanto.to_string()),
JsValue::from(Language::Estonian.to_string()),
JsValue::from(Language::Finnish.to_string()),
JsValue::from(Language::French.to_string()),
JsValue::from(Language::Ganda.to_string()),
JsValue::from(Language::Georgian.to_string()),
JsValue::from(Language::Greek.to_string()),
JsValue::from(Language::Gujarati.to_string()),
JsValue::from(Language::Hebrew.to_string()),
JsValue::from(Language::Hindi.to_string()),
JsValue::from(Language::Hungarian.to_string()),
JsValue::from(Language::Icelandic.to_string()),
JsValue::from(Language::Indonesian.to_string()),
JsValue::from(Language::Irish.to_string()),
JsValue::from(Language::Italian.to_string()),
JsValue::from(Language::Japanese.to_string()),
JsValue::from(Language::Kazakh.to_string()),
JsValue::from(Language::Korean.to_string()),
JsValue::from(Language::Latin.to_string()),
JsValue::from(Language::Latvian.to_string()),
JsValue::from(Language::Lithuanian.to_string()),
JsValue::from(Language::Macedonian.to_string()),
JsValue::from(Language::Malay.to_string()),
JsValue::from(Language::Maori.to_string()),
JsValue::from(Language::Marathi.to_string()),
JsValue::from(Language::Mongolian.to_string()),
JsValue::from(Language::Nynorsk.to_string()),
JsValue::from(Language::Persian.to_string()),
JsValue::from(Language::Polish.to_string()),
JsValue::from(Language::Portuguese.to_string()),
JsValue::from(Language::Punjabi.to_string()),
JsValue::from(Language::Romanian.to_string()),
JsValue::from(Language::Russian.to_string()),
JsValue::from(Language::Serbian.to_string()),
JsValue::from(Language::Shona.to_string()),
JsValue::from(Language::Slovak.to_string()),
JsValue::from(Language::Slovene.to_string()),
JsValue::from(Language::Somali.to_string()),
JsValue::from(Language::Sotho.to_string()),
JsValue::from(Language::Spanish.to_string()),
JsValue::from(Language::Swahili.to_string()),
JsValue::from(Language::Swedish.to_string()),
JsValue::from(Language::Tagalog.to_string()),
JsValue::from(Language::Tamil.to_string()),
JsValue::from(Language::Telugu.to_string()),
JsValue::from(Language::Thai.to_string()),
JsValue::from(Language::Tsonga.to_string()),
JsValue::from(Language::Tswana.to_string()),
JsValue::from(Language::Turkish.to_string()),
JsValue::from(Language::Ukrainian.to_string()),
JsValue::from(Language::Urdu.to_string()),
JsValue::from(Language::Vietnamese.to_string()),
JsValue::from(Language::Welsh.to_string()),
JsValue::from(Language::Xhosa.to_string()),
JsValue::from(Language::Yoruba.to_string()),
JsValue::from(Language::Zulu.to_string()),
]);
let result = WasmLanguageDetectorBuilder::fromAllLanguagesWithout(languages);
assert_eq!(
result.err(),
Some(JsValue::from(
"LanguageDetector needs at least 2 languages to choose from"
))
);
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_whitelist() {
let result = WasmLanguageDetectorBuilder::fromLanguages(Box::new([
JsValue::from(Language::German.to_string()),
JsValue::from(Language::English.to_string()),
]));
assert!(result.is_ok());
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_short_whitelist() {
let result = WasmLanguageDetectorBuilder::fromLanguages(Box::new([JsValue::from(
Language::German.to_string(),
)]));
assert_eq!(
result.err(),
Some(JsValue::from(
"LanguageDetector needs at least 2 languages to choose from"
))
);
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_iso_639_1_codes() {
let result = WasmLanguageDetectorBuilder::fromISOCodes6391(Box::new([
JsValue::from(IsoCode639_1::DE.to_string()),
JsValue::from(IsoCode639_1::SV.to_string()),
]));
assert!(result.is_ok());
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_few_iso_639_1_codes() {
let result = WasmLanguageDetectorBuilder::fromISOCodes6391(Box::new([JsValue::from(
IsoCode639_1::DE.to_string(),
)]));
assert_eq!(
result.err(),
Some(JsValue::from(
"LanguageDetector needs at least 2 languages to choose from"
))
);
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_iso_639_3_codes() {
let result = WasmLanguageDetectorBuilder::fromISOCodes6393(Box::new([
JsValue::from(IsoCode639_3::DEU.to_string()),
JsValue::from(IsoCode639_3::SWE.to_string()),
]));
assert!(result.is_ok());
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_few_iso_639_3_codes() {
let result = WasmLanguageDetectorBuilder::fromISOCodes6393(Box::new([JsValue::from(
IsoCode639_3::DEU.to_string(),
)]));
assert_eq!(
result.err(),
Some(JsValue::from(
"LanguageDetector needs at least 2 languages to choose from"
))
);
}

#[wasm_bindgen_test]
fn assert_detector_can_be_built_from_minimum_relative_distance() {
let mut builder = WasmLanguageDetectorBuilder::fromAllLanguages();
let result = builder.setMinimumRelativeDistance(0.25);
assert!(result.is_ok());
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_small_minimum_relative_distance() {
let mut builder = WasmLanguageDetectorBuilder::fromAllLanguages();
let result = builder.setMinimumRelativeDistance(-2.3);
assert_eq!(
result.err(),
Some(JsValue::from(
"Minimum relative distance must lie in between 0.0 and 0.99"
))
);
}

#[wasm_bindgen_test]
fn assert_detector_cannot_be_built_from_too_large_minimum_relative_distance() {
let mut builder = WasmLanguageDetectorBuilder::fromAllLanguages();
let result = builder.setMinimumRelativeDistance(1.7);
assert_eq!(
result.err(),
Some(JsValue::from(
"Minimum relative distance must lie in between 0.0 and 0.99"
))
);
}

#[wasm_bindgen_test]
fn assert_language_detection_works_correctly() {
let detector = WasmLanguageDetectorBuilder::fromLanguages(Box::new([
JsValue::from(Language::German.to_string()),
JsValue::from(Language::English.to_string()),
JsValue::from(Language::French.to_string()),
]))
.unwrap()
.build();

let mut language = detector.detectLanguageOf("mein Haus ist groß");
assert_eq!(language, Some(Language::German.to_string()));

language = detector.detectLanguageOf("my house is big");
assert_eq!(language, Some(Language::English.to_string()));

language = detector.detectLanguageOf("ma maison est grande");
assert_eq!(language, Some(Language::French.to_string()));
}

0 comments on commit d5cb60a

Please sign in to comment.