Skip to content

Commit 8b23ee2

Browse files
committed
started adding frontend testing harness
1 parent ef65307 commit 8b23ee2

2 files changed

Lines changed: 129 additions & 6 deletions

File tree

‎v3/screenshot-renderer/render-opt-screenshots.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ var page = require('webpage').create();
1313
var system = require('system');
1414
var fs = require('fs');
1515

16-
var fn, server, option_str;
17-
1816
if (system.args.length < 2) {
1917
console.log('\nUsage:');
2018
console.log('phantomjs render-opt-screenshots.js <filename> [custom server name] [custom option string]');
2119
console.log('\nVisualizes execution of a Python file at pythontutor.com and renders');
2220
console.log('the state diagram at each step i as <filename>.step.$i.png');
2321
phantom.exit(1);
2422
}
25-
else if (system.args.length == 2) {
26-
fn = system.args[1];
23+
24+
var fn = system.args[1];
25+
var basename = fn.split(/[\\/]/).pop();
26+
27+
var server, option_str;
28+
if (system.args.length == 2) {
2729
// default options
2830
server = 'www.pythontutor.com';
2931
option_str = 'cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=2';
3032
}
3133
else {
32-
fn = system.args[1];
3334
server = system.args[2];
3435
option_str = system.args[3];
3536
}
@@ -71,7 +72,7 @@ page.open(url, function () {
7172
myVisualizer.curInstr = i;
7273
myVisualizer.updateOutput();
7374
}, i /* pass i in here */);
74-
var outfn = fn + '.step.' + (i+1) + '.png';
75+
var outfn = basename + '.step.' + (i+1) + '.png';
7576
page.render(outfn);
7677
if (DEBUG) {
7778
console.log('Rendered step ' + (i+1) + ' / ' + (maxInstr+1), '\t' + outfn);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'''
2+
Golden tests for OPT frontend JS rendering
3+
Created on 2014-04-21
4+
5+
Pre-reqs:
6+
phantomjs installed (tested on v1.9.2)
7+
OPT running locally on localhost:8080 using "python bottle_server.py"
8+
must run from the current directory
9+
10+
TODO:
11+
for fuzzy image diffing, consider:
12+
http://www.imagemagick.org/Usage/compare/
13+
14+
---
15+
16+
Forked from ../golden_test.py
17+
18+
A simple framework for regression testing based on golden files
19+
by Philip Guo
20+
21+
(sloppily) customized for the Online Python Tutor project
22+
'''
23+
24+
# test with different display options
25+
OPTIONS_DICT = {
26+
'regular': 'cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=2',
27+
'csc108h': 'cumulative=false&heapPrimitives=true&drawParentPointers=false&textReferences=true&py=2',
28+
'composingprograms': 'cumulative=true&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=3',
29+
}
30+
31+
import os, re, shutil, optparse, filecmp
32+
from subprocess import *
33+
34+
RED = '\033[91m'
35+
ENDC = '\033[0m' # end color
36+
37+
38+
def execute(input_filename, option_name):
39+
assert os.path.isfile(input_filename)
40+
(base, ext) = os.path.splitext(input_filename)
41+
assert ext == INPUT_FILE_EXTENSION
42+
43+
(stdout, stderr) = Popen(PROGRAM + [input_filename, 'localhost:8080', OPTIONS_DICT[option_name]],
44+
stdout=PIPE, stderr=PIPE).communicate()
45+
46+
47+
def clobber_golden_file(outfile, golden_file):
48+
print ' Clobber %s => %s' % (outfile, golden_file)
49+
shutil.copy(outfile, golden_file)
50+
51+
52+
def run_test(input_filename, clobber_golden=False):
53+
print 'Testing', input_filename
54+
bn = os.path.basename(input_filename)
55+
56+
# test all frontend display options
57+
for option in OPTIONS_DICT:
58+
print " " + option
59+
execute(input_filename, option)
60+
61+
output_png_files = [e for e in os.listdir('.') if bn + '.step.' in e]
62+
for e in output_png_files:
63+
golden_file = 'golden-files/' + option + '/' + e
64+
65+
if os.path.isfile(golden_file):
66+
if not filecmp.cmp(e, golden_file):
67+
print " " + RED + e + " differs!" + ENDC
68+
if clobber_golden:
69+
clobber_golden_file(e, golden_file)
70+
else:
71+
clobber_golden_file(e, golden_file)
72+
73+
# do a crude cleanup pass at the very end of the test
74+
# to not interfere as much with Dropbox's weird behavior
75+
for e in os.listdir('.'):
76+
if '.step.' in e:
77+
os.remove(e)
78+
79+
80+
def run_all_tests(clobber=False):
81+
for t in ALL_TESTS:
82+
run_test(t, clobber)
83+
84+
85+
if __name__ == "__main__":
86+
parser = optparse.OptionParser()
87+
parser.add_option("--all", action="store_true", dest="run_all",
88+
help="Run all tests")
89+
parser.add_option("--clobber", action="store_true", dest="clobber",
90+
help="Clobber golden files when running tests")
91+
parser.add_option("--test", dest="test_name",
92+
help="Run one test")
93+
(options, args) = parser.parse_args()
94+
95+
INPUT_FILE_EXTENSION = '.txt' # input test files are .txt, NOT .py
96+
97+
PROGRAM = ['phantomjs', '../../screenshot-renderer/render-opt-screenshots.js']
98+
99+
# grab a representative subset of backend tests
100+
# don't need ALL of them since they run way too slowly
101+
ALL_TESTS = ['../example-code/aliasing.txt',
102+
'../example-code/fact.txt',
103+
'../example-code/filter.txt',
104+
'../example-code/aliasing/aliasing8.txt',
105+
'../example-code/oop_inherit.txt',
106+
'../example-code/linked-lists/ll1.txt',
107+
'../example-code/sum-list.txt',
108+
'../example-code/closures/closure3.txt',
109+
]
110+
111+
if options.run_all:
112+
if options.clobber:
113+
print 'Running all tests and clobbering results ...'
114+
else:
115+
print 'Running all tests ...'
116+
run_all_tests(options.clobber)
117+
elif options.test_name:
118+
assert options.test_name in ALL_TESTS
119+
run_test(options.test_name, options.clobber)
120+
else:
121+
parser.print_help()
122+

0 commit comments

Comments
 (0)