use std::path::{Path, PathBuf};
use assert_cmd::Command;
use predicates::prelude::*;
use serde_json::Value;
use tempfile::tempdir;
fn fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../testdata/fixtures").join(name)
}
#[test]
fn lists_sources() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "list-sources"])
.assert()
.success()
.stdout(predicate::str::contains("gnd"))
.stdout(predicate::str::contains("musicbrainz"))
.stdout(predicate::str::contains("orcid"))
.stdout(predicate::str::contains("scowl"))
.stdout(predicate::str::contains("stopwords-iso"))
.stdout(predicate::str::contains("wikidata"))
.stdout(predicate::str::contains("wordfreq"));
}
#[test]
fn shows_license_details() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "show-license", "stopwords-iso"])
.assert()
.success()
.stdout(predicate::str::contains("stopwords-iso"))
.stdout(predicate::str::contains("heuristics"));
}
#[test]
fn shows_wikidata_license_details() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "show-license", "wikidata"])
.assert()
.success()
.stdout(predicate::str::contains("wikidata"))
.stdout(predicate::str::contains("CC0"));
}
#[test]
fn shows_gnd_license_details() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "show-license", "gnd"])
.assert()
.success()
.stdout(predicate::str::contains("gnd"))
.stdout(predicate::str::contains("CC0"));
}
#[test]
fn shows_musicbrainz_license_details() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "show-license", "musicbrainz"])
.assert()
.success()
.stdout(predicate::str::contains("musicbrainz"))
.stdout(predicate::str::contains("CC0"));
}
#[test]
fn shows_orcid_license_details() {
Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "show-license", "orcid"])
.assert()
.success()
.stdout(predicate::str::contains("orcid"))
.stdout(predicate::str::contains("CC0"))
.stdout(predicate::str::contains("trademark"));
}
#[test]
fn fetches_from_local_fixture_and_writes_manifest() {
let temp = tempdir().unwrap();
let output = temp.path().join("raw.json");
let manifest = temp.path().join("raw.manifest.json");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"stopwords-iso",
"--from-file",
fixture("stopwords-en.json").to_str().unwrap(),
"--output",
output.to_str().unwrap(),
"--manifest",
manifest.to_str().unwrap(),
])
.assert()
.success();
assert!(output.exists());
assert!(manifest.exists());
}
#[test]
fn prepare_build_inspect_and_diff_plugins() {
let temp = tempdir().unwrap();
let raw = temp.path().join("raw.json");
let raw_manifest = temp.path().join("raw.json.manifest.json");
let prepared = temp.path().join("prepared.json");
let json_plugin = temp.path().join("plugin.json");
let fst_plugin = temp.path().join("plugin.mlatl");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"stopwords-iso",
"--from-file",
fixture("stopwords-en.json").to_str().unwrap(),
"--output",
raw.to_str().unwrap(),
"--manifest",
raw_manifest.to_str().unwrap(),
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"stopwords-iso",
"--input",
raw.to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
])
.assert()
.success()
.stdout(predicate::str::contains("prepared 4 stopwords-iso entries"))
.stdout(predicate::str::contains("4 input records -> 4 normalized entries"));
let prepared_json: Value = serde_json::from_slice(&std::fs::read(&prepared).unwrap()).unwrap();
assert_eq!(prepared_json["metadata"]["source_id"], "stopwords-iso");
assert_eq!(prepared_json["report"]["input_records"], 4);
assert!(prepared_json["metadata"]["source_url"].as_str().unwrap().starts_with("file://"));
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
json_plugin.to_str().unwrap(),
"--format",
"json",
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
fst_plugin.to_str().unwrap(),
"--format",
"fst",
])
.assert()
.success();
let inspect = Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "inspect-plugin", json_plugin.to_str().unwrap(), "--json"])
.assert()
.success()
.get_output()
.stdout
.clone();
let inspect: Value = serde_json::from_slice(&inspect).unwrap();
assert_eq!(inspect["format"], "json");
assert_eq!(inspect["entry_count"], 4);
let diff = Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"diff-plugin",
json_plugin.to_str().unwrap(),
fst_plugin.to_str().unwrap(),
"--json",
])
.assert()
.success()
.get_output()
.stdout
.clone();
let diff: Value = serde_json::from_slice(&diff).unwrap();
assert_eq!(diff["added"], 0);
assert_eq!(diff["removed"], 0);
assert_eq!(diff["changed"], 0);
}
#[test]
fn wordfreq_prepare_requires_acknowledgement() {
let temp = tempdir().unwrap();
let prepared = temp.path().join("prepared.json");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"wordfreq",
"--input",
fixture("wordfreq-mini.txt").to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
])
.assert()
.failure()
.stderr(predicate::str::contains("--acknowledge-cc-by-sa"));
}
#[test]
fn prepare_build_inspect_and_diff_wikidata_plugins() {
let temp = tempdir().unwrap();
let raw = temp.path().join("wikidata.json");
let raw_manifest = temp.path().join("wikidata.json.manifest.json");
let prepared = temp.path().join("wikidata-prepared.json");
let json_plugin = temp.path().join("wikidata.json.plugin");
let fst_plugin = temp.path().join("wikidata.mlatl");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"wikidata",
"--from-file",
fixture("wikidata-sample.json").to_str().unwrap(),
"--output",
raw.to_str().unwrap(),
"--manifest",
raw_manifest.to_str().unwrap(),
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"wikidata",
"--input",
raw.to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
"--payload-kind",
"multiword-map",
])
.assert()
.success()
.stdout(predicate::str::contains("prepared 3 wikidata entries"))
.stdout(predicate::str::contains("3 normalized entries"));
let prepared_json: Value = serde_json::from_slice(&std::fs::read(&prepared).unwrap()).unwrap();
assert_eq!(prepared_json["metadata"]["source_id"], "wikidata");
assert_eq!(prepared_json["payload"]["kind"], "multiword-map");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
json_plugin.to_str().unwrap(),
"--format",
"json",
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
fst_plugin.to_str().unwrap(),
"--format",
"fst",
])
.assert()
.success();
let inspect = Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "inspect-plugin", json_plugin.to_str().unwrap(), "--json"])
.assert()
.success()
.get_output()
.stdout
.clone();
let inspect: Value = serde_json::from_slice(&inspect).unwrap();
assert_eq!(inspect["payload_kind"], "multiword-map");
assert_eq!(inspect["multiword_entries"], 3);
let diff = Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"diff-plugin",
json_plugin.to_str().unwrap(),
fst_plugin.to_str().unwrap(),
"--json",
])
.assert()
.success()
.get_output()
.stdout
.clone();
let diff: Value = serde_json::from_slice(&diff).unwrap();
assert_eq!(diff["added"], 0);
assert_eq!(diff["removed"], 0);
assert_eq!(diff["changed"], 0);
}
#[test]
fn prepare_build_inspect_and_diff_gnd_plugins() {
let temp = tempdir().unwrap();
let raw = temp.path().join("gnd.json");
let raw_manifest = temp.path().join("gnd.json.manifest.json");
let prepared = temp.path().join("gnd-prepared.json");
let json_plugin = temp.path().join("gnd.json.plugin");
let fst_plugin = temp.path().join("gnd.mlatl");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"gnd",
"--from-file",
fixture("gnd-sample.json").to_str().unwrap(),
"--output",
raw.to_str().unwrap(),
"--manifest",
raw_manifest.to_str().unwrap(),
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"gnd",
"--input",
raw.to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
"--payload-kind",
"multiword-map",
])
.assert()
.success()
.stdout(predicate::str::contains("prepared 6 gnd entries"))
.stdout(predicate::str::contains("6 normalized entries"));
let prepared_json: Value = serde_json::from_slice(&std::fs::read(&prepared).unwrap()).unwrap();
assert_eq!(prepared_json["metadata"]["source_id"], "gnd");
assert_eq!(prepared_json["payload"]["kind"], "multiword-map");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
json_plugin.to_str().unwrap(),
"--format",
"json",
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
fst_plugin.to_str().unwrap(),
"--format",
"fst",
])
.assert()
.success();
let inspect = Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "inspect-plugin", json_plugin.to_str().unwrap(), "--json"])
.assert()
.success()
.get_output()
.stdout
.clone();
let inspect: Value = serde_json::from_slice(&inspect).unwrap();
assert_eq!(inspect["payload_kind"], "multiword-map");
assert_eq!(inspect["multiword_entries"], 6);
let diff = Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"diff-plugin",
json_plugin.to_str().unwrap(),
fst_plugin.to_str().unwrap(),
"--json",
])
.assert()
.success()
.get_output()
.stdout
.clone();
let diff: Value = serde_json::from_slice(&diff).unwrap();
assert_eq!(diff["added"], 0);
assert_eq!(diff["removed"], 0);
assert_eq!(diff["changed"], 0);
}
#[test]
fn prepare_build_inspect_and_diff_musicbrainz_plugins() {
let temp = tempdir().unwrap();
let raw = temp.path().join("musicbrainz.json");
let raw_manifest = temp.path().join("musicbrainz.json.manifest.json");
let prepared = temp.path().join("musicbrainz-prepared.json");
let json_plugin = temp.path().join("musicbrainz.json.plugin");
let fst_plugin = temp.path().join("musicbrainz.mlatl");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"musicbrainz",
"--from-file",
fixture("musicbrainz-sample.json").to_str().unwrap(),
"--output",
raw.to_str().unwrap(),
"--manifest",
raw_manifest.to_str().unwrap(),
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"musicbrainz",
"--input",
raw.to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
"--payload-kind",
"protected-spellings",
])
.assert()
.success()
.stdout(predicate::str::contains("prepared 2 musicbrainz entries"))
.stdout(predicate::str::contains("2 normalized entries"));
let prepared_json: Value = serde_json::from_slice(&std::fs::read(&prepared).unwrap()).unwrap();
assert_eq!(prepared_json["metadata"]["source_id"], "musicbrainz");
assert_eq!(prepared_json["payload"]["kind"], "protected-spellings");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
json_plugin.to_str().unwrap(),
"--format",
"json",
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
fst_plugin.to_str().unwrap(),
"--format",
"fst",
])
.assert()
.success();
let inspect = Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "inspect-plugin", json_plugin.to_str().unwrap(), "--json"])
.assert()
.success()
.get_output()
.stdout
.clone();
let inspect: Value = serde_json::from_slice(&inspect).unwrap();
assert_eq!(inspect["payload_kind"], "protected-spellings");
assert_eq!(inspect["entry_count"], 2);
let diff = Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"diff-plugin",
json_plugin.to_str().unwrap(),
fst_plugin.to_str().unwrap(),
"--json",
])
.assert()
.success()
.get_output()
.stdout
.clone();
let diff: Value = serde_json::from_slice(&diff).unwrap();
assert_eq!(diff["added"], 0);
assert_eq!(diff["removed"], 0);
assert_eq!(diff["changed"], 0);
}
#[test]
fn prepare_build_inspect_and_diff_orcid_plugins() {
let temp = tempdir().unwrap();
let raw = temp.path().join("orcid.json");
let raw_manifest = temp.path().join("orcid.json.manifest.json");
let prepared = temp.path().join("orcid-prepared.json");
let json_plugin = temp.path().join("orcid.json.plugin");
let fst_plugin = temp.path().join("orcid.mlatl");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"fetch",
"orcid",
"--from-file",
fixture("orcid-raw-sample.json").to_str().unwrap(),
"--output",
raw.to_str().unwrap(),
"--manifest",
raw_manifest.to_str().unwrap(),
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"prepare",
"orcid",
"--input",
raw.to_str().unwrap(),
"--output",
prepared.to_str().unwrap(),
"--payload-kind",
"multiword-map",
])
.assert()
.success()
.stdout(predicate::str::contains("prepared 3 orcid entries"))
.stdout(predicate::str::contains("3 normalized entries"));
let prepared_json: Value = serde_json::from_slice(&std::fs::read(&prepared).unwrap()).unwrap();
assert_eq!(prepared_json["metadata"]["source_id"], "orcid");
assert_eq!(prepared_json["payload"]["kind"], "multiword-map");
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
json_plugin.to_str().unwrap(),
"--format",
"json",
])
.assert()
.success();
Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"build-plugin",
prepared.to_str().unwrap(),
"--output",
fst_plugin.to_str().unwrap(),
"--format",
"fst",
])
.assert()
.success();
let inspect = Command::cargo_bin("mla-titlecase")
.unwrap()
.args(["lexicon", "inspect-plugin", json_plugin.to_str().unwrap(), "--json"])
.assert()
.success()
.get_output()
.stdout
.clone();
let inspect: Value = serde_json::from_slice(&inspect).unwrap();
assert_eq!(inspect["payload_kind"], "multiword-map");
assert_eq!(inspect["multiword_entries"], 3);
let diff = Command::cargo_bin("mla-titlecase")
.unwrap()
.args([
"lexicon",
"diff-plugin",
json_plugin.to_str().unwrap(),
fst_plugin.to_str().unwrap(),
"--json",
])
.assert()
.success()
.get_output()
.stdout
.clone();
let diff: Value = serde_json::from_slice(&diff).unwrap();
assert_eq!(diff["added"], 0);
assert_eq!(diff["removed"], 0);
assert_eq!(diff["changed"], 0);
}