Skip to content

Commit

Permalink
code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ieedan committed Nov 13, 2024
1 parent 0b1c954 commit 624de2b
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
coverage
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# std
Types and utility functions brokered with **ts-blocks**.

Types and utility functions brokered with ts-blocks.

```bash
npx ts-blocks@next add --repo https://github.com/ieedan/std
```
pnpm dlx ts-blocks@next add --repo https://github.com/ieedan/std
```

# Blocks

| Category | Block | Status |
| --------- | ------------ | -------------------------------------------------------------------------- |
| types | result | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | array-sum | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | array-to-map | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | dispatcher | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | ipv4-address | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | is-number | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | map-to-array | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | pad | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | sleep | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | stopwatch | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
| utilities | truncate | ![Tests](https://github.com/ieedan/std/actions/workflows/ci.yml/badge.svg) |
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "std",
"packageManager": "pnpm@9.12.3",
"type": "module",
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage",
"format": "biome format --write",
"lint": "biome lint --write",
"check": "biome check",
Expand All @@ -12,6 +14,7 @@
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@types/node": "^22.9.0",
"@vitest/coverage-v8": "^2.1.5",
"ts-blocks": "1.0.0-next.9",
"typedoc": "^0.26.11",
"typescript": "^5.6.3",
Expand Down
371 changes: 371 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/types/result.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, describe, expect, it } from 'vitest';
import { describe, expect, it } from 'vitest';
import { Err, Ok, type Result } from './result';

const failingFunction = <E>(err: E): Result<boolean, E> => Err(err);
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Result', () => {
it('Result.unwrap: Should throw if failed', () => {
const result = failingFunction('oops!');

assert.throws(result.unwrap);
expect(() => result.unwrap()).toThrow()
});

// --- unwrapOr ---
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('Result', () => {
it('Result.unwrapErr: Should throw if passed', () => {
const result = passingFunction(true);

assert.throws(result.unwrapErr);
expect(() => result.unwrapErr()).toThrow()
});

// --- unwrapErrOr ---
Expand Down Expand Up @@ -143,15 +143,15 @@ describe('Result', () => {

const result = failingFunction(expected);

expect(result.unwrapErrOr('nope')).toBe(expected);
expect(result.unwrapErrOrElse(() => 'nope')).toBe(expected);
});

it('Result.unwrapErrOrElse: Expect correct error on fail', () => {
const expected = 'I failed!';

const result = passingFunction('nope');

expect(result.unwrapErrOr(expected)).toBe(expected);
expect(result.unwrapErrOrElse(() => expected)).toBe(expected);
});

// --- expect ---
Expand All @@ -167,7 +167,7 @@ describe('Result', () => {
it('Result.expect: Should throw if failed', () => {
const result = failingFunction('oops!');

assert.throws(() => result.expect('Oh no'), 'Oh no');
expect(() => result.expect('Oh no')).toThrow('Oh no');
});

// --- expectErr ---
Expand All @@ -183,7 +183,7 @@ describe('Result', () => {
it('Result.expectErr: Should throw if ok', () => {
const result = passingFunction('oops!');

assert.throws(() => result.expectErr('Oh no'), 'Oh no');
expect(() => result.expectErr('Oh no')).toThrow('Oh no');
});

// --- map ---
Expand Down
60 changes: 37 additions & 23 deletions src/utilities/ipv4-address.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
import { describe, expect, it } from 'vitest';
import { formatToString, parse, validate } from './ipv4-address';
import { describe, expect, it } from "vitest";
import { formatToString, parse, validate } from "./ipv4-address";

describe('parse', () => {
describe("parse", () => {
it(`Allows '.' separator.`, () => {
const expected = [192, 168, 100, 10];

expect(parse(expected.join('.')).unwrap()).toStrictEqual(expected);
expect(parse(expected.join(".")).unwrap()).toStrictEqual(expected);
});

it(`Allows '_' separator.`, () => {
const expected = [192, 168, 100, 10];

expect(parse(expected.join('_')).unwrap()).toStrictEqual(expected);
expect(parse(expected.join("_")).unwrap()).toStrictEqual(expected);
});

it(`Allows ' ' separator.`, () => {
const expected = [192, 168, 100, 10];

expect(parse(expected.join(' ')).unwrap()).toStrictEqual(expected);
expect(parse(expected.join(" ")).unwrap()).toStrictEqual(expected);
});

it('Allows leading 0s', () => {
expect(parse('001.001.001.001').unwrap()).toStrictEqual([1, 1, 1, 1]);
it("Does not allow non numbers.", () => {
expect(parse("a.a.a.a").unwrapErr()).toStrictEqual({
message: "'a' is not a number.",
octet: 1,
});
});

it("Allows leading 0s", () => {
expect(parse("001.001.001.001").unwrap()).toStrictEqual([1, 1, 1, 1]);
});

it('Returns error when invalid', () => {
expect(parse('192.168.256.10').unwrapErr()).toStrictEqual({
it("Returns error when invalid", () => {
expect(parse("192.168.256.10").unwrapErr()).toStrictEqual({
octet: 3,
message: "'256' is out of range.",
});
expect(parse('192.168.0').unwrapErr()).toStrictEqual({
expect(parse("192.168.0").unwrapErr()).toStrictEqual({
message: "'192.168.0' is invalid as it doesn't contain 4 octets.",
});
});
});

describe('validate', () => {
it('Returns true when valid', () => {
describe("validate", () => {
it("Returns true when valid", () => {
expect(validate([192, 168, 100, 10])).toBe(true);
expect(validate('192.168.100.10')).toBe(true);
expect(validate('192_168_100_10')).toBe(true);
expect(validate("192.168.100.10")).toBe(true);
expect(validate("192_168_100_10")).toBe(true);
});

it('Returns false when invalid', () => {
it("Returns false when invalid", () => {
expect(validate([192, 168, 100, -10])).toBe(false);
expect(validate([192, 168, 100, -56])).toBe(false);
expect(validate('192.168.100.256')).toBe(false);
expect(validate('192.168.100.-10')).toBe(false);
expect(validate("192.168.100.256")).toBe(false);
expect(validate("192.168.100.-10")).toBe(false);
});
});

describe('formatToString', () => {
it('Returns the correct format', () => {
expect(formatToString([192, 168, 100, 10]).unwrap()).toBe('192.168.100.10');
expect(formatToString([192, 168, 100, 10], ' ').unwrap()).toBe('192 168 100 10');
expect(formatToString([192, 168, 100, 10], '_').unwrap()).toBe('192_168_100_10');
describe("formatToString", () => {
it("Returns the correct format", () => {
expect(formatToString([192, 168, 100, 10]).unwrap()).toBe("192.168.100.10");
expect(formatToString([192, 168, 100, 10], " ").unwrap()).toBe("192 168 100 10");
expect(formatToString([192, 168, 100, 10], "_").unwrap()).toBe("192_168_100_10");
expect(formatToString("192.168.100.10", "_").unwrap()).toBe("192_168_100_10");
expect(formatToString("192 168 100 10", "_").unwrap()).toBe("192_168_100_10");
expect(formatToString("192_168_100_10", ".").unwrap()).toBe("192.168.100.10");
});

it("Returns error when invalid", () => {
expect(formatToString("256.2.2.2").unwrapErr()).toBe("'256' is out of range.");
});
});
2 changes: 1 addition & 1 deletion src/utilities/ipv4-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const formatToString = (

if (parsed.isErr()) return Err(parsed.unwrapErr().message);

return formatToString(parsed.unwrap());
return formatToString(parsed.unwrap(), separator);
};

export { parse, validate, formatToString };
6 changes: 6 additions & 0 deletions src/utilities/truncate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { describe, expect, it } from 'vitest';
import { truncate } from './truncate';

describe('truncate', () => {
it('Returns string when string length is less than max length', () => {
const str = truncate('Hello World!', 100);

expect(str).toBe('Hello World!');
});

it('Correctly truncates forward', () => {
const str = truncate('Hello World!', 5);

Expand Down
12 changes: 12 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
provider: "v8",
reporter: ["json-summary"],
exclude: ["node_modules/", "**/*.d.ts", "**/*.test.ts"],
all: false,
},
},
});

0 comments on commit 624de2b

Please sign in to comment.