diff --git a/src/App.tsx b/src/App.tsx index 89e6bb7..4b48427 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import { useState, type DOMAttributes } from 'react'; +import { useState } from 'react'; import getSum, { defaultSeprator } from "./stringCalculator" const App = () => { diff --git a/src/stringCalculator.test.ts b/src/stringCalculator.test.ts index 037f80c..6ae7b72 100644 --- a/src/stringCalculator.test.ts +++ b/src/stringCalculator.test.ts @@ -1,18 +1,41 @@ -// Test cases possible - -/** - * 1. "" - * 2. "12" - * 3. "1,2" - * 4. "1,2,3,4" - * 5. "1,2,3,4" , "-" => custom limiter - * 6. "1,2,-3,4" => negative number - * 7. "1 , 2 , 3 , 4" => handle trailing space cases - * 8. "1, 2, - 3, 4" => any one number is not a valid number - * 9. 1--2-3-4 => throw error - */ - -/** - * limiter input if it is empty then what does it do - * it the - */ \ No newline at end of file +import {describe, test, expect } from 'vitest' +import getStringSum from "./stringCalculator" + + describe('getStringSum', () => { + test('returns 0 for empty string', () => { + expect(getStringSum("")).toBe(0) + }) + + test('returns the number testself for a single number string', () => { + expect(getStringSum("12")).toBe(12) + }) + + test('returns sum for comma-separated numbers', () => { + expect(getStringSum("1,2")).toBe(3) + expect(getStringSum("1,2,3,4")).toBe(10) + }) + + test('handles custom separator', () => { + expect(getStringSum("1-2-3-4", "-")).toBe(10) + }) + + test('handles negative numbers', () => { + expect(getStringSum("1,2,-3,4")).toBe(4) + }) + + test('handles extra spaces', () => { + expect(getStringSum("1 , 2 , 3 , 4")).toBe(10) + }) + + test('throws error if any number is invalid due to internal spaces', () => { + expect(() => getStringSum("1, 2, - 3, 4")).toThrowError( + new Error("input is not a list of valid number") + ) + }) + + test('throws error for invalid input like 1--2-3-4', () => { + expect(() => getStringSum("1--2-3-4", "-")).toThrowError( + new Error("input is not a list of valid number") + ) + }) +}) \ No newline at end of file