Debian Codegraph @

rhino/testsrc/tests/lc3/JSObject/ToLong-001.js

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation,
3 * All Rights Reserved.
4 */
5 /* This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8
9 gTestfile = 'ToLong-001.js';
10
11 /**
12 * JavaScript to Java type conversion.
13 *
14 * This test passes JavaScript number values to several Java methods
15 * that expect arguments of various types, and verifies that the value is
16 * converted to the correct value and type.
17 *
18 * This tests instance methods, and not static methods.
19 *
20 * Running these tests successfully requires you to have
21 * com.netscape.javascript.qa.liveconnect.DataTypeClass on your classpath.
22 *
23 * Specification: Method Overloading Proposal for Liveconnect 3.0
24 *
25 * @author: [email protected]
26 *
27 */
28 var SECTION = "JavaScript Object to int";
29 var VERSION = "1_4";
30 var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
31 SECTION;
32 startTest();
33
34 var dt = new DT();
35
36 var a = new Array();
37 var i = 0;
38
39 // 3.3.6.4 Other JavaScript Objects
40 // Passing a JavaScript object to a java method that that expects a int
41 // should:
42 // 1. Apply the ToPrimitive operator (ECMA 9.3) to the JavaScript object
43 // with hint Number
44 // 2. Convert Result(1) to Java numeric type using the rules in 3.3.3.
45
46 var bool = new Boolean(true);
47
48 a[i++] = new TestObject(
49 "dt.setLong( bool )",
50 "dt.PUB_LONG",
51 "dt.getLong()",
52 "typeof dt.getLong()",
53 1,
54 "number");
55
56 bool = new Boolean(false);
57
58 a[i++] = new TestObject(
59 "dt.setLong( bool )",
60 "dt.PUB_LONG",
61 "dt.getLong()",
62 "typeof dt.getLong()",
63 0,
64 "number");
65
66 var number = new Number(0);
67
68 a[i++] = new TestObject(
69 "dt.setLong( number )",
70 "dt.PUB_LONG",
71 "dt.getLong()",
72 "typeof dt.getLong()",
73 0,
74 "number");
75
76
77 var long_max = java.lang.Short.MAX_VALUE;
78 var long_min = java.lang.Short.MIN_VALUE;
79
80 var string = new String(long_max);
81
82 a[i++] = new TestObject(
83 "dt.setLong(string)",
84 "dt.PUB_LONG",
85 "dt.getLong()",
86 "typeof dt.getLong()",
87 long_max,
88 "number");
89
90 var string = new String(long_min);
91
92 a[i++] = new TestObject(
93 "dt.setLong(string)",
94 "dt.PUB_LONG",
95 "dt.getLong()",
96 "typeof dt.getLong()",
97 long_min,
98 "number");
99
100 var myobject = new MyObject( "5.5" );
101
102 a[i++] = new TestObject(
103 "dt.setLong( myobject )",
104 "dt.PUB_LONG",
105 "dt.getLong()",
106 "typeof dt.getLong()",
107 5,
108 "number");
109
110 myobject = new MyOtherObject( "-107.5");
111
112 a[i++] = new TestObject(
113 "dt.setLong( myobject )",
114 "dt.PUB_LONG",
115 "dt.getLong()",
116 "typeof dt.getLong()",
117 -107,
118 "number");
119
120 myobject = new AnotherObject( "6666");
121
122 a[i++] = new TestObject(
123 "dt.setLong( myobject )",
124 "dt.PUB_LONG",
125 "dt.getLong()",
126 "typeof dt.getLong()",
127 6666,
128 "number");
129
130 for ( i = 0; i < a.length; i++ ) {
131 new TestCase(
132 a[i].description +"; "+ a[i].javaFieldName,
133 a[i].jsValue,
134 a[i].javaFieldValue );
135
136 new TestCase(
137 a[i].description +"; " + a[i].javaMethodName,
138 a[i].jsValue,
139 a[i].javaMethodValue );
140
141 new TestCase(
142 a[i].javaTypeName,
143 a[i].jsType,
144 a[i].javaTypeValue );
145 }
146
147 test();
148
149 function MyObject( stringValue ) {
150 this.stringValue = String(stringValue);
151 this.toString = new Function( "return this.stringValue" );
152 }
153
154 function MyOtherObject( value ) {
155 this.toString = null;
156 this.value = value;
157 this.valueOf = new Function( "return this.value" );
158 }
159
160 function AnotherObject( value ) {
161 this.toString = new Function( "return new Number(666)" );
162 this.value = value;
163 this.valueOf = new Function( "return this.value" );
164 }
165
166 function TestObject( description, javaField, javaMethod, javaType,
167 jsValue, jsType )
168 {
169 eval (description );
170
171 this.description = description;
172 this.javaFieldName = javaField;
173 this.javaFieldValue = eval( javaField );
174 this.javaMethodName = javaMethod;
175 this.javaMethodValue = eval( javaMethod );
176 this.javaTypeName = javaType,
177 this.javaTypeValue = eval( javaType );
178
179 this.jsValue = jsValue;
180 this.jsType = jsType;
181 }