mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 00:55:19 +00:00
Add some unit tests, raise error on non-string types
This commit is contained in:
parent
35ed8be7ae
commit
f6d00845bb
@ -5733,6 +5733,9 @@ def _encode_string(value):
|
||||
if value is None:
|
||||
return encoded_null
|
||||
else:
|
||||
# Should we raise an error here, or attempt to cast to a string
|
||||
if not isinstance(value, six.text_type):
|
||||
raise TypeError('Value "%s" is not a string type' % repr(value))
|
||||
return b''.join([value.encode('utf-16-le'), encoded_null])
|
||||
|
||||
|
||||
|
55
tests/unit/modules/test_win_lgpo.py
Normal file
55
tests/unit/modules/test_win_lgpo.py
Normal file
@ -0,0 +1,55 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
:codeauthor: Shane Lee <slee@saltstack.com>
|
||||
'''
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import, unicode_literals, print_function
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
# Import Salt Libs
|
||||
import salt.modules.win_lgpo as win_lgpo
|
||||
|
||||
|
||||
class WinSystemTestCase(TestCase):
|
||||
'''
|
||||
Test cases for salt.modules.win_lgpo
|
||||
'''
|
||||
encoded_null = chr(0).encode('utf-16-le')
|
||||
|
||||
def test__encode_string(self):
|
||||
'''
|
||||
``_encode_string`` should return a null terminated ``utf-16-le`` encoded
|
||||
string when a string value is passed
|
||||
'''
|
||||
encoded_value = b''.join(['Salt is awesome'.encode('utf-16-le'),
|
||||
self.encoded_null])
|
||||
value = win_lgpo._encode_string('Salt is awesome')
|
||||
self.assertEqual(value, encoded_value)
|
||||
|
||||
def test__encode_string_empty_string(self):
|
||||
'''
|
||||
``_encode_string`` should return an encoded null when an empty string
|
||||
value is passed
|
||||
'''
|
||||
value = win_lgpo._encode_string('')
|
||||
self.assertEqual(value, self.encoded_null)
|
||||
|
||||
def test__encode_string_error(self):
|
||||
'''
|
||||
``_encode_string`` should raise an error if a non-string value is passed
|
||||
'''
|
||||
self.assertRaises(TypeError, win_lgpo._encode_string, [1])
|
||||
test_list = ['item1', 'item2']
|
||||
self.assertRaises(TypeError, win_lgpo._encode_string, [test_list])
|
||||
test_dict = {'key1': 'value1', 'key2': 'value2'}
|
||||
self.assertRaises(TypeError, win_lgpo._encode_string, [test_dict])
|
||||
|
||||
def test__encode_string_none(self):
|
||||
'''
|
||||
``_encode_string`` should return an encoded null when ``None`` is passed
|
||||
'''
|
||||
value = win_lgpo._encode_string(None)
|
||||
self.assertEqual(value, self.encoded_null)
|
Loading…
Reference in New Issue
Block a user