#!/usr/bin/env python
from os.path import abspath, join, dirname
from unittest import TestCase, main
from collections import defaultdict
import names


full_path = lambda filename: abspath(join(dirname(__file__), filename))

FILES = {
    'test1': full_path('test/file1.txt'),
}


class NamesTest(TestCase):

    def test_get_name(self):
        counts = defaultdict(int)
        rounds = 1000.0
        for i in range(1, int(rounds)):
            counts[names.get_name(FILES['test1'])] += 1
        self.assertAlmostEqual(counts['Test1'] / rounds, 0.333, delta=0.05)
        self.assertAlmostEqual(counts['Test2'] / rounds, 0.277, delta=0.05)
        self.assertAlmostEqual(counts['Test3'] / rounds, 0.222, delta=0.05)
        self.assertAlmostEqual(counts['Test4'] / rounds, 0.166, delta=0.05)
        self.assertEqual(counts['Test5'] / rounds, 0)


if __name__ == '__main__':
    main()

@@ -274,16 +274,285 @@
 '),%0A%7D%0A%0A%0A
+class patch_file:%0A%0A    def __init__(self, file_dict):%0A        self.files = file_dict%0A%0A    def __enter__(self):%0A        self.old_files = names.FILES%0A        names.FILES = self.files%0A%0A    def __exit__(self, type, value, traceback):%0A        names.FILES = self.old_files%0A%0A%0A
 class Na
@@ -570,16 +570,16 @@
 tCase):%0A
-
 %0A    def
@@ -651,17 +651,17 @@
 ounds = 
-1
+5
 000.0%0A  
@@ -1050,32 +1050,32 @@
 66, delta=0.05)%0A
-
         self.ass
@@ -1113,16 +1113,513 @@
 ds, 0)%0A%0A
+    def test_random_gender(self):%0A        counts = defaultdict(int)%0A        rounds = 5000.0%0A        with patch_file(%7B'first:male': 'test/male.txt',%0A                         'first:female': 'test/female.txt'%7D):%0A            for i in range(int(rounds)):%0A                names.get_first_name()%0A                counts%5Bnames.get_first_name()%5D += 1%0A        self.assertAlmostEquals(counts%5B'Male'%5D / rounds, 0.500, delta=0.05)%0A        self.assertAlmostEquals(counts%5B'Female'%5D / rounds, 0.500, delta=0.05)%0A%0A
 %0Aif __na

Add test for gender file random selection