PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/server/node_modules/rewire/test/testModules/moduleB.js

https://github.com/debdayal/TestApp
JavaScript | 137 lines | 121 code | 14 blank | 2 comment | 25 complexity | 62c2ada520094821deea6fac68bfd8e1 MD5 | raw file
  1"use strict"; // run code in ES5 strict mode
  2
  3var someOtherModule = require("./someOtherModule.js"),
  4    myNumber = 0,   // copy by value
  5    myObj = {},     // copy by reference
  6    env = "bla",
  7    fs;
  8
  9// We need getters and setters for private vars to check if our injected setters and getters actual work
 10function setMyNumber(newNumber) {
 11    myNumber = newNumber;
 12}
 13
 14function getMyNumber() {
 15    return myNumber;
 16}
 17
 18function setMyObj(newObj) {
 19    myObj = newObj;
 20}
 21
 22function getMyObj() {
 23    return myObj;
 24}
 25
 26function readFileSync() {
 27    fs.readFileSync("bla.txt", "utf8");
 28}
 29
 30function checkSomeGlobals() {
 31    var isLowerIE,
 32        typeOfGlobalFunc;
 33
 34    if (typeof navigator !== "undefined") {
 35        isLowerIE = /MSIE [6-8]\.[0-9]/g.test(navigator.userAgent);
 36    }
 37    if (isLowerIE) {
 38        typeOfGlobalFunc = "object";
 39    } else {
 40        typeOfGlobalFunc = "function";
 41    }
 42
 43    if (typeof global !== "object") {
 44        throw new ReferenceError("global is not an object");
 45    }
 46    if (typeof console !== "object") {
 47        throw new ReferenceError("console is not an object");
 48    }
 49    if (typeof require !== "function") {
 50        throw new ReferenceError("require is not a function");
 51    }
 52    if (typeof module !== "object") {
 53        throw new ReferenceError("module is not an object");
 54    }
 55    if (typeof exports !== "object") {
 56        throw new ReferenceError("exports is not an object");
 57    }
 58    if (module.exports === exports) {
 59        throw new Error("module.exports === exports returns true");
 60    }
 61    if (typeof __dirname !== "string") {
 62        throw new ReferenceError("__dirname is not a string");
 63    }
 64    if (typeof __filename !== "string") {
 65        throw new ReferenceError("__filename is not a string");
 66    }
 67    if (typeof setTimeout !== typeOfGlobalFunc) {
 68        throw new ReferenceError("setTimeout is not a function");
 69    }
 70    if (typeof clearTimeout !== typeOfGlobalFunc) {
 71        throw new ReferenceError("clearTimeout is not a function");
 72    }
 73    if (typeof setInterval !== typeOfGlobalFunc) {
 74        throw new ReferenceError("setInterval is not a function");
 75    }
 76    if (typeof clearInterval !== typeOfGlobalFunc) {
 77        throw new ReferenceError("clearInterval is not a function");
 78    }
 79    if (typeof Error !== "function") {
 80        throw new ReferenceError("Error is not a function");
 81    }
 82    if (typeof parseFloat !== "function") {
 83        throw new ReferenceError("parseFloat is not a function");
 84    }
 85    if (typeof parseInt !== "function") {
 86        throw new ReferenceError("parseInt is not a function");
 87    }
 88    if (typeof window === "undefined") {
 89        if (typeof process !== "object") {
 90            throw new ReferenceError("process is not an object");
 91        }
 92        if (typeof Buffer !== "function") {
 93            throw new ReferenceError("Buffer is not a function");
 94        }
 95    } else {
 96        if (typeof encodeURIComponent !== "function") {
 97            throw new ReferenceError("encodeURIComponent is not a function");
 98        }
 99        if (typeof decodeURIComponent !== "function") {
100            throw new ReferenceError("decodeURIComponent is not a function");
101        }
102        if (typeof document !== "object") {
103            throw new ReferenceError("document is not an object");
104        }
105    }
106}
107
108function getConsole() {
109    return console;
110}
111
112function getFilename() {
113    return __filename;
114}
115
116function getBuffer() {
117    return Buffer;
118}
119
120function getDocument() {
121    return document;
122}
123
124// different styles of exports in moduleA.js and moduleB.js
125module.exports = {
126    setMyNumber: setMyNumber,
127    getMyNumber: getMyNumber,
128    setMyObj: setMyObj,
129    getMyObj: getMyObj,
130    readFileSync: readFileSync,
131    checkSomeGlobals: checkSomeGlobals,
132    getConsole: getConsole,
133    getFilename: getFilename,
134    getBuffer: getBuffer,
135    getDocument: getDocument,
136    someOtherModule: someOtherModule
137};