Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / walkdir / test / emitter.js
1 var test = require('tape'),
2 walkdir = require('../walkdir.js');
3
4 var expectedPaths = {
5 'dir/foo/x':'file',
6 'dir/foo/a':'dir',
7 'dir/foo/a/y':'file',
8 'dir/foo/a/b':'dir',
9 'dir/foo/a/b/z':'file',
10 'dir/foo/a/b/c':'dir',
11 'dir/foo/a/b/c/w':'file'
12 };
13
14 test('async events',function(t){
15   var paths = [],
16   files = [],
17   dirs = [];
18
19
20   var emitter = walkdir(__dirname+'/dir/foo',function(path){
21     //console.log('path: ',path);
22     paths.push(path.replace(__dirname+'/',''));
23   });
24
25   emitter.on('directory',function(path,stat){
26     dirs.push(path.replace(__dirname+'/',''));
27   });
28
29   emitter.on('file',function(path,stat){
30     //console.log('file: ',path); 
31     files.push(path.replace(__dirname+'/',''));
32   });
33
34   emitter.on('end',function(){
35
36      files.forEach(function(v,k){
37        t.equals(expectedPaths[v],'file','path from file event should be file');  
38      });
39
40      var expected = Object.keys(expectedPaths);
41
42      t.ok(expected.length == paths.length, 'expected and emitted paths should have the same length');
43
44      expected.forEach(function(v,k){
45        if(expectedPaths[v] == 'file') {
46           t.ok(files.indexOf(v) > -1,'should have file in files array');
47        }
48      });
49
50      dirs.forEach(function(v,k){
51        t.equals(expectedPaths[v],'dir','path from dir event should be dir '+v);  
52      });
53
54      expected.forEach(function(v,k){
55        if(expectedPaths[v] == 'dir') {
56           t.ok(dirs.indexOf(v) > -1,'should have dir in dirs array');
57        }
58      });
59
60      expected.forEach(function(v,k){
61        t.ok(paths.indexOf(v) !== -1,'should have found all expected paths '+v);
62      });
63
64      t.end();
65   });
66 });