import unittest import io import re from unittest import mock import math import student_X as main #Replace student_X with your filename import sys import io from io import StringIO class PrintWhetherTriangleTests(unittest.TestCase): def test_default_case(self): function = main.print_whether_triangle try: docstring = function.__doc__ docstring = docstring.strip() except: docstring = "" correct = docstring != "" message = "docstring is blank." self.assertTrue(correct, message) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(100.0, 201.0, 300.0) output = fake_stdout.getvalue().strip() correct = "100.0" in output feedback = "The first parameter is not in the output." self.assertTrue(correct, feedback) correct = "201.0" in output feedback = "The second parameter is not in the output." self.assertTrue(correct, feedback) correct = "300.0" in output feedback = "The third parameter is not in the output." self.assertTrue(correct, feedback) correct = "not" not in output feedback = "Says no when the inputs can be a triangle." self.assertTrue(correct, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) regex = "A triangle can have sides of length 100.0 *, +201.0 *,? (and *)?300.0 ?.?" correct = re.match(regex, output) feedback = "Doesn't print the full correct output when the lengths can form a triangle." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(201.0, 300.0, 600.0) output = fake_stdout.getvalue().strip() correct = "201.0" in output feedback = "The first parameter is not in the output." self.assertTrue(correct, feedback) correct = "300.0" in output feedback = "The second parameter is not in the output." self.assertTrue(correct, feedback) correct = "600.0" in output feedback = "The third parameter is not in the output." self.assertTrue(correct, feedback) correct = "cannot" in output feedback = "Doesn't say 'cannot' when the inputs can't be a triangle." self.assertTrue(correct, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) regex = "A triangle cannot have sides of length 201.0 *, +300.0 *,? (and *)?600.0 ?.?" correct = re.match(regex, output) feedback = "Doesn't print the full correct output when the third parameter is too large to form a triangle." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(201.0, 3000.0, 600.0) output = fake_stdout.getvalue().strip() correct = "201.0" in output feedback = "The first parameter is not in the output." self.assertTrue(correct, feedback) correct = "3000.0" in output feedback = "The second parameter is not in the output." self.assertTrue(correct, feedback) correct = "600.0" in output feedback = "The third parameter is not in the output." self.assertTrue(correct, feedback) correct = "cannot" in output feedback = "Doesn't say 'cannot' when the inputs can't be a triangle." self.assertTrue(correct, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) regex = "A triangle cannot have sides of length 201.0 *, +3000.0 *,? (and *)?600.0 ?.?" correct = re.match(regex, output) feedback = "Doesn't print the full correct output when the second parameter is too large to form a triangle." self.assertTrue(correct, feedback) with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout: function(20100.0, 3000.0, 600.0) output = fake_stdout.getvalue().strip() correct = "20100.0" in output feedback = "The first parameter is not in the output." self.assertTrue(correct, feedback) correct = "3000.0" in output feedback = "The second parameter is not in the output." self.assertTrue(correct, feedback) correct = "600.0" in output feedback = "The third parameter is not in the output." self.assertTrue(correct, feedback) correct = "cannot" in output feedback = "Doesn't say 'cannot' when the inputs can't be a triangle." self.assertTrue(correct, feedback) correct = output.endswith(".") feedback = "The output doesn't end with a period." self.assertTrue(correct, feedback) regex = "A triangle cannot have sides of length 20100.0 *, +3000.0 *,? (and *)?600.0 ?.?" correct = re.match(regex, output) feedback = "Doesn't print the full correct output when the first parameter is too large to form a triangle." self.assertTrue(correct, feedback) if __name__ == "__main__": tests = PrintWhetherTriangleTests() tests.test_default_case()