blob: 11a7822c9220c2f04cbd069cfcd686c50a401d3c [file] [log] [blame]
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for string_utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import mock
import unittest
import common
from autotest_lib.client.common_lib.cros import string_utils
class JoinLongestWithLengthLimitTest(unittest.TestCase):
"""Unit test of join_longest_with_length_limit."""
def test_basic(self):
"""The basic test case."""
strings = ['abc', '12', 'sssss']
result = list(string_utils.join_longest_with_length_limit(
strings, 6, separator=','))
self.assertEqual(len(result), 2)
def test_short_strings(self):
"""Test with short strings to be joined with big limit."""
strings = ['abc', '12', 'sssss']
sep = mock.MagicMock()
result = list(string_utils.join_longest_with_length_limit(
strings, 100, separator=sep))
sep.join.assert_called()
def test_string_too_long(self):
"""Test with too long string to be joined."""
strings = ['abc', '12', 'sssss', 'a very long long long string']
with self.assertRaises(string_utils.StringTooLongError):
list(string_utils.join_longest_with_length_limit(strings, 6))
def test_long_sep(self):
"""Test with long seperator."""
strings = ['abc', '12', 'sssss']
result = list(string_utils.join_longest_with_length_limit(
strings, 6, separator='|very long separator|'))
# Though the string to be joined is short, we still will have 3 result
# because each of them plus separator is longer than the limit.
self.assertEqual(len(result), 3)
if __name__ == '__main__':
unittest.main()