var resource = require('../../lib/assert/resource');

var should = require('chai').should();

describe('assert.resource', function () {
    describe('hasExtension', function () {
        it('should throw exception if value undefined', function () {
            should.Throw(function () {
                resource.hasExtension();
            });
        });

        it('should return a function', function () {
            var result = resource.hasExtension('http://foo.com/bar');

            should.exist(result);
            result.should.be.a('Function');
        });

        it('should return false when resource does not have extension with url', function () {
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    }
                ]
            };

            var fn = resource.hasExtension('http://foo.com/bar');
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });

        it('should return true when resource has extension with url', function () {
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    },
                    {
                        url: 'http://foo.com/bar'
                    }
                ]
            };

            var fn = resource.hasExtension('http://foo.com/bar');
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(true);
        });
    });

    describe('hasExtensionEquals', function () {
        it('should throw exception if value undefined', function () {
            should.Throw(function () {
                resource.hasExtensionEquals();
            });
        });

        it('should return a function', function () {
            var match = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var result = resource.hasExtensionEquals(match);

            should.exist(result);
            result.should.be.a('Function');
        });

        it('should return false when resource does not have extension', function () {
            var match = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    }
                ]
            };

            var fn = resource.hasExtensionEquals('http://foo.com/bar');
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(false);
        });

        it('should return true when resource has extension', function () {
            var match = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    },
                    {
                        url: 'http://foo.com/bar',
                        valueFoo: 'bar'
                    }
                ]
            };

            var fn = resource.hasExtensionEquals(match);
            var result = fn(candidate);

            should.exist(result);
            result.should.equal(true);
        });
    });
});