<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase',
    'text.regex.Pattern'
  );

  /**
   * TestCase for pattern class
   *
   * @purpose  Unit test
   */
  class PatternTest extends TestCase {
  
    /**
     * Tests 
     *
     */
    #[@test]
    public function pattern() {
      $this->assertEquals(1, Pattern::compile('.')->matches('a')->length());
      $this->assertEquals(2, Pattern::compile('.')->matches('ab')->length());
    }

    /**
     * Tests the CASE_INSENSITIVE flag
     *
     */
    #[@test]
    public function caseInsensitive() {
      $this->assertEquals(1, Pattern::compile('a', Pattern::CASE_INSENSITIVE)->matches('A')->length());
    }

    /**
     * Tests the MatchResult::groups() method
     *
     */
    #[@test]
    public function groups() {
      $this->assertEquals(
        array(array('Hello')), 
        Pattern::compile('H[ea]llo')->matches('Hello')->groups()
      );
    }

    /**
     * Tests the MatchResult::group() method
     *
     */
    #[@test]
    public function group() {
      $this->assertEquals(
        array('Hello'), 
        Pattern::compile('H[ea]llo')->matches('Hello')->group(0)
      );
    }

    /**
     * Test a pattern with a missing ")" at the end throws a FormatException
     *
     */
    #[@test, @expect('lang.FormatException')]
    public function illegalPattern() {
      Pattern::compile('(');
    }
  }
?><commit_msg>- Add tests:   . stringRepresentation()   . unequality()   . equality()   . nonExistantGroup()   . nonUnicodePattern()   . unicodePattern()   . stringObjectInput()   . stringPrimitiveInput()<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase',
    'text.regex.Pattern',
    'lang.types.String'
  );

  /**
   * TestCase for pattern class
   *
   * @purpose  Unit test
   */
  class PatternTest extends TestCase {

    /**
     * Tests the MatchResult::length() method
     *
     */
    #[@test]
    public function length() {
      $this->assertEquals(
        0, 
        Pattern::compile('ABC')->matches('123')->length()
      );
    }

    /**
     * Tests "." as a pattern with string primitive
     *
     */
    #[@test]
    public function stringPrimitiveInput() {
      $this->assertEquals(0, Pattern::compile('.')->matches('')->length());
      $this->assertEquals(1, Pattern::compile('.')->matches('a')->length());
      $this->assertEquals(2, Pattern::compile('.')->matches('ab')->length());
    }

    /**
     * Tests "." as a pattern with lang.types.String instances
     *
     */
    #[@test]
    public function stringObjectInput() {
      $this->assertEquals(0, Pattern::compile('.')->matches(new String(''))->length());
      $this->assertEquals(1, Pattern::compile('.')->matches(new String('a'))->length());
      $this->assertEquals(2, Pattern::compile('.')->matches(new String('ab'))->length());
    }

    /**
     * Tests unicode pattern
     *
     */
    #[@test]
    public function unicodePattern() {
      $this->assertEquals(
        array('Gün'), 
        Pattern::compile('.ü.', Pattern::UTF8)->matches(new String('Gnter'))->group(0)
      );
    }

    /**
     * Tests non-unicode pattern
     *
     */
    #[@test]
    public function nonUnicodePattern() {
      $this->assertEquals(
        array('Gn'), 
        Pattern::compile('..')->matches(new String('Gnter'))->group(0)
      );
    }

    /**
     * Tests the CASE_INSENSITIVE flag
     *
     */
    #[@test]
    public function caseInsensitive() {
      $this->assertEquals(
        1, 
        Pattern::compile('a', Pattern::CASE_INSENSITIVE)->matches('A')->length()
      );
    }

    /**
     * Tests the MatchResult::groups() method
     *
     */
    #[@test]
    public function groups() {
      $this->assertEquals(
        array(array('Hello')), 
        Pattern::compile('H[ea]llo')->matches('Hello')->groups()
      );
    }

    /**
     * Tests the MatchResult::group() method
     *
     */
    #[@test]
    public function group() {
      $this->assertEquals(
        array('Hello'), 
        Pattern::compile('H[ea]llo')->matches('Hello')->group(0)
      );
    }

    /**
     * Tests the MatchResult::group() method
     *
     */
    #[@test, @expect('lang.IndexOutOfBoundsException')]
    public function nonExistantGroup() {
      Pattern::compile('H[ea]llo')->matches('Hello')->group(1);
    }

    /**
     * Tests the Pattern::equals() method
     *
     */
    #[@test]
    public function equality() {
      $this->assertEquals(
        Pattern::compile('[a-z]+'),
        Pattern::compile('[a-z]+')
      );
    }

    /**
     * Tests the Pattern::equals() method
     *
     */
    #[@test]
    public function unequality() {
      $this->assertNotEquals(
        Pattern::compile('[a-z]+', Pattern::CASE_INSENSITIVE),
        Pattern::compile('[a-z]+')
      );
    }

    /**
     * Tests the Pattern::toString() method
     *
     */
    #[@test]
    public function stringRepresentation() {
      $this->assertEquals(
        'text.regex.Pattern</[a-z]+/i>',
        Pattern::compile('[a-z]+', Pattern::CASE_INSENSITIVE)->toString()
      );
    }

    /**
     * Test a pattern with a missing ")" at the end throws a FormatException
     *
     */
    #[@test, @expect('lang.FormatException')]
    public function illegalPattern() {
      Pattern::compile('(');
    }
  }
?>
{'fraction_non_alphanumeric': 0.19030950626381724, 'fraction_numerical': 0.0036845983787767134, 'mean_word_length': 2.286319612590799, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6714501', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1615, 'n_words': 287}