use super::*;
fn learned(fixture: &Fixture, word: &str) -> i64 {
count(
&fixture.path().join(assets::ENGLISH_DICTIONARY),
&format!("SELECT count(*) FROM english_words WHERE word='{word}'"),
)
}
fn english_fixture() -> Fixture {
Fixture::new(QUANPIN_FIXTURE).with_english(ENGLISH_FIXTURE)
}
#[test]
fn by_default_enter_learns_only_dedicated_english() {
let fixture = english_fixture();
let mut session = fixture.session();
assert!(!SessionOptions::new(fixture.paths()).learn_typed_spellings_on_enter);
type_text(&mut session, "rustacean");
let result = session.command(Command::CommitRaw);
assert_eq!(result.commit.as_deref(), Some("rustacean"));
assert_eq!(result.diagnostic, None);
assert_eq!(learned(&fixture, "rustacean"), 0);
session.set_dedicated_english(true);
type_text(&mut session, "Zyzzyva");
let result = session.command(Command::CommitRaw);
assert_eq!(result.commit.as_deref(), Some("Zyzzyva"));
assert_eq!(learned(&fixture, "zyzzyva"), 1);
}
#[test]
fn typed_spellings_are_learned_when_the_option_is_on() {
let fixture = english_fixture();
let mut session = fixture.session_with(|options| options.learn_typed_spellings_on_enter = true);
type_text(&mut session, "rustacean");
let result = session.command(Command::CommitRaw);
assert_eq!(result.commit.as_deref(), Some("rustacean"));
assert_eq!(result.diagnostic, None);
assert_eq!(learned(&fixture, "rustacean"), 1);
assert_eq!(
count(
&fixture.journal(),
"SELECT count(*) FROM user_dictionary_operations WHERE dictionary='english' AND key='rustacean'"
),
1
);
type_text(&mut session, "nihao");
assert_eq!(
session.command(Command::CommitRaw).commit.as_deref(),
Some("nihao")
);
assert_eq!(learned(&fixture, "nihao"), 0);
assert!(session.character(b'Y', true).handled);
assert_eq!(
session.snapshot().local_mode,
LocalInputMode::TemporaryEnglish
);
type_text(&mut session, "ferris");
assert_eq!(
session.command(Command::CommitRaw).commit.as_deref(),
Some("Yferris")
);
assert_eq!(learned(&fixture, "ferris"), 1);
assert_eq!(learned(&fixture, "yferris"), 0);
let mut off = fixture.session_with(|options| {
options.learn_typed_spellings_on_enter = true;
options.learn_english_on_enter = false;
});
type_text(&mut off, "crustacean");
assert_eq!(
off.command(Command::CommitRaw).commit.as_deref(),
Some("crustacean")
);
assert_eq!(learned(&fixture, "crustacean"), 0);
}
#[test]
fn commit_raw_without_learning_commits_the_same_text_and_learns_nothing() {
let fixture = english_fixture();
let mut session = fixture.session_with(|options| options.learn_typed_spellings_on_enter = true);
type_text(&mut session, "rustacean");
let result = session.command(Command::CommitRawWithoutLearning);
assert_eq!(result.commit.as_deref(), Some("rustacean"));
assert!(session.snapshot().preedit.is_empty());
assert_eq!(learned(&fixture, "rustacean"), 0);
session.set_dedicated_english(true);
type_text(&mut session, "Zyzzyva");
let before = session.candidate_revision();
let result = session.command(Command::CommitRawWithoutLearning);
assert!(session.candidate_revision() > before);
assert_eq!(result.commit.as_deref(), Some("Zyzzyva"));
assert!(session.snapshot().preedit.is_empty());
assert_eq!(learned(&fixture, "zyzzyva"), 0);
assert_eq!(
Command::from_u8(14),
Some(Command::CommitRawWithoutLearning)
);
assert_eq!(
Command::from_name("CommitRawWithoutLearning"),
Some(Command::CommitRawWithoutLearning)
);
}
#[test]
fn enter_learns_nothing_without_an_english_dictionary_the_schemes_do_not_need() {
let fixture = Fixture::new(QUANPIN_FIXTURE);
let english = fixture.path().join(assets::ENGLISH_DICTIONARY);
let mut session = fixture.session_with(|options| {
options.scheme = SchemeType::Korean;
options.enabled_schemes = SchemeSet::of(&[SchemeType::Korean]);
options.learn_typed_spellings_on_enter = true;
});
session.set_dedicated_english(true);
type_text(&mut session, "Zyzzyva");
let result = session.command(Command::CommitRaw);
assert_eq!(result.commit.as_deref(), Some("Zyzzyva"));
assert_eq!(result.diagnostic, None);
assert!(!english.exists());
let mut chinese = fixture.session();
chinese.set_dedicated_english(true);
type_text(&mut chinese, "Zyzzyva");
assert_eq!(
chinese.command(Command::CommitRaw).commit.as_deref(),
Some("Zyzzyva")
);
assert_eq!(learned(&fixture, "zyzzyva"), 1);
}