Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ddt to improve test maintainability #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
26 changes: 16 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#!/usr/bin/env python

import os, json, imp
from setuptools import setup, find_packages

PROJ_NAME = 'romkan'
PACKAGE_NAME = 'romkan'

PROJ_METADATA = '%s.json' % PROJ_NAME

import os, json, imp
HERE = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(HERE, 'README.rst')).read()
CHANGELOG = open(os.path.join(HERE, 'CHANGELOG.rst')).read()
VERSION = imp.load_source('version', os.path.join(HERE, 'src/%s/version.py' % PACKAGE_NAME)).__version__
SRC = os.path.join(HERE, 'src')


proj_info = json.loads(open(os.path.join(HERE, PROJ_METADATA)).read())
test_requirements = ['ddt>=0.5.0', 'unicodecsv>=0.10.1']

here = os.path.abspath(os.path.dirname(__file__))
proj_info = json.loads(open(os.path.join(here, PROJ_METADATA)).read())
README = open(os.path.join(here, 'README.rst')).read()
CHANGELOG = open(os.path.join(here, 'CHANGELOG.rst')).read()
VERSION = imp.load_source('version', os.path.join(here, 'src/%s/version.py' % PACKAGE_NAME)).__version__

from setuptools import setup, find_packages
setup(
name = proj_info['name'],
version = VERSION,
Expand All @@ -28,10 +32,12 @@

long_description = README + '\n\n' + CHANGELOG,

packages = find_packages('src'),
package_dir = {'' : 'src'},
packages = find_packages(SRC),
package_dir = {'' : SRC},

test_suite = 'tests',
tests_require = test_requirements,
dependency_links = ['https://github.com/jdunck/python-unicodecsv/zipball/master#egg=unicodecsv-0.10.1'],

platforms = 'any',
zip_safe = False,
Expand Down
284 changes: 59 additions & 225 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,234 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import unittest
from ddt import ddt, data
from unicodecsv import DictReader
from romkan import to_kunrei, to_hepburn, to_hiragana, to_katakana
from romkan import is_vowel, is_consonant, expand_consonant

import sys
import os

# make sure to import romkan from ../romkan directory relative to this
# file
sys.path.insert(0,
(os.path.dirname(
os.path.dirname(
os.path.abspath(__file__)))))
from romkan import *
SEP = '/' # Separates variants in the same CSV field
HERE = os.path.abspath(os.path.dirname(__file__))
TESTS_CSV = os.path.join(HERE, 'tests.csv')

class RomkanTestCase(unittest.TestCase):

def test_to_katakana(self):
self.assertEqual(to_katakana("kanji"),
"カンジ")
self.assertEqual(to_katakana("kanzi"),
"カンジ")
self.assertEqual(to_katakana("kannji"),
"カンジ")
self.assertEqual(to_katakana("chie"),
"チエ")
self.assertEqual(to_katakana("tie"),
"チエ")
self.assertEqual(to_katakana("kyouju"),
"キョウジュ")
self.assertEqual(to_katakana("syuukyou"),
"シュウキョウ")
self.assertEqual(to_katakana("shuukyou"),
"シュウキョウ")
self.assertEqual(to_katakana("saichuu"),
"サイチュウ")
self.assertEqual(to_katakana("saityuu"),
"サイチュウ")
self.assertEqual(to_katakana("cheri-"),
"チェリー")
self.assertEqual(to_katakana("tyeri-"),
"チェリー")
self.assertEqual(to_katakana("shinrai"),
"シンライ")
self.assertEqual(to_katakana("sinrai"),
"シンライ")
self.assertEqual(to_katakana("hannnou"),
"ハンノウ")
self.assertEqual(to_katakana("han'nou"),
"ハンノウ")

self.assertEqual(to_katakana("wo"),
"ヲ")
self.assertEqual(to_katakana("we"),
"ウェ")
self.assertEqual(to_katakana("du"),
"ヅ")
self.assertEqual(to_katakana("she"),
"シェ")
self.assertEqual(to_katakana("di"),
"ヂ")
self.assertEqual(to_katakana("fu"),
"フ")
self.assertEqual(to_katakana("ti"),
"チ")
self.assertEqual(to_katakana("wi"),
"ウィ")

self.assertEqual(to_katakana("je"),
"ジェ")
self.assertEqual(to_katakana("e-jento"),
"エージェント")

def test_to_hiragana(self):
self.assertEqual(to_hiragana("kanji"),
"かんじ")
self.assertEqual(to_hiragana("kanzi"),
"かんじ")
self.assertEqual(to_hiragana("kannji"),
"かんじ")
self.assertEqual(to_hiragana("chie"),
"ちえ")
self.assertEqual(to_hiragana("tie"),
"ちえ")
self.assertEqual(to_hiragana("kyouju"),
"きょうじゅ")
self.assertEqual(to_hiragana("syuukyou"),
"しゅうきょう")
self.assertEqual(to_hiragana("shuukyou"),
"しゅうきょう")
self.assertEqual(to_hiragana("saichuu"),
"さいちゅう")
self.assertEqual(to_hiragana("saityuu"),
"さいちゅう")
self.assertEqual(to_hiragana("cheri-"),
"ちぇりー")
self.assertEqual(to_hiragana("tyeri-"),
"ちぇりー")
self.assertEqual(to_hiragana("shinrai"),
"しんらい")
self.assertEqual(to_hiragana("sinrai"),
"しんらい")
self.assertEqual(to_hiragana("hannnou"),
"はんのう")
self.assertEqual(to_hiragana("han'nou"),
"はんのう")

def test_to_kana(self):
self.assertEqual(to_kana("kanji"),
"カンジ")
self.assertEqual(to_kana("kanzi"),
"カンジ")
self.assertEqual(to_kana("kannji"),
"カンジ")
self.assertEqual(to_kana("chie"),
"チエ")
self.assertEqual(to_kana("tie"),
"チエ")
self.assertEqual(to_kana("kyouju"),
"キョウジュ")
self.assertEqual(to_kana("syuukyou"),
"シュウキョウ")
self.assertEqual(to_kana("shuukyou"),
"シュウキョウ")
self.assertEqual(to_kana("saichuu"),
"サイチュウ")
self.assertEqual(to_kana("saityuu"),
"サイチュウ")
self.assertEqual(to_kana("cheri-"),
"チェリー")
self.assertEqual(to_kana("tyeri-"),
"チェリー")
self.assertEqual(to_kana("shinrai"),
"シンライ")
self.assertEqual(to_kana("sinrai"),
"シンライ")
self.assertEqual(to_kana("hannnou"),
"ハンノウ")
self.assertEqual(to_kana("han'nou"),
"ハンノウ")

self.assertEqual(to_kana("wo"),
"ヲ")
self.assertEqual(to_kana("we"),
"ウェ")
self.assertEqual(to_kana("du"),
"ヅ")
self.assertEqual(to_kana("she"),
"シェ")
self.assertEqual(to_kana("di"),
"ヂ")
self.assertEqual(to_kana("fu"),
"フ")
self.assertEqual(to_kana("ti"),
"チ")
self.assertEqual(to_kana("wi"),
"ウィ")

self.assertEqual(to_kana("je"),
"ジェ")
self.assertEqual(to_kana("e-jento"),
"エージェント")

def test_to_hepburn(self):
self.assertEqual(to_hepburn("kannzi"),
"kanji")
self.assertEqual(to_hepburn("tie"),
"chie")

self.assertEqual(to_hepburn("KANNZI"),
"kanji")
self.assertEqual(to_hepburn("TIE"),
"chie")

self.assertEqual(to_hepburn("カンジ"),
"kanji")
self.assertEqual(to_hepburn("チエ"),
"chie")

self.assertEqual(to_hepburn("かんじ"),
"kanji")
self.assertEqual(to_hepburn("ちえ"),
"chie")

self.assertEqual(to_hepburn("しゃしん"),
"shashin")
self.assertEqual(to_hepburn("しゅっしょう"),
"shusshou")

def test_to_kunrei(self):
self.assertEqual(to_kunrei("kanji"),
"kanzi")
self.assertEqual(to_kunrei("chie"),
"tie")

self.assertEqual(to_kunrei("KANJI"),
"kanzi")
self.assertEqual(to_kunrei("CHIE"),
"tie")

self.assertEqual(to_kunrei("カンジ"),
"kanzi")
self.assertEqual(to_kunrei("チエ"),
"tie")

self.assertEqual(to_kunrei("かんじ"),
"kanzi")
self.assertEqual(to_kunrei("ちえ"),
"tie")

self.assertEqual(to_kunrei("しゃしん"),
"syasin")
self.assertEqual(to_kunrei("しゅっしょう"),
"syussyou")

def test_to_roma(self):
self.assertEqual(to_roma("カンジ"),
"kanji")
self.assertEqual(to_roma("チャウ"),
"chau")
self.assertEqual(to_roma("ハンノウ"),
"han'nou")

self.assertEqual(to_roma("かんじ"),
"kanji")
self.assertEqual(to_roma("ちゃう"),
"chau")
self.assertEqual(to_roma("はんのう"),
"han'nou")
def get_cases_from_csv(input_field, output_field):
fh = open(TESTS_CSV, 'r')
ucsv = DictReader(fh, delimiter=str('\t'))
for row in ucsv:
inputs, outputs = row[input_field], row[output_field]
for i in inputs.split(SEP):
if outputs != '':
yield i, outputs.split(SEP)


@ddt
class ConversionTestCase(unittest.TestCase):

@data(*list(get_cases_from_csv('hiragana', 'hepburn revised')))
def test_hiragana_to_hepburn(self, row):
in_text, expected = row
self.assertIn(to_hepburn(in_text), expected)

@data(*list(get_cases_from_csv('hiragana', 'kunrei-shiki')))
def test_hiragana_to_kunrei(self, row):
in_text, expected = row
self.assertIn(to_kunrei(in_text), expected)

@data(*list(get_cases_from_csv('hiragana', 'katakana')))
def test_hiragana_to_katakana(self, row):
in_text, expected = row
self.assertIn(to_katakana(in_text), expected)

@data(*list(get_cases_from_csv('katakana', 'hiragana')))
def test_katakana_to_hiragana(self, row):
in_text, expected = row
self.assertIn(to_katakana(in_text), expected)

@data(*list(get_cases_from_csv('wapuro', 'hiragana')))
def test_wapuro_to_hiragana(self, row):
in_text, expected = row
self.assertIn(to_hiragana(in_text), expected)

@data(*list(get_cases_from_csv('wapuro', 'katakana')))
def test_wapuro_to_katakana(self, row):
in_text, expected = row
self.assertIn(to_katakana(in_text), expected)

@data(*list(get_cases_from_csv('hiragana', 'hiragana')))
def test_reversibility(self, row):
in_text, _ = row
self.assertEquals(to_hiragana(to_kunrei(in_text)), in_text)


class RomkanTestCase(unittest.TestCase):

def test_is_consonant(self):
assert not is_consonant("a")
Expand Down Expand Up @@ -257,5 +90,6 @@ def test_expand_consonant(self):
self.assertEqual(sorted(expand_consonant("ch")),
["cha", "che", "chi", "cho", "chu"])


if __name__ == '__main__':
unittest.main()
Loading