-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactionsOperationsTest.js
138 lines (136 loc) · 3.7 KB
/
transactionsOperationsTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const sort=require('./transactionsOperations').sortTransactions;
const fs=require('fs');
const readline=require('readline');
const path=require('path');
const assert=require('assert');
var events=require('events');
function TransactionSortTester(testDirectory){
var self=this;
self.testDirectory=testDirectory;
var rl;
var EventEmitter=new events.EventEmitter();
var transactions=[];
var isFirst=true;
var isInput = false;
var isOutput=false;
var isPulse=false;
var isDigest=false;
var isExpected=false;
var transaction={};
var expected;
var input={};
var output={};
function getFiles(testDirectory){
var memberFiles=fs.readdirSync(testDirectory);
for(var i=0; i<memberFiles.length; i++){
memberFiles[i]=path.resolve(self.testDirectory+'\\'+memberFiles[i]);
}
return memberFiles;
}
var files=getFiles(self.testDirectory);
function readFromFile(testFile){
rl=readline.createInterface({
input: fs.createReadStream(testFile,'utf-8'),
output: process.stdout
});
rl.on('line',processLine);
rl.on('close',getTransactions);
}
function processLine(line) {
if(line === ''){
return;
}
if(line === 'input'){
if(!isFirst){
transactions.push(transaction);
}
transaction={};
input={};
output={};
isInput=true;
isFirst=false;
return;
}
if(line === 'output'){
isInput=false;
isOutput=true;
return;
}
if(line === 'pulse'){
isOutput=false;
isPulse=true;
return;
}
if(line === 'digest'){
isPulse=false;
isDigest=true;
return;
}
if(line === 'expected'){
isFirst=true;
isInput = false;
isOutput=false;
isPulse=false;
isDigest=false;
isExpected=true;
return;
}
if(isInput){
var nameVersionPair=line.split(' ');
var keyName='key'+nameVersionPair[0];
var key={};
key.name=keyName;
key.version=nameVersionPair[1];
input[keyName]=key;
transaction.input=input;
return;
}
if(isOutput){
var nameVersionPair=line.split(' ');
var keyName='key'+nameVersionPair[0];
var key={};
key.name=keyName;
key.version=nameVersionPair[1];
output[keyName]=key;
transaction.output=output;
return;
}
if(isPulse){
transaction.pulse=line;
return;
}
if(isDigest){
transaction.digest=line;
return;
}
if(isExpected){
expected=line.split(' ');
isExpected=false;
return;
}
}
function getTransactions(){
transactions.push(transaction);
testResults();
}
function testResults(){
sort(transactions);
var result=[];
for(var i=0; i<transactions.length; i++){
result.push(transactions[i].digest);
}
transactions=[];
if(assert.deepEqual(result,expected) == undefined){
console.log('Test passed');
}
EventEmitter.emit('done');
}
EventEmitter.on('done',function(){
if(files.length){
var file=files.shift();
readFromFile(file);
}
});
readFromFile(files.shift());
}
var tester=new TransactionSortTester('./testDirectory');