Skip to content

Commit

Permalink
[Tests] Move expectException closer to the place of the expectation t…
Browse files Browse the repository at this point in the history
…o avoid false positives
  • Loading branch information
OskarStark committed Oct 31, 2023
1 parent 40eff9d commit d036c6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 2 additions & 3 deletions Tests/CssSelectorConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function testParseExceptions()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Expected identifier, but <eof at 3> found.');
$converter = new CssSelectorConverter();
$converter->toXPath('h1:');
(new CssSelectorConverter())->toXPath('h1:');
}

/** @dataProvider getCssToXPathWithoutPrefixTestData */
Expand All @@ -60,7 +59,7 @@ public function testCssToXPathWithoutPrefix($css, $xpath)
$this->assertEquals($xpath, $converter->toXPath($css, ''), '->parse() parses an input string and returns a node');
}

public static function getCssToXPathWithoutPrefixTestData()
public static function getCssToXPathWithoutPrefixTestData(): array
{
return [
['h1', 'h1'],
Expand Down
10 changes: 6 additions & 4 deletions Tests/Parser/TokenStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public function testGetNextIdentifier()

public function testFailToGetNextIdentifier()
{
$this->expectException(SyntaxErrorException::class);

$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));

$this->expectException(SyntaxErrorException::class);

$stream->getNextIdentifier();
}

Expand All @@ -74,10 +75,11 @@ public function testGetNextIdentifierOrStar()

public function testFailToGetNextIdentifierOrStar()
{
$this->expectException(SyntaxErrorException::class);

$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));

$this->expectException(SyntaxErrorException::class);

$stream->getNextIdentifierOrStar();
}

Expand Down
24 changes: 18 additions & 6 deletions Tests/XPath/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,68 @@ public function testCssToXPath($css, $xpath)

public function testCssToXPathPseudoElement()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));

$this->expectException(ExpressionErrorException::class);

$translator->cssToXPath('e::first-line');
}

public function testGetExtensionNotExistsExtension()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));

$this->expectException(ExpressionErrorException::class);

$translator->getExtension('fake');
}

public function testAddCombinationNotExistsExtension()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$parser = new Parser();
$xpath = $parser->parse('*')[0];
$combinedXpath = $parser->parse('*')[0];

$this->expectException(ExpressionErrorException::class);

$translator->addCombination('fake', $xpath, $combinedXpath);
}

public function testAddFunctionNotExistsFunction()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$xpath = new XPathExpr();
$function = new FunctionNode(new ElementNode(), 'fake');

$this->expectException(ExpressionErrorException::class);

$translator->addFunction($xpath, $function);
}

public function testAddPseudoClassNotExistsClass()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$xpath = new XPathExpr();

$this->expectException(ExpressionErrorException::class);

$translator->addPseudoClass($xpath, 'fake');
}

public function testAddAttributeMatchingClassNotExistsClass()
{
$this->expectException(ExpressionErrorException::class);
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$xpath = new XPathExpr();

$this->expectException(ExpressionErrorException::class);

$translator->addAttributeMatching($xpath, '', '', '');
}

Expand Down

0 comments on commit d036c6c

Please sign in to comment.