blob: 0ae468ab03394e529ece2c408564440636e6cf44 [file]
"""Tests for typo_fixer."""
from __future__ import absolute_import
import os
import sys
import unittest
from unittest import mock
# Allow imports from cq/scripts folder
# pylint: disable=wrong-import-position
sys.path.insert(0, os.path.realpath(os.path.join(
os.path.dirname(__file__), os.pardir)))
from helpers import typo_fixer
class AssertItemsEqualMixin():
def assertItemsEqual(self, first, second):
return self.assertEqual(sorted(first), sorted(second))
# Behaviour tests for the typo fixer.
class TypoFixerTest(unittest.TestCase, AssertItemsEqualMixin):
def testDefaultDictionary_NoCorrections(self):
self.assertEqual([], typo_fixer.fix('This has no spelling mistakes.'))
def testDefaultDictionary_SingleWord_SingleCorrection(self):
self.assertItemsEqual([('shoudl', 'should')], typo_fixer.fix('shoudl'))
self.assertItemsEqual([('taget', 'target')], typo_fixer.fix('taget'))
self.assertItemsEqual([('santizier', 'sanitizer')],
typo_fixer.fix('santizier'))
def testDefaultDictionary_Sentence_SingleCorrection(self):
self.assertItemsEqual([('elasped', 'elapsed')],
typo_fixer.fix('Time has elasped'))
self.assertItemsEqual([('propogates', 'propagates')],
typo_fixer.fix('Foo propogates this.'))
self.assertItemsEqual([('Inhert', 'Inherit')],
typo_fixer.fix('Inhert from parent'))
def testDefaultDictionary_Sentence_MultipleCorrections(self):
self.assertItemsEqual([('libary', 'library'),
('guaratee', 'guarantee'),
('postive', 'positive')],
typo_fixer.fix('The libary guaratees this is postive'))
def testDefaultDictionary_PreserveCase(self):
self.assertItemsEqual([('LIBARY', 'LIBRARY')], typo_fixer.fix('LIBARY'))
self.assertItemsEqual([('Libary', 'Library')], typo_fixer.fix('Libary'))
self.assertItemsEqual([('libary', 'library')], typo_fixer.fix('libary'))
# A colleciton of positive / negative examples for individual typos.
class TyposTest(unittest.TestCase, AssertItemsEqualMixin):
def testThenThan(self):
# Positive
self.assertItemsEqual([('less then', 'less than')],
typo_fixer.fix('less then three'))
self.assertItemsEqual([('greater then', 'greater than')],
typo_fixer.fix('greater then'))
# Negative
self.assertItemsEqual([], typo_fixer.fix('bigger than 3, smaller than 4'))
def testDependency(self):
# Positive
self.assertItemsEqual([('depedency', 'dependency')],
typo_fixer.fix('depedency'))
self.assertItemsEqual([('depedencies', 'dependencies')],
typo_fixer.fix('depedencies'))
# Negative
self.assertItemsEqual([], typo_fixer.fix('dependency dependencies'))
def testPerference(self):
# Positive
self.assertItemsEqual([('prefferrance', 'preference')],
typo_fixer.fix('That is my prefferrance'))
# Negative
self.assertItemsEqual([], typo_fixer.fix('What are your preferences?'))
def testTodo(self):
# Positive
self.assertItemsEqual([('TOOO(abc)', 'TODO(abc)')],
typo_fixer.fix('TOOO(abc): A thing'))
self.assertItemsEqual([('TOOD(xyz@)', 'TODO(xyz@)')],
typo_fixer.fix('TOOD(xyz@)'))
self.assertItemsEqual([('TDO(b/1234)', 'TODO(b/1234)')],
typo_fixer.fix('TDO(b/1234): That last thing'))
# Negative
self.assertItemsEqual([], typo_fixer.fix(' TODO: something\n'
' TODO(abc@): something\n'
' TODO(b/1234): something\n'))
def testDefault(self):
#Negative
self.assertItemsEqual([], typo_fixer.fix('Make |kDefaultDelayMs| longer'))
def testSuccess(self):
# Postive
self.assertItemsEqual([('Successs', 'Success')],
typo_fixer.fix('Successs on the mission today.'))
# Negative
# Note: this particular example does not generate a fix even though
# it matches "SuccessS", because the match has mixed case.
self.assertItemsEqual([], typo_fixer.fix('kGenericSuccessStatus'))
if __name__ == '__main__':
unittest.main()