Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/liveconnect/tostring/ToString_001.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package com.netscape.javascript.qa.liveconnect.tostring;
6
7 import com.netscape.javascript.qa.liveconnect.*;
8 import netscape.javascript.*;
9
10 /**
11 * SomeJSObject.toString() should return the same String value as
12 * SomeJSObject.call( "toString", null ) and
13 * global.eval( "somejsobjectname +''" );
14 *
15 *
16 * this test:
17 * constructs a javascript object
18 * gets the javascript object
19 * gets the string value of the object using call
20 * gets the string value of the object using eval +""
21 * gets the string value of the object using toString
22 *
23 *
24 * ie it should return the JavaScript string representation of that object.
25 *
26 * @see netscape.javascript.JSObject
27 *
28 * @author christine m begle
29 */
30 public class ToString_001 extends LiveConnectTest {
31 public ToString_001() {
32 super();
33 }
34
35 public static void main( String[] args ) {
36 ToString_001 test = new ToString_001();
37 test.start();
38 }
39
40 public void setupTestEnvironment() {
41 super.setupTestEnvironment();
42 }
43
44 public void executeTest() {
45 Object data[] = getDataArray();
46
47 for ( int i = 0; i < data.length; i++ ) {
48 JSObject jsObject = getJSObject( (Object[]) data[i] );
49 getStrings( jsObject, (Object[]) data[i] );
50 }
51 }
52
53 public void getStrings( JSObject jsObject, Object[] data ) {
54 String eString = (String) data[2];
55 String resultOfToString = null;
56 String resultOfCall = null;
57 String resultOfEval = null;
58
59 try {
60 resultOfToString = jsObject.toString();
61 resultOfCall = (String) jsObject.call( "toString", null );
62 } catch ( Exception e ) {
63 file.exception = e.toString();
64 p( "getStrings threw " + e.toString() );
65 e.printStackTrace();
66 } finally {
67
68 addTestCase(
69 "[ jsObject.toString returned " + jsObject.toString() +" ]"+
70 "( " + jsObject +" ).toString().equals( " + eString +" )",
71 "true",
72 jsObject.toString().equals(eString) +"",
73 file.exception );
74
75 addTestCase(
76 "[ calling toString returned " + resultOfCall +" ]"+
77 "( " + jsObject +".call( \"toString\", null ).equals(" + eString+")",
78 "true",
79 resultOfCall.equals(eString)+"",
80 file.exception );
81 }
82 }
83
84 /**
85 * Create and return a JSObject using data in the data array.
86 *
87 * @param data Object array containing name of JSObject, and assignment
88 * expression
89 * @return the JSObject
90 */
91 public JSObject getJSObject( Object[] data ) {
92 global.eval( (String) data[1] );
93 JSObject theThis = (JSObject) global.getMember( (String) data[0] );
94 return theThis;
95 }
96
97 /**
98 * Get the data array, which is an object array data arrays, which are
99 * also object arrays. The data arrays consist of 8 items
100 *
101 * <ul>
102 * <li> Identifier for JavaScript object
103 * <li> Assignment expression to initialize JavaScript object
104 * <li> expected string value of the object
105 * </ul>
106 *
107 * To add test cases to this test, modify this method.
108 *
109 * @return the data array.
110 */
111 public Object[] getDataArray() {
112 Object d0[] = {
113 new String( "o" ), // 0 identifier
114 new String( "var o = new Object()"), // 1 complete expression for instantiation or assignment
115 new String( "[object Object]" )
116 };
117
118 Object d1[] = {
119 new String( "num" ),
120 new String( "num = new Number(12345)" ),
121 new String( "12345" )
122 };
123
124 Object d2[] = {
125 new String( "number" ),
126 new String( "number = new Number(Infinity)"),
127 new String( "Infinity" )
128 };
129
130 Object d3[] = {
131 new String( "string" ),
132 new String( "string = new String(\"JavaScript\")" ),
133 new String( "JavaScript" )
134 };
135
136 Object d4[] = {
137 new String ("array"),
138 new String( "array = new Array(1,2,3,4,5)"),
139 new String( "1,2,3,4,5")
140 };
141
142 Object dataArray[] = { d0, d1, d2, d3, d4 };
143 return dataArray;
144 }
145 }