【问题标题】:How to verify output from bash script controlling ghci (Haskell)如何验证控制 ghci 的 bash 脚本的输出(Haskell)
【发布时间】:2022-01-23 01:48:06
【问题描述】:

我正在尝试创建一个用于评估的测试脚本。该脚本需要打开 Haskell GHCI 并向 ghci 发送各种命令并检查输出是否正确。它需要一直持续到所有命令的结尾,并给出一个最终的分数,即不要只是输出不正确就停止运行。目前,我有这个。

#!/bin/env expect


spawn ghci
expect ".*> "
send ":l main.hs\n"
expect "*Main>"

send "transform [(5,1),(6,1),(8,15),(9,1)]\n"

expect "*Main>"

它会打开 ghci 并加载正确的 haskell 文件 (main.hs)。然后它使用该列表参数运行转换函数。我怎样才能得到这个来验证输出是否等于我想要的并相应地给出分数。例如:伪

#!/bin/env expect


spawn ghci
expect ".*> "
send ":l main.hs\n"
expect "*Main>"

send "transform [(5,1),(6,1),(8,15),(9,1)]\n"
if OUTPUT = [(5,5),(6,5),(8,5),(9,5)]
then POINTS+= 5

send "translate [(5,1),(6,1),(8,15),(9,1)]\n"
if OUTPUT = [(5,10),(6,10),(8,10),(9,10)]
then POINTS+= 5

expect "*Main>"
send ":quit"

OUTPUT SCORE

所有命令都在 Haskell 中完成,但从 shell 脚本运行。有人可以帮忙吗?

【问题讨论】:

  • bash 和expect 交互不是很方便。你可以使用我的sexpect。
  • 有什么特殊原因需要这样做吗?您是否了解 Haskell 中的测试工具组件,例如 HUnit 和 QuickCheck?

标签: shell haskell expect


【解决方案1】:

这是一种(未经测试的)方法:

#!/bin/env expect

spawn ghci
expect "*> "
send ":l main.hs\n"
expect "*Main>"
set POINTS 0

send "transform [(5,1),(6,1),(8,15),(9,1)]\n"
expect {
    -ex {[(5,5),(6,5),(8,5),(9,5)]} {
        incr POINTS 5
        exp_continue
    }
    *Main>
}

send "translate [(5,1),(6,1),(8,15),(9,1)]\n"
expect {
    -ex {[(5,10),(6,10),(8,10),(9,10)]} {
        incr POINTS 5
        exp_continue
    }
    *Main>
}

send ":quit"

puts $POINTS

有关详细文档,请参阅 https://www.tcl.tk/man/expect5.31/expect.1.html 和 https://www.tcl-lang.org/man/tcl/TclCmd/contents.htm。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多