File indexing completed on 2024-04-21 04:44:10

0001 # -*- coding: utf-8 -*-
0002 #     Copyright 2009 Simon Edwards <simon@simonzone.com>
0003 #
0004 # This program is free software; you can redistribute it and/or modify
0005 # it under the terms of the GNU General Public License as published by
0006 # the Free Software Foundation; either version 2 of the License, or
0007 # (at your option) any later version.
0008 #
0009 # This program is distributed in the hope that it will be useful,
0010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012 # GNU General Public License for more details.
0013 #
0014 # You should have received a copy of the GNU General Public License
0015 # along with this program; if not, write to the
0016 # Free Software Foundation, Inc.,
0017 # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018 
0019 import unittest
0020 import sipparser
0021 import sipsymboldata
0022 import sipmerger
0023 
0024 class TestSipMerger(unittest.TestCase):
0025     def merge(self, primarySipText, updateSipText):
0026         parser = sipparser.SipParser()
0027         syms = sipsymboldata.SymbolData()
0028         primaryScope = parser.parse(syms,primarySipText)
0029         updateScope = parser.parse(syms,updateSipText)
0030         
0031         print("Primary Sip -------------------------------------")
0032         print(primaryScope.format())
0033         print("Update Sip -------------------------------------")
0034         print(updateScope.format())
0035         
0036         sipmerger.MergeSipScope(syms,primaryScope,updateScope)
0037         
0038         print("Result --------------------------------------")
0039         print(primaryScope.format())
0040         return primaryScope
0041         
0042     def testNewFunction(self):
0043         newScope = self.merge("""
0044 void foo();
0045         ""","""
0046 void foo();
0047 void bar();
0048         """)
0049         self.assertTrue(len(newScope)==3)
0050         
0051     def testDeleteFunction(self):
0052         newScope = self.merge("""
0053 void foo();
0054 void bar();
0055         ""","""
0056 void foo();
0057         """)
0058         self.assertTrue(len(newScope)==2)
0059         
0060     def testFunctionIgnore(self):
0061         newScope = self.merge("""
0062 //ig void foo();
0063         ""","""
0064 void foo();
0065         """)
0066         fooFunc = newScope[1]
0067         self.assertTrue(fooFunc.name()=="foo")
0068         self.assertTrue(fooFunc.ignore())
0069         
0070     def testFunctionIgnore2(self):
0071         newScope = self.merge("""
0072 //ig void foo();
0073 void foo(int x);
0074         ""","""
0075 void foo();
0076 void foo(int x);
0077         """)
0078         fooFunc = newScope[1]
0079         self.assertTrue(fooFunc.name()=="foo")
0080         self.assertTrue(fooFunc.ignore())
0081         fooFunc = newScope[2]
0082         self.assertTrue(fooFunc.name()=="foo")
0083         self.assertTrue(not fooFunc.ignore())
0084         
0085     def testFunctionAnnotations(self):
0086         newScope = self.merge("""
0087 void foo() /PyName=Foozor/;
0088         ""","""
0089 void foo() /Factory/;
0090         """)
0091         fooFunc = newScope[1]
0092         self.assertTrue(len(fooFunc.annotations())==2)
0093 
0094     def testFunctionForce(self):
0095         newScope = self.merge("""
0096 //force
0097 void foo();
0098 //end
0099         ""","""
0100 void bar();
0101         """)
0102         fooFunc = newScope[1]
0103         self.assertTrue(fooFunc.force())
0104 
0105     def testFunctionMethodCode(self):
0106         newScope = self.merge("""
0107 void foo();
0108 %MethodCode
0109 // Method code goes here.
0110 %End
0111         ""","""
0112 void foo();
0113         """)
0114         fooFunc = newScope[1]
0115         self.assertTrue(len(fooFunc.blocks())==1)
0116 
0117     def testFunctionArguments(self):
0118         newScope = self.merge("""
0119 void foo(int &x /Out/);
0120         ""","""
0121 void foo(int &x);
0122         """)
0123         fooFunc = newScope[1]
0124         #self.assertTrue(len(fooFunc.blocks())==1)
0125 
0126     def testConstructor(self):
0127         newScope = self.merge("""
0128 class Foo {
0129     Foo() /Default/;
0130 };
0131         ""","""
0132 class Foo {
0133     Foo();
0134 };
0135         """)
0136         fooCtor = newScope[1][0]
0137         self.assertTrue(len(fooCtor.annotations())==1)
0138 
0139     def testClass(self):
0140         newScope = self.merge("""
0141 class Foo /Abstract/ {
0142 };
0143         ""","""
0144 class Foo {
0145 };
0146         """)
0147         fooClass = newScope[1]
0148         self.assertTrue(len(fooClass.annotations())==1)
0149 
0150     def testClassFunction(self):
0151         newScope = self.merge("""
0152 class Foo {
0153   void bar() /Factory/;
0154 };
0155         ""","""
0156 class Foo {
0157   void bar();
0158 };
0159         """)
0160         fooCtor = newScope[1][0]
0161         self.assertTrue(len(fooCtor.annotations())==1)
0162 
0163     def testClassDestructor(self):
0164         newScope = self.merge("""
0165 class Foo {
0166   Foo();
0167   ~Foo();
0168 };
0169         ""","""
0170 class Foo {
0171   Foo();
0172   ~Foo();
0173 };
0174         """)
0175         self.assertTrue(len(newScope[1])==2)
0176 
0177     def testNamespace(self):
0178         newScope = self.merge("""
0179 namespace Foo {
0180   void bar() /Factory/;
0181 };
0182         ""","""
0183 namespace Foo {
0184   void bar();
0185 };
0186         """)
0187 
0188     def testForceFunction(self):
0189         newScope = self.merge("""
0190 void foo();
0191 //force
0192 void bar();
0193 //end
0194         ""","""
0195 void foo();
0196         """)
0197 
0198     def testEnum(self):
0199         newScope = self.merge("""
0200 enum Foo {
0201    bar,
0202    zyzz
0203 };
0204         ""","""
0205 enum Foo {
0206    bar,
0207    zyzz,
0208    fooz
0209 };
0210         """)
0211 
0212     def testEnum2(self):
0213         newScope = self.merge("""
0214 enum Foo {
0215    bar,
0216    zyzz
0217 };
0218         ""","""
0219 enum Barz {
0220    bar,
0221    zyzz,
0222    fooz
0223 };
0224         """)
0225 
0226     def testOptionalArguments(self):
0227         newScope = self.merge("""
0228 //ig static void invokeTerminal (const QString& command, const QByteArray& startup_id = "");
0229         ""","""
0230 static void invokeTerminal (const QString& command, const QString& workdir = QString(), const QByteArray& startup_id = "");        
0231         """)
0232 
0233 if __name__ == '__main__':
0234     unittest.main()