开发者

Implementing my own higher level test scripting language

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
//sen开发者_Go百科d 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


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


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜