~kubuntu-members/korundum/4.11

« back to all changes in this revision

Viewing changes to qtruby/modules/qttest/qttest.rb

  • Committer: Ian Monroe
  • Date: 2010-11-21 15:55:01 UTC
  • Revision ID: git-v1:c37670e4e3c59f5eb2ba112f5341a5e706217f6f
Split up Smoke into Qt and KDE directories. 
Move libsmoke stuff into the generator directory
Split up Ruby into qtruby and korundum directories

svn path=/trunk/KDE/kdebindings/ruby/; revision=1199320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=begin
 
2
/***************************************************************************
 
3
                          qttest.rb  -  QtTest ruby client lib
 
4
                             -------------------
 
5
    begin                : 29-10-2008
 
6
    copyright            : (C) 2008 by Richard Dale
 
7
    email                : richard.j.dale@gmail.com
 
8
 ***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
=end
 
19
 
 
20
module QtTest
 
21
  module Internal
 
22
    def self.init_all_classes
 
23
      getClassList.each do |c|
 
24
        classname = Qt::Internal::normalize_classname(c)
 
25
        id = Qt::Internal::findClass(c);
 
26
        Qt::Internal::insert_pclassid(classname, id)
 
27
        Qt::Internal::cpp_names[classname] = c
 
28
        klass = Qt::Internal::isQObject(c) ? Qt::Internal::create_qobject_class(classname, Qt)  : Qt::Internal::create_qt_class(classname, Qt)
 
29
        Qt::Internal::classes[classname] = klass unless klass.nil?
 
30
      end
 
31
    end
 
32
  end
 
33
end
 
34
 
 
35
module Qt
 
36
  class Test < Base
 
37
      
 
38
    # This is the isValidSlot() function in testlib/qtestcase.cpp translated
 
39
    # to Ruby. Probably could be a bit shorter in Ruby..
 
40
    def self.validSlot?(sl)
 
41
      if sl.access != Qt::MetaMethod::Private || !sl.parameterTypes.empty? ||
 
42
         sl.typeName != "" || sl.methodType != Qt::MetaMethod::Slot
 
43
        return false
 
44
      end
 
45
      
 
46
      sig = sl.signature
 
47
      len = sig.length
 
48
      
 
49
      if len < 2
 
50
        return false
 
51
      end
 
52
      
 
53
      if sig[len - 2, 1] != '(' || sig[len - 1, 1] != ')'
 
54
        return false
 
55
      end
 
56
      
 
57
      if len > 7 && sig[len - 7, len] == "_data()"
 
58
        return false
 
59
      end
 
60
      
 
61
      if sig == "initTestCase()" || sig == "cleanupTestCase()" ||
 
62
         sig == "cleanup()" || sig == "init()"
 
63
        return false
 
64
      end
 
65
 
 
66
      return true
 
67
    end
 
68
    
 
69
    def self.current_binding
 
70
        @@current_binding
 
71
    end
 
72
    
 
73
    def self.current_binding=(b)
 
74
        @@current_binding = b
 
75
    end
 
76
    
 
77
    def self.qExec(*args)
 
78
      test_functions = []
 
79
      meta = args[0].metaObject
 
80
      className = meta.className
 
81
      for i in 0...meta.methodCount
 
82
        sl = meta.method(i)
 
83
        if Test.validSlot?(sl)
 
84
            test_functions << sl.signature.sub("()", "").to_sym
 
85
        end
 
86
      end
 
87
      
 
88
      # Trap calls to the test functions and save their binding, so that
 
89
      # the various test methods, like QVERIFY(), can evaluate the code strings
 
90
      # passed to them in the context of the test function
 
91
      trace_func = set_trace_func proc { |event, file, line, id, binding, klass|
 
92
        if event == 'call' && klass.name == className && test_functions.include?(id)
 
93
          Test.current_binding = binding
 
94
        end
 
95
      }
 
96
      
 
97
      if args.length == 2 && args[1].kind_of?(Array)
 
98
        super(args[0], args[1].length + 1, [$0] + args[1])
 
99
      else
 
100
        super(*args)
 
101
      end
 
102
      
 
103
      set_trace_func(trace_func)
 
104
    end
 
105
  end 
 
106
 
 
107
  class Base
 
108
    def QVERIFY(statement)
 
109
      file, line = caller(1)[0].split(':')
 
110
      if !Qt::Test.qVerify(eval(statement, Qt::Test.current_binding), statement, "", file, line.to_i)
 
111
          return eval('return', Qt::Test.current_binding)
 
112
      end
 
113
    end
 
114
    
 
115
    def QFAIL(message)
 
116
      file, line = caller(1)[0].split(':')
 
117
      Qt::Test.qFail(message, file, line.to_i)
 
118
      return eval('return', Qt::Test.current_binding)
 
119
    end
 
120
    
 
121
    def QVERIFY2(statement, description)
 
122
      file, line = caller(1)[0].split(':')
 
123
      if eval(statement, Qt::Test.current_binding)
 
124
        if !Qt::Test.qVerify(true, statement, description, file, line.to_i)
 
125
          return eval('return', Qt::Test.current_binding)
 
126
        end
 
127
      else
 
128
        if !Qt::Test.qVerify(false, statement, description, file, line.to_i)
 
129
          return eval('return', Qt::Test.current_binding)
 
130
        end
 
131
      end
 
132
    end
 
133
    
 
134
    def QCOMPARE(actual, expected)
 
135
      file, line = caller(1)[0].split(':')
 
136
      if !Qt::Test.qCompare(eval(actual, Qt::Test.current_binding), eval(expected, Qt::Test.current_binding), actual, expected, file, line.to_i)    
 
137
        return eval('return', Qt::Test.current_binding)
 
138
      end
 
139
    end
 
140
    
 
141
    def QSKIP(statement, mode)
 
142
      file, line = caller(1)[0].split(':')
 
143
      Qt::Test.qSkip(statement, mode, file, line.to_i)
 
144
      return eval('return', Qt::Test.current_binding)
 
145
    end
 
146
    
 
147
    def QEXPECT_FAIL(dataIndex, comment, mode)
 
148
      file, line = caller(1)[0].split(':')
 
149
      if !Qt::Test.qExpectFail(dataIndex, comment, mode, file, line.to_i)
 
150
        return eval('return', Qt::Test.current_binding)
 
151
      end
 
152
    end
 
153
  
 
154
    def QTEST(actual, testElement)
 
155
      file, line = caller(1)[0].split(':')
 
156
      if !Qt::Test.qTest(eval(actual, Qt::Test.current_binding), eval(testElement, Qt::Test.current_binding), actual, testElement, file, line.to_i)
 
157
        return eval('return', Qt::Test.current_binding)
 
158
      end
 
159
    end
 
160
    
 
161
    def QWARN(msg)
 
162
      Qt::Test.qWarn(msg)
 
163
    end
 
164
  end
 
165
end
 
166