Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
padsbanger committed Nov 6, 2015
1 parent 1c74324 commit dc9a329
Show file tree
Hide file tree
Showing 8 changed files with 2,716 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = function(grunt) {
karma: {
unit: {
configFile: 'karma.conf.js',
singleRun: true
singleRun: false
}
},

Expand Down
106 changes: 50 additions & 56 deletions js/src/directives/terminalDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pipboy.directive('terminal', function(CommandService) {
restirct: "E",
controller: 'terminalCtrl',
replace: true,
templateUrl: '/views/terminalTemplate.html',
templateUrl: 'views/terminalTemplate.html',
compile: function compile() {
return {
pre: function preLink() {},
Expand All @@ -14,18 +14,20 @@ pipboy.directive('terminal', function(CommandService) {
scope.prompt = '[~]$'
scope.currentDirName = '/home'
scope.currentDirIndex = 0
scope.commandLine = ''

var terminalWindow = element;
var terminal = document.querySelector('.terminal-main');
var input = angular.element(terminalWindow[0].querySelector('input[type="text"]'))
var results = angular.element(terminalWindow[0].querySelector('.terminal-results'))
var line = '';

var commandHistory = [];
var commandIndex = -1;
var currentDirList = {};
scope.commandHistory = [];
scope.commandIndex = -1;
scope.currentDirList = {};

CommandService.sendCommand('ls', scope.currentDirIndex).then(function(data) {
currentDirList = data.data.items
scope.currentDirList = data.data.items
start();
});

Expand Down Expand Up @@ -81,34 +83,30 @@ pipboy.directive('terminal', function(CommandService) {
var autocomplete = '';
if (Object.keys(data).length) {
for (var prop in data) {
// I want only first result, because.... give me a break;
// I want only first result, since server never returns more, even if i ask nicely
autocomplete = data[prop].name
break;
}

scope.commandLine = commandParser(scope.commandLine).command + ' ' + autocomplete
}

})
scope.$apply();
}

function drawLine(text, file) {
line = '';
var element = {}
if (text) {
element = document.createElement('pre');
if (!file) {
element = document.createElement('pre');
element.className = 'css-typing';
} else {
element = document.createElement('div');
}
} else {}
element.innerHTML = text;
results[0].appendChild(element)
}

var terminal = document.querySelector('.terminal-main');
terminal.scrollTop = terminal.scrollHeight;

terminalWindow[0].scrollTop = terminalWindow[0].scrollHeight;
}


Expand Down Expand Up @@ -140,8 +138,8 @@ pipboy.directive('terminal', function(CommandService) {
}

function handleCat(param) {
for (var prop in currentDirList) {
if (param === currentDirList[prop].name && currentDirList[prop].type === 'file') {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'file') {
CommandService.sendCommand('cat', prop).then(function(data) {
drawLine(data.data.content, true)
});
Expand All @@ -157,13 +155,13 @@ pipboy.directive('terminal', function(CommandService) {
function handleList(param) {

if (!param) {
for (var prop in currentDirList) {
line += currentDirList[prop].name + ' ';
for (var prop in scope.currentDirList) {
line += scope.currentDirList[prop].name + ' ';
}
drawLine(line);
} else {
for (var prop in currentDirList) {
if (param === currentDirList[prop].name) {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name) {
CommandService.sendCommand('ls', prop).then(function(data) {
var data = data.data.items;
for (var prop in data) {
Expand All @@ -182,51 +180,49 @@ pipboy.directive('terminal', function(CommandService) {

function handleCd(param) {
if (param) {
for (var prop in currentDirList) {
if (param === currentDirList[prop].name && currentDirList[prop].type === 'folder') {
for (var prop in scope.currentDirList) {
if (param === scope.currentDirList[prop].name && scope.currentDirList[prop].type === 'folder') {
scope.currentDirName += '/' + param;
scope.currentDirIndex = prop
scope.$apply();

CommandService.sendCommand('ls', prop).then(function(data) {
currentDirList = data.data.items
scope.currentDirList = data.data.items
})
break;
}
}
}
if (!param || param === '..') {
scope.currentDirName = '/home'
scope.currentDirIndex = 0
scope.$apply();

CommandService.sendCommand('ls', scope.currentDirIndex).then(function(data) {
currentDirList = data.data.items
scope.currentDirList = data.data.items
});
}

}

function nextCommand() {
if (commandIndex === -1) {
if (scope.commandIndex === -1) {
return;
}

if (commandIndex < commandHistory.length - 1) {
scope.commandLine = commandHistory[++commandIndex];
if (scope.commandIndex < scope.commandHistory.length - 1) {
scope.commandLine = scope.commandHistory[++scope.commandIndex];
scope.$apply();
} else {
scope.commandLine = '';
scope.$apply();
}
}

function previousCommand() {
if (commandIndex === -1) {
commandIndex = commandHistory.length;
if (scope.commandIndex === -1) {
scope.commandIndex = scope.commandHistory.length;
}
commandIndex--;
scope.commandIndex--;

scope.commandLine = commandHistory[commandIndex];
scope.$apply();
scope.commandLine = scope.commandHistory[scope.commandIndex];
}

function commandParser(commandString) {
Expand All @@ -238,42 +234,40 @@ pipboy.directive('terminal', function(CommandService) {
}
}

scope.inputActions = function($event) {

input.on('keydown', function(e) {
if (e.keyCode === 13) {

var commandObj = commandParser(scope.commandLine)

var fullCommandToPrint = scope.prompt + ' ' + scope.currentDirName + ' ' + scope.commandLine

drawLine(fullCommandToPrint)
commandHanlder(commandObj.command, commandObj.param)
commandHistory.push(scope.commandLine);

scope.commandLine = '';

scope.$apply();
}

if (e.keyCode === 38) {
if ($event.keyCode === 38) {
previousCommand();

}

if (e.keyCode === 40) {
if ($event.keyCode === 40) {
nextCommand();
}

if (e.keyCode === 9) {
e.preventDefault();
if ($event.keyCode === 9) {
$event.preventDefault();

var query = commandParser(scope.commandLine).param
if (query) {
handleAutoComplete(query)
}
}
});

}


scope.execute = function() {
var commandObj = commandParser(scope.commandLine)

var fullCommandToPrint = scope.prompt + ' ' + scope.currentDirName + ' ' + scope.commandLine

drawLine(fullCommandToPrint)
commandHanlder(commandObj.command, commandObj.param)
scope.commandHistory.push(scope.commandLine);

scope.commandLine = '';
}
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module.exports = function(config) {
config.set({
// Karma configuration

// base path, that will be used to resolve files and exclude
basePath: '',

// list of files / patterns to load in the browser
files: [
'libs/angular.min.js',
'views/*.html',
'libs/angular-mocks.js',
'js/src/**/*.js',
'tests/*.js'
],

preprocessors: {
'views/**/*.html': ['html2js'],
'js/src/**/*.js': 'coverage'
},

frameworks: [
'jasmine'
],

coverageReporter: {
type: 'lcov',
dir: 'test-coverage/'
},

// list of files to exclude
exclude: [],

// test results reporter to use
// possible values: dots || progress || growl
reporters: ['progress'],

// web server port
port: 8080,

// cli runner port
runnerPort: 9100,

// enable / disable colors in the output (reporters and logs)
colors: true,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 5000,

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true

});
};
Loading

0 comments on commit dc9a329

Please sign in to comment.