Skip to content

word-count: Add test case for unicode support #252

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

Merged
merged 1 commit into from
Nov 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion word-count/example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from collections import Counter


# to be backwards compatible with the old Python 2.X
def decode_if_needed(string):
try:
return string.decode('utf-8')
except AttributeError:
return string


def word_count(text):
replace_nonalpha = lambda c: c.lower() if c.isalnum() else ' '
text = ''.join(replace_nonalpha(c) for c in text)
text = ''.join(replace_nonalpha(c) for c in decode_if_needed(text))
return Counter(text.split())
15 changes: 15 additions & 0 deletions word-count/word_count_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# -*- coding: utf-8 -*-
import unittest

from wordcount import word_count


# to be backwards compatible with the old Python 2.X
def decode_if_needed(string):
try:
return string.decode('utf-8')
except AttributeError:
return string


class WordCountTests(unittest.TestCase):

def test_count_one_word(self):
Expand Down Expand Up @@ -69,5 +78,11 @@ def test_non_alphanumeric(self):
word_count('hey,my_spacebar_is_broken.')
)

def test_unicode(self):
self.assertEqual(
{decode_if_needed('до'): 1, decode_if_needed('свидания'): 1},
word_count('до🖖свидания!')
)

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