Skip to content

Commit

Permalink
add some tests and update the deadme for the code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexewerlof committed Jul 31, 2023
1 parent 147b18b commit 2aa5474
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ A simple SLI/SLO calculator that allows playing with different parameters to see

---

# Code

Like most of my hobby projects, this repo wasn't meant to grow this big.
It started as a single page application to compensate some shortcomings
I faced when using [uptime.is](https://uptime.is) with these preconditions:

1. No transpilation whatsoever ([because](https://medium.com/free-code-camp/you-might-not-need-to-transpile-your-javascript-4d5e0a438ca))
2. Use the latest version of Vue (to learn it)
3. Run tests with Deno (to learn it)
4. Use Github Copilot assistence (to learn it)

And here we are.

---

Made by [Alex Ewerlöf](https://www.alexewerlof.com)
33 changes: 33 additions & 0 deletions lib/math_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assertEquals, assertThrows } from "https://deno.land/std/testing/asserts.ts"
import { percent, percentToRatio, toFixed, clamp } from "./math.js"

Deno.test("percent", () => {
assertEquals(percent(50, 200), 100)
assertEquals(percent(25, 80), 20)
assertEquals(percent(75, 120), 90)
})

Deno.test("percent throws error for invalid input", () => {
assertThrows(() => percent("50", 200))
assertThrows(() => percent(50, "200"))
assertThrows(() => percent(150, 200))
assertThrows(() => percent(-50, 200))
})

Deno.test("percentToRatio", () => {
assertEquals(percentToRatio(50), 0.5)
assertEquals(percentToRatio(25), 0.25)
assertEquals(percentToRatio(75), 0.75)
})

Deno.test("toFixed", () => {
assertEquals(toFixed(1.2345), 1.234)
assertEquals(toFixed(3.14159, 2), 3.14)
assertEquals(toFixed(0.123456789, 6), 0.123457)
})

Deno.test("clamp", () => {
assertEquals(clamp(5, 0, 10), 5)
assertEquals(clamp(15, 0, 10), 10)
assertEquals(clamp(-5, 0, 10), 0)
})
19 changes: 17 additions & 2 deletions lib/svg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/**
* Taks a nested array of coordinates and returns a string suitable to pass to
* the `points` attribute of an SVG polygon element.
* @param {...any} coordinates each coordinate is an array like [x, y]
* @param {...any} coordinates an array of coordinates. Each coordinate is an array like [x, y].
* @returns a string like 'x1,y1 x2,y2 x3,y3'
*/
export function arrToPolygonPoints(...coordinates) {
return coordinates.map(pair => pair.join(',')).join(' ')
if (coordinates.length === 0) {
throw new Error('Cannot convert empty array to polygon points')
}

return coordinates.map(function (coordinate) {
if (!Array.isArray(coordinate)) {
throw new Error(`Each coordinate is supposed to be an array. Got ${coodinate}`)
}
if (coordinate.length !== 2) {
throw new Error(`Each coordinate is supposed to be an array of length 2. Got ${coordinate}`)
}
if (!Number.isFinite(coordinate[0]) || !Number.isFinite(coordinate[1])) {
throw new Error(`Each coordinate is supposed to be an array of finite numbers. Got ${coordinate}`)
}
return coordinate.join(',')
}).join(' ')
}
43 changes: 43 additions & 0 deletions lib/svg_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { assertEquals, assertThrows } from "https://deno.land/std/testing/asserts.ts"
import { arrToPolygonPoints } from "./svg.js"

Deno.test("arrToPolygonPoints", () => {
const coordinates = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
]
const expected = "0,0 0,1 1,1 1,0"
assertEquals(arrToPolygonPoints(...coordinates), expected)
})

Deno.test("arrToPolygonPoints throws with no input", () => {
assertThrows(() => arrToPolygonPoints())
})

Deno.test("arrToPolygonPoints with single point", () => {
assertEquals(arrToPolygonPoints(
[0, 0]
), '0,0')
})

Deno.test("arrToPolygonPoints with invalid input", () => {
const coordinates = [1, 2, 3]
assertThrows(() => arrToPolygonPoints(...coordinates))
})

Deno.test("arrToPolygonPoints with invalid input", () => {
assertThrows(() => arrToPolygonPoints([1, 2, 3]))
})

Deno.test("arrToPolygonPoints with invalid input", () => {
assertThrows(() => arrToPolygonPoints(
["1", 2],
[3, 4],
))
assertThrows(() => arrToPolygonPoints(
[1, 2],
[3, "4"],
))
})
6 changes: 2 additions & 4 deletions lib/time.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { toFixed } from "./math.js"

// For a given window unit, what would be the max?
// The title should be something Intl.RelativeTimeFormat can handle
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/format
// Short title matches OpenSLO spec: https://github.com/openslo/openslo#duration-shorthand
export const windowUnits = [
{
title: 'minute',
Expand Down Expand Up @@ -78,7 +76,7 @@ export function humanTime(seconds, useShortTitle = false) {

function addToResult(val, { title, shortTitle }) {
if (useShortTitle) {
result.push(`${val} ${shortTitle}`)
result.push(`${val}${shortTitle}`)
} else {
result.push(`${val} ${title}${val > 1 ? 's' : ''}`)
}
Expand Down
18 changes: 18 additions & 0 deletions lib/time_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"
import { humanTime } from "./time.js"

Deno.test('humanTime()', () => {
assertEquals(humanTime(10), '10 seconds')
assertEquals(humanTime(60), '1 minute')
assertEquals(humanTime(120), '2 minutes')
assertEquals(humanTime(3600), '1 hour')
assertEquals(humanTime(3660), '1 hour, 1 minute')
})

Deno.test('humanTime() short form', () => {
assertEquals(humanTime(10, true), '10s')
assertEquals(humanTime(60, true), '1m')
assertEquals(humanTime(120, true), '2m')
assertEquals(humanTime(3600, true), '1h')
assertEquals(humanTime(3660, true), '1h, 1m')
})

0 comments on commit 2aa5474

Please sign in to comment.