Projekt

Obecné

Profil

Stáhnout (5.12 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
if (typeof window === "undefined") {
4
    window = {};
5
}
6

    
7
var logMethods = [
8
    "trace",
9
    "debug",
10
    "info",
11
    "warn",
12
    "error"
13
];
14

    
15
define(function () {
16
    function getStorageKey(loggerName) {
17
        var key = "loglevel";
18
        if (loggerName) {
19
            key += ":" + loggerName;
20
        }
21
        return key;
22
    }
23

    
24
    var self = {};
25

    
26
    // Jasmine matcher to check the log level of a log object
27
    self.toBeAtLevel = function toBeAtLevel(level) {
28
        var log = this.actual;
29
        var expectedWorkingCalls = log.levels.SILENT - log.levels[level.toUpperCase()];
30
        var realLogMethod = window.console.log;
31
        var priorCalls = realLogMethod.calls.length;
32

    
33
        for (var ii = 0; ii < logMethods.length; ii++) {
34
            var methodName = logMethods[ii];
35
            log[methodName](methodName);
36
        }
37

    
38
        expect(realLogMethod.calls.length - priorCalls).toEqual(expectedWorkingCalls);
39
        return true;
40
    };
41

    
42
    self.isCookieStorageAvailable = function isCookieStorageAvailable() {
43
        if (window && window.document && window.document.cookie) {
44
            // We need to check not just that the cookie objects are available, but that they work, because
45
            // if we run from file:// URLs they appear present but are non-functional
46
            window.document.cookie = "test=hi;";
47

    
48
            var result = window.document.cookie.indexOf('test=hi') !== -1;
49
            window.document.cookie = "test=; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
50

    
51
            return result;
52
        } else {
53
            return false;
54
        }
55
    };
56

    
57
    self.isLocalStorageAvailable = function isLocalStorageAvailable() {
58
        try {
59
            return !!window.localStorage;
60
        } catch (e){
61
            return false;
62
        }
63
    };
64

    
65
    self.isAnyLevelStoragePossible = function isAnyLevelStoragePossible() {
66
        return self.isCookieStorageAvailable() || self.isLocalStorageAvailable();
67
    };
68

    
69
    self.toBeTheLevelStoredByCookie = function toBeTheLevelStoredByCookie(name) {
70
        var level = this.actual.toUpperCase();
71
        var storageKey = encodeURIComponent(getStorageKey(name));
72

    
73
        if (window.document.cookie.indexOf(storageKey + "=" + level) !== -1) {
74
            return true;
75
        } else {
76
            return false;
77
        }
78
    };
79

    
80
    self.toBeTheLevelStoredByLocalStorage = function toBeTheLevelStoredByLocalStorage(name) {
81
        var level = this.actual.toUpperCase();
82

    
83
        if (window.localStorage[getStorageKey(name)] === level) {
84
            return true;
85
        }
86

    
87
        return false;
88
    };
89

    
90
    // Jasmine matcher to check whether a given string was saved by loglevel
91
    self.toBeTheStoredLevel = function toBeTheStoredLevel(name) {
92
        return self.toBeTheLevelStoredByLocalStorage.call(this, name) ||
93
               self.toBeTheLevelStoredByCookie.call(this, name);
94
    };
95

    
96
    self.setCookieStoredLevel = function setCookieStoredLevel(level, name) {
97
        window.document.cookie =
98
            encodeURIComponent(getStorageKey(name)) + "=" +
99
            level.toUpperCase() + ";";
100
    };
101

    
102
    self.setLocalStorageStoredLevel = function setLocalStorageStoredLevel(level, name) {
103
        window.localStorage[getStorageKey(name)] = level.toUpperCase();
104
    };
105

    
106
    self.setStoredLevel = function setStoredLevel(level, name) {
107
        if (self.isCookieStorageAvailable()) {
108
            self.setCookieStoredLevel(level, name);
109
        }
110
        if (self.isLocalStorageAvailable()) {
111
            self.setLocalStorageStoredLevel(level, name);
112
        }
113
    };
114

    
115
    self.clearStoredLevels = function clearStoredLevels() {
116
        if (self.isLocalStorageAvailable()) {
117
            window.localStorage.clear();
118
        }
119
        if (self.isCookieStorageAvailable()) {
120
            var storedKeys = window.document.cookie.match(/(?:^|;\s)(loglevel(\:\w+)?)(?=\=)/g);
121
            if (storedKeys) {
122
                for (var i = 0; i < storedKeys.length; i++) {
123
                    window.document.cookie = storedKeys[i] + "=; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
124
                }
125
            }
126
        }
127
    };
128

    
129
    self.describeIf = function describeIf(condition, name, test) {
130
        if (condition) {
131
            jasmine.getEnv().describe(name, test);
132
        }
133
    };
134

    
135
    self.itIf = function itIf(condition, name, test) {
136
        if (condition) {
137
            jasmine.getEnv().it(name, test);
138
        }
139
    };
140

    
141
    // Forcibly reloads loglevel, and asynchronously hands the resulting log back to the given callback
142
    // via Jasmine async magic
143
    self.withFreshLog = function withFreshLog(toRun) {
144
        require.undef("lib/loglevel");
145

    
146
        var freshLog;
147

    
148
        waitsFor(function() {
149
            require(['lib/loglevel'], function(log) {
150
                freshLog = log;
151
            });
152
            return typeof freshLog !== "undefined";
153
        });
154

    
155
        runs(function() {
156
            toRun(freshLog);
157
        });
158
    };
159

    
160
    // Wraps Jasmine's it(name, test) call to reload the loglevel dependency for the given test
161
    self.itWithFreshLog = function itWithFreshLog(name, test) {
162
        jasmine.getEnv().it(name, function() {
163
            self.withFreshLog(test);
164
        });
165
    };
166

    
167
    return self;
168
});
(16-16/19)