|
1 /* -*- Mode: Java; 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 // shell.js |
|
7 // This file sets up the unit test environment by building an array of files |
|
8 // to be tested. It assumes it lives in a folder adjacent to the a folder |
|
9 // called 'xml', where the testcases live. |
|
10 // |
|
11 // The directory layout looks something like this: |
|
12 // |
|
13 // tests/ *shell.js* |
|
14 // | |
|
15 // - test.js |
|
16 // | |
|
17 // - xml/ -- rss1/... |
|
18 // | |
|
19 // -- rss2/... |
|
20 // | |
|
21 // -- atom/testcase.xml |
|
22 |
|
23 function trimString(s) |
|
24 { |
|
25 return(s.replace(/^\s+/,'').replace(/\s+$/,'')); |
|
26 } |
|
27 |
|
28 var tests = new Array(); |
|
29 const ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService); |
|
30 |
|
31 // find the directory containing our test XML |
|
32 |
|
33 if (0 < arguments.length) { |
|
34 |
|
35 // dir is specified on the command line |
|
36 |
|
37 var topDir = Components.classes["@mozilla.org/file/local;1"] |
|
38 .createInstance(Components.interfaces.nsILocalFile); |
|
39 |
|
40 topDir.initWithPath(arguments[0]); |
|
41 } |
|
42 else { |
|
43 const nsIDirectoryServiceProvider |
|
44 = Components.interfaces.nsIDirectoryServiceProvider; |
|
45 const nsIDirectoryServiceProvider_CONTRACTID |
|
46 = "@mozilla.org/file/directory_service;1"; |
|
47 |
|
48 var dirServiceProvider |
|
49 = Components.classes[nsIDirectoryServiceProvider_CONTRACTID] |
|
50 .getService(nsIDirectoryServiceProvider); |
|
51 |
|
52 var persistent = new Object(); |
|
53 |
|
54 var topDir = dirServiceProvider.getFile("CurWorkD", persistent); |
|
55 } |
|
56 |
|
57 var entries = topDir.directoryEntries; |
|
58 var xmlDir; |
|
59 while(entries.hasMoreElements()){ |
|
60 xmlDir = entries.getNext(); |
|
61 xmlDir.QueryInterface(Components.interfaces.nsILocalFile); |
|
62 if(xmlDir.leafName == "xml") |
|
63 break; |
|
64 else |
|
65 xmlDir = null; |
|
66 } |
|
67 |
|
68 // if we got our xmldir, find our testcases |
|
69 var testDir; |
|
70 if(xmlDir){ |
|
71 entries = xmlDir.directoryEntries; |
|
72 while(entries.hasMoreElements()){ |
|
73 testDir = entries.getNext(); |
|
74 testDir.QueryInterface(Components.interfaces.nsILocalFile); |
|
75 if(testDir.exists() && testDir.isDirectory()){ |
|
76 var testfiles = testDir.directoryEntries; |
|
77 while(testfiles.hasMoreElements()){ // inside an individual testdir, like "rss2" |
|
78 var test = testfiles.getNext(); |
|
79 test.QueryInterface(Components.interfaces.nsILocalFile); |
|
80 if(test.exists() && test.isFile()){ |
|
81 |
|
82 // Got a feed file, now we need to add it to our tests |
|
83 // and parse out the Description and Expect headers. |
|
84 // That lets us write one test.js file and keep the |
|
85 // tests with the XML. Convenient method learned from |
|
86 // Mark Pilgrim. |
|
87 |
|
88 var istream = Components.classes["@mozilla.org/network/file-input-stream;1"] |
|
89 .createInstance(Components.interfaces.nsIFileInputStream); |
|
90 |
|
91 try{ |
|
92 // open an input stream from file |
|
93 istream.init(test, 0x01, 0444, 0); |
|
94 istream.QueryInterface(Components.interfaces.nsILineInputStream); |
|
95 var line = {}, hasmore, testcase = {}, expectIndex, descIndex; |
|
96 |
|
97 do { |
|
98 hasmore = istream.readLine(line); |
|
99 expectIndex = line.value.indexOf('Expect:'); |
|
100 descIndex = line.value.indexOf('Description:'); |
|
101 baseIndex = line.value.indexOf('Base:'); |
|
102 if(descIndex > -1) |
|
103 testcase.desc = trimString(line.value.substring(line.value.indexOf(':')+1)); |
|
104 if(expectIndex > -1) |
|
105 testcase.expect = trimString(line.value.substring(line.value.indexOf(':')+1)); |
|
106 if(baseIndex > -1) |
|
107 testcase.base = ioService.newURI(trimString(line.value.substring(line.value.indexOf(':')+1)),null,null); |
|
108 if(testcase.expect && testcase.desc){ |
|
109 // got our fields, let's add it to our array and break |
|
110 testcase.path = xmlDir.leafName + '/' + testDir.leafName + '/' + test.leafName; |
|
111 testcase.file = test; |
|
112 tests.push(testcase); |
|
113 break; // out of do-while |
|
114 } |
|
115 |
|
116 } while(hasmore); |
|
117 |
|
118 }catch(e){ |
|
119 dump("FAILED! Error reading test case in file " + test.leafName + " " + e + "\n"); |
|
120 }finally{ |
|
121 istream.close(); |
|
122 } |
|
123 } |
|
124 } |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 load(topDir.path+'/test.js'); |