Skip to content

Commit

Permalink
using data providers and add more cases in testLevenshteinDistance()
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyeshhhh committed Apr 20, 2024
1 parent f5da963 commit 8fc14d8
Showing 1 changed file with 35 additions and 56 deletions.
91 changes: 35 additions & 56 deletions tests/FuzzyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,48 @@

class FuzzyTest extends TestCase
{
public function testFuzzySearch(): void
/**
* @dataProvider fuzzySearchDataProvider
*/
public function testFuzzySearch(string $searchTerm, array $strings, int $threshold, array $expected): void
{
$strings = ['Espresso', 'Cappuccino', 'Latte', 'Americano', 'Mocha'];

// Test with normal data (correct spelling)
$searchTerm = 'Espresso';
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertContains('Espresso', $result);
$this->assertNotContains('Mocha', $result);

// Test with misspelling within threshold
$searchTerm = 'Espreso'; // Missing 's'
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertContains('Espresso', $result);

// Test with misspelling exceeding threshold
$searchTerm = 'Espressso'; // Extra 's'
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertContains('Espresso', $result);

// Test with empty search term
$searchTerm = '';
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertEquals([], $result); // Expect no matches

// Test with non-string search term (integer)
$searchTerm = (string) 123;
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertEquals([], $result); // Expect no matches (type mismatch)

// Test with search term containing special characters
$searchTerm = 'Latte!';
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);

$this->assertContains('Latte', $result); // Should still match 'Latte'

// Test with case sensitivity (optional, depending on your needs)
$searchTerm = 'eSPRESSO'; // All uppercase
$threshold = 1;
$result = Utility::fuzzySearch($searchTerm, $strings, $threshold);
// Depending on your implementation, this might match (case-insensitive)
// or not match (case-sensitive). Update assertions accordingly.

$this->assertEquals($expected, $result);
}

public function testLevenshteinDistance(): void
public static function fuzzySearchDataProvider(): array
{
$str1 = 'Almond';
$str2 = 'Coconut';
$strings = ['Espresso', 'Cappuccino', 'Latte', 'Americano', 'Mocha'];
return [
['Espresso', $strings, 1, ['Espresso']],
['Espreso', $strings, 1, ['Espresso']], // Missing 's'
['Espressso', $strings, 1, ['Espresso']], // Extra 's'
['', $strings, 1, []], // Empty search term
[(string) 123, $strings, 1, []], // Non-string search term (integer)
['Latte!', $strings, 1, ['Latte']], // Search term containing special characters
['eSPRESSO', $strings, 1, ['Espresso']], // Case sensitivity test
];
}

/**
* @dataProvider levenshteinDistanceDataProvider
*/
public function testLevenshteinDistance(string $str1, string $str2, int $expected): void
{
$result = Utility::levenshteinDistance($str1, $str2);
$this->assertEquals($expected, $result);
}

$this->assertEquals(5, $result);
public static function levenshteinDistanceDataProvider(): array
{
return [
['Almond', 'Coconut', 5],
['Almond', 'Almond', 0], // Same strings
['Almond', 'Almon', 1], // Missing character
['Almond', 'Almondd', 1], // Extra character
['Almond', 'Almend', 1], // Different character
['Almond', '', 6], // One string is empty
['', '', 0], // Both strings are empty
];
}
}

0 comments on commit 8fc14d8

Please sign in to comment.