# file      : tests/standard-version/testscript
# license   : MIT; see accompanying LICENSE file

: valid
:
: Roundtrip version.
:
{
  : non-prerelease
  :
  $* <<EOF >>EOF
  1.2.3
  EOF

  : prerelease
  :
  {
    : final
    :
    $* <<EOF >>EOF
    1.2.3-a.1
    1.2.3-b.1
    EOF

    : snapshot
    :
    $* <<EOF >>EOF
    1.2.3-a.1.z
    1.2.3-a.0.456
    1.2.3-a.1.456.340c0a26a5ef
    EOF

    : earliest
    :
    $* <<EOF >>EOF
    1.2.3-
    EOF
  }

  : revision
  :
  $* <<EOF >>EOF
  1.2.3+4
  1.2.3-a.4+5
  1.2.3-a.4.z+5
  1.2.3-a.4.567+8
  1.2.3-a.4.567.340c0a26a5ef+8
  EOF

  : epoch
  :
  $* <<EOF >>EOF
  +4-1.2.3
  EOF

  : stub
  :
  $* <<EOF >>EOF
  0
  0+1
  EOF

  : empty
  :
  $* <<EOF >>EOF

  EOF

  : max
  :
  $* <<EOF >>EOF
  1.2.99999
  1.99999.99999
  99999.99999.99999
  EOF
}

: invalid
:
{
  $* <'+1+'           2>"'-' expected after epoch"              == 1 : epoch
  $* <'a'             2>'invalid major version'                 == 1 : major
  $* <'1'             2>"'.' expected after major version"      == 1 : no-major-dot
  $* <'1.a'           2>'invalid minor version'                 == 1 : minor
  $* <'1.2'           2>"'.' expected after minor version"      == 1 : no-minor-dot
  $* <'1.2.a'         2>'invalid patch version'                 == 1 : patch
  $* <'+1-0.0.0'      2>'0.0.0 version'                         == 1 : zero-version
  $* <'1.2.3-k'       2>"'a' or 'b' expected in pre-release"    == 1 : a-b-expected
  $* <'1.2.3-a'       2>"'.' expected after pre-release letter" == 1 : prerelease-dot-expected
  $* <'1.2.3-a.b'     2>'invalid pre-release'                   == 1 : prerelease
  $* <'1.2.3-b.0'     2>'invalid final pre-release'             == 1 : final-prerelease
  $* <'1.2.3-a.1.0'   2>'invalid snapshot number'               == 1 : snapshot-num
  $* <'1.2.3-a.1.1.@' 2>'invalid snapshot id'                   == 1 : snapshot-id

  : revision
  :
  {
    $* <'1.2.3+a'               2>'invalid revision'                   == 1 : non-prerelease
    $* <'1.2.3-a.1+a'           2>'invalid revision'                   == 1 : prerelease
    $* <'1.2.3-a.0.1+a'         2>'invalid revision'                   == 1 : snapshot-num
    $* <'1.2.3-a.0.1.83jdgsf+0' 2>'invalid revision'                   == 1 : snapshot-id
    $* <'1.2.3-+1'              2>"'a' or 'b' expected in pre-release" == 1 : earliest-prerelease
  }

  : trailing-junk-after
  :
  {
    $* <'1.2.3-a.1.z.a'  2>'junk after version' == 1 : snapshot-num
    $* <'1.2.3-a.1.z+1a' 2>'junk after version' == 1 : revision
  }
}

: release
:
{
  test.options += -rl

  $* '1.2.3'       >'y' : release
  $* '1.2.3-b.1'   >'n' : beta-final
  $* '1.2.3-a.1'   >'n' : alpha-final
  $* '1.2.3-a.0.1' >'n' : alpha-snapshot
  $* '1.2.3-b.0.1' >'n' : beta-snapshot
  $* '1.2.3-'      >'n' : earliest
  $* '0'           >'n' : stub
}

: pre-release
:
{
  test.options += -pr

  $* '1.2.3'       >'-'   : release
  $* '1.2.3-b.1'   >'501' : beta-final
  $* '1.2.3-a.1'   >'1'   : alpha-final
  $* '1.2.3-a.0.1' >'0'   : alpha-snapshot
  $* '1.2.3-b.0.1' >'500' : beta-snapshot
  $* '1.2.3-'      >'0'   : earliest
  $* '0'           >'-'   : stub
}

: alpha
:
{
  test.options += -al

  $* '1.2.3'       >'-' : release
  $* '1.2.3-b.1'   >'-' : beta-final
  $* '1.2.3-a.1'   >'1' : final
  $* '1.2.3-a.0.1' >'0' : snapshot
  $* '1.2.3-b.0.1' >'-' : beta-snapshot
  $* '1.2.3-'      >'0' : earliest
  $* '0'           >'-' : stub
}

: beta
:
{
  test.options += -bt

  $* '1.2.3'       >'-' : release
  $* '1.2.3-a.1'   >'-' : alpha
  $* '1.2.3-b.1'   >'1' : final
  $* '1.2.3-a.0.1' >'-' : alpha-snapshot
  $* '1.2.3-b.0.1' >'0' : snapshot
  $* '1.2.3-'      >'-' : earliest
  $* '0+1'         >'-' : stub
}

: stub
:
{
  test.options += -st

  $* '1.2.3'       >'n' : release
  $* '1.2.3-b.1'   >'n' : beta-final
  $* '1.2.3-a.1'   >'n' : alpha-final
  $* '1.2.3-a.0.1' >'n' : alpha-snapshot
  $* '1.2.3-b.0.1' >'n' : beta-snapshot
  $* '1.2.3-'      >'n' : earliest
  $* '0'           >'y' : stub
  $* '0+1'         >'y' : stub-1
}

: earliest
:
{
  test.options += -el

  $* '1.2.3'       >'n' : release
  $* '1.2.3-b.1'   >'n' : beta-final
  $* '1.2.3-a.1'   >'n' : alpha-final
  $* '1.2.3-a.0.1' >'n' : alpha-snapshot
  $* '1.2.3-b.0.1' >'n' : beta-snapshot
  $* '1.2.3-'      >'y' : earliest
  $* '0+2'         >'n' : stub
}

: snapshot
:
{
  test.options += -sn

  $* '1.2.3'       >'n' : release
  $* '1.2.3-b.1'   >'n' : beta-final
  $* '1.2.3-a.1'   >'n' : alpha-final
  $* '1.2.3-a.0.1' >'y' : alpha
  $* '1.2.3-b.0.1' >'y' : beta
  $* '1.2.3-a.1.z' >'y' : latest
  $* '1.2.3-'      >'n' : earliest
  $* '0'           >'n' : stub
}

: final
:
{
  test.options += -fn

  $* '1.2.3'       >'y' : release
  $* '1.2.3-b.1'   >'y' : beta-final
  $* '1.2.3-a.1'   >'y' : alpha-final
  $* '1.2.3-a.0.1' >'n' : alpha
  $* '1.2.3-b.0.1' >'n' : beta
  $* '1.2.3-'      >'y' : earliest
  $* '0'           >'n' : stub
}

: compare
:
{
  test.options += -cm

  : epoch
  :
  {
    $* '+4-1.2.3' '+4-1.2.3' >'0'  : equal
    $* '1.2.4'    '+4-1.2.3' >'-1' : less
  }

  : non-prerelease
  :
  {
    $* '1.2.3' '1.2.3' >'0'  : equal
    $* '1.2.3' '1.2.4' >'-1' : less
  }

  : prerelease
  :
  {
    $* '1.2.3-a.1'      '1.2.3-a.1'   >'0'  : equal
    $* '1.2.3'          '1.2.3-a.1'   >'1'  : release-gt-prerelease
    $* '1.2.3-a.2'      '1.2.3-b.1'   >'-1' : a-lt-b
    $* '1.2.3-a.1'      '1.2.3-a.1.2' >'-1' : final-lt-snapshot
    $* '1.2.3-a.1.2.xy' '1.2.3-a.1.2' >'0'  : ignore-snapshot-id
    $* '1.2.3'          '1.2.3-a.0.1' >'1'  : release-gt-snapshot
    $* '1.2.3-a.0.1'    '1.2.3-a.1'   >'-1' : snapshot-lt-final
    $* '1.2.3'          '1.2.3-'      >'1'  : release-gt-earliest
    $* '1.2.3-a.1'      '1.2.3-'      >'1'  : final-gt-earliest
    $* '1.2.3-a.0.1'    '1.2.3-'      >'1'  : snapshot-gt-earliest
    $* '1.2.2-b.499.z'  '1.2.3-'      >'-1' : prev-max-snapshot-lt-earliest
  }
}

: constraints
:
{
  test.options += -cr

  : range
  :
  {
    : valid
    :
    $* <<EOI >>EOE
    [1.2.3 1.2.4]
    (1.2.3 1.2.4)
    [ 1.2.3-  1.2.4- ]
    [1.99999.0 2.0.0)
    EOI
    [1.2.3 1.2.4]
    (1.2.3 1.2.4)
    [1.2.3- 1.2.4-]
    [1.99999.0 2.0.0)
    EOE

    : invalid
    :
    {
      $* <''                  2>'invalid constraint'                                    == 1 : empty
      $* <'1'                 2>'invalid constraint'                                    == 1 : no-opening
      $* <'[ '                2>'no min version'                                        == 1 : no-min
      $* <'[1.2.3'            2>'no max version'                                        == 1 : no-max
      $* <'[1.2.3 1.2.4'      2>'no closing bracket'                                    == 1 : no-closing
      $* <'[1.2.3 1.2.4)]'    2>'junk after constraint'                                 == 1 : junk
      $* <'[1'                2>"invalid min version: '.' expected after major version" == 1 : invalid-min
      $* <'[1.2.3 1'          2>"invalid max version: '.' expected after major version" == 1 : invalid-max
      $* <'[99999.0.0 1.0.0)' 2>'min version is greater than max version'               == 1 : min-gt-max
      $* <'[1.2.3 1.2.3)'     2>'equal version endpoints not closed'                    == 1 : open-end
      $* <'[1.2.3- 1.2.3-]'   2>'equal version endpoints are earliest'                  == 1 : earliest-prerelease
    }
  }

  : comparison
  :
  {
    : valid
    :
    $* <<EOI >>EOE
    == 1.2.3
    >= 1.2.3
    <= 1.2.3
    > 1.2.3
    < 1.2.3
    >=1.2.3-
    <=1.2.3-
    >1.2.3-
    <1.2.3-
    EOI
    == 1.2.3
    >= 1.2.3
    <= 1.2.3
    > 1.2.3
    < 1.2.3
    >= 1.2.3-
    <= 1.2.3-
    > 1.2.3-
    < 1.2.3-
    EOE

    : invalid
    :
    {
      $* <'>='                  2>'no version'                                          == 1 : no-version
      $* <'==1.2.3-'            2>"invalid version: 'a' or 'b' expected in pre-release" == 1 : eq-earliest
      $* <'==0'                 2>"invalid version: '.' expected after major version"   == 1 : eq-stub
      $* <'>= 1.2.3-a.1.1.ads@' 2>'invalid version: junk after version'                 == 1 : junk
    }
  }

  : shortcut
  :
  {
    : valid
    :
    $* <<EOI >>EOO
    ~1.2.3
    ^1.2.3
    ^0.2.3
    ~1.2.3-
    ^1.2.3-
    ^0.2.3-
    ~1.2.3-a.1
    ^1.2.3-a.1
    ^0.2.3-a.1
    ~1.2.3-a.1.123
    ^1.2.3-a.1.123
    ^0.2.3-a.1.123
    ~ 1.2.3
    EOI
    ~1.2.3
    ^1.2.3
    ^0.2.3
    ~1.2.3-
    ^1.2.3-
    ^0.2.3-
    ~1.2.3-a.1
    ^1.2.3-a.1
    ^0.2.3-a.1
    ~1.2.3-a.1.123
    ^1.2.3-a.1.123
    ^0.2.3-a.1.123
    ~1.2.3
    EOO

    : invalid
    :
    {
      $* <'-1.2.3'     2>'invalid constraint'                                == 1 : bad-char
      $* <'~'          2>'no version'                                        == 1 : no-version
      $* <'~1.2'       2>"invalid version: '.' expected after minor version" == 1 : bad-ver
      $* <'~1.99999.0' 2>"invalid version: invalid minor version"            == 1 : bad-min-tilde
      $* <'^0.99999.0' 2>"invalid version: invalid minor version"            == 1 : bad-min-caret
      $* <'^99999.0.0' 2>"invalid version: invalid major version"            == 1 : bad-maj-caret
    }
  }
}

: constraints-dependent
:
{
  test.options += -cr

  : range
  :
  {
    : valid
    :
    $* '1.2.3+1' <<EOI >>EOE
    [1.2.2 $]
    (1.2.2 $)
    [$ 1.2.4]
    ($ 1.2.4]
    EOI
    [1.2.2 1.2.3]
    (1.2.2 1.2.3)
    [1.2.3 1.2.4]
    (1.2.3 1.2.4]
    EOE
  }

  : comparison
  :
  {
    : valid
    :
    $* '1.2.3+1' <<EOI >>EOE
    == $
    >= $
    <= $
    >  $
    <  $
    >= $
    <= $
    >  $
    <  $
    EOI
    == 1.2.3
    >= 1.2.3
    <= 1.2.3
    > 1.2.3
    < 1.2.3
    >= 1.2.3
    <= 1.2.3
    > 1.2.3
    < 1.2.3
    EOE

    : invalid
    :
    {
      $* ''            <'== $' 2>'dependent version is empty'                            == 1 : empty-version
      $* '1.2.3-'      <'== $' 2>'dependent version is earliest'                         == 1 : earliest-version
      $* '1.2.3-a.0.z' <'== $' 2>'invalid version: dependent version is latest snapshot' == 1 : latest-version
      $* '0+1'         <'== $' 2>'invalid version: dependent version is stub'            == 1 : stub-version
    }
  }

  : shortcut
  :
  {
    : final
    :
    {
      $* '1.2.3+1' <<EOI >>EOE
        ~$
        ^$
        EOI
        ~1.2.0
        ^1.0.0
        EOE
    }

    : pre-release
    :
    {
      : tilda
      :
      {
        $* '1.2.0-b.2' <'~$' >'~1.2.0-a.1' : no-final
        $* '1.2.1-a.1' <'~$' >'~1.2.0'     : final-patch
      }

      : carrot
      :
      {
        $* '1.0.0-b.2' <'^$' >'^1.0.0-a.1' : no-final
        $* '1.0.1-a.1' <'^$' >'^1.0.0'     : final-patch
        $* '1.1.0-b.2' <'^$' >'^1.0.0'     : final-minor
      }
    }

    : snapshot
    :
    {
      : tilda
      :
      {
        $* '1.2.1-a.2.345' <'~$' >'~1.2.0'                  : patch
        $* '1.2.0-a.0.345' <'~$' >'[1.2.0-a.0.1 1.2.0-a.1)' : minor
        $* '1.0.0-a.0.345' <'~$' >'[1.0.0-a.0.1 1.0.0-a.1)' : major
      }

      : carrot
      :
      {
        $* '1.2.1-a.2.345' <'^$' >'^1.0.0'                  : patch
        $* '1.2.0-a.0.345' <'^$' >'[1.2.0-a.0.1 1.2.0-a.1)' : minor
        $* '1.0.0-a.0.345' <'^$' >'[1.0.0-a.0.1 1.0.0-a.1)' : major
      }
    }
  }

  : invalid
  :
  {
    $* <'[1.2.2 $]' 2>'invalid max version: invalid major version' != 0 : max
    $* <'[$ 1.2.2]' 2>'invalid min version: invalid major version' != 0 : min
    $* <'== $'      2>'invalid version: invalid major version'     != 0 : eq
    $* <'~$'        2>'invalid version: invalid major version'     != 0 : shortcut
  }
}

: satisfaction
:
{
  test.options += -sf

  : comparison
  :
  : Constraints have a single endpoint being present.
  :
  {
    : eq
    :
    {
      $* '1.2.3' '== 1.2.3' >y : eq
      $* '1.2.3' '<= 1.2.3' >y : le
      $* '1.2.3' '<  1.2.3' >n : lt
      $* '1.2.3' '>= 1.2.3' >y : ge
      $* '1.2.3' '>  1.2.3' >n : gt
    }

    : smaller-greater
    :
    {
      $* '1.2.3' '== 1.2.4' >n : eq
      $* '1.2.3' '<= 1.2.4' >y : le
      $* '1.2.3' '<  1.2.4' >y : lt
      $* '1.2.3' '>= 1.2.4' >n : ge
      $* '1.2.3' '>  1.2.4' >n : gt
    }

    : greater-smaller
    :
    {
      $* '1.2.4' '<= 1.2.3' >n : le
      $* '1.2.4' '<  1.2.3' >n : lt
      $* '1.2.4' '>= 1.2.3' >y : ge
      $* '1.2.4' '>  1.2.3' >y : gt
    }
  }

  : range
  :
  : Constraints have both endpoints being present.
  :
  {
    : left-out
    :
    {
      $* '1.2.3' '[1.2.4 1.2.5]' >n : closed
      $* '1.2.3' '(1.2.4 1.2.5]' >n : open
    }

    : left-endpoint
    :
    {
      $* '1.2.3' '[1.2.3 1.2.4]'  >y : closed
      $* '1.2.3' '(1.2.3 1.2.4]'  >n : open
    }

    : in
    :
    {
      $* '1.2.3' '[1.2.2 1.2.4]' >y : closed
      $* '1.2.3' '(1.2.2 1.2.4)' >y : open
    }

    : right-endpoint
    :
    {
      $* '1.2.3' '[1.2.2 1.2.3]'  >y : closed
      $* '1.2.3' '[1.2.2 1.2.3)'  >n : open
    }

    : right-out
    :
    {
      $* '1.2.3' '[1.2.1 1.2.2]' >n : closed
      $* '1.2.3' '[1.2.1 1.2.2)' >n : open
    }
  }

  : shortcut
  :
  {
    : tilde
    :
    {
      $* '1.2.3-b.499' '~1.2.3' >n : out-left
      $* '1.2.3'       '~1.2.3' >y : in-left
      $* '1.2.4'       '~1.2.3' >y : in
      $* '1.2.99999'   '~1.2.3' >y : in-right
      $* '1.3.0-'      '~1.2.3' >n : out-right
    }

    : caret
    :
    {
      $* '1.2.3-b.499'   '^1.2.3' >n : out-left
      $* '1.2.3'         '^1.2.3' >y : in-left
      $* '1.3.0'         '^1.2.3' >y : in
      $* '1.99999.99999' '^1.2.3' >y : in-right
      $* '2.0.0-'        '^1.2.3' >n : out-right
    }

    : caret-zero-major
    :
    {
      $* '0.2.3-b.499' '^0.2.3' >n : out-left
      $* '0.2.3'       '^0.2.3' >y : in-left
      $* '0.2.4'       '^0.2.3' >y : in
      $* '0.2.99999'   '^0.2.3' >y : in-right
      $* '0.3.0-'      '^0.2.3' >n : out-right
    }
  }
}