<commit_before>import { expect } from 'chai';
import { mock, SinonMock } from 'sinon';
import { DeserializeAs, DeserializeFieldName, FieldName, Serializer } from '../src';

class Foo {
    public attrString: string;
    public attrNumber: number;

    public getMe(): string {
        return this.attrString + ' - ' + this.attrNumber;
    }
}

class Bar {
    prop: string;

    getProp(): string {
        return this.prop;
    }
}

class Baz {
    @DeserializeAs(Bar)
    bar: Bar;
}

class BazArray {
    @DeserializeAs([Bar])
    bars: Bar[];
}

class DifferentFieldName {
    @FieldName('foo')
    bar: string;
}

class DifferentFieldNames {
    @FieldName('foo')
    bar: string;

    @DeserializeFieldName('name')
    firstName: string;

    @FieldName('count')
    length: number;
}

class NotOnlyDifferentFieldName {
    @DeserializeFieldName('foo')
    bar: string;

    hey: string;
}

class DifferentFieldNameWithClass {
    @DeserializeAs(Bar)
    @FieldName('foo')
    bar: Bar;
}

class DifferentFieldNameArray {
    @FieldName('foo')
    bar: number[];
}

describe('Serializer service', () => {

    let serializer: Serializer;

    it('Should have a working constructor', () => {
        expect(new Serializer()).to.be.instanceof(Serializer);
    });

    describe('Basic deserialization tests', () => {

        beforeEach(() => {
            serializer = new Serializer();
        });

        it('Should deserialize class instance', (() => {
            const res = serializer.deserialize<Foo>({
                'attrString': 'val',
                'attrNumber': 5,
                'attrBoolean': true
            }, Foo);
            expect(res).to.be.instanceof(Foo);
            expect(res.getMe()).to.eql('val - 5');
        }));

        it('Should deserialize array of class instances', (() => {
            const res = serializer.deserialize<Foo>([
                {
                    'attrString': 'val',
                    'attrNumber': 5,
                    'attrBoolean': true,
                }, {
                    'attrString': 'foo',
                    'attrNumber': 10
                },
            ], [Foo]);
            expect(res).to.be.an('array').of.length(2);

            expect(res[0]).to.be.instanceof(Foo);
            expect(res[0].getMe()).to.eql('val - 5');

            expect(res[1]).to.be.instanceof(Foo);
            expect(res[1].getMe()).to.eql('foo - 10');
        }));
    });
<commit_msg>test(serializer): test incoherent input params for deserialization<commit_after>import { expect } from 'chai';
import { mock, SinonMock } from 'sinon';
import { DeserializeAs, DeserializeFieldName, FieldName, Serializer } from '../src';

class Foo {
    public attrString: string;
    public attrNumber: number;

    public getMe(): string {
        return this.attrString + ' - ' + this.attrNumber;
    }
}

class Bar {
    prop: string;

    getProp(): string {
        return this.prop;
    }
}

class Baz {
    @DeserializeAs(Bar)
    bar: Bar;
}

class BazArray {
    @DeserializeAs([Bar])
    bars: Bar[];
}

class DifferentFieldName {
    @FieldName('foo')
    bar: string;
}

class DifferentFieldNames {
    @FieldName('foo')
    bar: string;

    @DeserializeFieldName('name')
    firstName: string;

    @FieldName('count')
    length: number;
}

class NotOnlyDifferentFieldName {
    @DeserializeFieldName('foo')
    bar: string;

    hey: string;
}

class DifferentFieldNameWithClass {
    @DeserializeAs(Bar)
    @FieldName('foo')
    bar: Bar;
}

class DifferentFieldNameArray {
    @FieldName('foo')
    bar: number[];
}

describe('Serializer service', () => {

    let serializer: Serializer;

    it('Should have a working constructor', () => {
        expect(new Serializer()).to.be.instanceof(Serializer);
    });

    describe('Basic deserialization tests', () => {

        beforeEach(() => {
            serializer = new Serializer();
        });

        it('Should deserialize class instance', (() => {
            const res = serializer.deserialize<Foo>({
                'attrString': 'val',
                'attrNumber': 5,
                'attrBoolean': true
            }, Foo);
            expect(res).to.be.instanceof(Foo);
            expect(res.getMe()).to.eql('val - 5');
        }));

        it('Should deserialize array of class instances', (() => {
            const res = serializer.deserialize<Foo>([
                {
                    'attrString': 'val',
                    'attrNumber': 5,
                    'attrBoolean': true,
                }, {
                    'attrString': 'foo',
                    'attrNumber': 10
                },
            ], [Foo]);
            expect(res).to.be.an('array').of.length(2);

            expect(res[0]).to.be.instanceof(Foo);
            expect(res[0].getMe()).to.eql('val - 5');

            expect(res[1]).to.be.instanceof(Foo);
            expect(res[1].getMe()).to.eql('foo - 10');
        }));

        it('Should throw an error if we ask to deserialize an array but the given param is not', () => {
            const obj: any = {attrString: 'val', attrNumber: 5};
            expect(() => serializer.deserialize(obj, [Foo]))
                .to.throw(TypeError, 'Deserializing an array of Foo can only work with an array of objects.');
        });

        it('Should throw an error if we ask to deserialize an object but the given param is not', () => {
            const obj: any = [{attrString: 'val', attrNumber: 5}];
            expect(() => serializer.deserialize(obj, Foo))
                .to.throw(TypeError, 'Deserializing an instance of Foo can only work with an object, but array given.');
        });

    });

{'fraction_non_alphanumeric': 0.14745732887559387, 'fraction_numerical': 0.004926975189160654, 'mean_word_length': 2.0707725553754726, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '3480118', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1635, 'n_words': 379}