-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add json,number,storage,string spec (#5)
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { tryParseJSON, prettyJSONObject } from '../src/json' | ||
|
||
describe('JSON utility functions', () => { | ||
it('should parse valid JSON strings', () => { | ||
const jsonString = '{"key": "value"}' | ||
const result = tryParseJSON(jsonString) | ||
expect(result).toEqual({ key: 'value' }) | ||
}) | ||
|
||
it('should return undefined for invalid JSON strings', () => { | ||
const invalidJsonString = '{"key": "value"' | ||
const result = tryParseJSON(invalidJsonString) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should pretty print JSON objects', () => { | ||
const jsonObject = { key: 'value', number: 42 } | ||
const prettyString = prettyJSONObject(jsonObject) | ||
expect(prettyString).toBe('{\n "key": "value",\n "number": 42\n}') | ||
}) | ||
}) |
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,44 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { toNumber, clamp, clampArrayRange, genNumberKey, randomNumber } from '../src/number' | ||
|
||
describe('Number utility functions', () => { | ||
it('should convert various types to number', () => { | ||
expect(toNumber(null)).toBe(0) | ||
expect(toNumber(undefined)).toBe(0) | ||
expect(toNumber('42')).toBe(42) | ||
expect(toNumber('42.5')).toBe(42.5) | ||
expect(toNumber('not a number')).toBe(0) | ||
expect(toNumber(true)).toBe(1) | ||
expect(toNumber(false)).toBe(0) | ||
expect(toNumber(100)).toBe(100) | ||
}) | ||
|
||
it('should clamp a number within a range', () => { | ||
expect(clamp(5, 1, 10)).toBe(5) | ||
expect(clamp(0, 1, 10)).toBe(1) | ||
expect(clamp(15, 1, 10)).toBe(10) | ||
}) | ||
|
||
it('should clamp an index within an array range', () => { | ||
const arr = [1, 2, 3, 4, 5] | ||
expect(clampArrayRange(2, arr)).toBe(2) | ||
expect(clampArrayRange(-1, arr)).toBe(0) | ||
expect(clampArrayRange(10, arr)).toBe(4) | ||
}) | ||
|
||
it('should generate unique number keys', () => { | ||
const key1 = genNumberKey() | ||
const key2 = genNumberKey() | ||
expect(key1).not.toBe(key2) | ||
expect(key1).toBe(0) | ||
expect(key2).toBe(1) | ||
}) | ||
|
||
it('should generate a random number within a range', () => { | ||
const min = 5 | ||
const max = 10 | ||
const random = randomNumber(min, max) | ||
expect(random).toBeGreaterThanOrEqual(min) | ||
expect(random).toBeLessThanOrEqual(max) | ||
}) | ||
}) |
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,64 @@ | ||
import { describe, it, expect, beforeEach, vi } from 'vitest' | ||
import { createStorage, sessionStorage, localStorage } from '../src/storage' | ||
|
||
describe('Storage utility functions', () => { | ||
let mockStorage: Storage | ||
|
||
beforeEach(() => { | ||
const store: Record<string, string> = {} | ||
mockStorage = { | ||
length: 0, | ||
clear: vi.fn(() => { | ||
Object.keys(store).forEach(key => { | ||
delete store[key] | ||
}) | ||
}), | ||
getItem: vi.fn((key: string) => store[key] || null), | ||
key: vi.fn((index: number) => Object.keys(store)[index] || null), | ||
removeItem: vi.fn((key: string) => { | ||
delete store[key] | ||
}), | ||
setItem: vi.fn((key: string, value: string) => { | ||
store[key] = value | ||
}), | ||
} | ||
}) | ||
|
||
it('should set and get string values', () => { | ||
const storage = createStorage(mockStorage) | ||
storage.set('key1', 'value1') | ||
expect(storage.get('key1')).toBe('value1') | ||
}) | ||
|
||
it('should set and get object values', () => { | ||
const storage = createStorage(mockStorage) | ||
const obj = { a: 1 } | ||
storage.set('key2', obj) | ||
expect(storage.get('key2')).toEqual(obj) | ||
}) | ||
|
||
it('should return null for non-existent keys', () => { | ||
const storage = createStorage(mockStorage) | ||
expect(storage.get('nonExistentKey')).toBeNull() | ||
}) | ||
|
||
it('should remove items', () => { | ||
const storage = createStorage(mockStorage) | ||
storage.set('key3', 'value3') | ||
storage.remove('key3') | ||
expect(storage.get('key3')).toBeNull() | ||
}) | ||
|
||
it('should handle null or undefined values gracefully', () => { | ||
const storage = createStorage(mockStorage) | ||
storage.set('key4', null) | ||
expect(storage.get('key4')).toBeNull() | ||
storage.set('key5', undefined) | ||
expect(storage.get('key5')).toBeNull() | ||
}) | ||
|
||
it('should use sessionStorage and localStorage', () => { | ||
expect(sessionStorage).toBeDefined() | ||
expect(localStorage).toBeDefined() | ||
}) | ||
}) |
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,37 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import { pascalCase, camelize, kebabCase, slash, genStringKey, capitalizeFirstLetter } from '../src/string' | ||
|
||
describe('string utility functions', () => { | ||
it('should convert string to pascal case', () => { | ||
expect(pascalCase('hello-world')).toBe('HelloWorld') | ||
expect(pascalCase('foo-bar')).toBe('FooBar') | ||
}) | ||
|
||
it('should convert string to camel case', () => { | ||
expect(camelize('hello-world')).toBe('helloWorld') | ||
expect(camelize('foo-bar')).toBe('fooBar') | ||
}) | ||
|
||
it('should convert string to kebab case', () => { | ||
expect(kebabCase('HelloWorld')).toBe('hello-world') | ||
expect(kebabCase('FooBar')).toBe('foo-bar') | ||
}) | ||
|
||
it('should replace backslashes with slashes', () => { | ||
expect(slash('C:\\path\\to\\file')).toBe('C:/path/to/file') | ||
expect(slash('\\\\?\\C:\\path\\to\\file')).toBe('\\\\?\\C:\\path\\to\\file') | ||
}) | ||
|
||
it('should generate unique string keys', () => { | ||
const key1 = genStringKey() | ||
const key2 = genStringKey() | ||
expect(key1).not.toBe(key2) | ||
expect(key1).toBe('0') | ||
expect(key2).toBe('1') | ||
}) | ||
|
||
it('should capitalize the first letter of a string', () => { | ||
expect(capitalizeFirstLetter('hello')).toBe('Hello') | ||
expect(capitalizeFirstLetter('world')).toBe('World') | ||
}) | ||
}) |