Skip to content

Commit

Permalink
[test] add unit tests for parseSize
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhyde committed Aug 14, 2024
1 parent 3c5d573 commit b4dc04b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
8 changes: 1 addition & 7 deletions source/utils/parseSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ export const parseSize = (size: string): number => {
}

const value = Number.parseFloat(match[1] ?? '')
if (Number.isNaN(value)) {
throw new Error('Invalid numeric value')
}

const unit = match[2] ?? 'b'
if (!(unit in units)) {
throw new Error('Invalid unit')
}

// @ts-expect-error regex handles this
return Math.floor(value * units[unit])
}
59 changes: 59 additions & 0 deletions source/utils/parseSize.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from 'vitest'
import { parseSize } from './parseSize.ts' // Adjust the import path as needed

describe('parseSize', () => {
it('should parse bytes correctly', () => {
expect.assertions(3)

expect(parseSize('100b')).toBe(100)
expect(parseSize('100B')).toBe(100)
expect(parseSize('100')).toBe(100)
})

it('should parse kilobytes correctly', () => {
expect.assertions(3)

expect(parseSize('1kb')).toBe(1024)
expect(parseSize('1KB')).toBe(1024)
expect(parseSize('1.5kb')).toBe(1536)
})

it('should parse megabytes correctly', () => {
expect.assertions(3)

expect(parseSize('1mb')).toBe(1048576)
expect(parseSize('1MB')).toBe(1048576)
expect(parseSize('2.5mb')).toBe(2621440)
})

it('should parse gigabytes correctly', () => {
expect.assertions(3)

expect(parseSize('1gb')).toBe(1073741824)
expect(parseSize('1GB')).toBe(1073741824)
expect(parseSize('0.5gb')).toBe(536870912)
})

it('should handle whitespace', () => {
expect.assertions(3)

expect(parseSize('100 b')).toBe(100)
expect(parseSize('1 kb')).toBe(1024)
expect(parseSize('1 MB')).toBe(1048576)
})

it('should throw an error for invalid size format', () => {
expect.assertions(3)

expect(() => parseSize('100x')).toThrow('Invalid size format')
expect(() => parseSize('kb')).toThrow('Invalid size format')
expect(() => parseSize('')).toThrow('Invalid size format')
})

it('should round down to nearest integer', () => {
expect.assertions(2)

expect(parseSize('1.7b')).toBe(1)
expect(parseSize('2.8kb')).toBe(2867)
})
})

0 comments on commit b4dc04b

Please sign in to comment.