1#!/usr/bin/env python2
2#
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from common.testing                  import ToUnicode
18from file_format.c1visualizer.parser import ParseC1visualizerStream
19from file_format.c1visualizer.struct import C1visualizerFile, C1visualizerPass
20
21import io
22import unittest
23
24class C1visualizerParser_Test(unittest.TestCase):
25
26  def createFile(self, passList):
27    """ Creates an instance of CheckerFile from provided info.
28
29    Data format: [ ( <case-name>, [ ( <text>, <assert-variant> ), ... ] ), ... ]
30    """
31    c1File = C1visualizerFile("<c1_file>")
32    for passEntry in passList:
33      passName = passEntry[0]
34      passBody = passEntry[1]
35      c1Pass = C1visualizerPass(c1File, passName, passBody, 0)
36    return c1File
37
38  def assertParsesTo(self, c1Text, expectedData):
39    expectedFile = self.createFile(expectedData)
40    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(ToUnicode(c1Text)))
41    return self.assertEqual(expectedFile, actualFile)
42
43  def test_EmptyFile(self):
44    self.assertParsesTo("", [])
45
46  def test_SingleGroup(self):
47    self.assertParsesTo(
48      """
49        begin_compilation
50          method "MyMethod"
51        end_compilation
52        begin_cfg
53          name "pass1"
54          foo
55          bar
56        end_cfg
57      """,
58      [ ( "MyMethod pass1", [ "foo", "bar" ] ) ])
59
60  def test_MultipleGroups(self):
61    self.assertParsesTo(
62      """
63        begin_compilation
64          name "xyz1"
65          method "MyMethod1"
66          date 1234
67        end_compilation
68        begin_cfg
69          name "pass1"
70          foo
71          bar
72        end_cfg
73        begin_cfg
74          name "pass2"
75          abc
76          def
77        end_cfg
78      """,
79      [ ( "MyMethod1 pass1", [ "foo", "bar" ] ),
80        ( "MyMethod1 pass2", [ "abc", "def" ] ) ])
81    self.assertParsesTo(
82      """
83        begin_compilation
84          name "xyz1"
85          method "MyMethod1"
86          date 1234
87        end_compilation
88        begin_cfg
89          name "pass1"
90          foo
91          bar
92        end_cfg
93        begin_compilation
94          name "xyz2"
95          method "MyMethod2"
96          date 5678
97        end_compilation
98        begin_cfg
99          name "pass2"
100          abc
101          def
102        end_cfg
103      """,
104      [ ( "MyMethod1 pass1", [ "foo", "bar" ] ),
105        ( "MyMethod2 pass2", [ "abc", "def" ] ) ])
106