/** * All writing related code here. This is so that we can separate the async code from sync code * for testability */ var qfs = require('q-fs'); var Q = require('qq'); var OUTPUT_DIR = "build/docs/"; var fs = require('fs'); exports.output = function(file, content) { var fullPath = OUTPUT_DIR + file; var dir = parent(fullPath); return Q.when(exports.makeDir(dir), function(error) { qfs.write(fullPath, exports.toString(content)); }); }; //recursively create directory exports.makeDir = function(path) { var parts = path.split(/\//); var path = "."; //Sequentially create directories var done = Q.defer(); (function createPart() { if(!parts.length) { done.resolve(); } else { path += "/" + parts.shift(); qfs.isDirectory(path).then(function(isDir) { if(!isDir) { qfs.makeDirectory(path); } createPart(); }); } })(); return done.promise; }; exports.copyTpl = function(filename) { return exports.copy('docs/src/templates/' + filename, OUTPUT_DIR + filename); }; /* Copy files from one place to another. * @param from{string} path of the source file to be copied * @param to{string} path of where the copied file should be stored * @param transform{function=} transfromation function to be applied before return */ exports.copy = function(from, to, transform) { var args = Array.prototype.slice.call(arguments, 3); // We have to use binary reading, Since some characters are unicode. return qfs.read(from, 'b').then(function(content) { if (transform) { args.unshift(content.toString()); content = transform.apply(null, args); } qfs.write(to, content); }); } /* Replace placeholders in content accordingly * @param content{string} content to be modified * @param replacements{obj} key and value pairs in which key will be replaced with value in content */ exports.replace = function(content, replacements) { for(key in replacements) { content = content.replace(key, replacements[key]); } return content; } exports.copyDir = function copyDir(dir) { return qfs.listDirectoryTree('docs/' + dir).then(function(dirs) { var done; dirs.forEach(function(dirToMake) { done = Q.when(done, function() { return exports.makeDir("./build/" + dirToMake); }); }); return done; }).then(function() { return qfs.listTree('docs/' + dir); }).then(function(files) { files.forEach( function(file) { exports.copy(file,'./build/' + file); }); }); }; exports.merge = function(srcs, to) { return merge(srcs.map(function(src) { return 'docs/src/templates/' + src; }), OUTPUT_DIR + to); }; function merge(srcs, to) { var contents = []; //Sequentially read file var done; srcs.forEach(function(src) { done = Q.when(done, function(content) { if(content) contents.push(content); return qfs.read(src, 'b'); }); }); // write to file return Q.when(done, function(content) { contents.push(content); qfs.write(to, contents.join('\n')); }); } //----------------------- Synchronous Methods ---------------------------------- function parent(file) { var parts = file.split('/'); parts.pop(); return parts.join('/'); } exports.toString = function toString(obj) { switch (typeof obj) { case 'string': return obj; case 'object': if (obj instanceof Array) { obj.forEach(function(value, key) { obj[key] = toString(value); }); return obj.join(''); } else { return JSON.stringify(obj); } } return obj; }; function noop() {}; ' href='#n12'>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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
'use strict';
describe('mocks', function(){
describe('TzDate', function() {
function minutes(min) {
return min*60*1000;
}
it('should take millis as constructor argument', function() {
expect(new TzDate(0, 0).getTime()).toBe(0);
expect(new TzDate(0, 1283555108000).getTime()).toBe(1283555108000);
});
it('should take dateString as constructor argument', function() {
expect(new TzDate(0, '1970-01-01T00:00:00.000Z').getTime()).toBe(0);
expect(new TzDate(0, '2010-09-03T23:05:08.023Z').getTime()).toBe(1283555108023);
});
it('should fake getLocalDateString method', function() {
//0 in -3h
var t0 = new TzDate(-3, 0);
expect(t0.toLocaleDateString()).toMatch('1970');
//0 in +0h
var t1 = new TzDate(0, 0);
expect(t1.toLocaleDateString()).toMatch('1970');
//0 in +3h
var t2 = new TzDate(3, 0);
expect(t2.toLocaleDateString()).toMatch('1969');
});
it('should fake getHours method', function() {
//0 in -3h
var t0 = new TzDate(-3, 0);
expect(t0.getHours()).toBe(3);
//0 in +0h
var t1 = new TzDate(0, 0);
expect(t1.getHours()).toBe(0);
//0 in +3h
var t2 = new TzDate(3, 0);
expect(t2.getHours()).toMatch(21);
});
it('should fake getMinutes method', function() {
//0:15 in -3h
var t0 = new TzDate(-3, minutes(15));
expect(t0.getMinutes()).toBe(15);
//0:15 in -3.25h
var t0a = new TzDate(-3.25, minutes(15));
expect(t0a.getMinutes()).toBe(30);
//0 in +0h
var t1 = new TzDate(0, minutes(0));
expect(t1.getMinutes()).toBe(0);
//0:15 in +0h
var t1a = new TzDate(0, minutes(15));
expect(t1a.getMinutes()).toBe(15);
//0:15 in +3h
var t2 = new TzDate(3, minutes(15));
expect(t2.getMinutes()).toMatch(15);
//0:15 in +3.25h
var t2a = new TzDate(3.25, minutes(15));
expect(t2a.getMinutes()).toMatch(0);
});
it('should fake getSeconds method', function() {
//0 in -3h
var t0 = new TzDate(-3, 0);
expect(t0.getSeconds()).toBe(0);
//0 in +0h
var t1 = new TzDate(0, 0);
expect(t1.getSeconds()).toBe(0);
//0 in +3h
var t2 = new TzDate(3, 0);
expect(t2.getSeconds()).toMatch(0);
});
it('should create a date representing new year in Bratislava', function() {
var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00.000Z');
expect(newYearInBratislava.getTimezoneOffset()).toBe(-60);
expect(newYearInBratislava.getFullYear()).toBe(2010);
expect(newYearInBratislava.getMonth()).toBe(0);
expect(newYearInBratislava.getDate()).toBe(1);
expect(newYearInBratislava.getHours()).toBe(0);
expect(newYearInBratislava.getMinutes()).toBe(0);
});
it('should delegate all the UTC methods to the original UTC Date object', function() {
//from when created from string
var date1 = new TzDate(-1, '2009-12-31T23:00:00.000Z');
expect(date1.getUTCFullYear()).toBe(2009);
expect(date1.getUTCMonth()).toBe(11);
expect(date1.getUTCDate()).toBe(31);
expect(date1.getUTCHours()).toBe(23);
expect(date1.getUTCMinutes()).toBe(0);
expect(date1.getUTCSeconds()).toBe(0);
//from when created from millis
var date2 = new TzDate(-1, angular.String.toDate('2009-12-31T23:00:00.000Z').getTime());
expect(date2.getUTCFullYear()).toBe(2009);
expect(date2.getUTCMonth()).toBe(11);
expect(date2.getUTCDate()).toBe(31);
expect(date2.getUTCHours()).toBe(23);
expect(date2.getUTCMinutes()).toBe(0);
expect(date2.getUTCSeconds()).toBe(0);
});
it('should throw error when no third param but toString called', function() {
expect(function() { new TzDate(0,0).toString(); }).
toThrow('Method \'toString\' is not implemented in the TzDate mock');
});
});
describe('$log mock', function() {
var $log;
beforeEach(function() {
$log = MockLogFactory();
});
it('should provide log method', function() {
expect(function() { $log.log(''); }).not.toThrow();
});
it('should provide info method', function() {
expect(function() { $log.info(''); }).not.toThrow();
});
it('should provide warn method', function() {
expect(function() { $log.warn(''); }).not.toThrow();
});
it('should provide error method', function() {
expect(function() { $log.error(''); }).not.toThrow();
});
it('should store log messages', function() {
$log.log('fake log');
expect($log.log.logs).toContain(['fake log']);
});
it('should store info messages', function() {
$log.info('fake log');
expect($log.info.logs).toContain(['fake log']);
});
it('should store warn messages', function() {
$log.warn('fake log');
expect($log.warn.logs).toContain(['fake log']);
});
it('should store error messages', function() {
$log.error('fake log');
expect($log.error.logs).toContain(['fake log']);
});
});
describe('defer', function(){
var browser, log;
beforeEach(function(){
browser = new MockBrowser();
log = '';
});
function logFn(text){ return function(){
log += text +';';
};
}
it('should flush', function(){
browser.defer(logFn('A'));
expect(log).toEqual('');
browser.defer.flush();
expect(log).toEqual('A;');
});
it('should flush delayed', function(){
browser.defer(logFn('A'));
browser.defer(logFn('B'), 10);
browser.defer(logFn('C'), 20);
expect(log).toEqual('');
expect(browser.defer.now).toEqual(0);
browser.defer.flush(0);
expect(log).toEqual('A;');
browser.defer.flush();
expect(log).toEqual('A;B;C;');
});
it('should defer and flush over time', function(){
browser.defer(logFn('A'), 1);
browser.defer(logFn('B'), 2);
browser.defer(logFn('C'), 3);
browser.defer.flush(0);
expect(browser.defer.now).toEqual(0);
expect(log).toEqual('');
browser.defer.flush(1);
expect(browser.defer.now).toEqual(1);
expect(log).toEqual('A;');
browser.defer.flush(2);
expect(browser.defer.now).toEqual(3);
expect(log).toEqual('A;B;C;');
});
});
describe('$exceptionHandler', function() {
it('should rethrow exceptions', function() {
var rootScope = angular.scope(),
exHandler = rootScope.$service('$exceptionHandler');
expect(function() { exHandler('myException'); }).toThrow('myException');
});
});
});