diff --git a/CMakeLists.txt b/CMakeLists.txt index 45b324b..15da246 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,5 +143,6 @@ if (BUILD_TESTS) add_sdcv_shell_test(t_utf8input) add_sdcv_shell_test(t_datadir) add_sdcv_shell_test(t_return_code) + add_sdcv_shell_test(t_multiple_results) endif (BUILD_TESTS) diff --git a/tests/stardict-test_multiple_results-2.4.2/test.dict b/tests/stardict-test_multiple_results-2.4.2/test.dict new file mode 100644 index 0000000..53fc2d5 Binary files /dev/null and b/tests/stardict-test_multiple_results-2.4.2/test.dict differ diff --git a/tests/stardict-test_multiple_results-2.4.2/test.idx b/tests/stardict-test_multiple_results-2.4.2/test.idx new file mode 100644 index 0000000..2613d8c Binary files /dev/null and b/tests/stardict-test_multiple_results-2.4.2/test.idx differ diff --git a/tests/stardict-test_multiple_results-2.4.2/test.ifo b/tests/stardict-test_multiple_results-2.4.2/test.ifo new file mode 100644 index 0000000..b6ccf64 --- /dev/null +++ b/tests/stardict-test_multiple_results-2.4.2/test.ifo @@ -0,0 +1,7 @@ +StarDict's dict ifo file +version=3.0.0 +bookname=Test multiple results +wordcount=246 +idxfilesize=5977 +synwordcount=124 +description= diff --git a/tests/stardict-test_multiple_results-2.4.2/test.syn b/tests/stardict-test_multiple_results-2.4.2/test.syn new file mode 100644 index 0000000..9251600 Binary files /dev/null and b/tests/stardict-test_multiple_results-2.4.2/test.syn differ diff --git a/tests/t_json b/tests/t_json index 79db5f6..40d3128 100755 --- a/tests/t_json +++ b/tests/t_json @@ -18,7 +18,10 @@ test_json() { fi } -test_json '[{"name": "Test synonyms", "wordcount": "2"},{"name": "Sample 1 test dictionary", "wordcount": "1"},{"name": "test_dict", "wordcount": "1"}]' -x -j -l -n --data-dir "$TEST_DIR" +test_json '[{"name": "Test synonyms", "wordcount": "2"}, + {"name": "Test multiple results", "wordcount": "246"}, + {"name": "Sample 1 test dictionary", "wordcount": "1"}, + {"name": "test_dict", "wordcount": "1"}]' -x -j -l -n --data-dir "$TEST_DIR" test_json '[{"dict": "Test synonyms","word":"test","definition":"\u000aresult of test"}]' -x -j -n --data-dir "$TEST_DIR" foo test_json '[]' -x -j -n --data-dir "$TEST_DIR" foobarbaaz diff --git a/tests/t_multiple_results b/tests/t_multiple_results new file mode 100755 index 0000000..07a4a70 --- /dev/null +++ b/tests/t_multiple_results @@ -0,0 +1,67 @@ +#!/bin/sh + +set -e + +SDCV="$1" +TEST_DIR="$2" + +unset SDCV_PAGER +unset STARDICT_DATA_DIR + +test_json() { + word="$1" + jq_cmp="$2" + result="$("$SDCV" --data-dir "$TEST_DIR" -exjn "$word" | sed 's|\\n|\\u000a|g')" + cmp_result="$(echo "$result" | jq "$jq_cmp")" + if [ "$cmp_result" != "true" ]; then + echo "expected '$jq_cmp' to return true, but $result didn't" + exit 1 + fi +} + +# Basic two-result search for the same headword. +test_json bark \ + '. == [ + {"dict":"Test multiple results","word":"bark","definition":"\u000aThe harsh sound made by a dog."}, + {"dict":"Test multiple results","word":"bark","definition":"\u000aThe tough outer covering of trees and other woody plants."} + ]' + +# Multi-result search where one word exists as both a synyonym and a separate +# headword. This ensures that if there is a matching synyonym we don't skip the +# regular search. +test_json cat \ + '. == [ + {"dict":"Test multiple results","word":"cat","definition":"\u000aA cute animal which (rarely) barks."}, + {"dict":"Test multiple results","word":"lion","definition":"\u000aA larger cat which might bite your head off."}, + {"dict":"Test multiple results","word":"panther","definition":"\u000aI know very little about panthers, sorry."} + ]' + +# Many-result search for a word that matches 120 distinct headwords. +test_json many_headwords 'length == 120' +test_json many_headwords 'all(.word == "many_headwords")' +test_json many_headwords \ + 'to_entries | map(.value.definition == "\u000aDefinition for [many_headwords] entry #\(.key+1) (same headword).") | all' + +# Many-result search for 120 words that have the same synonym. +test_json many_synonyms 'length == 120' +test_json many_synonyms \ + 'to_entries | map(.value.word == "many_synonyms-\(.key+101)") | all' +test_json many_synonyms \ + 'to_entries | map(.value.definition == "\u000aDefinition for [many_synonyms-\(.key+101)] (same synonym).") | all' + +# Ensure that we don't return more than one result even if a word can be +# resolved in more than one way. +# +# Most well-formed dictionaries don't have entries like this (it basically +# requires you to have a dictionary where there is a synonym that is identical +# to a word's headword or multiple identical synyonym entries). +# +# This entry was created by creating extra synonyms with different names then +# modifying the .syn file manually. +test_json many_resolution_paths \ + '. == [ + {"dict":"Test multiple results","word":"many_resolution_paths", + "definition":"\u000aDefinition for [many_resolution_paths] headword (same word, multiple synonym entries)."} + ]' + +exit 0