mirror of https://github.com/kivy/pyjnius.git
Add test for Java constructor
This commit is contained in:
parent
83a9a00cf3
commit
62a8e10dbc
|
@ -0,0 +1,23 @@
|
||||||
|
package org.jnius;
|
||||||
|
import java.lang.Character;
|
||||||
|
|
||||||
|
|
||||||
|
public class ConstructorTest {
|
||||||
|
public int ret;
|
||||||
|
|
||||||
|
public ConstructorTest() {
|
||||||
|
ret = 753;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstructorTest(int cret) {
|
||||||
|
ret = cret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstructorTest(char charet) {
|
||||||
|
ret = (int) charet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstructorTest(int cret, char charet) {
|
||||||
|
ret = cret + (int) charet;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
'''
|
||||||
|
Test creating an instance of a Java class and fetching its values.
|
||||||
|
'''
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
import unittest
|
||||||
|
from jnius import autoclass, JavaException
|
||||||
|
|
||||||
|
|
||||||
|
class TestConstructor(unittest.TestCase):
|
||||||
|
'''
|
||||||
|
TestCase for using constructors with PyJNIus.
|
||||||
|
'''
|
||||||
|
|
||||||
|
def test_constructor_none(self):
|
||||||
|
'''
|
||||||
|
Empty constructor, baked value in the .java file.
|
||||||
|
'''
|
||||||
|
|
||||||
|
ConstructorTest = autoclass('org.jnius.ConstructorTest')
|
||||||
|
inst = ConstructorTest()
|
||||||
|
self.assertEqual(inst.ret, 753)
|
||||||
|
self.assertTrue(ConstructorTest.getClass().isInstance(inst))
|
||||||
|
|
||||||
|
def test_constructor_int(self):
|
||||||
|
'''
|
||||||
|
Constructor expecting int and setting it as public 'ret' property.
|
||||||
|
'''
|
||||||
|
|
||||||
|
ConstructorTest = autoclass('org.jnius.ConstructorTest')
|
||||||
|
inst = ConstructorTest(123)
|
||||||
|
self.assertEqual(inst.ret, 123)
|
||||||
|
self.assertTrue(ConstructorTest.getClass().isInstance(inst))
|
||||||
|
|
||||||
|
def test_constructor_string(self):
|
||||||
|
'''
|
||||||
|
Constructor expecting char, casting it to int and setting it
|
||||||
|
as public 'ret' property.
|
||||||
|
'''
|
||||||
|
|
||||||
|
ConstructorTest = autoclass('org.jnius.ConstructorTest')
|
||||||
|
inst = ConstructorTest('a')
|
||||||
|
self.assertEqual(inst.ret, ord('a'))
|
||||||
|
self.assertTrue(ConstructorTest.getClass().isInstance(inst))
|
||||||
|
|
||||||
|
def test_constructor_int_string(self):
|
||||||
|
'''
|
||||||
|
Constructor expecting int and char, casting char to int and summing it
|
||||||
|
as public 'ret' property.
|
||||||
|
'''
|
||||||
|
|
||||||
|
ConstructorTest = autoclass('org.jnius.ConstructorTest')
|
||||||
|
inst = ConstructorTest(3, 'a')
|
||||||
|
self.assertEqual(inst.ret, 3 + ord('a'))
|
||||||
|
self.assertTrue(ConstructorTest.getClass().isInstance(inst))
|
||||||
|
|
||||||
|
def test_constructor_exception(self):
|
||||||
|
'''
|
||||||
|
No such constructor found (no such signature),
|
||||||
|
should raise JavaException.
|
||||||
|
'''
|
||||||
|
|
||||||
|
ConstructorTest = autoclass('org.jnius.ConstructorTest')
|
||||||
|
with self.assertRaises(JavaException):
|
||||||
|
ConstructorTest('1.', 2.0, False)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue