实现我自己的更高级别的测试脚本语言
我继承了一堆测试脚本,它们看起来像这样:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于如此简单的测试,您不需要任何特殊的解析器。只需使用Python。一个简单的解决方案可以这样开始:
运行
script_one()
将生成一个包含以下内容的文件:You don't need any special parsers for such simple tests. Just use Python. A simple solution could start like this:
Running
script_one()
will generate a file with these contents:我建议您将其实现为 Python 之上的内部 DSL。实现:
它是真正的 Python 代码,可以自动生成您的专有脚本。 Python 的可读性和编写起来都很舒适,通过将其作为内部 DSL,您可以使用 Python 的所有功能。例如,您可以执行以下操作:
将
somefunc
用作任意计算。要在外部 DSL(即您自己的脚本语言)中实现此功能,您必须实现循环和更多构造。 Python 免费为您提供此功能,这只是这些功能的一个小示例。如果这听起来很有趣,请搜索一下“外部 DSL”与“内部 DSL”,以了解我在说什么。这个世界上有足够多的奇怪的、专有的、执行不力的“脚本语言”。再生一个孩子通常是一个坏主意。
I recommend you to implement it as an internal DSL on top of Python. Make it:
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:
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.
pyParsing 和 PLY 是用于在 Python 中编写解析器的两个更流行的库,您可以创建自己的语法等。
或者,您可以通过编写生成所需输出的简单函数来使用纯 Python 来完成此操作:
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: