TableTower/tests/test_spellcheck.gd

98 lines
4 KiB
GDScript3
Raw Permalink Normal View History

extends AppTestBase
## Spellchecking in Build (Spellcheck, SpellWords, SpellUnderline), with a
## stand-in checker so the test needs no native add-on: which words are
## checked, underlines on card and inspector fields, suggestions replacing a
## word, the spread's own dictionary (saved with it) and Ignore.
## Run: tools/run_tests.sh spellcheck
## Knows a few words; suggests from a fixed table.
class FakeChecker:
extends RefCounted
const KNOWN := ["the", "result", "work", "joe", "tinker", "bob", "workday", "job", "fixes", "press", "after", "hard", "day's", "new"]
func spell(word: String) -> bool:
return KNOWN.has(word.to_lower())
func suggest(word: String) -> PackedStringArray:
return PackedStringArray({"tinkerr": ["tinker", "tinkers"], "wrok": ["work"]}.get(word.to_lower(), []))
func run() -> void:
_test_words()
Spellcheck.use_backend(FakeChecker.new())
_test_checking()
if not await start_main(false):
return
main.set_mode(Main.Mode.BUILD)
main.data.load_dict({"format_version": 2, "id": "spell-test", "name": "Spell test", "tables": [
{"id": "work", "name": "Workday Job", "x": 40, "y": 40, "entries": [
{"id": "a", "text": "Joe the tinkerr", "details": "Fixes the press after a hard day's wrok"},
{"id": "b", "text": "Vargr"}]}]})
main._rebuild_cards()
await _frames(4)
await _test_card_fields()
await _test_dictionary()
Spellcheck.use_backend(null)
finish()
func _words(text: String) -> Array:
return SpellWords.ranges(text).map(func(r: Vector2i) -> String: return text.substr(r.x, r.y - r.x))
func _test_words() -> void:
_eq(_words("[b]Joe[/b] the {option} tinker @hp [[Spread::Card]]"), ["Joe", "the", "tinker"], "tags, placeholders, values and links are skipped")
_eq(_words("Roll 2d6 for 3 HP, don't"), ["Roll", "for", "don't"], "numbers, dice and short capitals are skipped")
func _test_checking() -> void:
var board := TowerBoard.new()
var text := "Joe the tinkerr"
_eq(Spellcheck.misspelled(text, board), [Vector2i(8, 15)] as Array[Vector2i], "a misspelled word is found")
board.dictionary.append("Tinkerr")
_check(Spellcheck.misspelled(text, board).is_empty(), "the spread's dictionary accepts it, in any case")
var back := TowerBoard.new()
back.load_dict(board.to_dict())
_eq(back.dictionary, ["Tinkerr"] as Array[String], "the dictionary is saved with the spread")
func _underline(c: Control) -> SpellUnderline:
return c.get_meta("spell") if c.has_meta("spell") else null
func _test_card_fields() -> void:
var card := main.card_for("work")
var row: Control = card.rows["a"]
var edits := row.find_children("*", "LineEdit", true, false)
var u: SpellUnderline = _underline(edits[0]) if not edits.is_empty() else null
_check(u != null, "a result's text field on the card is checked")
if u == null:
return
_eq(u.bad, [Vector2i(8, 15)] as Array[Vector2i], "the misspelled word is underlined")
u.open_menu(u.bad[0], Vector2(100, 100))
_eq(u._menu.get_item_text(0), "tinker", "the menu starts with suggestions")
u._on_menu(0)
await _frames()
_eq(main.data.get_table("work").find_entry("a").text, "Joe the tinker", "picking a suggestion fixes the result")
main.select_result("work", "a")
await _frames()
var det: Control = main.inspector.fields.get("details")
_check(det != null and _underline(det) != null and _underline(det).bad.size() == 1, "the inspector's Details are checked too")
func _test_dictionary() -> void:
var card := main.card_for("work")
var edits := (card.rows["b"] as Control).find_children("*", "LineEdit", true, false)
var u: SpellUnderline = _underline(edits[0])
_eq(u.bad.size(), 1, "a setting's name is flagged at first")
u.open_menu(u.bad[0], Vector2(100, 100))
u._on_menu(SpellUnderline.ID_ADD)
_check(main.data.dictionary.has("Vargr"), "Add to dictionary saves the word in the spread")
_check(u.bad.is_empty(), "the underline goes away")
Spellcheck.ignore(main, "wrok")
_check(Spellcheck.is_ok("Wrok", main.data), "Ignore accepts the word for this session")
main.set_mode(Main.Mode.PLAY)
await _frames()
main.set_mode(Main.Mode.BUILD)