# t450 --- co -p -s
#
# Copyright (C) 2010-2022 Thien-Thi Nguyen
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. $srcdir/common
. $srcdir/common-kn
split_std_out_err no
$hey echo '                                    -*- org -*-'
$hey type co

##
# Do "co -p -sSTATE" for various values of STATE,
# on various branches.  Check output and diagnostics.
##

prep_b_comparison

coerr=$wd/co.err
doubt=$wd/diff.out

tip1=1.9       ; stip1=QQQ
tip111=1.1.1.7 ; stip111=Exp
tip161=1.6.1.2 ; stip161=Exp

xfail ()
{
    # $1 -- state
    # $2 -- source (valid branch or revision number)
    # $3+ -- expected diagnostic
    state=$1
    source=$2
    shift ; shift
    badmsg="$@"
    $hey echo "* xfail: $state $source"

    cmd="co -p$source -s$state"
    $cmd $v 1> $actual 2> $coerr \
        && problem "unexpected exit value (success): $cmd"
    noiselessness_rules $actual "$cmd"
    grep -F "$badmsg" $coerr \
        || problem "unexpected diagnostic output: $cmd"
}

xfail_brnum ()
{
    # $1 -- state
    # $2 -- branch number
    state=$1
    brnum=$2
    xfail $state $brnum "No revision on branch $brnum has state $state."
}

xfail_revno ()
{
    # $1 -- state
    # $2 -- revision number
    state=$1
    revno=$2
    tip_varname=`echo tip$revno | sed 's/[.]//g'`
    eval tip='$'$tip_varname
    eval tipstate='$s'$tip_varname
    xfail $state $revno "Revision $tip has state $tipstate."
}

xpass ()
{
    # $1 -- STEM in fake/b.d/STEM.cou
    # $2 -- state
    # $3 -- source (valid branch or revision number)
    expected=`bundled_commav b.d/$1.cou`
    state=$2
    source=$3
    cmd="co -p$source -s$state"
    $hey echo "* xpass: $state $source"

    must '$cmd $v 1> $actual 2> $coerr'
    normalize_b_and_diff "$cmd"
}

##
# * First, try a state that is not in the RCS file.
##

xfail_brnum bliss 1
xfail_revno bliss 1.
xfail_brnum bliss 1.1.1
xfail_revno bliss 1.1.1.
xfail_brnum bliss 1.6.1
xfail_revno bliss 1.6.1.

##
# * Next, try a state that is only on one branch.
##

xpass 19    QQQ 1
xpass 19    QQQ 1.
xpass 19    QQQ 1.9
xfail_brnum QQQ 1.1.1
xfail_revno QQQ 1.1.1.
xfail_brnum QQQ 1.6.1
xfail_revno QQQ 1.6.1.

##
# * Last, try a state that is everywhere.
##

xpass 18    Exp 1
xfail_revno Exp 1.
xpass 1117  Exp 1.1.1
xpass 1117  Exp 1.1.1.
xpass 1612  Exp 1.6.1
xpass 1612  Exp 1.6.1.

exit 0

# t450 ends here
