File: DescriptorExposerTest.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (240 lines) | stat: -rw-r--r-- 8,046 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.python.expose.generate;

import org.objectweb.asm.Type;
import org.python.core.BytecodeLoader;
import org.python.core.Py;
import org.python.core.PyDataDescr;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.expose.ExposedDelete;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;

public class DescriptorExposerTest extends InterpTestCase implements PyTypes {

    private static final String RETURN_STRING_DESCRIPTOR = Type.getMethodDescriptor(STRING,
                                                                                    new Type[0]);

    private static final PyType PY_TYPE = PyType.fromClass(DescExposed.class);

    private static final Type ASM_TYPE = Type.getType(DescExposed.class);

    private DescExposed se = new DescExposed();

    private static final String NEW_VAL = "This is not what was there before";

    interface DescSetup {

        void setup(DescriptorExposer de);
    }

    public PyDataDescr makeDescriptor(DescSetup setup, String name) throws Exception {
        DescriptorExposer de = new DescriptorExposer(ASM_TYPE, name);
        setup.setup(de);
        Class descriptor = de.load(new BytecodeLoader.Loader());
        PyDataDescr descr = (PyDataDescr)descriptor.newInstance();
        descr.setType(PY_TYPE);
        return descr;
    }

    public PyDataDescr makeDescriptor(DescSetup setup) throws Exception {
        return makeDescriptor(setup, "desc");
    }

    public void testNoDescriptors() throws Exception {
        DescriptorExposer de = new DescriptorExposer(ASM_TYPE, "desc");
        try {
            de.load(new BytecodeLoader.Loader());
            fail("Should not be able to generate a descriptor with no getter");
        } catch(InvalidExposingException ise) {
            // Should be thrown when a getter isn't added.
        }
    }

    public void testMethodGetter() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addMethodGetter("toString", RETURN_STRING_DESCRIPTOR);
            }
        });
        assertEquals(SimpleExposed.TO_STRING_RETURN, instance.__get__(se, PY_TYPE).toString());
        assertFalse(instance.implementsDescrSet());
        assertFalse(instance.implementsDescrDelete());
    }

    public void testFieldGetter() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("toStringVal", STRING);
            }
        });
        assertEquals(SimpleExposed.TO_STRING_RETURN, instance.__get__(new DescExposed(), PY_TYPE)
                .toString());
        assertFalse(instance.implementsDescrSet());
        assertFalse(instance.implementsDescrDelete());
    }
    
    public void testNullReturns() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("nullString", STRING);
            }
        });
        assertEquals(Py.None, instance.__get__(se, PY_TYPE));
    }

    public void testMethodSetter() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addMethodGetter("toString", RETURN_STRING_DESCRIPTOR);
                de.addMethodSetter("setToString", "(Ljava/lang/String;)V");
            }
        });
        instance.__set__(se, Py.newString(NEW_VAL));
        assertEquals(NEW_VAL, se.toString());
        assertTrue(instance.implementsDescrSet());
        assertFalse(instance.implementsDescrDelete());
    }

    public void testFieldSetter() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("toStringVal", STRING);
                de.addFieldSetter("toStringVal", STRING);
            }
        });
        instance.__set__(se, Py.newString(NEW_VAL));
        assertEquals(NEW_VAL, se.toString());
        assertTrue(instance.implementsDescrSet());
        assertFalse(instance.implementsDescrDelete());
    }

    public void testMethodDel() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addMethodGetter("toString", RETURN_STRING_DESCRIPTOR);
                de.addMethodDeleter("deleteToString", "()V");
            }
        });
        instance.__delete__(se);
        assertNull(se.toString());
        assertTrue(instance.implementsDescrDelete());
    }

    public void testInt() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("i", INT);
                de.addFieldSetter("i", INT);
            }
        }, "i");
        assertEquals(7, instance.__get__(se, PY_TYPE).asInt());
        instance.__set__(se, Py.newInteger(12));
        assertEquals(12, instance.__get__(se, PY_TYPE).asInt());
    }

    public void testByte() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("b", BYTE);
                de.addMethodSetter("setB", "(B)");
            }
        }, "b");
        assertEquals(0, instance.__get__(se, PY_TYPE).asInt());
        instance.__set__(se, Py.newInteger(-1));
        assertEquals(-1, instance.__get__(se, PY_TYPE).asInt());
    }

    public void testLong() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addMethodGetter("l", "()J");
                de.addFieldSetter("l", Type.LONG_TYPE);
            }
        }, "l");
        assertEquals(0, instance.__get__(se, PY_TYPE).asInt());
        instance.__set__(se, Py.newInteger(12));
        assertEquals(12, instance.__get__(se, PY_TYPE).asInt());
    }

    public void testDouble() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addMethodGetter("getD", "()D");
                de.addMethodSetter("setD", "(D)");
            }
        }, "d");
        assertEquals(98.7, Py.py2double(instance.__get__(se, PY_TYPE)));
        instance.__set__(se, Py.newInteger(12));
        assertEquals(12.0, Py.py2double(instance.__get__(se, PY_TYPE)));
    }

    public void testBool() throws Exception {
        PyDataDescr instance = makeDescriptor(new DescSetup() {

            public void setup(DescriptorExposer de) {
                de.addFieldGetter("bool", BOOLEAN);
                de.addFieldSetter("bool", BOOLEAN);
            }
        }, "bool");
        assertEquals(false, Py.py2boolean(instance.__get__(se, PY_TYPE)));
        instance.__set__(se, Py.True);
        assertEquals(true, Py.py2boolean(instance.__get__(se, PY_TYPE)));
    }

    public class DescExposed extends PyObject {

        public void setToString(String newVal) {
            toStringVal = newVal;
        }

        public void deleteToString() {
            toStringVal = null;
        }

        public String toString() {
            return toStringVal;
        }

        public long l() {
            return l;
        }

        public void setB(byte newB) {
            b = newB;
        }

        public void setD(double d) {
            this.d = d;
        }

        public double getD() {
            return d;
        }

        public double d = 98.7;

        public int i = 7;

        public long l;

        public byte b;
        
        public boolean bool;

        public String toStringVal = SimpleExposed.TO_STRING_RETURN;
        
        public String nullString = null;
    }
}