1#!/usr/bin/python2 2# -*- coding: utf-8 -*- 3# Copyright 2018 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6"""Tests for string_utils.""" 7 8from __future__ import absolute_import 9from __future__ import division 10from __future__ import print_function 11 12import mock 13import unittest 14 15import common 16from autotest_lib.client.common_lib.cros import string_utils 17 18 19class JoinLongestWithLengthLimitTest(unittest.TestCase): 20 """Unit test of join_longest_with_length_limit.""" 21 def setUp(self): 22 """Setup.""" 23 self.strings = ['abc', '12', 'sssss'] 24 25 def test_basic(self): 26 """The basic test case.""" 27 result = list(string_utils.join_longest_with_length_limit( 28 self.strings, 6, separator=',')) 29 self.assertEqual(len(result), 2) 30 self.assertTrue(type(result[0]) is str) 31 32 def test_short_strings(self): 33 """Test with short strings to be joined with big limit.""" 34 sep = mock.MagicMock() 35 result = list(string_utils.join_longest_with_length_limit( 36 self.strings, 100, separator=sep)) 37 sep.join.assert_called_once() 38 39 def test_string_too_long(self): 40 """Test with too long string to be joined.""" 41 strings = ['abc', '12', 'sssss', 'a very long long long string'] 42 with self.assertRaises(string_utils.StringTooLongError): 43 list(string_utils.join_longest_with_length_limit(strings, 6)) 44 45 def test_long_sep(self): 46 """Test with long seperator.""" 47 result = list(string_utils.join_longest_with_length_limit( 48 self.strings, 6, separator='|very long separator|')) 49 # Though the string to be joined is short, we still will have 3 result 50 # because each of them plus separator is longer than the limit. 51 self.assertEqual(len(result), 3) 52 53 def test_do_not_join(self): 54 """Test yielding list instead of string.""" 55 result = list(string_utils.join_longest_with_length_limit( 56 self.strings, 6, separator=',', do_join=False)) 57 self.assertEqual(len(result), 2) 58 self.assertTrue(type(result[0]) is list) 59 60 61if __name__ == '__main__': 62 unittest.main() 63