package TestCase::LexicalVar {
  use TestCase::Minimal;
  
  our $VAR_PREC : int;
  our $VAR_EXPANSION1 : int;
  
  BEGIN {
    $VAR_PREC = 11;
  }
  
  sub var_brace : int () {
    
    my ${var} = 5;
    
    unless (${var} == 5) {
      return 0;
    }
    
    return 1;
  }

  sub var_expansion : int () {
    
    # Variable access
    {
      my $var1 = 1;
      my $var2 = 2;
      
      my $string = "a $var1 b ${var2}c ${var1}->2";
      
      unless ($string eq "a 1 b 2c 1->2") {
        return 0;
      }
    }
    
    # Exception variable
    {
      $@ = "Error";
      
      my $string = "a $@b ${@}c";
      
      unless ($string eq "a Errorb Errorc") {
        return 0;
      }
      $@ = undef;
    }

    # Dereference variable
    {
      my $num = 5;
      my $num_ref = \$num;
      
      my $string = "a $$num_ref b $${num_ref}c";
      
      unless ($string eq "a 5 b 5c") {
        return 0;
      }
    }
    
    # Package variable
    {
      $VAR_EXPANSION1 = 6;
      
      my $string = "a $TestCase::LexicalVar::VAR_EXPANSION1 b";
      
      unless ($string eq "a 6 b") {
        return 0;
      }
    }

    # Field and hash access
    {
      my $nums = [1, 2, 3];
      my $minimal = TestCase::Minimal->new;
      $minimal->{x} = 5;
      
      my $string = "a $nums->[1]b $minimal->{x}c";
      
      unless ($string eq "a 2b 5c") {
        return 0;
      }
    }

    # Multiple field and array access
    {
      my $minimals = new TestCase::Minimal[2];
      $minimals->[1] = new TestCase::Minimal;
      $minimals->[1]{x} = 5;
      
      my $string = "a $minimals->[1]{x}b $minimals->[ 1 ]{ x }c";
      
      unless ($string eq "a 5b 5c") {
        return 0;
      }
    }
    
    return 1;
  }

  sub lexical_var_upper_case : int () {
    
    my $VAR1 = 1;
    
    unless ($VAR1 == 1) {
      return 0;
    }
    
    return 1;
  }

  sub lexical_var_high_precidence_than_package_var : int () {
    
    my $VAR_PREC = 3;
    
    unless ($VAR_PREC == 3) {
      return 0;
    }
    
    return 1;
  }

  sub lexical_var_high_precidence_than_capture_var : int () {
    
    my $var = 13;
    my $cb = [$var : int] sub : int ($self : self) {
      
      my $var = 5;
      
      return $var;
    };
    
    unless ($cb->() == 5) {
      return 0;
    }
    
    return 1;
  }

}
