Skip to content

Commit baedace

Browse files
#98 : test cases and bug fixes
1 parent f27d20e commit baedace

File tree

4 files changed

+981
-53
lines changed

4 files changed

+981
-53
lines changed

src/commands/showfile.ts

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export default class ShowFile extends Command {
99

1010
static flags = {
1111
help: flags.help({char: 'h'}),
12-
file: flags.string({char: 'f' , description: 'formatted file to be shown'})
12+
file: flags.string({char: 'f' , description: 'formatted file to be shown'}),
13+
num: flags.string({char: 'n' , description: 'rows to show'})
1314
}
1415

1516
static args = [{name: 'file'}]
@@ -19,6 +20,9 @@ export default class ShowFile extends Command {
1920
const {args, flags} = this.parse(ShowFile)
2021

2122
args.file = this.getFilePath(flags, args)
23+
args.num = this.getFileLinesToShow(flags)
24+
25+
// args.rowsToShow
2226

2327
this.checkParameters(flags, args)
2428
this.showFile(args)
@@ -28,83 +32,71 @@ export default class ShowFile extends Command {
2832
if (args.file)
2933
return args.file
3034
if (flags.file)
31-
return flags.file
35+
return flags.file9
3236
Logger.error(this, 'File path not passed')
3337
}
3438

39+
private getFileLinesToShow(flags: any) {
40+
return flags.num || 10
41+
}
3542
// tslint:disable-next-line:no-unused
3643
private checkParameters(flags: unknown, args: { [p: string]: any }) {
3744
if (args.file === undefined || args.file === '')
3845
Logger.error(this, 'File path is empty')
3946
// others already checked
4047
}
4148

42-
4349
private showFile(args: any) {
44-
4550
let data = Utilities.getStringFromFile(this, args.file)
46-
4751
let rows = data.split('\n')
48-
4952
let widthArray = []
5053

51-
let recordsToShow = 10
54+
let recordsToShow = parseInt(args.num, 10) + 1
5255

53-
for(let i = 0; i < rows[0].length; i++){
54-
widthArray[i] = 0
56+
for (let i = 0; i < rows[0].length; i++) {
57+
widthArray[i] = 0
5558
}
5659

57-
if(recordsToShow > rows.length){
58-
recordsToShow = rows.length
60+
if (recordsToShow > rows.length) {
61+
recordsToShow = rows.length - 1
5962
}
6063

61-
for(let i = 0; i < recordsToShow; i++){
62-
let row = rows[i].split(",");
63-
let s = ''
64-
for(let j = 0; j < row.length; j ++){
65-
s = s + row[j]
66-
if(widthArray[j] < row[j].length){
67-
widthArray[j] = row[j].length
68-
}
64+
for (let i = 0; i < recordsToShow; i++) {
65+
let row = rows[i].split(',')
66+
for (let j = 0; j < row.length; j ++) {
67+
if (widthArray[j] < row[j].length) {
68+
widthArray[j] = row[j].length
6969
}
70+
}
7071
}
7172

72-
this.printRow(rows[0].split(","), widthArray, "+", true)
73-
for(let i = 0; i < recordsToShow-1; i++){
74-
let row = rows[i]
75-
this.printRow(row.split(','), widthArray, '|', false)
76-
this.printRow(row.split(','), widthArray, '+', true)
73+
this.printRow(rows[0].split(','), widthArray, '+', true)
74+
for (let i = 0; i < recordsToShow; i++) {
75+
let row = rows[i]
76+
this.printRow(row.split(','), widthArray, '|', false)
77+
this.printRow(row.split(','), widthArray, '+', true)
7778
}
78-
79+
this.log(`showing top ${recordsToShow} rows.`)
7980
}
8081

81-
private printRow(row: any, widthArray: any, seperator: any, isLineSeparator: any){
82-
let output = seperator
83-
for(let i = 0; i < row.length; i ++){
84-
let totalSize = widthArray[i]
85-
86-
let dataLength = 0;
87-
let space = "-"
88-
if(!isLineSeparator){
89-
let data = row[i]
90-
data = data.split(/\r/)[0]
91-
output += data
92-
dataLength = data.length
93-
space = ' '
94-
// this.log(`The output log upto here is : ${output}`)
95-
}
96-
97-
for(let j = 0; j < totalSize - dataLength; j++){
98-
output += space
99-
}
100-
101-
output += seperator
102-
if(!isLineSeparator){
103-
// this.log(`The output log upto here is : ${output}`)
104-
// Logger.info(this, output)
105-
}
82+
private printRow(row: any, widthArray: any, seperator: any, isLineSeparator: any) {
83+
let output = seperator
84+
for (let i = 0; i < row.length; i ++) {
85+
let totalSize = widthArray[i]
86+
let dataLength = 0
87+
let space = '-'
88+
if (!isLineSeparator) {
89+
let data = row[i]
90+
data = data.split(/\r/)[0]
91+
output += data
92+
dataLength = data.length
93+
space = ' '
10694
}
107-
this.log('' + output)
108-
output = ''
95+
for (let j = 0; j < totalSize - dataLength; j++) {
96+
output += space
97+
}
98+
output += seperator
99+
}
100+
this.log('' + output)
109101
}
110102
}

test/commands/avro.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('avro', () => {
5757
.it('if get_schema outputs to console', ctx => {
5858
setTimeout(() =>
5959
expect(ctx.stdout).to.contain('success')
60-
, 5000) // wait for it to write stuff on console
60+
, 9000) // wait for it to write stuff on console
6161
})
6262

6363
test

test/commands/showfile.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {expect, test} from '@oclif/test'
2+
3+
describe('showfile', () => {
4+
test
5+
.stdout()
6+
.command(['showfile'])
7+
.exit(0)
8+
.it('File path not passed', ctx => {
9+
expect(ctx.stdout).to.contain('File path not passed')
10+
})
11+
12+
test
13+
.stdout()
14+
.command(['showfile', 'test/resources/showfile.csv'])
15+
.it('check 10 lines are found', ctx => {
16+
expect(ctx.stdout).to.contain('|1 |2 |1 |14.0 |30.0708|1 |0 |')
17+
})
18+
test
19+
.stdout()
20+
.command(['showfile', 'test/resources/showfile.csv', '-n', '5'])
21+
.it('check 5th line is found', ctx => {
22+
expect(ctx.stdout).to.contain('|0 |3 |0 |35.0|8.05 |0 |0 |')
23+
})
24+
25+
test
26+
.stdout()
27+
.command(['showfile', 'test/resources/showfile.csv', '-n', '900'])
28+
.it('check if the given number is greater than total lines', ctx => {
29+
expect(ctx.stdout).to.contain('|0 |3 |0 |32.0 |7.75 |0 |0 |')
30+
})
31+
test
32+
.stdout()
33+
.command(['showfile', 'test/resources/showfile.csv', '-n', '900'])
34+
.it('check if 900 count is used, it shows only total present rows', ctx => {
35+
expect(ctx.stdout).to.contain('showing top 892 rows.')
36+
})
37+
test
38+
.stdout()
39+
.command(['showfile', 'test/resources/showfile.csv', '-n', '0'])
40+
.it('check if the given number is greater than total lines', ctx => {
41+
expect(ctx.stdout).to.contain('|Survived|Pclass|Sex|Age|Fare|Family_count|Cabin_ind |')
42+
})
43+
//|Survived|Pclass|Sex|Age|Fare|Family_count|Cabin_ind |
44+
})

0 commit comments

Comments
 (0)