From 3b0e3453a5873bceb8f4228c154bed96d5365658 Mon Sep 17 00:00:00 2001 From: "Andris Jansons (jansonsa)" Date: Tue, 21 Nov 2017 14:50:05 +0000 Subject: [PATCH] UnitTest for FoodFinder --- FoodFinder.py | 11 +++++++---- UnitTesting.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 UnitTesting.py diff --git a/FoodFinder.py b/FoodFinder.py index c8d288d..53f9223 100644 --- a/FoodFinder.py +++ b/FoodFinder.py @@ -1,6 +1,7 @@ # Made by Andris Jansons -KEYWORDS = "the, a, an, get, good, best, is, are, to, nice, pleasant, lovely, cool, fine, stupid, goddamn, great, large, small, medium, new, newest, old, closest, smallest, biggest, coolest".split(", ") +KEYWORDS = "the, a, an, get, good, best, is, are, to, nice, pleasant, lovely, cool, fine, stupid, goddamn, nearest, great, large, small, medium, new, newest, old, closest, smallest, biggest, coolest".split(", ") +SKIPS = "get, is, are, to".split(", ") def findFood(text): """Finds food after special KEYWORDS""" @@ -10,15 +11,17 @@ def findFood(text): if found == False: if word in KEYWORDS: found = True #Finds the first keyword - food = word + if not(word in SKIPS): + food = word else: if word in KEYWORDS: - food = food + " " + word + if not(word in SKIPS): + food = food + " " + word continue food = food + " " + word #If multiple keywords follow each other break # take all of them in account and the word # that follows them as well e.g. "The best burger" - food = food.replace("get",'').replace("is",'').replace("are",'').replace("to",'').strip(' ') # Make the string neat for printing it out + food = food.strip(' ') # Make the string neat for printing it out return food diff --git a/UnitTesting.py b/UnitTesting.py new file mode 100644 index 0000000..782fdb0 --- /dev/null +++ b/UnitTesting.py @@ -0,0 +1,25 @@ +from QuestionToList import makeList +print("Running Food finder tests") +print("-"*50) +try: + from FoodFinder import findFood + questions = ["Where can a man get himself a burger?", "I would like an icecream" , "I'm craving a lettuce at this time of the day", "How far is the nearest hospital?", "The closest atm", "A nice place to sleep"] + answers = ["a burger", "an icecream", "a lettuce", "the nearest hospital", "the closest atm", "a nice place to sleep"] + n = len(questions) + count=0 + for i in range(n): + que = questions[i] + ans = findFood(makeList(que)) + if ans!=answers[i]: + print("Food finder test for \"" + que + "\" failed") + print("Expected: "+answers[i]) + print("Got: "+ans) + print("-"*50) + count = count+1 + print(str(n-count) + "/" + str(n) + " food finder tests passed") +except ImportError: + print("The function findFood was not present in the module FoodFinder") +print("-"*50) +print("End of Food finder tests") + +