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

AT_SETUP([SORT VARIABLES])
# This reverses the order of its input lines.
# From the GNU sed manual.
tac () {
    sed -n '1! G
$ p
h'
}

test_sort () {
    cat > sort-variables.sps <<EOF
DATA LIST FREE/$1.
$2
SORT VARIABLES $3.
DISPLAY NAMES.
SORT VARIABLES $3(D).
DISPLAY NAMES.
EOF
    AT_CHECK_UNQUOTED([pspp -O format=csv sort-variables.sps], [0],
[Table: Variables
Name
`for var in $4; do echo $var; done`

Table: Variables
Name
`for var in $4; do echo $var; done | tac`
])
}

test_sort 'x100 c b x99 a y400 y5' '' NAME 'a b c x99 x100 y5 y400'
test_sort 'a c e B D F' '' NAME 'a B c D e F'
test_sort 'c(a10) a(a5) b' '' TYPE 'b a c'
test_sort 'a (datetime) b (f) c (a5) d (a2) e (a1)' '' FORMAT 'e d c b a'
test_sort 'a b c' \
    'VARIABLE LABEL a "hi there".' \
    LABEL 'b c a'
test_sort 'a b c' \
    'VALUE LABELS a 123 "xyzzy".' \
    VALUES 'b c a'
test_sort 'a b c' \
    'MISSING VALUES a (123).' \
    MISSING 'b c a'
test_sort 'a b c' \
    'VARIABLE LEVEL a (SCALE) b (ORDINAL) c (NOMINAL).' \
    MEASURE 'c b a'
test_sort 'b n i s t p' \
    'VARIABLE ROLE /INPUT i /TARGET t /BOTH b /NONE n /PARTITION p /SPLIT s.' \
    ROLE 'i t b n p s'
test_sort 'c10 c5 c15 c9' \
    'VARIABLE WIDTH c10(10) c5(5) c15(15) c9(9).' \
    COLUMNS 'c5 c9 c10 c15'
test_sort 'c l r' \
    'VARIABLE ALIGNMENT c (CENTER) l (LEFT) r (RIGHT).' \
    ALIGNMENT 'l r c'
test_sort 'az ax ay ab' \
    'VARIABLE ATTRIBUTE VARIABLES=az ATTRIBUTE=key("z").
     VARIABLE ATTRIBUTE VARIABLES=ax ATTRIBUTE=key("x").
     VARIABLE ATTRIBUTE VARIABLES=ay ATTRIBUTE=key("y").
     VARIABLE ATTRIBUTE VARIABLES=ab ATTRIBUTE=key("b").' \
    'ATTRIBUTE key' 'ab ax ay az'
AT_CLEANUP

AT_SETUP([SORT VARIABLES syntax errors])
AT_DATA([sort-variables.sps], [dnl
DATA LIST LIST NOTABLE /x y z.
SORT VARIABLES BY **.
SORT VARIABLES BY ATTRIBUTE **.
SORT VARIABLES BY NAME (**).
SORT VARIABLES BY NAME (A **).
])
AT_CHECK([pspp -O format=csv sort-variables.sps], [1], [dnl
"sort-variables.sps:2.19-2.20: error: SORT VARIABLES: Syntax error expecting one of the following: NAME, TYPE, FORMAT, LABEL, VALUES, MISSING, MEASURE, ROLE, COLUMNS, ALIGNMENT, ATTRIBUTE.
    2 | SORT VARIABLES BY **.
      |                   ^~"

"sort-variables.sps:3.29-3.30: error: SORT VARIABLES: Syntax error expecting identifier.
    3 | SORT VARIABLES BY ATTRIBUTE **.
      |                             ^~"

"sort-variables.sps:4.25-4.26: error: SORT VARIABLES: Syntax error expecting A or D.
    4 | SORT VARIABLES BY NAME (**).
      |                         ^~"

"sort-variables.sps:5.27-5.28: error: SORT VARIABLES: Syntax error expecting `)'.
    5 | SORT VARIABLES BY NAME (A **).
      |                           ^~"
])
AT_CLEANUP
