Debian Codegraph @

rhino/testsrc/tests/lc2/JSToJava/integer-001.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'integer-001.js';
7
8 /**
9 Template for LiveConnect Tests
10
11 File Name: number-001.js
12 Description:
13
14 When setting the value of a Java field with a JavaScript number or
15 when passing a JavaScript number to a Java method as an argument,
16 LiveConnect should be able to convert the number to any one of the
17 following types:
18
19 byte
20 short
21 int
22 long
23 float
24 double
25 char
26 java.lang.Double
27
28 Note that the value of the Java field may not be as precise as the
29 JavaScript value's number, if for example, you pass a large number to
30 a short or byte, or a float to integer.
31
32 JavaScript numbers cannot be converted to instances of java.lang.Float
33 or java.lang.Integer.
34
35 This test does not cover the cases in which a Java method returns one
36 of the above primitive Java types.
37
38 Currently split up into numerous tests, since rhino live connect fails
39 to translate numbers into the correct types.
40
41 test for creating java.lang.Integer objects.
42
43
44 @author [email protected]
45 @version 1.00
46 */
47 var SECTION = "LiveConnect";
48 var VERSION = "1_3";
49 var TITLE = "LiveConnect JavaScript to Java Data Type Conversion";
50
51 var BUGNUMBER="196276";
52
53 startTest();
54 writeHeaderToLog( SECTION + " "+ TITLE);
55
56 // typeof all resulting objects is "object";
57 var E_TYPE = "object";
58
59
60 // JS class of all resulting objects is "JavaObject";
61 var E_JSCLASS = "[object JavaObject]";
62
63
64 var a = new Array();
65 var i = 0;
66
67
68 a[i++] = new TestObject( "new java.lang.Integer(\"0.\")",
69 new java.lang.Integer("0"), 0 );
70
71 a[i++] = new TestObject( "new java.lang.Integer( 5 )",
72 new java.lang.Integer(5), 5 );
73
74 a[i++] = new TestObject( "new java.lang.Integer( 5.5 )",
75 new java.lang.Integer(5.5), 5 );
76
77 a[i++] = new TestObject( "new java.lang.Integer( Number(5) )",
78 new java.lang.Integer(Number(5)), 5 );
79
80 a[i++] = new TestObject( "new java.lang.Integer( Number(5.5) )",
81 new java.lang.Integer(Number(5.5)), 5 );
82
83 for ( var i = 0; i < a.length; i++ ) {
84
85 // check typeof
86 new TestCase(
87 SECTION,
88 "typeof (" + a[i].description +")",
89 a[i].type,
90 typeof a[i].javavalue );
91 /*
92 // check the js class
93 new TestCase(
94 SECTION,
95 "("+ a[i].description +").getJSClass()",
96 E_JSCLASS,
97 a[i].jsclass );
98 */
99 // check the number value of the object
100 new TestCase(
101 SECTION,
102 "Number(" + a[i].description +")",
103 a[i].jsvalue,
104 Number( a[i].javavalue ) );
105 }
106
107 test();
108
109 function TestObject( description, javavalue, jsvalue ) {
110 this.description = description;
111 this.javavalue = javavalue;
112 this.jsvalue = jsvalue;
113 this.type = E_TYPE;
114 // LC2 does not support the proto property in Java objects
115 // this.javavalue.__proto__.getJSClass = Object.prototype.toString;
116 // this.jsclass = this.javavalue.getJSClass();
117 return this;
118 }