michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // shell.js michael@0: // This file sets up the unit test environment by building an array of files michael@0: // to be tested. It assumes it lives in a folder adjacent to the a folder michael@0: // called 'xml', where the testcases live. michael@0: // michael@0: // The directory layout looks something like this: michael@0: // michael@0: // tests/ *shell.js* michael@0: // | michael@0: // - test.js michael@0: // | michael@0: // - xml/ -- rss1/... michael@0: // | michael@0: // -- rss2/... michael@0: // | michael@0: // -- atom/testcase.xml michael@0: michael@0: function trimString(s) michael@0: { michael@0: return(s.replace(/^\s+/,'').replace(/\s+$/,'')); michael@0: } michael@0: michael@0: var tests = new Array(); michael@0: const ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService); michael@0: michael@0: // find the directory containing our test XML michael@0: michael@0: if (0 < arguments.length) { michael@0: michael@0: // dir is specified on the command line michael@0: michael@0: var topDir = Components.classes["@mozilla.org/file/local;1"] michael@0: .createInstance(Components.interfaces.nsILocalFile); michael@0: michael@0: topDir.initWithPath(arguments[0]); michael@0: } michael@0: else { michael@0: const nsIDirectoryServiceProvider michael@0: = Components.interfaces.nsIDirectoryServiceProvider; michael@0: const nsIDirectoryServiceProvider_CONTRACTID michael@0: = "@mozilla.org/file/directory_service;1"; michael@0: michael@0: var dirServiceProvider michael@0: = Components.classes[nsIDirectoryServiceProvider_CONTRACTID] michael@0: .getService(nsIDirectoryServiceProvider); michael@0: michael@0: var persistent = new Object(); michael@0: michael@0: var topDir = dirServiceProvider.getFile("CurWorkD", persistent); michael@0: } michael@0: michael@0: var entries = topDir.directoryEntries; michael@0: var xmlDir; michael@0: while(entries.hasMoreElements()){ michael@0: xmlDir = entries.getNext(); michael@0: xmlDir.QueryInterface(Components.interfaces.nsILocalFile); michael@0: if(xmlDir.leafName == "xml") michael@0: break; michael@0: else michael@0: xmlDir = null; michael@0: } michael@0: michael@0: // if we got our xmldir, find our testcases michael@0: var testDir; michael@0: if(xmlDir){ michael@0: entries = xmlDir.directoryEntries; michael@0: while(entries.hasMoreElements()){ michael@0: testDir = entries.getNext(); michael@0: testDir.QueryInterface(Components.interfaces.nsILocalFile); michael@0: if(testDir.exists() && testDir.isDirectory()){ michael@0: var testfiles = testDir.directoryEntries; michael@0: while(testfiles.hasMoreElements()){ // inside an individual testdir, like "rss2" michael@0: var test = testfiles.getNext(); michael@0: test.QueryInterface(Components.interfaces.nsILocalFile); michael@0: if(test.exists() && test.isFile()){ michael@0: michael@0: // Got a feed file, now we need to add it to our tests michael@0: // and parse out the Description and Expect headers. michael@0: // That lets us write one test.js file and keep the michael@0: // tests with the XML. Convenient method learned from michael@0: // Mark Pilgrim. michael@0: michael@0: var istream = Components.classes["@mozilla.org/network/file-input-stream;1"] michael@0: .createInstance(Components.interfaces.nsIFileInputStream); michael@0: michael@0: try{ michael@0: // open an input stream from file michael@0: istream.init(test, 0x01, 0444, 0); michael@0: istream.QueryInterface(Components.interfaces.nsILineInputStream); michael@0: var line = {}, hasmore, testcase = {}, expectIndex, descIndex; michael@0: michael@0: do { michael@0: hasmore = istream.readLine(line); michael@0: expectIndex = line.value.indexOf('Expect:'); michael@0: descIndex = line.value.indexOf('Description:'); michael@0: baseIndex = line.value.indexOf('Base:'); michael@0: if(descIndex > -1) michael@0: testcase.desc = trimString(line.value.substring(line.value.indexOf(':')+1)); michael@0: if(expectIndex > -1) michael@0: testcase.expect = trimString(line.value.substring(line.value.indexOf(':')+1)); michael@0: if(baseIndex > -1) michael@0: testcase.base = ioService.newURI(trimString(line.value.substring(line.value.indexOf(':')+1)),null,null); michael@0: if(testcase.expect && testcase.desc){ michael@0: // got our fields, let's add it to our array and break michael@0: testcase.path = xmlDir.leafName + '/' + testDir.leafName + '/' + test.leafName; michael@0: testcase.file = test; michael@0: tests.push(testcase); michael@0: break; // out of do-while michael@0: } michael@0: michael@0: } while(hasmore); michael@0: michael@0: }catch(e){ michael@0: dump("FAILED! Error reading test case in file " + test.leafName + " " + e + "\n"); michael@0: }finally{ michael@0: istream.close(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: load(topDir.path+'/test.js');