class acceptance_tests: def __init__(self): parser = Parser() fileSystem = FileSystem() matcher = Matcher() self.sut = Finder(parser, fileSystem, matcher) def test_finds_test_log_with_and(self): files = self.sut.find("Lorem ipsum") assert_equal(1, len(files)) assert_equal(".\\tests\\test.log", files[0]) def test_does_not_find_test_log_with_and(self): files = self.sut.find("Lorem gigi") assert_equal(0, len(files)) def test_finds_test_log_with_or(self): files = self.sut.find("Lorem gigi", "-o") assert_equal(1, len(files)) assert_equal(".\\tests\\test.log", files[0]) def test_running_the_program_works(self): # execute the main program and capture the output result = subprocess.check_output(["python", "main.py", "-o", "Lorem", "gigi"]) print result # the string being returned is the print form of a list, need to transform that into a real list result = eval(result) assert_equal(1, len(result)) assert_equal(".\\tests\\test.log", result[0])
def main(argv=None): if argv is None: argv = sys.argv options = "" if "-o" in argv: options = "-o" argv.remove("-o") finder = Finder(Parser(), FileSystem(), Matcher()) args = " ".join(argv[1:]) print finder.find(args, options)
class finder_tests: def __init__(self): self.parser = MagicMock() self.fileSystem = MagicMock() self.matcher = MagicMock() self.sut = Finder(self.parser, self.fileSystem, self.matcher) def test_parses_the_words(self): self.sut.find("Lorem ipsum") self.parser.parse_words.assert_called_once_with("Lorem ipsum") def test_parses_the_options(self): self.sut.find("Lorem ipsum", "-o") self.parser.parse_options.assert_called_once_with("-o") def test_loads_the_logfind_file(self): self.sut.find("Lorem ipsum") self.fileSystem.load.assert_called_once_with(".logfind") def test_asks_for_names_of_files_matching_the_logfind_specifications(self): self.fileSystem.load.return_value = "*.log" self.sut.find("Lorem ipsum") self.fileSystem.get_files.assert_called_with("*.log") def test_loads_all_files_returned_by_get_files(self): self.fileSystem.load.return_value = "*.log" self.fileSystem.get_files.return_value = ["a.log", "b.log"] self.sut.find("Lorem ipsum") self.fileSystem.load.assert_any_call("a.log") self.fileSystem.load.assert_any_call("b.log") def test_matches_each_file_against_the_words_and_options(self): self.parser.parse_words.return_value = ["Lorem", "ipsum"] self.parser.parse_options.return_value = Logical.And self.fileSystem.load.side_effect = ["*.log", "aaa", "bbb"] self.fileSystem.get_files.return_value = ["a.log", "b.log"] self.sut.find("Lorem ipsum") self.matcher.match.assert_any_call("aaa", ["Lorem", "ipsum"], Logical.And) self.matcher.match.assert_any_call("bbb", ["Lorem", "ipsum"], Logical.And) def test_returns_each_matched_file(self): self.parser.parse_words.return_value = ["Lorem", "ipsum"] self.parser.parse_options.return_value = Logical.And self.fileSystem.load.side_effect = ["*.log", "aaa", "bbb"] self.fileSystem.get_files.return_value = ["a.log", "b.log"] self.matcher.match.side_effect = [False, True] result = self.sut.find("Lorem ipsum") assert_equal(1, len(result)) assert_equal("b.log", result[0])