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