From 6208cefe20f46c5de1e9f337079919115acf3523 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Tue, 4 Apr 2017 23:29:46 -0700 Subject: Split up tests, improve naming --- test/test_match.c | 127 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 40 deletions(-) (limited to 'test') diff --git a/test/test_match.c b/test/test_match.c index 38c1d20..5117666 100644 --- a/test/test_match.c +++ b/test/test_match.c @@ -5,95 +5,129 @@ #include "greatest/greatest.h" -TEST test_match() { +/* has_match(char *needle, char *haystack) */ +TEST exact_match_should_return_true() { ASSERT(has_match("a", "a")); + PASS(); +} + +TEST partial_match_should_return_true() { ASSERT(has_match("a", "ab")); ASSERT(has_match("a", "ba")); + PASS(); +} + +TEST match_with_delimiters_in_between() { ASSERT(has_match("abc", "a|b|c")); + PASS(); +} - /* non-match */ +TEST non_match_should_return_false() { ASSERT(!has_match("a", "")); ASSERT(!has_match("a", "b")); ASSERT(!has_match("ass", "tags")); + PASS(); +} +TEST empty_query_should_always_match() { /* match when query is empty */ ASSERT(has_match("", "")); ASSERT(has_match("", "a")); - PASS(); } -TEST test_relative_scores() { +/* match(char *needle, char *haystack) */ + +TEST should_prefer_starts_of_words() { /* App/Models/Order is better than App/MOdels/zRder */ ASSERT(match("amor", "app/models/order") > match("amor", "app/models/zrder")); + PASS(); +} +TEST should_prefer_consecutive_letters() { /* App/MOdels/foo is better than App/M/fOo */ ASSERT(match("amo", "app/m/foo") < match("amo", "app/models/foo")); + PASS(); +} +TEST should_prefer_contiguous_over_letter_following_period() { /* GEMFIle.Lock < GEMFILe */ ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); + PASS(); +} - /* GEMFIle.Lock < GEMFILe */ - ASSERT(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile")); - - /* Prefer shorter matches */ +TEST should_prefer_shorter_matches() { ASSERT(match("abce", "abcdef") > match("abce", "abc de")); + ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); + ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); + PASS(); +} - /* Prefer shorter candidates */ +TEST should_prefer_shorter_candidates() { ASSERT(match("test", "tests") > match("test", "testing")); + PASS(); +} +TEST should_prefer_start_of_candidate() { /* Scores first letter highly */ ASSERT(match("test", "testing") > match("test", "/testing")); - - /* Prefer shorter matches */ - ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); - ASSERT(match("abc", " a b c ") > match("abc", " a b c ")); - PASS(); } -TEST test_exact_scores() { +TEST score_exact_match() { /* Exact match is SCORE_MAX */ ASSERT_EQ(SCORE_MAX, match("abc", "abc")); ASSERT_EQ(SCORE_MAX, match("aBc", "abC")); + PASS(); +} +TEST score_empty_query() { /* Empty query always results in SCORE_MIN */ ASSERT_EQ(SCORE_MIN, match("", "")); ASSERT_EQ(SCORE_MIN, match("", "a")); ASSERT_EQ(SCORE_MIN, match("", "bb")); + PASS(); +} - /* Gaps */ +TEST score_gaps() { ASSERT_EQ(SCORE_GAP_LEADING, match("a", "*a")); ASSERT_EQ(SCORE_GAP_LEADING*2, match("a", "*ba")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING, match("a", "**a*")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2, match("a", "**a**")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2, match("aa", "**aa**")); ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING, match("aa", "**a*a**")); + PASS(); +} - /* Consecutive */ +TEST score_consecutive() { ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE, match("aa", "*aa")); ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2, match("aaa", "*aaa")); ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE, match("aaa", "*a*aa")); + PASS(); +} - /* Slash */ +TEST score_slash() { ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_SLASH, match("a", "/a")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH, match("a", "*/a")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE, match("aa", "a/aa")); + PASS(); +} - /* Capital */ +TEST score_capital() { ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL, match("a", "bA")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL, match("a", "baA")); ASSERT_EQ(SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE, match("aa", "baAa")); + PASS(); +} - /* Dot */ +TEST score_dot() { ASSERT_EQ(SCORE_GAP_LEADING + SCORE_MATCH_DOT, match("a", ".a")); ASSERT_EQ(SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT, match("a", "*a.a")); ASSERT_EQ(SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT, match("a", "*a.a")); - PASS(); } -TEST test_positions_1() { +TEST positions_consecutive() { size_t positions[3]; match_positions("amo", "app/models/foo", positions); ASSERT_EQ(0, positions[0]); @@ -103,7 +137,7 @@ TEST test_positions_1() { PASS(); } -TEST test_positions_2() { +TEST positions_start_of_word() { /* * We should prefer matching the 'o' in order, since it's the beginning * of a word. @@ -113,21 +147,17 @@ TEST test_positions_2() { ASSERT_EQ(0, positions[0]); ASSERT_EQ(4, positions[1]); ASSERT_EQ(11, positions[2]); + ASSERT_EQ(12, positions[3]); PASS(); } -TEST test_positions_3() { +TEST positions_no_bonuses() { size_t positions[2]; match_positions("as", "tags", positions); ASSERT_EQ(1, positions[0]); ASSERT_EQ(3, positions[1]); - PASS(); -} - -TEST test_positions_4() { - size_t positions[2]; match_positions("as", "examples.txt", positions); ASSERT_EQ(2, positions[0]); ASSERT_EQ(7, positions[1]); @@ -135,7 +165,7 @@ TEST test_positions_4() { PASS(); } -TEST test_positions_5() { +TEST positions_multiple_candidates_start_of_words() { size_t positions[3]; match_positions("abc", "a/a/b/c/c", positions); ASSERT_EQ(2, positions[0]); @@ -145,7 +175,7 @@ TEST test_positions_5() { PASS(); } -TEST test_positions_exact() { +TEST positions_exact_match() { size_t positions[3]; match_positions("foo", "foo", positions); ASSERT_EQ(0, positions[0]); @@ -156,13 +186,30 @@ TEST test_positions_exact() { } SUITE(match_suite) { - RUN_TEST(test_match); - RUN_TEST(test_relative_scores); - RUN_TEST(test_exact_scores); - RUN_TEST(test_positions_1); - RUN_TEST(test_positions_2); - RUN_TEST(test_positions_3); - RUN_TEST(test_positions_4); - RUN_TEST(test_positions_5); - RUN_TEST(test_positions_exact); + RUN_TEST(exact_match_should_return_true); + RUN_TEST(partial_match_should_return_true); + RUN_TEST(empty_query_should_always_match); + RUN_TEST(non_match_should_return_false); + RUN_TEST(match_with_delimiters_in_between); + + RUN_TEST(should_prefer_starts_of_words); + RUN_TEST(should_prefer_consecutive_letters); + RUN_TEST(should_prefer_contiguous_over_letter_following_period); + RUN_TEST(should_prefer_shorter_matches); + RUN_TEST(should_prefer_shorter_candidates); + RUN_TEST(should_prefer_start_of_candidate); + + RUN_TEST(score_exact_match); + RUN_TEST(score_empty_query); + RUN_TEST(score_gaps); + RUN_TEST(score_consecutive); + RUN_TEST(score_slash); + RUN_TEST(score_capital); + RUN_TEST(score_dot); + + RUN_TEST(positions_consecutive); + RUN_TEST(positions_start_of_word); + RUN_TEST(positions_no_bonuses); + RUN_TEST(positions_multiple_candidates_start_of_words); + RUN_TEST(positions_exact_match); } -- cgit v1.2.3