# file      : tests/name/extension.testscript
# license   : MIT; see accompanying LICENSE file

.include ../common.testscript

+cat <<EOI >=build/root.build
define txt: file
txt{*}: extension = txt
EOI

: name
:
{{
  test.arguments = --match-only update

  : unspecified
  :
  touch foo.txt;
  $* <<EOI >>EOO
  ./: txt{foo}
  print $name(txt{foo})
  print $extension(txt{foo})
  print txt{fo?}
  EOI
  foo
  [null]
  txt{foo}
  EOO

  : specified
  :
  touch foo.text;
  $* <<EOI >>EOO
  ./: txt{foo.text}
  print $name(txt{foo.text})
  print $extension(txt{foo.text})
  print txt{fo?.text}
  EOI
  foo
  text
  txt{foo.text}
  EOO

  : specified-triple
  :
  touch fo.o.text;
  $* <<EOI >>EOO
  ./: txt{fo...o.text}
  print $name(txt{fo...o.text})
  print $extension(txt{fo...o.text})
  print txt{f?...o.text}
  EOI
  fo
  o.text
  txt{fo...o.text}
  EOO

  : multiple-triples
  :
  $* <'./: txt{f...o...o}' 2>>EOE != 0
  <stdin>:1:5: error: multiple triple dots in target name 'f...o...o'
  EOE

  : multiple-singles
  :
  touch fo.o.text;
  $* <<EOI >>EOO
  ./: txt{fo.o.text}
  print $name(txt{fo.o.text})
  print $extension(txt{fo.o.text})
  print txt{f?.o.text}
  EOI
  fo.o
  text
  txt{fo.o.text}
  EOO

  : escape-basename
  :
  touch fo.o.text;
  $* <<EOI >>EOO
  ./: txt{fo..o.text}
  print $name(txt{fo..o.text})
  print $extension(txt{fo..o.text})
  print txt{f?..o.text}
  EOI
  fo.o
  text
  txt{fo.o.text}
  EOO

  : escape-extension
  :
  touch fo.o.text;
  $* <<EOI >>EOO
  ./: txt{fo.o..text}
  print $name(txt{fo.o..text})
  print $extension(txt{fo.o..text})
  print txt{f?.o..text}
  EOI
  fo
  o.text
  txt{fo...o.text}
  EOO

  : trailing
  :
  {{
    : no-extension
    :
    touch foo;
    $* <<EOI >>EOO
    ./: txt{foo.}
    print $name(txt{foo.})
    print $extension(txt{foo.})
    print txt{fo?.}
    EOI
    foo

    txt{foo.}
    EOO

    : dot-no-extension
    :
    touch f.oo;
    $* <<EOI >>EOO
    ./: txt{f.oo.}
    print $name(txt{f.oo.})
    print $extension(txt{f.oo.})
    print txt{f.o?.}
    EOI
    f.oo


    EOO

    : default-extension
    :
    touch foo.testscript.txt;
    $* <<EOI >>EOO
    ./: txt{foo.testscript...}
    print $name(txt{foo.testscript...})
    print $extension(txt{foo.testscript...})
    print txt{fo?.testscript...}
    EOI
    foo.testscript
    [null]
    txt{foo.testscript...}
    EOO

    # Trailing dots are not allowed on Windows.
    #

    : escape-one
    :
    if ($cxx.target.class != 'windows')
    {
      touch foo.
      $* <<EOI >>EOO
      ./: txt{foo..}
      print $name(txt{foo..})
      print $extension(txt{foo..})
      print txt{fo?..}
      EOI
      foo.

      txt{foo..}
      EOO
    }

    : escape-two
    :
    if ($cxx.target.class != 'windows')
    {
      touch foo..
      $* <<EOI >>EOO
      ./: txt{foo....}
      print $name(txt{foo....})
      print $extension(txt{foo....})
      print txt{fo?....}
      EOI
      foo..

      txt{foo....}
      EOO
    }
  }}

  : leading
  :
  {{
    : single
    :
    touch .foo.txt;
    $* <<EOI >>EOO
    ./: txt{.foo}
    print $name(txt{.foo})
    print $extension(txt{.foo})
    print txt{.fo?}
    EOI
    .foo
    [null]
    txt{.foo}
    EOO

    : triple
    :
    $* <'./: txt{...foo}' 2>>EOE != 0
    <stdin>:1:5: error: leading triple dots in target name '...foo'
    EOE

    : escape-one
    :
    touch .foo.txt;
    $* <<EOI >>EOO
    ./: txt{..foo}
    print $name(txt{..foo})
    print $extension(txt{..foo})
    print txt{..fo?}
    EOI
    .foo
    [null]
    txt{.foo}
    EOO

    : escape-two
    :
    touch ..foo.txt;
    $* <<EOI >>EOO
    ./: txt{....foo}
    print $name(txt{....foo})
    print $extension(txt{....foo})
    print txt{....fo?}
    EOI
    ..foo
    [null]
    txt{....foo}
    EOO
  }}

  : specified-invalid
  :
  $* <'./: txt{fo.....o}' 2>>EOE != 0
  <stdin>:1:5: error: invalid dot sequence in target name 'fo.....o'
  EOE

  : specified-dot-only
  :
  $* <'./: txt{..}' 2>>EOE != 0
  <stdin>:1:5: error: invalid target name '..'
  EOE
}}

: pattern
:
{{
  : specified-none
  :
  touch foo;
  $* <'print txt{fo?.}' >'txt{foo.}'

  : specified-default
  :
  touch foo.testscript.txt;
  $* <'print txt{fo?.testscript...}' >'txt{foo.testscript...}'

  : dir
  :
  touch foo.txt;
  $* <'print txt{./f*}' >'txt{./foo}'

  : dir-leading-triple
  :
  $* <'print txt{./...f*}' 2>>EOE != 0
  <stdin>:1:11: error: leading triple dots in target name './...f*'
  EOE

  # Trailing dots are not allowed on Windows.
  #

  : specified-escape
  :
  if ($cxx.target.class != 'windows')
  {
    touch foo.
    $* <'print txt{fo?..}' >'txt{foo..}'
  }
}}
