forked from TooTallNate/nTunes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecifier-test.js
127 lines (116 loc) · 3.98 KB
/
specifier-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var join = require("path").join;
require.paths.unshift(join(__dirname, "../lib"));
var fs = require('fs');
var sys = require('sys');
var colors = require('colors');
var xml2js = require('xml2js');
var nClass = require("nTunes-class");
var applescript = require("applescript");
var nSpecifier = require("nTunes-specifier");
require.paths.shift();
var parser = new xml2js.Parser();
parser.on('end', function(result) {
var iTunesSuite = result.suite[1];
// Prepare the environment by loading and parsing the iTunes.sdef file.
//nCommand.processCommands(iTunesSuite.command);
nClass.processClasses(iTunesSuite['class']);
nSpecifier.setNClass(nClass);
parser = null;
});
fs.readFile(join(__dirname, '../lib/iTunes.sdef'), function(err, data) { parser.parseString(data); });
// Accepts an array of "specifiers" to test. First tries to parse the String
// into an nSpecifier instance, then executes the nSpecifier's AppleScript
// code and print out the result. As long as 'osascript' returns a 0 exit
// code, then the specifier passes overall.
var numPass = 0;
var numFail = 0;
function test() {
if (parser) {
var args = arguments;
return parser.on("end", function() {
process.nextTick(function() {
test.apply(null, args);
});
});
}
var tests = Array.prototype.slice.call(arguments);
var specifier = tests.shift();
console.error('Input:'.blue.bold.italic.underline);
console.error(' ' + specifier.blue);
try {
specifier = new nSpecifier(specifier);
var command = specifier.vars + 'get ' + specifier.finalVar;
console.error('Output:'.cyan.bold);
command.split('\n').forEach(function(line) {
console.error(' ' + line.cyan);
});
applescript.execString('tell application "iTunes"\n' + command + '\nend tell', function(err, out) {
if (err) {
console.error('Result:'.red.bold);
console.error((' Exit Code: ' + err.exitCode).red.italic);
err.message.split('\n').forEach(function(line) {
if (line.length>0)
console.error(' ' + line.red);
});
console.error('Fail...'.red.bold.italic.underline);
numFail++;
} else {
console.error('Result:'.green.bold);
sys.inspect(out).split('\n').forEach(function(line) {
if (line.length>0)
console.error(' ' + line.green);
});
console.error('PASS!'.green.bold.italic.underline);
numPass++;
}
console.error('\n');
if (tests.length > 0) {
test.apply(null, tests);
} else {
if (done) {
done({
pass: numPass,
fail: numFail
});
}
}
});
} catch(e) {
console.error('Error:'.red.bold);
sys.inspect(e).split('\n').forEach(function(obj) {
console.error(' ' + obj);
});
console.error('Fail...'.red.bold.italic.underline);
numFail++;
}
}
//function done() {
// console.error("Completed ".magenta + String(numPass+numFail).magenta.bold.underline + " specifier tests:".magenta);
// console.error(" " + String(numPass).green.bold + " passed!".green);
// console.error(" " + String(numFail).red.bold + " failed...".red);
//}
exports.test = function(callback) {
//return callback(); //Skip
done = callback;
// do some tests
test(
"/name,version,mute",
"/current track",
"/current track/name,artist,album,id",
"/current track/artwork/1/id,format,kind,description,downloaded",
"/source",
"/source/1",
"/source/-40",
"/source/name,kind",
"/source/1/name,kind,id",
"/source/1/playlist/name",
"/source/1/playlist/2/name",
"/source/1/playlist/1/track/1",
"/source/1/playlist/1/track/artist",
"/source/1/playlist/1/track/artist=Jimi Hendrix&genre=Rock",
"/source/1/playlist/1/track/artist=Jimi Hendrix&genre=Rock/1",
"/source/1/playlist/1/track/artist=Jimi Hendrix&genre=Rock/1/name,artist,album,genre,duration",
"/selection",
"/selection/album,name,artist,location"
);
}