# Process with autom4te to create an -*- Autotest -*- test suite.

AT_INIT([expr-test])
# AT_COPYRIGHT([])

# AT_TESTED([expr-test])

AT_ARG_OPTION_ARG([baselines],
    [--baselines=yes|no   Build the baseline file for parser test 'arg'],
    [echo "baselines set to $at_arg_baselines";
     baselines=$at_arg_baselines],[baselines=])

# Usage: _EXPR_TEST(<flags>, <dds>, <constraint>, <baseline file>, <xfail?>)

m4_define([_EXPR_TEST], [
    # AT_BANNER([Test $1 $2 $3])
    AT_SETUP([expr-test $1 $2 -k $3 ($5)])
    AT_KEYWORDS([expr])

    # Added || true because expr-test returns 0 (failure) for some of
    # these tests which is correct for the test - we expect some of
    # the input to trigger an error response from the parser, et
    # cetera. That's also why we capture both stdout and stderr -
    # successful runs are sent to stdout while the errors are sent to
    # stderr.

    AS_IF([test -n "$baselines" -a x$baselines = xyes],
        [
        AT_CHECK([$abs_builddir/expr-test $1 $2 -k $3 -f "dummy" || true], [0], [stdout], [stderr])
        AT_CHECK([mv stdout $4.tmp])
        ],
        [
        AT_CHECK([$abs_builddir/expr-test $1 $2 -k $3 -f "dummy" || true], [0], [stdout], [stderr])
        AT_CHECK([diff -b -B $4 stdout || diff -b -B $4 stderr], [0], [ignore])
        AT_XFAIL_IF([test "$5" = "xfail"])
        ])
    
    AT_CLEANUP
])

m4_define([EXPR_RESPONSE_P], [
    # AT_BANNER([EXPR response for $1, $2, $4.])
    # Test the serialize/deserialize methods
    _EXPR_TEST([-w], [$abs_srcdir/expr-testsuite/$1], [$2], [$abs_srcdir/expr-testsuite/$3.base], $4)
    # Test the intern_data method (which combines serialize and deserialze)
    _EXPR_TEST([-W], [$abs_srcdir/expr-testsuite/$1], [$2], [$abs_srcdir/expr-testsuite/$3.base], $4)
])

# The -b option to expr-test uses the 'series values' feature of the Test
# classes.
m4_define([EXPR_RESPONSE_B], [
    # AT_BANNER([EXPR response for $1, $2, $4.])
    _EXPR_TEST([-b -w], [$abs_srcdir/expr-testsuite/$1], [$2], [$abs_srcdir/expr-testsuite/$3.base], $4)
    _EXPR_TEST([-b -W], [$abs_srcdir/expr-testsuite/$1], [$2], [$abs_srcdir/expr-testsuite/$3.base], $4)
])

# Tests that capture/evaluate the error text from the parser. jhrg 4/15/20
#
# Usage: EXPR_RESPONSE_E(DDS, CE, BASELINE, [pass/xfail])
#
m4_define([EXPR_RESPONSE_E], [dnl
    AT_SETUP([Error: $1 $2 ($4)])
    AT_KEYWORDS([expr])

    AS_IF([test -z "$at_verbose"], [echo "COMMAND: expr-test -e -p expr-testsuite/$1 -k $2"])

    dds=$abs_srcdir/expr-testsuite/$1
    ce=$2

    AS_IF([test -n "$baselines" -a x$baselines = xyes],
        [
        AT_CHECK([$abs_builddir/expr-test -e -p $dds -k $ce], [0], [stdout], [stderr])
        AT_CHECK([mv stderr $abs_srcdir/expr-testsuite/$3.tmp])
        ],
        [
        AT_CHECK([$abs_builddir/expr-test -e -p $dds -k $ce], [0], [stdout], [stderr])
        AT_CHECK([diff -b -B $abs_srcdir/expr-testsuite/$3 stderr], [0])
        AT_XFAIL_IF([test z$4 = zxfail])
        ])

    AT_CLEANUP
])

EXPR_RESPONSE_P([test.1], [i], [test.1], [pass])
EXPR_RESPONSE_P([test.1], [i,j], [test.1a], [pass])
EXPR_RESPONSE_P([test.1], ['i,j&i=j'], [test.1b], [pass])
EXPR_RESPONSE_P([test.1], ['i&i=j'], [test.1d], [pass])
EXPR_RESPONSE_P([test.2], [s1], [test.2], [pass])
EXPR_RESPONSE_P([test.2], [s2], [test.2a], [pass])
EXPR_RESPONSE_P([test.2], [s2,s3], [test.2b], [pass])
EXPR_RESPONSE_P([test.2], [s2[[2:2:4]],s3.o], [test.2c], [pass])

EXPR_RESPONSE_P([test.2], [s2[[2:2:4]].m], [test.2d], [pass])
EXPR_RESPONSE_P([test.2], [s2[[2:2:4]].m,s2[[2:2:4]].l], [test.2e], [pass])

EXPR_RESPONSE_P([test.2a], [s2[[2:4]].m[[0:4]],s2[[2:4]].l[[0:5]]], [test.2f], [pass])
EXPR_RESPONSE_P([test.3], [i[[1:10]]], [test.3], [pass])
EXPR_RESPONSE_P([test.4], ['s&s=~"^Silly.*"'], [test.4], [pass])

dnl In changing the TestStr class so that it writes a constant value (Silly ...: 1)
dnl I had to hack this test to use the -b option to expr-test. EXPR_RESPONSE_P uses
dnl only the -w/-W options, so I dropped using that macro kluge and went right for the
dnl actual test macro. 12/2/13/ jhrg
dnl
dnl EXPR_RESPONSE_P([test.e], ['names.s&names.s=~".*: 3"'], [test.ea], [pass])
    
_EXPR_TEST([-b -w], [$abs_srcdir/expr-testsuite/test.e], ['names.s&names.s=~".*: 3"'], 
    [$abs_srcdir/expr-testsuite/test.ea.base], [pass])
    
_EXPR_TEST([-b -W], [$abs_srcdir/expr-testsuite/test.e], ['names.s&names.s=~".*: 3"'], 
    [$abs_srcdir/expr-testsuite/test.ea.base], [pass])

dnl since 'Silly ...:5' will never be produced, this tests what happens when the sequence
dnl does not contain the value. Note that this test does not have to be rewritten because
dnl of the change to the TestStr code. 12/2/13 jhrg
EXPR_RESPONSE_P([test.e], ['names.s&names.s=~".*: 5"'], [test.eb], [pass])

EXPR_RESPONSE_P([test.5], [g[[0:2:4]][[0]][[0]]], [test.5], [pass])
EXPR_RESPONSE_P([test.5], [g[[0:2:4]][[0:2:4]][[0:2:4]]], [test.5a], [pass])
EXPR_RESPONSE_P([test.6], [i], [test.6], [pass])
EXPR_RESPONSE_P([test.6], [i[[1:2]][[2:4]]], [test.6a], [pass])
EXPR_RESPONSE_P([test.5], [g.val[[0:1]][[0:1]][[0:1]]], [test.5b], [pass])
EXPR_RESPONSE_P([test.5], [g.length], [test.5c], [pass])
EXPR_RESPONSE_P([test.5], [g.length,g.width], [test.5d], [pass])
EXPR_RESPONSE_P([test.2], [j,o], [test.2g], [pass])
EXPR_RESPONSE_P([test.8], ["data%23i[[0:2:9]][[0:2]]"], [test.8], [pass])
EXPR_RESPONSE_P([test.7], [x,y,f], [test.7], [pass])
EXPR_RESPONSE_P([test.8], ["x%23y,y"], [test.8a], [pass])
EXPR_RESPONSE_P([test.8], ["data%20name,y"], [test.8b], [pass])
EXPR_RESPONSE_P([test.9], ["Data-Set-2.fakeDim0[[0:3]],Data-Set-2.fakeDim1[[0:3]]"], [test.9], [pass])
EXPR_RESPONSE_P([test.5], [g[[1:4:9]]], [test.5e], [pass])
EXPR_RESPONSE_P([test.6], [i[[1:4:9]]], [test.6b], [pass])

# New tests for server functions that take arrays and include array projections
# Added 10/23/13 jhrg
EXPR_RESPONSE_P([test.6], [scale\(i,2\)], [test.6.func1], [pass])
EXPR_RESPONSE_P([test.6], [scale\(i[[2:4]][[3:6]],2\)], [test.6.func2], [pass])

EXPR_RESPONSE_P([test.5], [scale\(i[[3]],2\)], [test.5.func3], [pass])
# This doesn't work yet 10/23/13 jhrg 
EXPR_RESPONSE_P([test.5], [scale\(j,2\)], [test.5.func4], [pass])

EXPR_RESPONSE_P([test.a], ["" -b], [test.a], [pass])
EXPR_RESPONSE_P([test.a], ["&i<2000" -b], [test.aa], [pass])
EXPR_RESPONSE_P([test.a], ["j&i>2000" -b], [test.ab], [pass])
EXPR_RESPONSE_P([test.a], ["i,j&i<0" -b], [test.ac], [pass])
EXPR_RESPONSE_P([test.b], ["" -b], [test.b], [pass])
EXPR_RESPONSE_P([test.b], ["i,f" -b], [test.ba], [pass])
EXPR_RESPONSE_P([test.b], ["i,f&i<2000" -b], [test.bb], [pass])
EXPR_RESPONSE_P([test.b], ["i,f&f<0" -b], [test.bc], [pass])
EXPR_RESPONSE_P([test.b], ["i,j&i<2000" -b], [test.bd], [pass])
EXPR_RESPONSE_P([test.b], ["&i<0" -b], [test.be], [pass])
EXPR_RESPONSE_P([test.d], ["" -b], [test.d], [pass])
EXPR_RESPONSE_P([test.d], ["i,f,a" -b], [test.da], [pass])
EXPR_RESPONSE_P([test.d], ["i,f,a&i<2000" -b], [test.db], [pass])
EXPR_RESPONSE_P([test.d], ["i,f,a&f<0" -b], [test.dc], [pass])
EXPR_RESPONSE_P([test.d], ["i,f,a&a<10" -b], [test.dd], [pass])
EXPR_RESPONSE_P([test.d], ["i,f&i<2000" -b], [test.de], [pass])
EXPR_RESPONSE_P([test.d], ["i&i<2000" -b], [test.df], [pass])
EXPR_RESPONSE_P([test.d], ["i,f,a&i<0" -b], [test.dg], [pass])

EXPR_RESPONSE_B([test.61], [i], [data.61a], [pass])
EXPR_RESPONSE_B([test.61], [ i[[0:2]][[0:2]] ], [data.61b], [pass])
EXPR_RESPONSE_B([test.61], [ i[[1:2]][[0:2]] ], [data.61c], [pass])
EXPR_RESPONSE_B([test.61], [ i[[1:2]][[1:2]] ], [data.61d], [pass])
EXPR_RESPONSE_B([test.c0], [SST], [data.z1], [pass])

EXPR_RESPONSE_B([test.f], [""], [test.fa], [pass])
EXPR_RESPONSE_B([test.f], ["&i<3000"], [test.fb], [pass])

# tests for zero-length arrays. jhrg 1/28/16

EXPR_RESPONSE_B([test.21.dds], [""], [data.21], [pass])
EXPR_RESPONSE_B([test.22.dds], [""], [data.22], [pass])
EXPR_RESPONSE_B([test.23.dds], [""], [data.23], [pass])
EXPR_RESPONSE_B([test.24.dds], [""], [data.24], [pass])

# Empty Structures. jhrg 1/29/16

EXPR_RESPONSE_B([test.25.dds], [""], [data.25], [pass])
EXPR_RESPONSE_B([test.26.dds], [""], [data.26], [pass])

# Test error responses. None of the parsers should allow any part of
# the malformed CE into the error messages. jhrg 4/15/20

EXPR_RESPONSE_E([test.1], [d1rox%253cscript%253ealert%25281%2529%253c%252fscript%253ed55je=1], [test.1.error], [pass])
EXPR_RESPONSE_E([test.2], [d1rox%253cscript%253ealert%25281%2529%253c%252fscript%253ed55je=1], [test.2.error], [pass])
EXPR_RESPONSE_E([test.3], [d1rox%253cscript%253ealert%25281%2529%253c%252fscript%253ed55je=1], [test.3.error], [pass])
EXPR_RESPONSE_E([test.5], [d1rox%253cscript%253ealert%25281%2529%253c%252fscript%253ed55je=1], [test.5.error], [pass])

