Skip to content

ui::testing::TestSpellChecker

Header: winui/testing/TestSpellChecker.hpp.

Derives from ui::SpellChecker.

Deterministic, in-process SpellChecker for headless tests. It uses no system dictionary, only the words the test declared misspelled.

Every change (a new misspelling, a settings flip, add/ignore/autocorrect) bumps generation(), which tells a synced control to check again. Always reports "en-US" while available. Words are runs of letters, digits, underscores and apostrophes, matched with exact case.

Example:

ui::testing::HeadlessTextMeasurer measurer;
ui::TextBoxControl tb{"Ths is a tset"};
const ui::Size size = tb.measure({200.0f, ui::Unbounded}, measurer, ui::Theme::default_light());
tb.arrange({100.0f, 100.0f, 200.0f, size.height});
ui::testing::set_focused(tb, true);

ui::testing::TestSpellChecker checker;
checker.add_misspelling("Ths", {"This"});
checker.add_misspelling("tset", {"test"});
tb.sync_spell_checker(checker);
tb.tick(ui::Duration{0}); // checking happens on the next tick, never inside the sync

REQUIRE(tb.spelling_errors().size() == 2);
REQUIRE(checker.check_count() == 1);
REQUIRE(checker.suggest("Ths") == std::vector<std::string>{"This"});

add_misspelling

void add_misspelling(std::string word, std::vector<std::string> suggestions = {})

Declares word (exact case) misspelled, with the list suggest() returns for it.

set_available

void set_available(bool available)

Simulates a missing or present system dictionary. While unavailable, checks find nothing and language() is empty. Default true.

set_highlight_enabled

void set_highlight_enabled(bool enabled)

Flips the user's "highlight misspelled words" setting (default true).

set_autocorrect_enabled

void set_autocorrect_enabled(bool enabled)

Flips the user's "autocorrect misspelled words" setting (default true).

check_count

int check_count() const

How many times check() ran, available or not, so a test can show a control didn't check again.

added

const std::vector<std::string>& added() const

Words passed to add_to_dictionary(), in order.

ignored

const std::vector<std::string>& ignored() const

Words passed to ignore_for_session(), in order.

available

bool available() const override

The set_available() state.

language

std::string language() const override

"en-US" while available, empty otherwise.

highlight_enabled

bool highlight_enabled() const override

The set_highlight_enabled() state.

autocorrect_enabled

bool autocorrect_enabled() const override

The set_autocorrect_enabled() state.

generation

std::uint64_t generation() const override

Starts at 1 and bumps on every change of the checker's state.

check

std::vector<SpellingError> check(std::string_view text) override

Reports each declared misspelling (SpellingAction::Suggest) or autocorrection (SpellingAction::Replace) in text, as byte offsets; added or ignored words are skipped. Words are runs of letters, digits, underscores and apostrophes, matched with exact case. This doesn't reproduce how the system checker splits words.

suggest

std::vector<std::string> suggest(std::string_view word) override

The suggestions declared with add_misspelling() for word, or empty.

add_to_dictionary

void add_to_dictionary(std::string_view word) override

Accepts word from now on and records it in added().

ignore_for_session

void ignore_for_session(std::string_view word) override

Accepts word from now on and records it in ignored().

add_autocorrection

void add_autocorrection(std::string_view from, std::string_view to) override

Makes check() report from as a Replace error carrying to; wins over a misspelling.