File: PythonTreeWalkerTestCase.java

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (93 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.python.antlr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import junit.framework.TestCase;

/**
 * Decorates the PythonTreeWalker class as a JUnit test case for a single .py
 * file
 */
public class PythonTreeWalkerTestCase extends TestCase {

	private String _path;

	/**
	 * Constructor called from rerun menu point in eclipse
	 * 
	 * @param name The name of the test.
	 */
	public PythonTreeWalkerTestCase(String name) {
		super(name);
		setPath(name);
	}

	/**
	 * Create a test case which walks <code>pyFile</code>.
	 * 
	 * @param pyFile The *.py file
	 */
	public PythonTreeWalkerTestCase(File pyFile) {
		this(pyFile.getAbsolutePath());
	}

	@Override
	protected void runTest() throws Throwable {
		String path = getPath();
		File file = new File(path);
		assertTrue("file " + path + " not found", file.exists());
		PythonTreeWalker treeWalker = new PythonTreeWalker();
		treeWalker.setTolerant(false);
		treeWalker.setParseOnly(false);
		PythonTree tree = treeWalker.parse(new String[] { path });
		if (tree == null) {
			if (!isEmpty(file)) {
				assertNotNull("no tree generated for file ".concat(path), tree);
			}
		}
	}

	@Override
	public int countTestCases() {
		return 1;
	}

	private void setPath(String path) {
		_path = path;
	}

	private String getPath() {
		return _path;
	}

	/**
	 * 'empty' check for a .py file
	 * 
	 * @param file The file to check
	 * @return <code>true</code> if the file is really empty, or only contains
	 *         comments
	 * @throws IOException
	 */
	private boolean isEmpty(File file) throws IOException {
		boolean isEmpty = true;
		assertTrue("file " + file.getAbsolutePath() + " is not readable", file.canRead());
		BufferedReader reader = new BufferedReader(new FileReader(file));
		try {
			String line = reader.readLine();
			while (line != null && isEmpty) {
				line = line.trim();
				if (line.length() > 0 && !line.startsWith("#")) {
					isEmpty = false;
				}
				line = reader.readLine();
			}
		} finally {
			reader.close();
		}
		return isEmpty;
	}

}