Skip to content

Commit 282f3d6

Browse files
committed
write tests, converted to method
1 parent 6a9a97d commit 282f3d6

8 files changed

Lines changed: 180 additions & 60 deletions

File tree

β€Ž.gitignoreβ€Ž

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
# Created by https://www.gitignore.io/api/python
3+
# Edit at https://www.gitignore.io/?templates=python
4+
5+
### Python ###
6+
# Byte-compiled / optimized / DLL files
7+
__pycache__/
8+
*.py[cod]
9+
*$py.class
10+
11+
# C extensions
12+
*.so
13+
14+
# Distribution / packaging
15+
.Python
16+
build/
17+
develop-eggs/
18+
dist/
19+
downloads/
20+
eggs/
21+
.eggs/
22+
lib/
23+
lib64/
24+
parts/
25+
sdist/
26+
var/
27+
wheels/
28+
pip-wheel-metadata/
29+
share/python-wheels/
30+
*.egg-info/
31+
.installed.cfg
32+
*.egg
33+
MANIFEST
34+
35+
# PyInstaller
36+
# Usually these files are written by a python script from a template
37+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
38+
*.manifest
39+
*.spec
40+
41+
# Installer logs
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Unit test / coverage reports
46+
htmlcov/
47+
.tox/
48+
.nox/
49+
.coverage
50+
.coverage.*
51+
.cache
52+
nosetests.xml
53+
coverage.xml
54+
*.cover
55+
.hypothesis/
56+
.pytest_cache/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
67+
# Flask stuff:
68+
instance/
69+
.webassets-cache
70+
71+
# Scrapy stuff:
72+
.scrapy
73+
74+
# Sphinx documentation
75+
docs/_build/
76+
77+
# PyBuilder
78+
target/
79+
80+
# Jupyter Notebook
81+
.ipynb_checkpoints
82+
83+
# IPython
84+
profile_default/
85+
ipython_config.py
86+
87+
# pyenv
88+
.python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# celery beat schedule file
98+
celerybeat-schedule
99+
100+
# SageMath parsed files
101+
*.sage.py
102+
103+
# Environments
104+
.env
105+
.venv
106+
env/
107+
venv/
108+
ENV/
109+
env.bak/
110+
venv.bak/
111+
112+
# Spyder project settings
113+
.spyderproject
114+
.spyproject
115+
116+
# Rope project settings
117+
.ropeproject
118+
119+
# mkdocs documentation
120+
/site
121+
122+
# mypy
123+
.mypy_cache/
124+
.dmypy.json
125+
dmypy.json
126+
127+
# Pyre type checker
128+
.pyre/
129+
130+
# End of https://www.gitignore.io/api/python
131+

β€ŽREADME.mdβ€Ž

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,37 @@ git clone https://github.com/codeclassroom/cc-judge.git
1515
```bash
1616
pip install -r requirements.txt
1717
```
18-
4. Run this command to test the script.
18+
4. Run tests.
1919
```bash
20-
python3 judge.py test_java.java Java output.txt
20+
python3 test.py
2121
```
2222

2323
### Usage
24-
Currently it can be used as a script only.
2524

26-
```bash
27-
python3 judge.py *arg1* *arg2* *arg3* *arg4*
28-
```
29-
30-
1. `arg1` = Program to compile/interpret (e.g helloword.java, test.cpp).
31-
32-
2. `arg2` = programming language, currently available languages :
33-
- C++ (g++ 7.2.0)
34-
- Python (3.6.0)
35-
- Java (OpenJDK 8)
36-
- C (gcc 6.4.0)
25+
Import the `run` method from *judge.py*.
3726

38-
3. `arg3` = Expected Output textfile (e.g output.txt)
27+
```python
28+
from judge import run
3929

40-
4. `arg4` = Standard Input, a textfile which contains the input to program (e.g input.txt)
30+
program_name = "test_python.py"
31+
language = "Python"
32+
output = "output.txt"
4133

42-
The whole command should look like.
43-
```bash
44-
python3 judge.py myfile.java Java output.txt input.txt
45-
```
46-
or if you don't have a `stdin`.
47-
```bash
48-
python3 judge.py myfile.java Java output.txt
34+
status = run(program_name, language, output)
35+
print(status)
4936
```
5037

5138

52-
### Pointers
53-
- In a `Java` program the class name should always be ***Main***.
39+
### Pointers ✏
40+
- In a `Java` program the class name should always be ***`Main`***.
5441

5542

5643
### TODO πŸ“‘
5744
```
5845
❌ Compile multiple files asynchronously.
59-
❌ Convert the whole script into a module.
46+
βœ… Convert the whole script into a module.
6047
❌ Add --help to display script usage.
48+
❌ Return live status of the submission.
6149
```
6250

6351
### Author

β€Žjudge.pyβ€Ž

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import time
33
import sys
44

5-
# language IDs on judge0
5+
# language IDs on judge0, see README.md
66
languages = {
77
"C++" : 10,
88
"Java" : 27,
@@ -37,7 +37,7 @@ def readExpectedOutput(output_file):
3737
return data
3838

3939

40-
def read_stdin(output_file):
40+
def readStandardInput(output_file):
4141
with open(output_file, 'r') as out:
4242
data = out.read()
4343
return data
@@ -48,24 +48,18 @@ def readStatus(token):
4848
res = requests.get("https://api.judge0.com/submissions/" + token['token'])
4949
response = res.json()
5050
status = response['status']['description']
51-
#time.sleep(0.5) # wait for judge0 to compile
5251
if status != "Processing" and status != "In Queue":
5352
break
54-
55-
print("Processing πŸ”΅ , \n" + program)
56-
print("Expected Output : \n" + stdout)
53+
print(f'{status} ...')
5754

5855
if response['status']['description'] == "Accepted":
59-
print("Output : \n" + str(response['stdout']))
56+
print(f'Output : \n{response["stdout"]}')
6057
print("Compile Success βœ…")
58+
return response['status']['description']
6159
else:
62-
print("Compile Failed ❌")
63-
print("Output : " + str(response['stdout']))
64-
print("Error : " + str(response['stderr']))
65-
print("Message : " + str(response['message']) + ", " + response['status']['description'])
66-
60+
return response
6761

68-
def compile(program, language_id, *argv):
62+
def submit(program, language_id, *argv):
6963
if len(argv) == 2:
7064
stdout = argv[0]
7165
stdin = argv[1]
@@ -79,24 +73,18 @@ def compile(program, language_id, *argv):
7973

8074
res = requests.post("https://api.judge0.com/submissions", data=api_params)
8175
token = res.json()
82-
readStatus(token)
83-
84-
85-
if __name__ == '__main__':
86-
if len(sys.argv) == 4:
87-
program = sys.argv[1]
88-
language = sys.argv[2]
89-
output_file = sys.argv[3]
90-
stdout = readExpectedOutput(output_file)
91-
inputCode = readCode(program)
92-
compile(inputCode, languages[language],stdout)
93-
94-
elif len(sys.argv) == 5:
95-
program = sys.argv[1]
96-
language = sys.argv[2]
97-
output_file = sys.argv[3]
98-
in_file = sys.argv[4]
99-
stdin = read_stdin(in_file)
100-
stdout = readExpectedOutput(output_file)
101-
inputCode = readCode(program)
102-
compile(inputCode, languages[language], stdout, stdin)
76+
return token
77+
78+
79+
def run(program, language, *argv):
80+
program = readCode(program)
81+
stdout = readExpectedOutput(argv[0])
82+
83+
if len(argv) == 2:
84+
stdin = readStandardInput(argv[1])
85+
token = submit(program, languages[language], stdout, stdin)
86+
status = readStatus(token)
87+
elif len(argv) == 1:
88+
token = submit(program, languages[language], stdout)
89+
status = readStatus(token)
90+
return status

β€Žtest.pyβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
from judge import run
3+
4+
class TestRun(unittest.TestCase):
5+
def test_run(self):
6+
program_name = "testfiles/" + "test_java.java"
7+
language = "Java"
8+
output = "testfiles/" + "output.txt"
9+
self.assertEqual(run(program_name, language, output),
10+
"Accepted", "Something Wrong")
11+
12+
if __name__ == '__main__':
13+
unittest.main()
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
Β (0)