Skip to content

Commit c8b0e6c

Browse files
committed
Refactored get_view_description, moved appropriate tests to test_description.py
1 parent 2bf5f63 commit c8b0e6c

File tree

4 files changed

+34
-49
lines changed

4 files changed

+34
-49
lines changed

rest_framework/tests/test_description.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from __future__ import unicode_literals
44
from django.test import TestCase
55
from rest_framework.views import APIView
6-
from rest_framework.compat import apply_markdown
6+
from rest_framework.compat import apply_markdown, smart_text
77
from rest_framework.utils.formatting import get_view_name, get_view_description
8+
from rest_framework.tests.views import (
9+
ViewWithNonASCIICharactersInDocstring, UTF8_TEST_DOCSTRING)
810

911
# We check that docstrings get nicely un-indented.
1012
DESCRIPTION = """an example docstring
@@ -83,11 +85,10 @@ def test_view_description_supports_unicode(self):
8385
Unicode in docstrings should be respected.
8486
"""
8587

86-
class MockView(APIView):
87-
"""Проверка"""
88-
pass
89-
90-
self.assertEqual(get_view_description(MockView), "Проверка")
88+
self.assertEqual(
89+
get_view_description(ViewWithNonASCIICharactersInDocstring),
90+
smart_text(UTF8_TEST_DOCSTRING)
91+
)
9192

9293
def test_view_description_can_be_empty(self):
9394
"""

rest_framework/tests/test_utils.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

rest_framework/tests/views.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from rest_framework.views import APIView
4+
5+
6+
# test strings snatched from http://www.columbia.edu/~fdc/utf8/,
7+
# http://winrus.com/utf8-jap.htm and memory
8+
UTF8_TEST_DOCSTRING = (
9+
'zażółć gęślą jaźń'
10+
'Sîne klâwen durh die wolken sint geslagen'
11+
'Τη γλώσσα μου έδωσαν ελληνική'
12+
'யாமறிந்த மொழிகளிலே தமிழ்மொழி'
13+
'На берегу пустынных волн'
14+
'てすと'
15+
'アイウエオカキクケコサシスセソタチツテ'
16+
)
17+
18+
19+
# Apparently there is an issue where docstrings of imported view classes
20+
# do not retain their encoding information even if a module has a proper
21+
# encoding declaration at the top of its source file. Therefore for tests
22+
# to catch unicode related errors, a mock view has to be declared in a separate
23+
# module.
24+
class ViewWithNonASCIICharactersInDocstring(APIView):
25+
__doc__ = UTF8_TEST_DOCSTRING

rest_framework/utils/formatting.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from django.utils.html import escape
77
from django.utils.safestring import mark_safe
8-
from rest_framework.compat import apply_markdown
8+
from rest_framework.compat import apply_markdown, smart_text
99
import re
1010

1111

@@ -24,11 +24,6 @@ def _remove_leading_indent(content):
2424
Remove leading indent from a block of text.
2525
Used when generating descriptions from docstrings.
2626
"""
27-
try:
28-
content = content.decode('utf-8')
29-
except (AttributeError, UnicodeEncodeError):
30-
pass # the string should keep the default 'ascii' encoding in
31-
# Python 2.x or stay a unicode string in Python 3.x
3227
whitespace_counts = [len(line) - len(line.lstrip(' '))
3328
for line in content.splitlines()[1:] if line.lstrip()]
3429

@@ -68,7 +63,7 @@ def get_view_description(cls, html=False):
6863
Return a description for an `APIView` class or `@api_view` function.
6964
"""
7065
description = cls.__doc__ or ''
71-
description = _remove_leading_indent(description)
66+
description = _remove_leading_indent(smart_text(description))
7267
if html:
7368
return markup_description(description)
7469
return description

0 commit comments

Comments
 (0)