Skip to content

Commit

Permalink
100% coverage for codec.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jul 26, 2018
1 parent a29d66a commit 81bad89
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/zope/mimetype/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from __future__ import absolute_import

import codecs
import os
Expand Down Expand Up @@ -70,7 +71,7 @@ def addCharset(encoding, name, preferred=False):

def initialize(_context):
# if any ICodec has been registered, we're done:
for unused in component.getUtilitiesFor(ICodec):
for _ in component.getUtilitiesFor(ICodec):
return
_names = []
_codecs = {}
Expand Down Expand Up @@ -106,7 +107,7 @@ def findPyCodecs(self):

type, name, preferred = m.groups()
if type == "Name":
if name in _codecs:
if name in _codecs: # pragma: no cover (its our datafile, this shouldn't happen)
raise ValueError("codec %s already exists" % name)
_names.append(name)
lastname = name
Expand All @@ -115,10 +116,10 @@ def findPyCodecs(self):
_codecs[name].preferred_alias = name.lower()

elif type == "Alias" and name != "None":
if not lastname:
if not lastname: # pragma: no cover (its our datafile, this shouldn't happen)
raise ValueError("Parsing failed. Alias found without a name.")
name = name.lower()
if name in _aliases:
if name in _aliases: # pragma: no cover (its our datafile, this shouldn't happen)
raise ValueError("Alias %s already exists." % name)
codec = _codecs[lastname]
codec.aliases.append(name)
Expand Down
38 changes: 38 additions & 0 deletions src/zope/mimetype/tests/test_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
# -*- coding: utf-8 -*-
"""
Tests for codec.py.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import unittest

from zope import component
from zope.testing import cleanup

class TestCodecInitialize(cleanup.CleanUp,
unittest.TestCase):

def test_does_nothing_if_codec_registered(self):
from zope.mimetype.interfaces import ICodec
from zope.mimetype import codec

component.provideUtility(self, ICodec)
codec.initialize(None)

self.assertEqual(list(component.getUtilitiesFor(ICodec)),
[('', self)])

0 comments on commit 81bad89

Please sign in to comment.