Files
MusicPlayer/tests/unit/test_soundtrack_search.gd
T
Sansan 845d08c260 Rework the Search popup and highlight what matched
- The search field is a single-line field, so Enter no longer adds a
  line break that made every search come up empty.
- The field, button and list use real font sizes from the theme instead
  of being drawn at half size and scaled up, so the text is sharp, and
  everything fits inside the panel.
- Results have alternating row backgrounds like the other lists, a count
  ("3 of 1045 soundtracks") and a message when nothing matches.
- The letters that matched are highlighted.
- Searches shorter than Settings.LETTERS_FOR_LOOSE_SEARCH (3) only find
  names that contain the text, instead of names with the letters far
  apart, which matched most of the library. The Statistics tab uses the
  same search.
2026-10-07 14:53:51 +02:00

93 lines
3.0 KiB
GDScript

extends GdUnitTestSuite
## How SoundtrackSearch, used by the Search popup and the statistics view, finds and
## orders matching soundtracks.
var _search: SoundtrackSearch
func before_test() -> void:
_search = SoundtrackSearch.new()
_search.set_names([
"Chrono Trigger",
"Final Fantasy VI",
"Mario & Luigi: Superstar Saga",
"Pokémon Red",
"Q*bert",
"Super Mario 64",
"The Legend of Zelda: A Link to the Past",
])
func test_plain_text_matches_case_insensitively() -> void:
assert_array(_search.find_matches("chrono")).contains_exactly(["Chrono Trigger"])
func test_contains_matches_come_before_letter_order_matches() -> void:
var matches: Array[String] = _search.find_matches("mario")
assert_array(matches).contains_exactly([
"Mario & Luigi: Superstar Saga",
"Super Mario 64",
])
func test_ignores_spaces() -> void:
assert_array(_search.find_matches("finalfantasy")).contains_exactly(["Final Fantasy VI"])
func test_and_matches_ampersand() -> void:
assert_array(_search.find_matches("mario and luigi")) \
.contains(["Mario & Luigi: Superstar Saga"])
func test_accented_letters_match_plain_letters() -> void:
assert_array(_search.find_matches("pokemon")).contains_exactly(["Pokémon Red"])
func test_letters_in_order_match_abbreviations() -> void:
assert_array(_search.find_matches("lozlttp")).contains_exactly([
"The Legend of Zelda: A Link to the Past",
])
func test_regex_characters_are_matched_literally() -> void:
assert_array(_search.find_matches("q*b")).contains_exactly(["Q*bert"])
func test_no_matches() -> void:
assert_array(_search.find_matches("metroid")).is_empty()
func test_clean_term() -> void:
assert_str(SoundtrackSearch.clean_term("Rock 'n Roll Racing")).is_equal("rockandrollracing")
assert_str(SoundtrackSearch.clean_term("Ys I & II")).is_equal("ysiandii")
func test_short_search_only_finds_names_containing_it() -> void:
# "sm" is in order in "Super Mario 64" but not next to each other
assert_int(Settings.LETTERS_FOR_LOOSE_SEARCH).is_equal(3)
assert_array(_search.find_matches("sm")).is_empty()
assert_array(_search.find_matches("smr")).contains_exactly(["Super Mario 64"])
func test_matched_letters_of_plain_match() -> void:
assert_array(SoundtrackSearch.matched_letters("Chrono Trigger", "trig")) \
.contains_exactly([7, 8, 9, 10])
func test_matched_letters_ignoring_spaces_and_special_characters() -> void:
# "mario and luigi" matches "Mario & Luigi" with & read as "and"
var letters: Array[int] = SoundtrackSearch.matched_letters(
"Mario & Luigi: Superstar Saga", "mario and luigi")
assert_array(letters).contains_exactly([0, 1, 2, 3, 4, 6, 8, 9, 10, 11, 12])
func test_matched_letters_in_order() -> void:
assert_array(SoundtrackSearch.matched_letters("Super Mario 64", "smr")) \
.contains_exactly([0, 6, 8])
func test_matched_letters_of_no_match() -> void:
assert_array(SoundtrackSearch.matched_letters("Chrono Trigger", "metroid")).is_empty()
assert_array(SoundtrackSearch.matched_letters("Super Mario 64", "sm")).is_empty()