<commit_before><?php namespace net\xp_framework\unittest\core;

use xp\runtime\Code;

class CodeTest extends \unittest\TestCase {

  #[@test]
  public function can_create() {
    new Code('"Test"');
  }

  #[@test]
  public function can_create_with_empty() {
    new Code('');
  }

  #[@test]
  public function fragment() {
    $this->assertEquals('var_dump("Test");', (new Code('var_dump("Test")'))->fragment());
  }

  #[@test]
  public function fragment_with_semicolon() {
    $this->assertEquals('var_dump("Test");', (new Code('var_dump("Test");'))->fragment());
  }

  #[@test, @values([
  #  '<?php var_dump("Test")', '<?php    var_dump("Test")',
  #  '<? var_dump("Test")', '<?          var_dump("Test")',
  #  '<?=var_dump("Test")', '<?=         var_dump("Test")',
  #  '<?hh var_dump("Test")', '<?hh      var_dump("Test")'
  #])]
  public function fragment_with_php_tag($input) {
    $this->assertEquals('var_dump("Test");', (new Code($input))->fragment());
  }

  #[@test]
  public function expression() {
    $this->assertEquals('return "Test";', (new Code('"Test"'))->expression());
  }

  #[@test]
  public function expression_with_semicolon() {
    $this->assertEquals('return "Test";', (new Code('"Test";'))->expression());
  }

  #[@test]
  public function expression_with_existing_return() {
    $this->assertEquals('return "Test";', (new Code('return "Test";'))->expression());
  }

  #[@test, @values([
  #  'use util\Date; Date::now()',
  #  'use util\Date, util\TimeZone; Date::now()',
  #  'use util\Date; use util\TimeZone; Date::now()',
  #  'use util\{Date, TimeZone}; Date::now()'
  #])]
  public function use_is_stripped_from_fragment($input) {
    $this->assertEquals('Date::now();', (new Code($input))->fragment());
  }

  #[@test, @values([
  #  'use util\Date; Date::now()',
  #  'use util\Date, util\TimeZone; Date::now()',
  #  'use util\Date; use util\TimeZone; Date::now()',
  #  'use util\{Date, TimeZone}; Date::now()'
  #])]
  public function use_is_stripped_from_expression($input) {
    $this->assertEquals('return Date::now();', (new Code($input))->expression());
  }

  #[@test]
  public function empty_code_has_no_imports() {
    $this->assertEquals([], (new Code(''))->imports());
  }

  #[@test]
  public function code_without_imports() {
    $this->assertEquals([], (new Code('"Test"'))->imports());
  }

  #[@test]
  public function code_with_single_import() {
    $this->assertEquals(['util\Date'], (new Code('use util\Date; "Test"'))->imports());
  }

  #[@test]
  public function code_with_multiple_imports() {
    $this->assertEquals(['util\Date', 'util\TimeZone'], (new Code('use util\Date; use util\TimeZone; "Test"'))->imports());
  }

  #[@test]
  public function code_with_combined_import() {
    $this->assertEquals(['util\Date', 'util\TimeZone'], (new Code('use util\Date, util\TimeZone; "Test"'))->imports());
  }

  #[@test]
  public function code_with_grouped_import() {<commit_msg>Verify use statement can be prefixed with whitespace<commit_after><?php namespace net\xp_framework\unittest\core;

use xp\runtime\Code;

class CodeTest extends \unittest\TestCase {

  #[@test]
  public function can_create() {
    new Code('"Test"');
  }

  #[@test]
  public function can_create_with_empty() {
    new Code('');
  }

  #[@test]
  public function fragment() {
    $this->assertEquals('var_dump("Test");', (new Code('var_dump("Test")'))->fragment());
  }

  #[@test]
  public function fragment_with_semicolon() {
    $this->assertEquals('var_dump("Test");', (new Code('var_dump("Test");'))->fragment());
  }

  #[@test, @values([
  #  '<?php var_dump("Test")', '<?php    var_dump("Test")',
  #  '<? var_dump("Test")', '<?          var_dump("Test")',
  #  '<?=var_dump("Test")', '<?=         var_dump("Test")',
  #  '<?hh var_dump("Test")', '<?hh      var_dump("Test")'
  #])]
  public function fragment_with_php_tag($input) {
    $this->assertEquals('var_dump("Test");', (new Code($input))->fragment());
  }

  #[@test]
  public function expression() {
    $this->assertEquals('return "Test";', (new Code('"Test"'))->expression());
  }

  #[@test]
  public function expression_with_semicolon() {
    $this->assertEquals('return "Test";', (new Code('"Test";'))->expression());
  }

  #[@test]
  public function expression_with_existing_return() {
    $this->assertEquals('return "Test";', (new Code('return "Test";'))->expression());
  }

  #[@test, @values([
  #  'use util\Date; Date::now()',
  #  'use util\Date, util\TimeZone; Date::now()',
  #  'use util\Date; use util\TimeZone; Date::now()',
  #  'use util\{Date, TimeZone}; Date::now()',
  #  ' use util\Date; Date::now()',
  #  '<?php use util\Date; Date::now()',
  #  '<?php  use util\Date; Date::now()'
  #])]
  public function use_is_stripped_from_fragment($input) {
    $this->assertEquals('Date::now();', (new Code($input))->fragment());
  }

  #[@test, @values([
  #  'use util\Date; Date::now()',
  #  'use util\Date, util\TimeZone; Date::now()',
  #  'use util\Date; use util\TimeZone; Date::now()',
  #  'use util\{Date, TimeZone}; Date::now()',
  #  ' use util\Date; Date::now()',
  #  '<?php use util\Date; Date::now()',
  #  '<?php  use util\Date; Date::now()'
  #])]
  public function use_is_stripped_from_expression($input) {
    $this->assertEquals('return Date::now();', (new Code($input))->expression());
  }

  #[@test]
  public function empty_code_has_no_imports() {
    $this->assertEquals([], (new Code(''))->imports());
  }

  #[@test]
  public function code_without_imports() {
    $this->assertEquals([], (new Code('"Test"'))->imports());
  }

  #[@test, @values([
  #  'use util\Date;',
  #  ' use util\Date;',
  #  '<?php use util\Date;',
  #  '<?php  use util\Date;'
  #])]
  public function code_with_single_import($input) {
    $this->assertEquals(['util\Date'], (new Code($input))->imports());
  }

  #[@test]
  public function code_with_multiple_imports() {
    $this->assertEquals(['util\Date', 'util\TimeZone'], (new Code('use util\Date; use util\TimeZone; "Test"'))->imports());
  }

  #[@test]
  public function code_with_combined_import() {
    $this->assertEquals(['util\Date', 'util\TimeZone'], (new Code('use util\Date, util\TimeZone; "Test"'))->imports());
  }

  #[@test]
  public function code_with_grouped_import() {
{'fraction_non_alphanumeric': 0.2314313037934332, 'fraction_numerical': 0.0, 'mean_word_length': 4.499561787905346, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 55, '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': '6015912', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2192, 'n_words': 396}