def _assert_month(month_num, computed_month): """ Asserts that `computed_month` has month_num as its month number and has the proper configuration for that month number. Precondition: computed_month is an a5.Month month_num: int in 1..12 """ testcase.assert_true(isinstance(computed_month, a5.Month)) # Test that right set of attributes were added _assert_attr_set(computed_month, set(["month_num", "day_list"])) if month_num == 2: # February days_in = 28 elif month_num in [4, 6, 9, 11]: # the months with 30 days days_in = 30 else: # Any month that makes it to this point has 31 days in it days_in = 31 testcase.assert_equals(month_num, computed_month.month_num) testcase.assert_equals(days_in, len(computed_month.day_list)) empty_day_list = [None] * 24 for i in range(days_in): cmp_day = computed_month.day_list[i] testcase.assert_true(isinstance(cmp_day, a3_classes.Day)) testcase.assert_equals("/".join([str(month_num), str(i + 1)]), cmp_day.name) testcase.assert_equals(empty_day_list, cmp_day.time_slots) testcase.assert_equals(0, cmp_day.num_tasks_scheduled)
def test_report_section(): """Test function a1_second.report_section""" print("Testing a1_second.report_section") # simple version of template s0 = '<ul class="section section-last aria-label="Class Section DIS 202"' s0 = s0 + 'class="pattern-only"><span filler>T</span>As you set out for ' s0 = s0 + 'Ithaka <class="time">11:15am - 12:05pm</time>hope your road i' s0 = s0 + 's a long one<li class="open-status"> full of adventure, full ' s0 = s0 + 'data-content="Open" of discovery.' result = a1_second.report_section(s0) testcase.assert_equals('DIS 202 T 11:15am - 12:05pm Open', result) # another simple version of template s1 = '<ul class="section Laistrygonians, aria-label="Class Section AB 2"' s1 = s1 + 'class="pattern-only"><span aria-label="Class Section EVL 666">R' s1 = s1 + '</span> <class="time">9:05am - 12:05pm</time>aria-label="NO"' s1 = s1 + 'Cyclops, angry <li class="open-status">Poseidon --- don\'t be' s1 = s1 + 'afraid data-content="Closed" of them:' result = a1_second.report_section(s1) testcase.assert_equals('AB 2 R 9:05am - 12:05pm Closed', result) # real example s2 = '<ul class="section " aria-label="Class Section LEC 001">' s2 = s2 + '<li class="class-numbers"><h5 class="hidden">Class Number & ' s2 = s2 + '<Section Details</h5><p><strong class="tooltip-iws" ' s2 = s2 + 'data-toggle="popover" data-content="14442" title="Class Number">' s2 = s2 + '14442</strong><span class="course-repeater">CS 1132 ' s2 = s2 + '</span><em class="tooltip-iws" data-toggle="popover" ' s2 = s2 + 'data-content="Lecture" title="Component">LEC</em> 001\n' s2 = s2 + '<span class="favorite fav-14442"><a class="tooltip-iws" ' s2 = s2 + 'data-toggle="popover" data-content="Add to Favorites" aria-' s2 = s2 + 'label="Add to Favorites" href="#" data-class-nbr="14442" data-s' s2 = s2 + 'sr-component="LEC" data-section="001"></a></span></p></li><li c' s2 = s2 + 'lass="consent"> \n</li><li class="meeting-pattern"><h5 cla' s2 = s2 + 'ss="hidden">Meeting Pattern</h5><ul class="meetings meetings-f' s2 = s2 + 'irst"><li class="dates"><span class="pattern"><span class="patt' s2 = s2 + 'ern-only"><span class="tooltip-iws" data-toggle="popover" data-' s2 = s2 + 'content="Mon & Wed">MW</span></span><time class="time">3:35' s2 = s2 + 'pm - 4:25pm</time></span><a class="facility-search" href="http:' s2 = s2 + '//www.cornell.edu/about/maps/?q=Thurston%20Hall#CUmap" target="' s2 = s2 + '_blank" rel="nofollow">Thurston Hall 203</a></li><li class="dat' s2 = s2 + 'e-range">\nJan 21 - Mar 10, 2020\n</li><li class="instructors">' s2 = s2 + '<h5 class="hidden">Instructors</h5><p><span class="tooltip-iws"' s2 = s2 + ' data-toggle="popover" data-content="Nate Veldt (lnv22)">Veldt,' s2 = s2 + ' N</span></p></li></ul></li><li class="open-status"><span class' s2 = s2 + '="tooltip-iws" data-toggle="popover" data-content="Open"><span ' s2 = s2 + 'class="' result = a1_second.report_section(s2) testcase.assert_equals('LEC 001 MW 3:35pm - 4:25pm Open', result) print("finished test\n")
def test_first_between(): """Test function a1_second.first_between""" print("Testing a1_second.first_between") # simple markers, typical case (given, guaranteed correct) result = a1_second.first_between('A1A2A3bA4ccccA5', 'A', 'b') testcase.assert_equals('1A2A3', result) # end_str before start_str (given, guaranteed correct) result = a1_second.first_between('hello and hi) bye(x) shoo', '(', ')') testcase.assert_equals("x", result) # start_str and end_str are like real html (given, guaranteed correct) t = '<a href="x">yes</a><span>toodles</span>' result = a1_second.first_between(t, '<span>', '<') testcase.assert_equals("toodles", result) print("finished test\n")
def test_first_between(): """Test function a1_second.first_between""" print("Testing a1_second.first_between") # simple markers, typical case (given, guaranteed correct) result = a1_second.first_between('A1A2A3bA4ccccA5', 'A', 'b') testcase.assert_equals('1A2A3', result) # end_str before start_str (given, guaranteed correct) result = a1_second.first_between('hello and hi) bye(x) shoo', '(', ')') testcase.assert_equals("x", result) # start_str and end_str are like real html (given, guaranteed correct) t = '<a href="x">yes</a><span>toodles</span>' result = a1_second.first_between(t, '<span>', '<') testcase.assert_equals("toodles", result) # text has no string between start_str and end_str result = a1_second.first_between('coursework', 'course', 'work') testcase.assert_equals('', result) # end_str same as start_str result = a1_second.first_between('banana', 'n', 'n') testcase.assert_equals('a', result) # end_str same as start_str same as text result = a1_second.first_between('N', 'N', 'N') testcase.assert_equals('', result) # multiple start_str result = a1_second.first_between('hello hello world', 'hello', 'world') testcase.assert_equals(' hello ', result) # start_str contains space result = a1_second.first_between('hello world', ' ', 'd') testcase.assert_equals('worl', result) # end_str contains space result = a1_second.first_between('hello world', 'h', ' ') testcase.assert_equals('ello', result) # start_str contains punctuation result = a1_second.first_between('Please! No more testing.', '!', 'i') testcase.assert_equals(' No more test', result) # end_str contains punctuation result = a1_second.first_between('Please no more testing!', 'Please ', '!') testcase.assert_equals('no more testing', result) # start_str and end_str are case-sensitive result = a1_second.first_between('Bonobo', 'Bo', 'bo') testcase.assert_equals('no', result) print("finished test\n")
def test_after(): """Test function a1_second.after""" print("Testing a1_second.after") ### STUDENTS: see instructions in assignment writeup!!! # 1. tag at beginning result = a1_second.after('start', 'st') testcase.assert_equals('art', result) # 2. (given, guaranteed ok) tag in middle (and tag like one expects in html) result = a1_second.after('start <a id="c111"> this that', '<a id="c111">') testcase.assert_equals(' this that', result) # 3. (given, guaranteed ok) tag at end result = a1_second.after('start <a id="c111"> this that the other', 'other') testcase.assert_equals('', result) # 4. tag not in text # ... STUDENT-DELETED CASE ... # ... REASON: violates precondition #result = a1_second.after('start', 'x') #testcase.assert_equals(None, result) # 5. tag in twice # ... STUDENT-FIXED ERROR ... result = a1_second.after('start A start B', 'start') #testcase.assert_equals(' A ', result) testcase.assert_equals(' A start B', result) # 6. text and tag are the same result = a1_second.after('start', 'start') testcase.assert_equals('', result) # 7. tag contains punctuation result = a1_second.after("Hi! How's it going?", "'") testcase.assert_equals('s it going?', result) # 8. parts of tag show up before actual tag result = a1_second.after('aaa1aaaa2', 'aaaa') testcase.assert_equals('2', result) # 9. tag contains space(s) result = a1_second.after("Hi! How's it going?", "it going") testcase.assert_equals('?', result) # 10. empty tag # ... STUDENT-DELETED CASE ... # ... REASON: violates precondition #result = a1_second.after('aaa1aaaa2', '') #testcase.assert_equals('aaa1aaaa2', result) # 11. tag is case-sensitive result = a1_second.after("UPPER upper", "up") testcase.assert_equals('per', result) # 12. tag is space (probably redundant with #9?) result = a1_second.after("Hello, world", " ") testcase.assert_equals('world', result) # 13. tag and text are single character result = a1_second.after("a", "a") testcase.assert_equals('', result) # 14. tag contains a number (redundant with #2 and #3) result = a1_second.after("0100", "1") testcase.assert_equals('00', result) print("finished test\n")
def test_after(): """Test function a1_second.after""" print("Testing a1_second.after") ### STUDENTS: see instructions in assignment writeup!!! # 1. tag at beginning result = a1_second.after('start', 'st') testcase.assert_equals('art', result) # 2. (given, guaranteed ok) tag in middle (and tag like one expects in html) result = a1_second.after('start <a id="c111"> this that', '<a id="c111">') testcase.assert_equals(' this that', result) # 3. (given, guaranteed ok) tag at end result = a1_second.after('start <a id="c111"> this that the other', 'other') testcase.assert_equals('', result) # 4. tag not in text #... STUDENT DELETED CASE ... #... REASON: violates precondition, `text` has at least one instance of `tag` #result = a1_second.after('start', 'x') #testcase.assert_equals(None, result) # 5. tag in twice # ... STUDENT FIXED ERROR ... result = a1_second.after('start A start B', 'start') #testcase.assert_equals(' A ', result) testcase.assert_equals(' A start B', result) # 6. text and tag are the same result = a1_second.after('start', 'start') testcase.assert_equals('', result) # 7. tag contains punctuation result = a1_second.after("Hi! How's it going?", "'") testcase.assert_equals('s it going?', result) # 8. parts of tag show up before actual tag result = a1_second.after('aaa1aaaa2', 'aaaa') testcase.assert_equals('2', result) # 9. tag contains space(s) result = a1_second.after("Hi! How's it going?", "it going") testcase.assert_equals('?', result) # 10. empty tag # ... STUDENT DELETED CASE ... # ... REASON: violates precondition, `tag` is string of length > 0 #result = a1_second.after('aaa1aaaa2', '') #testcase.assert_equals('aaa1aaaa2', result) print("finished test\n")
def test_update_unscheduled(): """ Tests whether the updateUnsched method in SplittableTask is working properly. """ print("Testing update_unscheduled") # 1 hour task, to schedule practice = a5.SplittableTask("practice piano", 1) day = a3_classes.Day("Monday") practice.updateUnsched(day, 7) _assert_attr_set(practice, _splittask_attributes) # Should not have changed expected_timeslots = [None] * 24 expected_timeslots[7] = practice testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(0, practice.time_unscheduled) testcase.assert_equals(1, practice.length) # Multi hour task, to schedule hacker_typer = a5.SplittableTask("hack into the mainframe", 4) hacker_typer.updateUnsched(day, 12) _assert_attr_set(hacker_typer, _splittask_attributes) expected_timeslots[12] = hacker_typer testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(3, hacker_typer.time_unscheduled) testcase.assert_equals(4, hacker_typer.length) # Multi hour task, but is partially scheduled already hacker_typer.updateUnsched(day, 15) _assert_attr_set(hacker_typer, _splittask_attributes) expected_timeslots[15] = hacker_typer testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(2, hacker_typer.time_unscheduled) testcase.assert_equals(4, hacker_typer.length) # Schedule one more hour of it, but adjacent to another occurrence of the same task hacker_typer.updateUnsched(day, 16) _assert_attr_set(hacker_typer, _splittask_attributes) expected_timeslots[16] = hacker_typer testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(1, hacker_typer.time_unscheduled) testcase.assert_equals(4, hacker_typer.length) print("Done testing update_unscheduled")
def test_splittable_task_init(): """ Tests whether the initializer for SplittableTask is working properly. """ print("Testing splittable task initializer") # task of length 1 workout = a5.SplittableTask("go to the gym", 1) _assert_attr_set(workout, _splittask_attributes) testcase.assert_equals("go to the gym", workout.name) testcase.assert_equals(1, workout.length) testcase.assert_equals(1, workout.time_unscheduled) # task of length 1 < length < 24 sleep = a5.SplittableTask("go to bed", 8) _assert_attr_set(sleep, _splittask_attributes) testcase.assert_equals("go to bed", sleep.name) testcase.assert_equals(8, sleep.length) testcase.assert_equals(8, sleep.time_unscheduled) # task of length 24 your_duty = a5.SplittableTask("try your best", 24) _assert_attr_set(your_duty, _splittask_attributes) testcase.assert_equals("try your best", your_duty.name) testcase.assert_equals(24, your_duty.length) testcase.assert_equals(24, your_duty.time_unscheduled) print("Finished testing splittable task initializer")
def test_schedule_some(): """ Tests whether the scheduleSome method in SplittableTask is working properly. """ print("Testing scheduleSome") # scheduling into an empty day sleep = a5.SplittableTask("sleep 2.0", 8) day = a3_classes.Day("Just a normal Friday") result = sleep.scheduleSome(day) _assert_attr_set(sleep, _splittask_attributes) testcase.assert_true(result) testcase.assert_equals(1, day.num_tasks_scheduled) expected_timeslots = [sleep] * 8 + [None] * 16 testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(0, sleep.time_unscheduled) testcase.assert_equals(8, sleep.length) # scheduling more in the same day classes = a5.SplittableTask("answer email", 12) result = classes.scheduleSome(day) _assert_attr_set(classes, _splittask_attributes) testcase.assert_true(result) testcase.assert_equals(2, day.num_tasks_scheduled) expected_timeslots[8:20] = [classes] * 12 testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(0, classes.time_unscheduled) testcase.assert_equals(12, classes.length) # scheduling more, but would spill over (cannot fit all in same day) assignments = a5.SplittableTask("finishing psets", 9) result = assignments.scheduleSome(day) _assert_attr_set(assignments, _splittask_attributes) testcase.assert_true(result) testcase.assert_equals(3, day.num_tasks_scheduled) expected_timeslots[20:24] = [assignments] * 4 testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(5, assignments.time_unscheduled) testcase.assert_equals(9, assignments.length) # day is already full, but new task self_care = a5.SplittableTask("treat yo self", 4) result = self_care.scheduleSome(day) _assert_attr_set(self_care, _splittask_attributes) testcase.assert_false(result) testcase.assert_equals(3, day.num_tasks_scheduled) testcase.assert_equals(expected_timeslots, day.time_slots) testcase.assert_equals(4, self_care.time_unscheduled) testcase.assert_equals(4, self_care.length) # only partially schedule a task earlier on, then call scheduleSome with rest new_day = a3_classes.Day("Saturday") errands = a5.SplittableTask("running errands", 4) new_day.time_slots[20] = new_day.time_slots[21] = errands new_day.num_tasks_scheduled = 1 errands.time_unscheduled = errands.time_unscheduled - 2 # actually try and schedule now result = errands.scheduleSome(new_day) _assert_attr_set(errands, _splittask_attributes) testcase.assert_true(result) expected_timeslots = [errands] * 2 + [None] * 18 + [errands] * 2 + [None ] * 2 testcase.assert_equals(1, new_day.num_tasks_scheduled) testcase.assert_equals(expected_timeslots, new_day.time_slots) testcase.assert_equals(0, errands.time_unscheduled) testcase.assert_equals(4, errands.length) # call scheduleSome where it must be split over noncontiguous blocks of time netflix = a5.SplittableTask("watch netflix", 2) disney = a5.SplittableTask("watch disney+", 3) netflix.scheduleSome(new_day) disney.scheduleSome(new_day) _assert_attr_set(netflix, _splittask_attributes) _assert_attr_set(disney, _splittask_attributes) # as of now, schedule is: 0-2: errands, 2-4: netflix, 4-7, disney+. Let's move stuff. # now let's move around some hours new_day.time_slots[8] = new_day.time_slots[ 4] # moving one hour of disney+ from hour 4 to hour 8 new_day.time_slots[4] = None new_day.time_slots[10] = new_day.time_slots[ 2] # moving one hour of netflix from hour 2 to hour 10 new_day.time_slots[2] = None expected_timeslots[3] = netflix expected_timeslots[5] = expected_timeslots[6] = disney expected_timeslots[8] = disney expected_timeslots[10] = netflix testcase.assert_equals(expected_timeslots, new_day.time_slots) testcase.assert_equals(3, new_day.num_tasks_scheduled) # this is where the actual test kicks in thought = a5.SplittableTask("ponder the complexities of human language", 5) result = thought.scheduleSome(new_day) _assert_attr_set(thought, _splittask_attributes) testcase.assert_true(result) expected_timeslots[2] = expected_timeslots[4] = expected_timeslots[ 7] = expected_timeslots[9] = expected_timeslots[11] = thought testcase.assert_equals(expected_timeslots, new_day.time_slots) testcase.assert_equals(4, new_day.num_tasks_scheduled) testcase.assert_equals(0, thought.time_unscheduled) testcase.assert_equals(5, thought.length) print("Done testing scheduleSome")