command-parser.js 2.22 KB
"use strict";
/* global describe it */

const should = require('should');

const commandParser = require('../lib/command-parser');
describe('#command-parser', function() {

    describe('#splitToken', function() {
        it('should return correct tokens', function() {
            commandParser.splitToken('SAL.1234')[0].should.equal('SAL');
            commandParser.splitToken('SAL')[0].should.equal('SAL');
            
            commandParser.splitToken('SAL.1234')[1].should.equal('1234');
            commandParser.splitToken('SAL 1234')[1].should.equal('1234');
            
        })

        it('should handle repeating splitter', function() {
            commandParser.splitToken('SAL  1234')[1].should.equal('1234');
        })

        it('should handle empty message', function() {
            should.not.exists(commandParser.splitToken(''));
            should.not.exists(commandParser.splitToken());
        })
    })

    describe('#extractCommand', function() {
        it('should handle array of token as input', function() {
            commandParser.extractCommand(['SAL', '1234']).should.equal('SAL');
        })

        it('should handle message string as input', function() {
            commandParser.extractCommand('SAL.1234').should.equal('SAL');
            commandParser.extractCommand('SAL 1234').should.equal('SAL');
            commandParser.extractCommand('SAL  1234').should.equal('SAL');
        })

        it('should handle empty message', function() {
            should.not.exists(commandParser.extractCommand(''));
            should.not.exists(commandParser.extractCommand());
        })
    })

    describe('#extractCommandGroup', function() {
        it('should return correct command group from a message string', function() {
            commandParser.extractCommandGroup('SAL.1234').should.equal('balance');
        })

        it('should return correct command group from an array of tokens', function() {
            commandParser.extractCommandGroup(['SAL', '1234']).should.equal('balance');
        })

        it('should handle invalid message', function() {
            should.not.exists(commandParser.extractCommandGroup());
            should.not.exists(commandParser.extractCommandGroup(''));
        })
    })
    
})