xref: /llvm-project/clang/tools/scan-build-py/tests/unit/test_shell.py (revision dd3c26a045c081620375a878159f536758baba6e)
1# -*- coding: utf-8 -*-
2# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3# See https://llvm.org/LICENSE.txt for license information.
4# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6import libscanbuild.shell as sut
7import unittest
8
9
10class ShellTest(unittest.TestCase):
11    def test_encode_decode_are_same(self):
12        def test(value):
13            self.assertEqual(sut.encode(sut.decode(value)), value)
14
15        test("")
16        test("clang")
17        test("clang this and that")
18
19    def test_decode_encode_are_same(self):
20        def test(value):
21            self.assertEqual(sut.decode(sut.encode(value)), value)
22
23        test([])
24        test(["clang"])
25        test(["clang", "this", "and", "that"])
26        test(["clang", "this and", "that"])
27        test(["clang", "it's me", "again"])
28        test(["clang", 'some "words" are', "quoted"])
29
30    def test_encode(self):
31        self.assertEqual(
32            sut.encode(["clang", "it's me", "again"]), 'clang "it\'s me" again'
33        )
34        self.assertEqual(
35            sut.encode(["clang", "it(s me", "again)"]), 'clang "it(s me" "again)"'
36        )
37        self.assertEqual(
38            sut.encode(["clang", "redirect > it"]), 'clang "redirect > it"'
39        )
40        self.assertEqual(
41            sut.encode(["clang", '-DKEY="VALUE"']), 'clang -DKEY=\\"VALUE\\"'
42        )
43        self.assertEqual(
44            sut.encode(["clang", '-DKEY="value with spaces"']),
45            'clang -DKEY=\\"value with spaces\\"',
46        )
47