#!perl
use Cassandane::Tiny;

sub test_variable_modifiers
{
    my ($self) = @_;

    $self->{instance}->install_sieve_script(<<'EOF'
require ["variables", "editheader", "regex", "enotify"];

set                        "a" "juMBlEd?lETteRS=.*";
set :length                "b" "${a}";  #  => "18"
set :lower                 "c" "${a}";  #  => "jumbled?letters=.*"
set :upper                 "d" "${a}";  #  => "JUMBLED?LETTERS=.*"
set :lowerfirst            "e" "${a}";  #  => "juMBlEd?lETteRS=.*"
set :lowerfirst :upper     "f" "${a}";  #  => "jUMBLED?LETTERS.*"
set :upperfirst            "g" "${a}";  #  => "JuMBlEd?lETteRS=.*"
set :upperfirst :lower     "h" "${a}";  #  => "Jumbled?letters=.*"
set :quotewildcard         "i" "${a}";  #  => "juMBlEd\?lETteRS=.\*"
set :quoteregex            "j" "${a}";  #  => "juMBlEd\?lETteRS=\.\*"
set :encodeurl             "k" "${a}";  #  => "juMBlEd%3FlETteRS%3D.%2A"
set :encodeurl :upper      "l" "${a}";  #  => "JUMBLED%3FLETTERS%3D.%2A"
set :quotewildcard :upper  "m" "${a}";  #  => "JUMBLED\?LETTERS=.\*"
set :quoteregex :upper     "n" "${a}";  #  => "JUMBLED\?LETTERS=\.\*"
set :quoteregex :encodeurl
    :upperfirst :lower     "o" "${a}";  #  => "Jumbled%5C%3fletters%3D%5C.%5C%2A"
set :quoteregex :encodeurl
    :upper :length         "p" "${a}";  #  => "33"

addheader "X-Cassandane-Test" "len = \"${b}\"";
addheader "X-Cassandane-Test" "lower = \"${c}\"";
addheader "X-Cassandane-Test" "upper = \"${d}\"";
addheader "X-Cassandane-Test" "lowerfirst = \"${e}\"";
addheader "X-Cassandane-Test" "lowerfirst+upper = \"${f}\"";
addheader "X-Cassandane-Test" "upperfirst = \"${g}\"";
addheader "X-Cassandane-Test" "upperfirst+lower = \"${h}\"";
addheader "X-Cassandane-Test" "wild = \"${i}\"";
addheader "X-Cassandane-Test" "regex = \"${j}\"";
addheader "X-Cassandane-Test" "url = \"${k}\"";
addheader "X-Cassandane-Test" "url+upper = \"${l}\"";
addheader "X-Cassandane-Test" "wild+upper = \"${m}\"";
addheader "X-Cassandane-Test" "regex+upper = \"${n}\"";
addheader "X-Cassandane-Test" "regex+url+upperfirst+lower = \"${o}\"";
addheader "X-Cassandane-Test" "regex+url+upper+len = \"${p}\"";
EOF
        );

    xlog $self, "Deliver a message";
    my $msg1 = $self->{gen}->generate(subject => "Message 1");
    $self->{instance}->deliver($msg1);

    my $imaptalk = $self->{store}->get_client();
    $imaptalk->select("INBOX");
    my $res = $imaptalk->fetch(1, 'rfc822');

    $msg1 = $res->{1}->{rfc822};

    $self->assert_matches(qr/X-Cassandane-Test: len = "18"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: lower = "jumbled\?letters=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: upper = "JUMBLED\?LETTERS=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: lowerfirst = "juMBlEd\?lETteRS=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: lowerfirst\+upper = "jUMBLED\?LETTERS=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: upperfirst = "JuMBlEd\?lETteRS=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: upperfirst\+lower = "Jumbled\?letters=\.\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: wild = "juMBlEd\\\?lETteRS=\.\\\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: regex = "juMBlEd\\\?lETteRS=\\\.\\\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: url = "juMBlEd%3FlETteRS%3D\.%2A"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: wild\+upper = "JUMBLED\\\?LETTERS=\.\\\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: regex\+upper = "JUMBLED\\\?LETTERS=\\\.\\\*"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: url\+upper = "JUMBLED%3FLETTERS%3D\.%2A"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: regex\+url\+upperfirst\+lower = "Jumbled%5C%3Fletters%3D%5C.%5C%2A"\r\n/, $msg1);
    $self->assert_matches(qr/X-Cassandane-Test: regex\+url\+upper\+len = "33"\r\n/, $msg1);
}
