<commit_before>describe('StackTraceGPS', function () {
    var callback;
    var debugCallback;
    var errback;
    var debugErrback;
    var server;

    beforeEach(function () {
        callback = jasmine.createSpy('callback');
        errback = jasmine.createSpy('errback');
        debugCallback = function (stackframes) {
            console.log(stackframes);
        };
        debugErrback = function (e) {
            console.log(e.message);
            console.log(e.stack);
        };
        server = sinon.fakeServer.create();
    });

    afterEach(function () {
        server.restore();
    });

    describe('#findFunctionName', function () {
        it('rejects given non-object StackFrame', function () {
            runs(function() {
                StackTraceGPS().findFunctionName('').then(callback, errback); // jshint ignore:line
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                // FIXME: fails in Safari 7.1
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given StackFrame is not an object'));
            });
        });

        it('rejects given invalid URL String', function () {
            runs(function() {
                new StackTraceGPS().findFunctionName(new StackFrame()).then(callback, errback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                // FIXME: fails in Safari 7.1
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given file name is not a String'));
            });
        });

        it('rejects given invalid line number', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js');
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                // FIXME: fails in Safari 7.1
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given line number must be a positive integer'));
            });
        });

        it('rejects if source file could not be found', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 23, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                server.requests[0].respond(404, {}, 'Not Found');
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                expect(errback).toHaveBeenCalledWith(new Error('Unable to retrieve http://localhost:9999/file.js'));
            });
        });

        it('rejects in offline mode if sources not in source cache', function() {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 23, 0);
                new StackTraceGPS({offline: true}).findFunctionName(stackframe).then(callback, errback)['catch'](debugErrback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                expect(errback).toHaveBeenCalledWith(new Error('Cannot make network requests in offline mode'));
            });
        });

        it('resolves sources from given sourceCache', function() {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 4);
                var sourceCache = {'http://localhost:9999/file.js': 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")'};
                new StackTraceGPS({sourceCache: sourceCache}).findFunctionName(stackframe).then(callback, errback);
                // NOTE: no fake server response necessary
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('foo', [], 'http://localhost:9999/file.js', 1, 4));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function expression', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 4);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                // FIXME(ew): IE 10 "Invalid calling object" and IE 9 "INVALID_STATE_ERR" in the process of responding
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, source);
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('foo', [], 'http://localhost:9999/file.js', 1, 4));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function declaration', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 2, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, source);
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('bar', [], 'http://localhost:9999/file.js', 2, 0));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function evaluation', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 3, 3);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, source);
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('baz', [], 'http://localhost:9999/file.js', 3, 3));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('resolves to undefined if function name could not be found', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, '');
            });
            waits(100);<commit_msg>Add invalid column number test and catch more errors for debugging.<commit_after>describe('StackTraceGPS', function () {
    var callback;
    var debugCallback;
    var errback;
    var debugErrback;
    var server;

    beforeEach(function () {
        callback = jasmine.createSpy('callback');
        errback = jasmine.createSpy('errback');
        debugCallback = function (stackframes) {
            console.log(stackframes);
        };
        debugErrback = function (e) {
            console.log(e.message);
            console.log(e.stack);
        };
        server = sinon.fakeServer.create();
    });

    afterEach(function () {
        server.restore();
    });

    describe('#findFunctionName', function () {
        it('rejects given non-object StackFrame', function () {
            runs(function() {
                StackTraceGPS().findFunctionName('').then(callback, errback); // jshint ignore:line
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                // FIXME: fails in Safari 7.1
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given StackFrame is not an object'));
            });
        });

        it('rejects given invalid URL String', function () {
            runs(function() {
                new StackTraceGPS().findFunctionName(new StackFrame()).then(callback, errback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given file name is not a String'));
            });
        });

        it('rejects given invalid line number', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js');
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given line number must be a positive integer'));
            });
        });

        it('rejects given invalid column number', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 10, -1);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                //expect(errback).toHaveBeenCalledWith(new TypeError('Given line number must be a non-negative integer'));
            });
        });

        it('rejects if source file could not be found', function () {
            runs(function() {
                server.respondWith('GET', 'http://localhost:9999/file.js', [404, { 'Content-Type': 'application/x-javascript' }, '']);
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 23, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                server.requests[0].respond(404, {}, 'Not Found');
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                expect(errback).toHaveBeenCalledWith(new Error('Unable to retrieve http://localhost:9999/file.js'));
            });
        });

        it('rejects in offline mode if sources not in source cache', function() {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 23, 0);
                new StackTraceGPS({offline: true}).findFunctionName(stackframe).then(callback, errback)['catch'](debugErrback);
            });
            waits(100);
            runs(function() {
                expect(callback).not.toHaveBeenCalled();
                expect(errback).toHaveBeenCalled();
                expect(errback).toHaveBeenCalledWith(new Error('Cannot make network requests in offline mode'));
            });
        });

        it('resolves sources from given sourceCache', function() {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 4);
                var sourceCache = {'http://localhost:9999/file.js': 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")'};
                new StackTraceGPS({sourceCache: sourceCache}).findFunctionName(stackframe).then(callback, errback)['catch'](debugErrback);
                // NOTE: no fake server response necessary
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('foo', [], 'http://localhost:9999/file.js', 1, 4));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function expression', function () {
            runs(function() {
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                server.respondWith('GET', 'http://localhost:9999/file.js', [200, { 'Content-Type': 'application/x-javascript' }, source]);
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 4);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, debugErrback)['catch'](debugErrback);
                server.respond();
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('foo', [], 'http://localhost:9999/file.js', 1, 4));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function declaration', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 2, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback)['catch'](debugErrback);
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, source);
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('bar', [], 'http://localhost:9999/file.js', 2, 0));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('finds function name within function evaluation', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 3, 3);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, source);
            });
            waits(100);
            runs(function() {
                expect(callback).toHaveBeenCalledWith(new StackFrame('baz', [], 'http://localhost:9999/file.js', 3, 3));
                expect(errback).not.toHaveBeenCalled();
            });
        });

        it('resolves to undefined if function name could not be found', function () {
            runs(function() {
                var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 0);
                new StackTraceGPS().findFunctionName(stackframe).then(callback, errback);
                server.requests[0].respond(200, { 'Content-Type': 'application/x-javascript' }, '');
            });
            waits(100);
{'fraction_non_alphanumeric': 0.1448341836734694, 'fraction_numerical': 0.018558673469387756, 'mean_word_length': 2.0777232580961726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899828', 'n_tokens_mistral': 4317, 'n_tokens_neox': 4023, 'n_words': 802}