1# Copyright (C) 2023 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import textwrap 15import unittest 16from io import StringIO 17 18from clone import ModuleName 19from clone import _extract_templates_helper 20from clone import module_defs 21from clone import name_in 22from clone import type_in 23 24 25class CloneTest(unittest.TestCase): 26 def setUp(self) -> None: 27 self.bp = textwrap.dedent( 28 """\ 29 //licence text 30 alias=blah 31 cc_library { 32 out: ["dont"], 33 name: "a", 34 other: 45 35 } 36 genrule { 37 name: "b", 38 out: [ 39 "oph" 40 ] 41 other: { 42 name: 'not-a-name' 43 blah: "nested" 44 } 45 } 46 """ 47 ) 48 49 def test_module_def(self): 50 defs = list(module_defs(StringIO(self.bp))) 51 self.assertEqual(len(defs), 2) 52 name, content = defs[0] 53 self.assertEqual(name, "cc_library") 54 self.assertEqual( 55 content, 56 textwrap.dedent( 57 """\ 58 cc_library { 59 out: ["dont"], 60 name: "a", 61 other: 45 62 } 63 """ 64 ), 65 ) 66 name, content = defs[1] 67 self.assertEqual(name, "genrule") 68 self.assertEqual( 69 content, 70 textwrap.dedent( 71 """\ 72 genrule { 73 name: "b", 74 out: [ 75 "oph" 76 ] 77 other: { 78 name: 'not-a-name' 79 blah: "nested" 80 } 81 } 82 """ 83 ), 84 ) 85 86 def test_non_existent(self): 87 cloners = _extract_templates_helper(StringIO(self.bp), name_in("not-a-name")) 88 self.assertEqual(len(cloners), 0) 89 90 def test_by_type(self): 91 cloners = _extract_templates_helper(StringIO(self.bp), type_in("genrule")) 92 self.assertEqual(len(cloners), 1) 93 self.assertEqual( 94 cloners[ModuleName("b")].substitute(suffix="test"), 95 textwrap.dedent( 96 """\ 97 genrule { 98 name: "b-test", 99 out: [ 100 "oph-test" 101 ] 102 other: { 103 name: 'not-a-name' 104 blah: "nested" 105 } 106 } 107 """ 108 ), 109 ) 110 111 def test_by_name(self): 112 cloners = _extract_templates_helper(StringIO(self.bp), name_in("a", "b")) 113 self.assertEqual(len(cloners), 2) 114 self.assertEqual( 115 cloners[ModuleName("a")].substitute(suffix="test"), 116 textwrap.dedent( 117 """\ 118 cc_library { 119 out: ["dont"], 120 name: "a-test", 121 other: 45 122 } 123 """ 124 ), 125 ) 126 127 128if __name__ == "__main__": 129 unittest.main()