Changeset View
Changeset View
Standalone View
Standalone View
src/search/__tests__/PhutilSearchQueryCompilerTestCase.php
| Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | $stemming_tests = array( | ||||
| '+"mail" +"user" +"for" +"mention" +"on" +"task"', | '+"mail" +"user" +"for" +"mention" +"on" +"task"', | ||||
| ), | ), | ||||
| ); | ); | ||||
| $stemmer = new PhutilSearchStemmer(); | $stemmer = new PhutilSearchStemmer(); | ||||
| $this->assertCompileQueries($stemming_tests, null, $stemmer); | $this->assertCompileQueries($stemming_tests, null, $stemmer); | ||||
| } | } | ||||
| public function testCompileQueriesWithFunctions() { | |||||
| $op_and = PhutilSearchQueryCompiler::OPERATOR_AND; | |||||
| $op_sub = PhutilSearchQueryCompiler::OPERATOR_SUBSTRING; | |||||
| $op_exact = PhutilSearchQueryCompiler::OPERATOR_EXACT; | |||||
| $function_tests = array( | |||||
| 'cat' => array( | |||||
| array(null, $op_and, 'cat'), | |||||
| ), | |||||
| ':cat' => array( | |||||
| array(null, $op_and, 'cat'), | |||||
| ), | |||||
| 'title:cat' => array( | |||||
| array('title', $op_and, 'cat'), | |||||
| ), | |||||
| 'title:cat:dog' => array( | |||||
| array('title', $op_and, 'cat:dog'), | |||||
| ), | |||||
| 'title:~cat' => array( | |||||
| array('title', $op_sub, 'cat'), | |||||
| ), | |||||
| 'cat title:="Meow Meow"' => array( | |||||
| array(null, $op_and, 'cat'), | |||||
| array('title', $op_exact, 'Meow Meow'), | |||||
| ), | |||||
| 'title:cat title:dog' => array( | |||||
| array('title', $op_and, 'cat'), | |||||
| array('title', $op_and, 'dog'), | |||||
| ), | |||||
| '~"core and seven years ag"' => array( | |||||
| array(null, $op_sub, 'core and seven years ag'), | |||||
| ), | |||||
| ); | |||||
| $this->assertCompileFunctionQueries($function_tests); | |||||
| } | |||||
| private function assertCompileQueries( | private function assertCompileQueries( | ||||
| array $tests, | array $tests, | ||||
| $operators = null, | $operators = null, | ||||
| PhutilSearchStemmer $stemmer = null) { | PhutilSearchStemmer $stemmer = null) { | ||||
| foreach ($tests as $input => $expect) { | foreach ($tests as $input => $expect) { | ||||
| $caught = null; | $caught = null; | ||||
| $query = null; | $query = null; | ||||
| Show All 40 Lines | foreach ($tests as $input => $expect) { | ||||
| ($literal_query === false) | ($literal_query === false) | ||||
| ? false | ? false | ||||
| : array($literal_query, $stemmed_query), | : array($literal_query, $stemmed_query), | ||||
| pht('Stemmed compilation of query: %s', $input)); | pht('Stemmed compilation of query: %s', $input)); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| private function assertCompileFunctionQueries(array $tests) { | |||||
| foreach ($tests as $input => $expect) { | |||||
| $compiler = id(new PhutilSearchQueryCompiler()) | |||||
| ->setEnableFunctions(true); | |||||
| $tokens = $compiler->newTokens($input); | |||||
| $result = array(); | |||||
| foreach ($tokens as $token) { | |||||
| $result[] = array( | |||||
| $token->getFunction(), | |||||
| $token->getOperator(), | |||||
| $token->getValue(), | |||||
| ); | |||||
| } | |||||
| $this->assertEqual( | |||||
| $expect, | |||||
| $result, | |||||
| pht('Function compilation of query: %s', $input)); | |||||
| } | |||||
| } | |||||
| } | } | ||||