[Lldb-commits] [lldb] r111572 - in /lldb/trunk/test: lldbtest.py unsigned_types/TestUnsignedTypes.py

Johnny Chen johnny.chen at apple.com
Thu Aug 19 16:26:59 PDT 2010


Author: johnny
Date: Thu Aug 19 18:26:59 2010
New Revision: 111572

URL: http://llvm.org/viewvc/llvm-project?rev=111572&view=rev
Log:
Abstracted the running of command through the command interpreter and checking
its return status into lldbtest.TestBase.runCmd(); and runCmd() in combination
with checking the output against matching substrings (including startswith) into
lldbtest.TestBase.expect().

TestUnsignedTypes.py is refactored to use the abstracted APIs.  Other test cases
to be modified later.

Modified:
    lldb/trunk/test/lldbtest.py
    lldb/trunk/test/unsigned_types/TestUnsignedTypes.py

Modified: lldb/trunk/test/lldbtest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbtest.py?rev=111572&r1=111571&r2=111572&view=diff
==============================================================================
--- lldb/trunk/test/lldbtest.py (original)
+++ lldb/trunk/test/lldbtest.py Thu Aug 19 18:26:59 2010
@@ -104,7 +104,6 @@
         # And the result object.
         self.res = lldb.SBCommandReturnObject()
 
-
     def tearDown(self):
         # Finish the inferior process, if it was "run" previously.
         if self.runStarted:
@@ -114,3 +113,43 @@
 
         # Restore old working directory.
         os.chdir(self.oldcwd)
+
+    def runCmd(self, cmd, msg=None, check=True):
+        """
+        Ask the command interpreter to handle the command and then check its
+        return status.
+        """
+        # Fail fast if 'cmd' is not meaningful.
+        if not cmd or len(cmd) == 0:
+            raise Exception("Bad 'cmd' parameter encountered")
+        self.ci.HandleCommand(cmd, self.res)
+        if cmd == "run":
+            self.runStarted = True
+        if check:
+            self.assertTrue(self.res.Succeeded(),
+                            msg if msg else CMD_MSG(cmd))
+
+    def expect(self, cmd, msg, startstr=None, substrs=None):
+        """
+        Similar to runCmd; with additional expect style output matching ability.
+
+        Ask the command interpreter to handle the command and then check its
+        return status.  The 'msg' parameter specifies an informational assert
+        message.  We expect the output from running the command to start with
+        'startstr' and matches the substrings contained in 'substrs'.
+        """
+        # Fail fast if 'msg' is not meaningful.
+        if not msg or len(msg) == 0:
+            raise Exception("Bad 'msg' parameter encountered")
+        self.runCmd(cmd)
+
+        output = self.res.GetOutput()
+        matched = output.startswith(startstr) if startstr else True
+        if substrs:
+            for str in substrs:
+                matched = output.find(str) > 0
+                if not matched:
+                    break
+
+        self.assertTrue(matched, msg)
+

Modified: lldb/trunk/test/unsigned_types/TestUnsignedTypes.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/unsigned_types/TestUnsignedTypes.py?rev=111572&r1=111571&r2=111572&view=diff
==============================================================================
--- lldb/trunk/test/unsigned_types/TestUnsignedTypes.py (original)
+++ lldb/trunk/test/unsigned_types/TestUnsignedTypes.py Thu Aug 19 18:26:59 2010
@@ -14,58 +14,31 @@
 
     def test_unsigned_types(self):
         """Test that variables with unsigned types display correctly."""
-        res = self.res
         exe = os.path.join(os.getcwd(), "a.out")
-        self.ci.HandleCommand("file " + exe, res)
-        self.assertTrue(res.Succeeded(), CURRENT_EXECUTABLE_SET)
+        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
 
         # Break on line 19 in main() aftre the variables are assigned values.
-        self.ci.HandleCommand("breakpoint set -f main.cpp -l 19", res)
-        self.assertTrue(res.Succeeded(), CMD_MSG('breakpoint set'))
-        self.assertTrue(res.GetOutput().startswith(
-            "Breakpoint created: 1: file ='main.cpp', line = 19, locations = 1"
-            ),
-                        BREAKPOINT_CREATED)
-
-        self.ci.HandleCommand("run", res)
-        self.runStarted = True
-        self.assertTrue(res.Succeeded(), RUN_STOPPED)
+        self.expect("breakpoint set -f main.cpp -l 19", BREAKPOINT_CREATED,
+            startstr = "Breakpoint created: 1: file ='main.cpp', line = 19, locations = 1")
+
+        self.runCmd("run", RUN_STOPPED)
 
         # The stop reason of the thread should be breakpoint.
-        self.ci.HandleCommand("thread list", res)
-        self.assertTrue(res.Succeeded(), CMD_MSG('thread list'))
-        self.assertTrue(res.GetOutput().find('state is Stopped') > 0 and
-                        res.GetOutput().find('stop reason = breakpoint') > 0,
-                        STOPPED_DUE_TO_BREAKPOINT)
+        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
+            substrs = ['state is Stopped', 'stop reason = breakpoint'])
 
         # The breakpoint should have a hit count of 1.
-        self.ci.HandleCommand("breakpoint list", res)
-        self.assertTrue(res.Succeeded(), CMD_MSG('breakpoint list'))
-        self.assertTrue(res.GetOutput().find(' resolved, hit count = 1') > 0,
-                        BREAKPOINT_HIT_ONCE)
+        self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
+            substrs = [' resolved, hit count = 1'])
 
         # Test that unsigned types display correctly.
-        self.ci.HandleCommand("variable list -a", res)
-        #print "variable list -a ->", res.GetOutput()
-        self.assertTrue(res.Succeeded(), CMD_MSG('variable list -a'))
-        output = res.GetOutput()
-        self.assertTrue(
-            output.startswith("the_unsigned_char = (unsigned char) 'c'")
-            and
-            output.find("the_unsigned_short = (short unsigned int) 0x0063") > 0
-            and
-            output.find("the_unsigned_int = (unsigned int) 0x00000063") > 0
-            and
-            output.find("the_unsigned_long = (long unsigned int) "
-                        "0x0000000000000063") > 0
-            and
-            output.find("the_unsigned_long_long = (long long unsigned int)"
-                        " 0x0000000000000063") > 0
-            and
-            output.find("the_uint32 = (uint32_t) 0x00000063"),
-
-            VARIABLES_DISPLAYED_CORRECTLY
-            )
+        self.expect("variable list -a", VARIABLES_DISPLAYED_CORRECTLY,
+            startstr = "the_unsigned_char = (unsigned char) 'c'",
+            substrs = ["the_unsigned_short = (short unsigned int) 0x0063",
+                       "the_unsigned_int = (unsigned int) 0x00000063",
+                       "the_unsigned_long = (long unsigned int) 0x0000000000000063",
+                       "the_unsigned_long_long = (long long unsigned int) 0x0000000000000063",
+                       "the_uint32 = (uint32_t) 0x00000063"])
 
 
 if __name__ == '__main__':





More information about the lldb-commits mailing list