|
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 |
|
7 /** |
|
8 Filename: slice.js |
|
9 Description: 'This tests the String object method: slice' |
|
10 |
|
11 Author: Nick Lerissa |
|
12 Date: Fri Feb 13 09:58:28 PST 1998 |
|
13 */ |
|
14 |
|
15 var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"'; |
|
16 var VERSION = 'no version'; |
|
17 startTest(); |
|
18 var TITLE = 'String.slice'; |
|
19 |
|
20 writeHeaderToLog('Executing script: slice.js'); |
|
21 writeHeaderToLog( SECTION + " "+ TITLE); |
|
22 |
|
23 var a = new String("abcdefghijklmnopqrstuvwxyz1234567890"); |
|
24 var b = new String("this is a test string"); |
|
25 |
|
26 exhaustiveStringSliceTest("exhaustive String.slice test 1", a); |
|
27 exhaustiveStringSliceTest("exhaustive String.slice test 2", b); |
|
28 |
|
29 test(); |
|
30 |
|
31 |
|
32 function myStringSlice(a, from, to) |
|
33 { |
|
34 var from2 = from; |
|
35 var to2 = to; |
|
36 var returnString = new String(""); |
|
37 var i; |
|
38 |
|
39 if (from2 < 0) from2 = a.length + from; |
|
40 if (to2 < 0) to2 = a.length + to; |
|
41 |
|
42 if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length)) |
|
43 { |
|
44 if (from2 < 0) from2 = 0; |
|
45 if (to2 > a.length) to2 = a.length; |
|
46 |
|
47 for (i = from2; i < to2; ++i) returnString += a.charAt(i); |
|
48 } |
|
49 return returnString; |
|
50 } |
|
51 |
|
52 // This function tests the slice command on a String |
|
53 // passed in. The arguments passed into slice range in |
|
54 // value from -5 to the length of the array + 4. Every |
|
55 // combination of the two arguments is tested. The expected |
|
56 // result of the slice(...) method is calculated and |
|
57 // compared to the actual result from the slice(...) method. |
|
58 // If the Strings are not similar false is returned. |
|
59 function exhaustiveStringSliceTest(testname, a) |
|
60 { |
|
61 var x = 0; |
|
62 var y = 0; |
|
63 var errorMessage; |
|
64 var reason = ""; |
|
65 var passed = true; |
|
66 |
|
67 for (x = -(2 + a.length); x <= (2 + a.length); x++) |
|
68 for (y = (2 + a.length); y >= -(2 + a.length); y--) |
|
69 { |
|
70 var b = a.slice(x,y); |
|
71 var c = myStringSlice(a,x,y); |
|
72 |
|
73 if (String(b) != String(c)) |
|
74 { |
|
75 errorMessage = |
|
76 "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" + |
|
77 " test: " + "a.slice(" + x + "," + y + ")\n" + |
|
78 " a: " + String(a) + "\n" + |
|
79 " actual result: " + String(b) + "\n" + |
|
80 " expected result: " + String(c) + "\n"; |
|
81 writeHeaderToLog(errorMessage); |
|
82 reason = reason + errorMessage; |
|
83 passed = false; |
|
84 } |
|
85 } |
|
86 var testCase = new TestCase(SECTION, testname, true, passed); |
|
87 if (passed == false) |
|
88 testCase.reason = reason; |
|
89 return testCase; |
|
90 } |