# control

function: control.scope.block.fn.fail {
    a: [1..2] -> foreach i { err: $i | out: bar }
}

test: unit function control.scope.block.fn.fail {
    "StderrMatch": "1\n2\n",
    "StdoutMatch": "bar\nbar\n"
}

function: control.runmode.fail {
    out "foo"
    runmode try function
    out "bar"
}

test: unit function control.runmode.fail {
    "StdoutMatch": "foo\nbar\n",
    "StderrRegex": "should only be used as the first statement in a block",
    "ExitNum": 0
}

# try

function: try.scope.block.pass {
    try { out: "foo"; out "bar" }
}

test: unit function try.scope.block.pass {
    "StdoutMatch": "foo\nbar\n"
}

function: try.scope.block.fail {
    try { err: "foo"; out "bar" }
}

test: unit function try.scope.block.fail {
    "StderrRegex": "foo",
    "ExitNum":     1
}

function: try.scope.block.fn {
    function: fn.try.foobar { err: "foo"; out "bar" }
    try { fn.try.foobar }
}

test: unit function try.scope.block.fn {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n"
}

function: try.scope.block.pvt {
    private: pvt.try.foobar { err: "foo"; out "bar" }
    try { pvt.try.foobar }
}

test: unit function try.scope.block.pvt {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n"
}

function: try.scope.block.foreach.fail {
    try {
        a: [1..2] -> foreach i { err: $i; out: bar }
    }
}

test: unit function try.scope.block.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

## function

function: try.scope.fn.fn {
    runmode try function
    function: fn.try.foobar { err: "foo"; out "bar" }
    fn.try.foobar
}

test: unit function try.scope.fn.fn {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n"
}

function: try.scope.fn.foreach.fail {
    runmode try function
    a: [1..2] -> foreach i { err: $i; out: bar }
}

test: unit function try.scope.fn.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

function try.scope.fn.read.fail {
    # Short conversation, exit on error
    
    runmode: try function

    bg {
        read: q "? "
        out: "!"
    }
    sleep: 2
}

test: unit function try.scope.fn.read.fail {
    "StderrRegex": "cannot read from",
    "ExitNum": 1
}

## module

function: try.scope.fn.mod {
    runmode try module
    function: fn.try.foobar { err: "foo"; out "bar" }
    fn.try.foobar
}

test: unit function try.scope.fn.mod {
    "StderrMatch": "foo\n",
    "ExitNum": 1
}

function: try.scope.mod.foreach.fail {
    runmode try module
    a: [1..2] -> foreach i { err: $i; out: bar }
}

test: unit function try.scope.mod.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

function try.scope.mod.read.fail {
    # Short conversation, exit on error
    
    runmode: try module

    bg {
        read: q "? "
        out: "!"
    }
    sleep: 2
}

test: unit function try.scope.mod.read.fail {
    "StderrRegex": "cannot read from",
    "ExitNum": 1
}

# trypipe

function: trypipe.scope.block.pass {
    trypipe { out: "foo" | out "bar" }
}

test: unit function trypipe.scope.block.pass {
    "StdoutMatch": "bar\n"
}

function: trypipe.scope.block.fail {
    trypipe { err: "foo" | out "bar" }
}

test: unit function trypipe.scope.block.fail {
    "StderrRegex": "foo",
    "ExitNum":     1
}

function: trypipe.scope.block.fn {
    function: fn.trypipe.foobar { err: "foo" | out "bar" }
    trypipe { fn.trypipe.foobar }
}

test: unit function trypipe.scope.block.fn {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n",
    "ExitNum": 0
}

function: trypipe.scope.block.pvt {
    private:  pvt.trypipe.foobar { err: "foo" | out "bar" }
    trypipe { pvt.trypipe.foobar }
}

test: unit function trypipe.scope.block.pvt {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n"
}

function: trypipe.scope.block.foreach.fail {
    trypipe {
        a: [1..2] -> foreach i { err: $i | out: bar }
    }
}

test: unit function trypipe.scope.block.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

### function

function: trypipe.scope.fn.fn {
    runmode trypipe function
    function: fn.try.foobar { err: "foo" | out "bar" }
    fn.trypipe.foobar
}

test: unit function trypipe.scope.fn.fn {
    "StderrMatch": "foo\n",
    "StdoutMatch": "bar\n"
}

function: trypipe.scope.fn.foreach.fail {
    runmode trypipe function
    a: [1..2] -> foreach i { err: $i | out: bar }
}

test: unit function trypipe.scope.fn.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

function trypipe.scope.fn.read.fail {
    # Short conversation, exit on error
    
    runmode: trypipe function

    bg {
        read: q "? "
        out: "!"
    }
    sleep: 2
}

test: unit function trypipe.scope.fn.read.fail {
    "StderrRegex": "cannot read from",
    "ExitNum": 1
}

## module

function: trypipe.scope.fn.mod {
    runmode trypipe module
    function: trypipe.try.foobar { err: "foo" | out "bar" }
    fn.trypipe.foobar
}

test: unit function trypipe.scope.fn.mod {
    "StderrMatch": "foo\n",
    "ExitNum": 1
}

function: trypipe.scope.mod.foreach.fail {
    runmode trypipe module
    a: [1..2] -> foreach i { err: $i | out: bar }
}

test: unit function trypipe.scope.mod.foreach.fail {
    "StderrMatch": "1\n2\n",
    "ExitNum": 1
}

function trypipe.scope.mod.read.fail {
    # Short conversation, exit on error
    
    runmode: trypipe module

    bg {
        read: q "? "
        out: "!"
    }
    sleep: 2
}

test: unit function trypipe.scope.mod.read.fail {
    "StderrRegex": "cannot read from",
    "ExitNum": 1
}