{"size": 5319, "ext": "js", "max_stars_repo_path": "test/interfaces.js", "max_stars_repo_name": "ArnaudBuchholz/gpf-js", "max_stars_repo_stars_event_min_datetime": "2015-05-15T18:42:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-03T23:03:32.000Z", "max_issues_repo_path": "test/interfaces.js", "max_issues_repo_name": "ArnaudBuchholz/gpf-js", "max_issues_repo_issues_event_min_datetime": "2015-01-28T00:04:32.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T00:45:14.000Z", "max_forks_repo_path": "test/interfaces.js", "max_forks_repo_name": "ArnaudBuchholz/gpf-js", "max_forks_repo_forks_event_min_datetime": "2019-06-19T16:41:22.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-02T15:41:50.000Z"}
{"max_stars_count": 14, "max_issues_count": 301, "max_forks_count": 4, "avg_line_length": 34.538961039, "max_line_length": 107, "alphanum_fraction": 0.5164504606}
"use strict";

describe("interfaces", function () {

    function _ignore () {}

    describe("gpf.interfaces.IUnknown", function () {

        it("exists", function () {
            assert(typeof gpf.interfaces.IUnknown === "function");
        });

        it("is an interface", function () {
            var exceptionCaught;
            try {
                var instance = new gpf.interfaces.IUnknown();
                assert(typeof instance.queryInterface === "function");
            } catch (e) {
                exceptionCaught = e;
            }
            assert(exceptionCaught instanceof gpf.Error.InterfaceConstructorFunction);
        });

    });

    describe("gpf.interfaces.isImplementedBy", function () {

        function TestRawClass () {}
        TestRawClass.prototype = {
            queryInterface: function (oneParameter) {
                return oneParameter;
            }
        };

        var TestGPFClass = gpf.define({
            $class: "TestGPFClass",
            queryInterface: function (oneParameter) {
                return oneParameter;
            }
        });

        it("validates literal objects (using interface constructor)", function () {
            assert(gpf.interfaces.isImplementedBy(gpf.interfaces.IUnknown, {
                queryInterface: function (oneParameter) {
                    return oneParameter;
                }
            }) === true);
        });

        it("validates instances from a raw JavaScript class (using interface name)", function () {
            var obj = new TestRawClass();
            assert(gpf.interfaces.isImplementedBy("gpf.interfaces.IUnknown", obj) === true);
        });

        it("validates a raw JavaScript class", function () {
            assert(gpf.interfaces.isImplementedBy("gpf.interfaces.IUnknown", TestRawClass) === true);
        });

        it("validates instances from a GPF class", function () {
            var obj = new TestGPFClass();
            assert(gpf.interfaces.isImplementedBy(gpf.interfaces.IUnknown, obj) === true);
        });

        it("validates a GPF class", function () {
            assert(gpf.interfaces.isImplementedBy(gpf.interfaces.IUnknown, TestGPFClass) === true);
        });

        [false, null, undefined, 0, NaN, ""].forEach(function (falsyValue) {
            it("rejects " + JSON.stringify(falsyValue), function () {
                assert(gpf.interfaces.isImplementedBy("gpf.interfaces.IUnknown", falsyValue) === false);
            });
        });

        it("rejects member if not a method", function () {
            assert(gpf.interfaces.isImplementedBy("gpf.interfaces.IUnknown", {
                queryInterface: "Not a method"
            }) === false);
        });

        it("rejects member if parameters count does not match (none)", function () {
            assert(gpf.interfaces.isImplementedBy("gpf.interfaces.IUnknown", {
                queryInterface: function () {}
            }) === false);
        });

        it("rejects member if parameters count does not match (more)", function () {
            assert(gpf.interfaces.isImplementedBy(gpf.interfaces.IUnknown, {
                queryInterface: function (a, b, c) {
                    return a + b + c;
                }
            }) === false);
        });

    });

    describe("gpf.interfaces.query", function () {

        function ITest () {}
        ITest.prototype.test = function (a, b) {
            return a + b;
        };

        it("returns the object if it implements the interface (using interface name)", function () {
            var obj = {
                queryInterface: function (oneParameter) {
                    return oneParameter;
                }
            };
            assert(obj === gpf.interfaces.query("gpf.interfaces.IUnknown", obj));
        });

        it("returns the object if it implements the interface (using interface specifier)", function () {
            var obj = {
                test: function (a, b) {
                    _ignore(a, b);
                }
            };
            assert(obj === gpf.interfaces.query(ITest, obj));
        });

        it("returns null if IUnknown is implemented but not the expected interface", function () {
            assert(gpf.interfaces.query(ITest, {
                queryInterface: function (oneParameter) {
                    _ignore(oneParameter);
                    return null; // Because this is the expected interface
                }
            }) === null);
        });

        it("uses IUnknown to get the expected interface", function () {
            assert(gpf.interfaces.query(ITest, {
                queryInterface: function (interfaceSpecifier) {
                    assert(interfaceSpecifier === ITest);
                    return {
                        test: function (a, b) {
                            _ignore(a, b);
                        }
                    };
                }
            }) !== null);
        });

        it("returns undefined if neither the expected interface or IUnknown are implemented", function () {
            assert(undefined === gpf.interfaces.query(ITest, {
                queryInterface: function () { // Not matching IUnknown
                    return null;
                }
            }));
        });

    });

});
