blob: 19c89c5ec3602d98c2008e6c6c34042ef1a380fe [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unit tests for csv_helpers functions."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from framework import csv_helpers
14
15
16class IssueListCSVFunctionsTest(unittest.TestCase):
17
18 def testRewriteColspec(self):
19 self.assertEqual('', csv_helpers.RewriteColspec(''))
20
21 self.assertEqual('a B c', csv_helpers.RewriteColspec('a B c'))
22
23 self.assertEqual('a Summary AllLabels B Opened OpenedTimestamp c',
24 csv_helpers.RewriteColspec('a summary B opened c'))
25
26 self.assertEqual('Closed ClosedTimestamp Modified ModifiedTimestamp',
27 csv_helpers.RewriteColspec('Closed Modified'))
28
29 self.assertEqual('OwnerModified OwnerModifiedTimestamp',
30 csv_helpers.RewriteColspec('OwnerModified'))
31
32 def testReformatRowsForCSV(self):
33 # TODO(jojwang): write this test
34 pass
35
36 def testEscapeCSV(self):
37 self.assertEqual('', csv_helpers.EscapeCSV(None))
38 self.assertEqual(0, csv_helpers.EscapeCSV(0))
39 self.assertEqual('', csv_helpers.EscapeCSV(''))
40 self.assertEqual('hello', csv_helpers.EscapeCSV('hello'))
41 self.assertEqual('hello', csv_helpers.EscapeCSV(' hello '))
42
43 # Double quotes are escaped as two double quotes.
44 self.assertEqual("say 'hello'", csv_helpers.EscapeCSV("say 'hello'"))
45 self.assertEqual('say ""hello""', csv_helpers.EscapeCSV('say "hello"'))
46
47 # Things that look like formulas are prefixed with a single quote because
48 # some formula functions can have side-effects. See:
49 # https://www.contextis.com/resources/blog/comma-separated-vulnerabilities/
50 self.assertEqual("'=2+2", csv_helpers.EscapeCSV('=2+2'))
51 self.assertEqual("'=CMD| del *.*", csv_helpers.EscapeCSV('=CMD| del *.*'))
52
53 # Some spreadsheets apparently allow formula cells that start with
54 # plus, minus, and at-signs.
55 self.assertEqual("'+2+2", csv_helpers.EscapeCSV('+2+2'))
56 self.assertEqual("'-2+2", csv_helpers.EscapeCSV('-2+2'))
57 self.assertEqual("'@2+2", csv_helpers.EscapeCSV('@2+2'))
58
59 self.assertEqual(
60 u'division\xc3\xb7sign',
61 csv_helpers.EscapeCSV(u'division\xc3\xb7sign'))