实现我自己的更高级别的测试脚本语言

发布于 2024-11-08 20:27:17 字数 590 浏览 0 评论 0原文

我继承了一堆测试脚本,它们看起来像这样:

// make connection on standard port (FD)
ot 11 02 00 0F FD
// wait for ACK
in 12 ackValue
// wait for connection confirmation
in 13 ackValue 09 88
//send 5 bytes of arbitary data
ot 21 ackValue 05 01 02 03 04 05

等等。

我想编写自己的更简单的测试脚本,它是上面的一层,可以被解析以自动生成上面的脚本,这样我的脚本看起来像这样:

MAKE_STANDARD_CONNECTION
SEND_DATA (01,02,03,04,05)

因此,当解析 MAKE_STANDARD_CONNECTION 时,它将生成前六行在之前的脚本中。如果标准端口从 0xFD 更改为其他端口,这将使我能够快速更新所有脚本...

我猜有可用的工具可以执行此操作,但它们是什么?我是一名程序员,因此如果有必要,我可以使用 python 作为我选择的语言来编写此代码。

谢谢你的帮助,

巴里

I've inherited a bunch of test scripts which look something like this:

// make connection on standard port (FD)
ot 11 02 00 0F FD
// wait for ACK
in 12 ackValue
// wait for connection confirmation
in 13 ackValue 09 88
//send 5 bytes of arbitary data
ot 21 ackValue 05 01 02 03 04 05

And so on.

I would like to write my own simpiler test scripts which are a layer above the above and which can be parsed to automatically produce the above script, such that my script looks something like this:

MAKE_STANDARD_CONNECTION
SEND_DATA (01,02,03,04,05)

So when MAKE_STANDARD_CONNECTION is parsed it will produce the first six lines in the former script. This will allow me to quickly update all scripts if standard port should change from 0xFD to something else...

I'm guessing that there are tools available to do this but what are they? I am a programmer so I can code this if neccessary with python being my language of choice.

Thanks for your help,

Barry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖兔^ω^ 2024-11-15 20:27:17

对于如此简单的测试,您不需要任何特殊的解析器。只需使用Python。一个简单的解决方案可以这样开始:

class TestGen(object):

    def __init__(self, filename):
        self._filename = filename

    def __enter__(self):
        self._file = open(self._filename, 'w')
        return self

    def __exit__(self, *args):
        self._file.close()

    def make_standard_connection(self):
        self._write('ot 11 02 00 0F FD')
        self._write('in 12 ackValue')
        self._write('in 13 ackValue 09 88')

    def send_data(self, *data):
        data_str = ' '.join(data)
        self._write('ot 21 ackValue 05 %s' % (data_str,))

    def _write(self, line):
        self._file.write('%s\n' % line)


def script_one():
    with TestGen('output1.txt') as g:
        g.make_standard_connection()
        g.send_data('01', '02', '03', '04', '05')

运行 script_one() 将生成一个包含以下内容的文件:

ot 11 02 00 0F FD
in 12 ackValue
in 13 ackValue 09 88
ot 21 ackValue 05 01 02 03 04 05

You don't need any special parsers for such simple tests. Just use Python. A simple solution could start like this:

class TestGen(object):

    def __init__(self, filename):
        self._filename = filename

    def __enter__(self):
        self._file = open(self._filename, 'w')
        return self

    def __exit__(self, *args):
        self._file.close()

    def make_standard_connection(self):
        self._write('ot 11 02 00 0F FD')
        self._write('in 12 ackValue')
        self._write('in 13 ackValue 09 88')

    def send_data(self, *data):
        data_str = ' '.join(data)
        self._write('ot 21 ackValue 05 %s' % (data_str,))

    def _write(self, line):
        self._file.write('%s\n' % line)


def script_one():
    with TestGen('output1.txt') as g:
        g.make_standard_connection()
        g.send_data('01', '02', '03', '04', '05')

Running script_one() will generate a file with these contents:

ot 11 02 00 0F FD
in 12 ackValue
in 13 ackValue 09 88
ot 21 ackValue 05 01 02 03 04 05
猫七 2024-11-15 20:27:17

我建议您将其实现为 Python 之上的内部 DSL。实现:

MAKE_STANDARD_CONNECTION()
SEND_DATA('01','02','03','04','05')

它是真正的 Python 代码,可以自动生成您的专有脚本。 Python 的可读性和编写起来都很舒适,通过将其作为内部 DSL,您可以使用 Python 的所有功能。例如,您可以执行以下操作:

values = [somefunc(i) for i in range(5)]
MAKE_STANDARD_CONNECTION()
SEND_DATA(*values)

somefunc 用作任意计算。要在外部 DSL(即您自己的脚本语言)中实现此功能,您必须实现循环和更多构造。 Python 免费为您提供此功能,这只是这些功能的一个小示例。

如果这听起来很有趣,请搜索一下“外部 DSL”与“内部 DSL”,以了解我在说什么。这个世界上有足够多的奇怪的、专有的、执行不力的“脚本语言”。再生一个孩子通常是一个坏主意。

I recommend you to implement it as an internal DSL on top of Python. Make it:

MAKE_STANDARD_CONNECTION()
SEND_DATA('01','02','03','04','05')

With it being real Python code that auto-generates your proprietary script. Python is very readable and comfortable to write, and by doing it as an internal DSL, you get all the power of Python at your disposal. For example, you can do:

values = [somefunc(i) for i in range(5)]
MAKE_STANDARD_CONNECTION()
SEND_DATA(*values)

With somefunc being an arbitrary computation. To implement this in an external DSL (i.e. a scripting language of your own) you'd have to implement loops and more constructs. Python gives you this for free, and this is just a small example of the capabilities.

If this sounds interesting, google a bit for "external DSL" versus "internal DSL" to get a feel of what I'm talking about. There are enough weird, proprietary, poorly implemented "scripting languages" in this world. Giving birth to another one is usually a bad idea.

北座城市 2024-11-15 20:27:17

pyParsingPLY 是用于在 Python 中编写解析器的两个更流行的库,您可以创建自己的语法等。

或者,您可以通过编写生成所需输出的简单函数来使用纯 Python 来完成此操作:

make_standard_connection()
send_data(1,2,3,4,5)

pyParsing and PLY are two of the more popular libraries for writing parsers in Python, you could create your own grammar, etc.

alternately, you could do it in pure python by writing simple functions that generate the output you want:

make_standard_connection()
send_data(1,2,3,4,5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文