buildtests.py (2253B)
1 #!/usr/bin/python3 2 3 import os 4 import urllib.parse 5 import hashlib 6 7 doctmpl = """\ 8 <!doctype html> 9 <title>WebVTT cue data parser test %s</title> 10 <link rel="help" href="https://w3c.github.io/webvtt/#cue-text-parsing-rules"> 11 <style>video { display:none }</style> 12 <script src=/resources/testharness.js></script> 13 <script src=/resources/testharnessreport.js></script> 14 <script src=/html/syntax/parsing/template.js></script> 15 <script src=/html/syntax/parsing/common.js></script> 16 <script src=../common.js></script> 17 <div id=log></div> 18 <script> 19 runTests([ 20 %s 21 ]); 22 </script> 23 """ 24 25 testobj = "{name:'%s', input:'%s', expected:'%s'}" 26 27 def appendtest(tests, input, expected): 28 tests.append(testobj % (hashlib.sha1(input.encode('UTF-8')).hexdigest(), urllib.parse.quote(input[:-1]), urllib.parse.quote(expected[:-1]))) 29 30 files = os.listdir('dat/') 31 for file in files: 32 if os.path.isdir('dat/'+file) or file[0] == ".": 33 continue 34 35 tests = [] 36 input = "" 37 expected = "" 38 state = "" 39 with open('dat/'+file, "r") as f: 40 while True: 41 line = f.readline() 42 if not line: 43 if state != "": 44 appendtest(tests, input, expected) 45 input = "" 46 expected = "" 47 state = "" 48 break 49 50 if line[0] == "#": 51 state = line 52 if line == "#document-fragment\n": 53 expected += bytes(line, 'UTF-8').decode('unicode-escape') 54 elif state == "#data\n": 55 input += bytes(line, 'UTF-8').decode('unicode-escape') 56 elif state == "#errors\n": 57 pass 58 elif state == "#document-fragment\n": 59 if line == "\n": 60 appendtest(tests, input, expected) 61 input = "" 62 expected = "" 63 state = "" 64 else: 65 expected += bytes(line, 'UTF-8').decode('unicode-escape') 66 else: 67 raise Exception("failed to parse file %s:%s (state: %s)" % (file, line, state)) 68 69 name = os.path.splitext(file)[0] 70 with open('tests/'+name+".html", "w") as out: 71 out.write(doctmpl % (name, ",\n".join(tests)))