-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split StopwatchEvent into a separate file, updated and added addition…
…al tests
- Loading branch information
1 parent
dbe0a8b
commit 1d8068b
Showing
7 changed files
with
363 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { end } from '../lib/utils'; | ||
import { Stopwatch } from './Stopwatch'; | ||
|
||
export class StopwatchEvent { | ||
protected name: string | undefined; | ||
protected laps: number[] = []; | ||
protected startedAt: number; | ||
protected endedAt: number; | ||
protected lapTime: number; | ||
|
||
constructor(sw: Stopwatch, lapTime: number | null = null) { | ||
this.name = sw.name?.slice(0); | ||
this.laps = sw.laps.slice(0); | ||
this.startedAt = sw.startedAt; | ||
this.endedAt = sw.endedAt; | ||
this.lapTime = lapTime ?? new Date().getTime(); | ||
} | ||
|
||
public getDuration(): number { | ||
return this.laps.reduce((prev, cur) => cur + prev, 0); | ||
} | ||
|
||
public getMemory() { | ||
return 0; | ||
} | ||
|
||
public getPeriods(): number[] { | ||
return this.laps.slice(); | ||
} | ||
|
||
public getPreviousDuration(): number { | ||
return end(this.laps) - (new Date().getTime() - this.lapTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
/* eslint-disable no-undef */ | ||
|
||
import { usleep } from '../../src/lib/utils'; | ||
import Stopwatch from './../../src/Stopwatch/Stopwatch'; | ||
|
||
let stopwatch: Stopwatch; | ||
|
||
beforeEach(() => { | ||
stopwatch = new Stopwatch('one'); | ||
}); | ||
|
||
describe('Stopwatch', () => { | ||
it('can created an unnamed stopwatch', () => { | ||
const sw = new Stopwatch(); | ||
expect(sw.name).toBeUndefined(); | ||
}); | ||
|
||
it('can be reset', () => { | ||
stopwatch.startedAt = 100; | ||
stopwatch.endedAt = 200; | ||
|
||
stopwatch.start('one'); | ||
stopwatch.lap(); | ||
stopwatch.stop(); | ||
|
||
stopwatch.reset(); | ||
|
||
expect(stopwatch.startedAt).toBe(0); | ||
expect(stopwatch.endedAt).toBe(0); | ||
expect(stopwatch.getLaps().length).toBe(0); | ||
}); | ||
|
||
it('can get the total duration', async () => { | ||
stopwatch.start('one'); | ||
usleep(100); | ||
stopwatch.stop(); | ||
|
||
expect(Math.floor(stopwatch.totalDuration() / 10)).toBe(10); | ||
}); | ||
|
||
it('can get the previous duration', async () => { | ||
stopwatch.start('one'); | ||
usleep(100); | ||
const ev1 = stopwatch.lap(); | ||
usleep(100); | ||
const ev2 = stopwatch.lap(); | ||
expect(Math.floor(ev2.getPreviousDuration() / 10)).toBe(10); | ||
stopwatch.stop(); | ||
}); | ||
|
||
it('can get the laps', async () => { | ||
stopwatch.start('one'); | ||
usleep(5); | ||
stopwatch.stop(); | ||
|
||
expect(stopwatch.getLaps().length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.