<commit_before>/// <reference path='tape.d.ts' />
import {
    observe, computed, observable, autorun, autorunAsync, extendObservable, action,
    IObservableObject, IObservableArray, IArrayChange, IArraySplice, IObservableValue, isObservable, isObservableObject,
    extras, Atom, transaction, IObjectChange, spy, useStrict, isAction
} from "../../lib/mobx";
import * as test from 'tape';
import * as mobx from "../../lib/mobx";

var v = observable(3);
observe(v, () => {});

var a = observable([1,2,3]);

var testFunction = function(a:any) {};



class Order {
    @observable price:number = 3;
    @observable amount:number = 2;
    @observable orders:string[] = [];
    @observable aFunction = testFunction;

    @computed get total() {
        return this.amount * this.price * (1 + this.orders.length);
    }

    // Typescript classes cannot be defined inside functions,
    // but if the next line is enabled it should throw...
    // @observable hoepie() { return 3; }
}

test('decorators', function(t) {
	var o = new Order();
	t.equal(isObservableObject(o), true);
	t.equal(isObservable(o, 'amount'), true);
	t.equal(isObservable(o, 'total'), true);

	var events: any[] = [];
	var d1 = observe(o, (ev: IObjectChange) => events.push(ev.name, ev.oldValue));
	var d2 = observe(o, 'price', (ev) => events.push(ev.newValue, ev.oldValue));
	var d3 = observe(o, 'total', (ev) => events.push(ev.newValue, ev.oldValue));

	o.price = 4;

	d1();
	d2();
	d3();

	o.price = 5;

	t.deepEqual(events, [
		8, // new total
		6, // old total
		4, // new price
		3, // old price
		"price", // event name
		3, // event oldValue
	]);

	t.end();
})

test('observable', function(t) {
    var a = observable(3);
    var b = computed(() => a.get() * 2);
    t.equal(b.get(), 6);
    t.end();
})

test('annotations', function(t) {
    var order1totals:number[] = [];
    var order1 = new Order();
    var order2 = new Order();

    var disposer = autorun(() => {
        order1totals.push(order1.total)
    });

    order2.price = 4;
    order1.amount = 1;

    t.equal(order1.price, 3);
    t.equal(order1.total, 3);
    t.equal(order2.total, 8);
    order2.orders.push('bla');
    t.equal(order2.total, 16);

    order1.orders.splice(0,0,'boe', 'hoi');
    t.deepEqual(order1totals, [6,3,9]);

    disposer();
    order1.orders.pop();
    t.equal(order1.total, 6);
    t.deepEqual(order1totals, [6,3,9]);

    t.equal(order1.aFunction, testFunction);
    var x = function() { return 3; };
    order1.aFunction = x;
    t.equal(order1.aFunction, x);

    t.end();
})

test('scope', function(t) {
    var x = observable({
        y: 3,
        // this wo't work here.
        get z () {
			return 2 * this.y
		}
    });

    t.equal(x.z, 6);
    x.y = 4;
    t.equal(x.z, 8);

    interface IThing {
        z: number;
        y: number;
    }

    const Thing = function() {
        extendObservable(this, {
            y: 3,
            // this will work here
            z: computed(() => 2 * this.y)
        });
    }

    var x3: IThing = new (<any>Thing)();
    t.equal(x3.z, 6);
    x3.y = 4;
    t.equal(x3.z, 8);

    t.end();
})

test('typing', function(t) {
    var ar:IObservableArray<number> = observable([1,2]);
    ar.observe((d:IArrayChange<number>|IArraySplice<number>) => {
        console.log(d.type);
    });

    var ar2:IObservableArray<number> = observable([1,2]);
    ar2.observe((d:IArrayChange<number>|IArraySplice<number>) => {
        console.log(d.type);
    });

    var x:IObservableValue<number> = observable(3);

    var d2 = autorunAsync(function() {<commit_msg>Add unit test for new intercept typing<commit_after>/// <reference path='tape.d.ts' />
import {
    observe, computed, observable, autorun, autorunAsync, extendObservable, action,
    IObservableObject, IObservableArray, IArrayChange, IArraySplice, IArrayWillChange, IArrayWillSplice,
    IObservableValue, isObservable, isObservableObject,
    extras, Atom, transaction, IObjectChange, spy, useStrict, isAction
} from "../../lib/mobx";
import * as test from 'tape';
import * as mobx from "../../lib/mobx";

var v = observable(3);
observe(v, () => {});

var a = observable([1,2,3]);

var testFunction = function(a:any) {};



class Order {
    @observable price:number = 3;
    @observable amount:number = 2;
    @observable orders:string[] = [];
    @observable aFunction = testFunction;

    @computed get total() {
        return this.amount * this.price * (1 + this.orders.length);
    }

    // Typescript classes cannot be defined inside functions,
    // but if the next line is enabled it should throw...
    // @observable hoepie() { return 3; }
}

test('decorators', function(t) {
	var o = new Order();
	t.equal(isObservableObject(o), true);
	t.equal(isObservable(o, 'amount'), true);
	t.equal(isObservable(o, 'total'), true);

	var events: any[] = [];
	var d1 = observe(o, (ev: IObjectChange) => events.push(ev.name, ev.oldValue));
	var d2 = observe(o, 'price', (ev) => events.push(ev.newValue, ev.oldValue));
	var d3 = observe(o, 'total', (ev) => events.push(ev.newValue, ev.oldValue));

	o.price = 4;

	d1();
	d2();
	d3();

	o.price = 5;

	t.deepEqual(events, [
		8, // new total
		6, // old total
		4, // new price
		3, // old price
		"price", // event name
		3, // event oldValue
	]);

	t.end();
})

test('observable', function(t) {
    var a = observable(3);
    var b = computed(() => a.get() * 2);
    t.equal(b.get(), 6);
    t.end();
})

test('annotations', function(t) {
    var order1totals:number[] = [];
    var order1 = new Order();
    var order2 = new Order();

    var disposer = autorun(() => {
        order1totals.push(order1.total)
    });

    order2.price = 4;
    order1.amount = 1;

    t.equal(order1.price, 3);
    t.equal(order1.total, 3);
    t.equal(order2.total, 8);
    order2.orders.push('bla');
    t.equal(order2.total, 16);

    order1.orders.splice(0,0,'boe', 'hoi');
    t.deepEqual(order1totals, [6,3,9]);

    disposer();
    order1.orders.pop();
    t.equal(order1.total, 6);
    t.deepEqual(order1totals, [6,3,9]);

    t.equal(order1.aFunction, testFunction);
    var x = function() { return 3; };
    order1.aFunction = x;
    t.equal(order1.aFunction, x);

    t.end();
})

test('scope', function(t) {
    var x = observable({
        y: 3,
        // this wo't work here.
        get z () {
			return 2 * this.y
		}
    });

    t.equal(x.z, 6);
    x.y = 4;
    t.equal(x.z, 8);

    interface IThing {
        z: number;
        y: number;
    }

    const Thing = function() {
        extendObservable(this, {
            y: 3,
            // this will work here
            z: computed(() => 2 * this.y)
        });
    }

    var x3: IThing = new (<any>Thing)();
    t.equal(x3.z, 6);
    x3.y = 4;
    t.equal(x3.z, 8);

    t.end();
})

test('typing', function(t) {
    var ar:IObservableArray<number> = observable([1,2]);
    ar.intercept((c:IArrayWillChange<number>|IArrayWillSplice<number>) => {
        console.log(c.type);
    });
    ar.observe((d:IArrayChange<number>|IArraySplice<number>) => {
        console.log(d.type);
    });

    var ar2:IObservableArray<number> = observable([1,2]);
    ar2.intercept((c:IArrayWillChange<number>|IArrayWillSplice<number>) => {
        console.log(c.type);
    });
    ar2.observe((d:IArrayChange<number>|IArraySplice<number>) => {
        console.log(d.type);
    });

    var x:IObservableValue<number> = observable(3);

    var d2 = autorunAsync(function() {
{'fraction_non_alphanumeric': 0.18035426731078905, 'fraction_numerical': 0.02241009125067096, 'mean_word_length': 2.8122762148337594, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 45, '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': '6183764', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2707, 'n_words': 594}