'use strict';

/**
 * 辅助方法扩展 单元测试
 *
 * @author Mai
 * @data 2018/4/20
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
const mathjs = require('mathjs');

describe('test/app/extend/helper.test.js', () => {
    it('ValidTemplateCode test', function () {
        const ctx = app.mockContext();
        assert(ctx.helper.ValidTemplateCode('z') === true);
        assert(ctx.helper.ValidTemplateCode('Z') === true);
        assert(ctx.helper.ValidTemplateCode('-') === false);
        assert(ctx.helper.ValidTemplateCode('1') === false);
        assert(ctx.helper.ValidTemplateCode('z1') === true);
        assert(ctx.helper.ValidTemplateCode('z1-') === false);
        assert(ctx.helper.ValidTemplateCode('zav') === true);
        assert(ctx.helper.ValidTemplateCode('z1-e') === true);
        assert(ctx.helper.ValidTemplateCode('z1-e-1') === true);
    });
    it('ValidTemplateIndexCode test', function () {
        const ctx = app.mockContext();
        assert(ctx.helper.ValidTemplateIndexCode('z2-d-d-1～z2-d-d-5   高填方路段处理   同z2-d-c-1～z2-d-c-5') === false);
    });
    it('validMatchCode test', function () {
        const ctx = app.mockContext();
        assert(ctx.helper.validMatchCode('1-1-1'));
        assert(ctx.helper.validMatchCode('1-1-X'));
        assert(ctx.helper.validMatchCode('1-X-1'));
        assert(!ctx.helper.validMatchCode('1-1-'));
        assert(!ctx.helper.validMatchCode('X-1-1'));
        assert(!ctx.helper.validMatchCode('1-人-1'));
    });
    it('loadJsonFile test', function () {
        const ctx = app.mockContext();
        const vJ = ctx.helper.loadJsonFile(app.baseDir + '/test/app/extend/test.json');
        assert(vJ.bills);
        assert(vJ.properties);
    });
    it('operationJson test', function () {
        const ctx = app.mockContext();
        let testjson = {name: 'hello'};
        const result = ctx.helper.operationJson(testjson,'ok','world');
        assert(result.ok === 'world');
    });
    it('calcExprStr test', function () {
        const ctx = app.mockContext();
        assert(ctx.helper.calcExprStr('1+2').toFixed(2) == 3);
        assert(ctx.helper.calcExprStr('1*2').toFixed(2) == 2);
        assert(ctx.helper.calcExprStr('-1/2').toFixed(2) == -0.5);
        assert(ctx.helper.calcExprStr('1-2').toFixed(2) == -1);

        assert(ctx.helper.calcExprStr('1-2+3').toFixed(2) == 2);
        assert(ctx.helper.calcExprStr('1-2*2').toFixed(2) == -3);
        assert(ctx.helper.calcExprStr('1-2/2').toFixed(2) == 0);

        assert(ctx.helper.calcExprStr('(2-1)/2').toFixed(2) == 0.5);
        assert(ctx.helper.calcExprStr('(2+1)*2').toFixed(2) == 6);

        assert(ctx.helper.calcExprStr('(2+1)*(3-2)').toFixed(2) == 3);
        assert(ctx.helper.calcExprStr('(327+3)*(892-882)').toFixed(2) == 3300);

        assert(!ctx.helper.calcExprStr('1-'));
        assert(!ctx.helper.calcExprStr('1+'));
        assert(!ctx.helper.calcExprStr('1/'));
        assert(!ctx.helper.calcExprStr('1*'));
        assert(!ctx.helper.calcExprStr('2/0'));
        assert(ctx.helper.calcExprStr('-3').toFixed(2) == -3);
    });
    it('calcExprStrRpn test', function () {
        const ctx = app.mockContext();
        assert(ctx.helper.calcExprStrRpn('1+2').toFixed(2) == 3);
        assert(ctx.helper.calcExprStrRpn('1*2').toFixed(2) == 2);
        assert(ctx.helper.calcExprStrRpn('-1/2').toFixed(2) == -0.5);
        assert(ctx.helper.calcExprStrRpn('1-2').toFixed(2) == -1);

        assert(ctx.helper.calcExprStrRpn('1-2+3').toFixed(2) == 2);
        assert(ctx.helper.calcExprStrRpn('1-2*2').toFixed(2) == -3);
        assert(ctx.helper.calcExprStrRpn('1-2/2').toFixed(2) == 0);

        assert(ctx.helper.calcExprStrRpn('(2-1)/2').toFixed(2) == 0.5);
        assert(ctx.helper.calcExprStrRpn('(2+1)*2').toFixed(2) == 6);

        assert(ctx.helper.calcExprStrRpn('(2+1)*(3-2)').toFixed(2) == 3);
        assert(ctx.helper.calcExprStrRpn('(327+3)*(892-882)').toFixed(2) == 3300);

        assert(!ctx.helper.calcExprStrRpn('1-'));
        assert(!ctx.helper.calcExprStrRpn('1+'));
        assert(!ctx.helper.calcExprStrRpn('1/'));
        assert(!ctx.helper.calcExprStrRpn('1*'));
        assert(!ctx.helper.calcExprStrRpn('2/0'));
        assert(ctx.helper.calcExprStrRpn('-3').toFixed(2) == -3);
    });
});