|
4 | 4 |
|
5 | 5 | from _extensions import * |
6 | 6 |
|
7 | | -## CHECKS FROM CHECK50 |
8 | | - |
9 | | -# @check50.check(exists) |
10 | | -# def test_dinner(): |
11 | | -# """correctly identifies \"dinner\" in birdman.txt""" |
12 | | -# check50.run("python3 indexer.py texts/birdman.txt").stdin("dinner").stdout("The word \"dinner\" can be found on lines: 549, 1542.\n", "The word \"dinner\" can be found on lines: 549, 1542.\n", timeout=5) |
13 | | -# |
14 | | -# @check50.check(test_dinner) |
15 | | -# def test_crash(): |
16 | | -# """handles nonexistent words gracefully (i.e. does not crash)""" |
17 | | -# check50.run("python3 indexer.py texts/birdman.txt").stdin("women").stdout("Enter search term: ", "Enter search term:", timeout=5) |
18 | | - |
19 | | -## EXAMPLE CHECKS FROM LEESBAARHEID |
| 7 | +def sandbox(): |
| 8 | + lib.require("stopwords.txt", "https://raw.githubusercontent.com/minprog/pyprog/2022/opdrachten/week5/indexer/dist/indexer/stopwords.txt") |
| 9 | + lib.require("test.txt", "https://raw.githubusercontent.com/minprog/pyprog/2022/opdrachten/week5/indexer/dist/indexer/texts/birdman.txt") |
20 | 10 |
|
21 | 11 | @t.test(1) |
22 | | -def checks_coleman_liau(test): |
| 12 | +def checks_type(test): |
23 | 13 | def testMethod(): |
24 | | - coleman_liau = lib.getFunction("coleman_liau", test.fileName) |
25 | | - if coleman_liau(537, 4.2) == 14.532399999999996: |
26 | | - return True |
27 | | - else: |
28 | | - return False |
| 14 | + fn = lib.getFunction("create_index", test.fileName) |
| 15 | + index = fn("stopwords.txt", []) |
| 16 | + if len(index) != 138: |
| 17 | + return False, "the index does not have the correct amount of words" |
29 | 18 |
|
30 | | - test.test = testMethod |
31 | | - test.description = lambda : "'coleman_liau' works correctly." |
| 19 | + for key, value in index.items(): |
32 | 20 |
|
33 | | -@t.test(2) |
34 | | -def checks_calculate_grade(test): |
35 | | - def testMethod(): |
36 | | - calculate_grade = lib.getFunction("calculate_grade", test.fileName) |
37 | | - if calculate_grade(119, 5, 639) == 15: |
38 | | - return True |
39 | | - else: |
40 | | - return False |
| 21 | + if not isinstance(key, str): |
| 22 | + return False, "the keys in the index are not strings" |
| 23 | + |
| 24 | + if not isinstance(value, list): |
| 25 | + return False, "the values in the index are not lists" |
| 26 | + |
| 27 | + try: |
| 28 | + if not isinstance(value[0], int): |
| 29 | + return False, "the values in the index are not lists that contain integers" |
| 30 | + except: |
| 31 | + return ( |
| 32 | + False, |
| 33 | + "some of the values in the index have emtpy lists", |
| 34 | + ) |
| 35 | + |
| 36 | + return True |
41 | 37 |
|
42 | 38 | test.test = testMethod |
43 | | - test.description = lambda : "'calculate_grade' works correctly." |
| 39 | + test.description = lambda: "'create_index' returns a well-formed index with correctly loaded data" |
44 | 40 |
|
45 | | -@t.test(3) |
| 41 | + |
| 42 | +@t.test(2) |
46 | 43 | def checks_tekst1(test): |
47 | 44 | def testMethod(): |
48 | | - output = lib.outputOf(test.fileName, stdinArgs=["In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since."], |
49 | | - overwriteAttributes = [("__name__", "__main__")]) |
50 | | - return asserts.exact(output.strip(), "Grade 7") |
| 45 | + output = lib.outputOf( |
| 46 | + test.fileName, |
| 47 | + argv=["indexer.py", "test.txt"], |
| 48 | + stdinArgs=["dinner", ""], |
| 49 | + overwriteAttributes=[("__name__", "__main__")], |
| 50 | + ) |
| 51 | + return re.search(r".*dinner.*\ 549, 1542", output) |
51 | 52 |
|
52 | 53 | test.test = testMethod |
53 | | - test.description = lambda : "Determines the correct Grade 7 for a short sentence." |
| 54 | + test.description = ( |
| 55 | + lambda: "finds the word 'dinner' on lines 51 and 306 in a modified text file" |
| 56 | + ) |
54 | 57 |
|
55 | 58 |
|
56 | | -@t.test(4) |
57 | | -def checks_tekst2(test): |
| 59 | +@t.test(3) |
| 60 | +def checks_crash(test): |
58 | 61 | def testMethod(): |
59 | | - output = lib.outputOf(test.fileName, stdinArgs=["It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him."], |
60 | | - overwriteAttributes = [("__name__", "__main__")]) |
61 | | - return asserts.exact(output.strip(), "Grade 10") |
| 62 | + output = lib.outputOf( |
| 63 | + test.fileName, |
| 64 | + argv=["indexer.py", "test.txt"], |
| 65 | + stdinArgs=["women", "dinner", ""], |
| 66 | + overwriteAttributes=[("__name__", "__main__")], |
| 67 | + ) |
| 68 | + return re.search(r".*dinner.*\ 549, 1542", output) |
62 | 69 |
|
63 | 70 | test.test = testMethod |
64 | | - test.description = lambda : "Determines the correct Grade 10 for a long sentence." |
| 71 | + test.description = lambda: "allows for another search if a word is not found" |
0 commit comments