[Kde-bindings] KDE/kdebindings/ruby/qttest

Richard Dale Richard_Dale at tipitina.demon.co.uk
Mon Nov 16 12:04:22 UTC 2009


SVN commit 1050017 by rdale:

* The code strings for methods like QVERIFY are now evaluated in the context
of the test function that they are in. A trace_func_proc is used to trap each
call to a test function slot, and the binding is then saved as 
'current_binding' in the Qt::Test class.
* If a method like QVERIFY() fails, the rest of the code in the test function
isn't executed.
* Added an example 'myfirsttest.rb' converted from the C++ version by Jan Pilz.

CCMAIL: kde-bindings at kde.org



 A             ChangeLog  
 A             examples (directory)  
 AM            examples/myfirsttest.rb  
 M  +113 -5    qttest.rb  


** trunk/KDE/kdebindings/ruby/qttest/examples/myfirsttest.rb #property svn:executable
   + *
--- trunk/KDE/kdebindings/ruby/qttest/qttest.rb #1050016:1050017
@@ -33,26 +33,134 @@
 end
 
 module Qt
+  class Test < Base
+      
+    # This is the isValidSlot() function in testlib/qtestcase.cpp translated
+    # to Ruby. Probably could be a bit shorter in Ruby..
+    def self.validSlot?(sl)
+      if sl.access != Qt::MetaMethod::Private || !sl.parameterTypes.empty? ||
+         sl.typeName != "" || sl.methodType != Qt::MetaMethod::Slot
+        return false
+      end
+      
+      sig = sl.signature
+      len = sig.length
+      
+      if len < 2
+        return false
+      end
+      
+      if sig[len - 2, 1] != '(' || sig[len - 1, 1] != ')'
+        return false
+      end
+      
+      if len > 7 && sig[len - 7, len] == "_data()"
+        return false
+      end
+      
+      if sig == "initTestCase()" || sig == "cleanupTestCase()" ||
+         sig == "cleanup()" || sig == "init()"
+        return false
+      end
+
+      return true
+    end
+    
+    def self.current_binding
+        @@current_binding
+    end
+    
+    def self.current_binding=(b)
+        @@current_binding = b
+    end
+    
+    def self.qExec(*args)
+      test_functions = []
+      meta = args[0].metaObject
+      className = meta.className
+      for i in 0...meta.methodCount
+        sl = meta.method(i)
+        if Test.validSlot?(sl)
+            test_functions << sl.signature.sub("()", "").to_sym
+        end
+      end
+      
+      # Trap calls to the test functions and save their binding, so that
+      # the various test methods, like QVERIFY(), can evaluate the code strings
+      # passed to them in the context of the test function
+      trace_func = set_trace_func proc { |event, file, line, id, binding, klass|
+        if event == 'call' && klass.name == className && test_functions.include?(id)
+          Test.current_binding = binding
+        end
+      }
+      
+      if args.length == 2 && args[1].kind_of?(Array)
+        super(args[0], args[1].length + 1, [$0] + args[1])
+      else
+        super(*args)
+      end
+      
+      set_trace_func(trace_func)
+    end
+  end 
+
   class Base
     def QVERIFY(statement)
-      Qt::Test.qVerify(eval(statement), statement, "", __FILE__, __LINE__)
+      file, line = caller(1)[0].split(':')
+      if !Qt::Test.qVerify(eval(statement, Qt::Test.current_binding), statement, "", file, line.to_i)
+          return eval('return', Qt::Test.current_binding)
+      end
     end
     
     def QFAIL(message)
-      Qt::Test.qFail(message, __FILE__, __LINE__)
+      file, line = caller(1)[0].split(':')
+      Qt::Test.qFail(message, file, line.to_i)
+      return eval('return', Qt::Test.current_binding)
     end
     
     def QVERIFY2(statement, description)
-      Qt::Test.qVerify(eval(statement), statement, description, __FILE__, __LINE__)
+      file, line = caller(1)[0].split(':')
+      if eval(statement, Qt::Test.current_binding)
+        if !Qt::Test.qVerify(true, statement, description, file, line.to_i)
+          return eval('return', Qt::Test.current_binding)
+        end
+      else
+        if !Qt::Test.qVerify(false, statement, description, file, line.to_i)
+          return eval('return', Qt::Test.current_binding)
+        end
+      end
     end
     
     def QCOMPARE(actual, expected)
-      Qt::Test.qCompare(eval(actual), eval(expected), actual, expected, __FILE__, __LINE__)    
+      file, line = caller(1)[0].split(':')
+      if !Qt::Test.qCompare(eval(actual, Qt::Test.current_binding), eval(expected, Qt::Test.current_binding), actual, expected, file, line.to_i)    
+        return eval('return', Qt::Test.current_binding)
+      end
     end
     
     def QSKIP(statement, mode)
-      Qt::Test.qSkip(statement, mode, __FILE__, __LINE__)
+      file, line = caller(1)[0].split(':')
+      Qt::Test.qSkip(statement, mode, file, line.to_i)
+      return eval('return', Qt::Test.current_binding)
     end
+    
+    def QEXPECT_FAIL(dataIndex, comment, mode)
+      file, line = caller(1)[0].split(':')
+      if !Qt::Test.qExpectFail(dataIndex, comment, mode, file, line.to_i)
+        return eval('return', Qt::Test.current_binding)
+      end
+    end
+  
+    def QTEST(actual, testElement)
+      file, line = caller(1)[0].split(':')
+      if !Qt::Test.qTest(eval(actual, Qt::Test.current_binding), eval(testElement, Qt::Test.current_binding), actual, testElement, file, line.to_i)
+        return eval('return', Qt::Test.current_binding)
+      end
+    end
+    
+    def QWARN(msg)
+      Qt::Test.qWarn(msg)
+    end
   end
 end
 



More information about the Kde-bindings mailing list