LogarithmPlotter/common/test/basics/utils.mjs

184 lines
7.9 KiB
JavaScript
Raw Permalink Normal View History

2024-10-12 01:22:49 +00:00
/**
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
* Copyright (C) 2021-2024 Ad5001
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { textsup, textsub, parseName, getRandomColor, escapeHTML, exponentsToExpression } from "../../src/utils.mjs"
import { describe, it } from "mocha"
import { expect } from "chai"
2024-10-12 03:31:42 +00:00
describe("Lib/PrototypeExtensions", function() {
2024-10-12 01:22:49 +00:00
describe("#String.toLatinUppercase", function() {
2024-10-12 03:31:42 +00:00
it("transforms latin characters from strings to their uppercase version", function() {
2024-10-12 01:22:49 +00:00
expect("abc".toLatinUppercase()).to.equal("ABC")
expect("abCd".toLatinUppercase()).to.equal("ABCD")
expect("ab123cd456".toLatinUppercase()).to.equal("AB123CD456")
expect("ABC".toLatinUppercase()).to.equal("ABC")
})
2024-10-12 03:31:42 +00:00
it("does not transform non latin characters to their uppercase version", function() {
2024-10-12 01:22:49 +00:00
expect("abαπ".toLatinUppercase()).to.equal("ABαπ")
expect("abαπ".toLatinUppercase()).to.not.equal("abαπ".toUpperCase())
})
})
describe("#String.removeEnclosure", function() {
2024-10-12 03:31:42 +00:00
it("is able to remove the first and last characters", function() {
2024-10-12 01:22:49 +00:00
expect("[1+t]".removeEnclosure()).to.equal("1+t")
expect('"a+b+c*d"'.removeEnclosure()).to.equal("a+b+c*d")
expect("(pi/2)".removeEnclosure()).to.equal("pi/2")
})
})
describe("#Number.toDecimalPrecision", function() {
2024-10-12 03:31:42 +00:00
it("rounds a number to a fixed decimal precision", function() {
2024-10-12 01:22:49 +00:00
expect(123.456789.toDecimalPrecision()).to.equal(123)
expect(123.456789.toDecimalPrecision(1)).to.equal(123.5)
expect(123.456789.toDecimalPrecision(2)).to.equal(123.46)
expect(123.456789.toDecimalPrecision(3)).to.equal(123.457)
expect(123.456789.toDecimalPrecision(4)).to.equal(123.4568)
expect(123.456789.toDecimalPrecision(5)).to.equal(123.45679)
expect(123.456789.toDecimalPrecision(6)).to.equal(123.456789)
expect(123.111111.toDecimalPrecision(5)).to.equal(123.11111)
})
})
})
2024-10-12 03:31:42 +00:00
describe("Lib/Utils", function() {
2024-10-12 01:22:49 +00:00
describe("#textsup", function() {
2024-10-12 03:31:42 +00:00
it("transforms characters which have a sup unicode equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsup("-+=()")).to.equal("⁻⁺⁼⁽⁾")
expect(textsup("0123456789")).to.equal("⁰¹²³⁴⁵⁶⁷⁸⁹")
expect(textsup("abcdefghijklmnoprstuvwxyz")).to.equal("ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ")
})
2024-10-12 03:31:42 +00:00
it("does not transform characters without a sup equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsup("ABCDEFGHIJKLMNOPQRSTUVWXYZq")).to.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZq")
})
2024-10-12 03:31:42 +00:00
it("partially transforms strings which only have a few characters with a sup equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsup("ABCabcABC")).to.equal("ABCᵃᵇᶜABC")
})
})
describe("#textsub", function() {
2024-10-12 03:31:42 +00:00
it("transforms characters which have a sub unicode equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsub("-+=()")).to.equal("₋₊₌₍₎")
expect(textsub("0123456789")).to.equal("₀₁₂₃₄₅₆₇₈₉")
expect(textsub("aehijklmnoprstuvx")).to.equal("ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ")
})
2024-10-12 03:31:42 +00:00
it("does not transform characters without a sub equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsub("ABCDEFGHIJKLMNOPQRSTUVWXYZ")).to.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
expect(textsub("bcdfgqyz")).to.equal("bcdfgqyz")
})
2024-10-12 03:31:42 +00:00
it("partially transforms strings which only have a few characters with a sub equivalent", function() {
2024-10-12 01:22:49 +00:00
expect(textsub("ABC123ABC")).to.equal("ABC₁₂₃ABC")
})
})
describe("#parseName", function() {
2024-10-12 03:31:42 +00:00
it("parses greek letter names", function() {
2024-10-12 01:22:49 +00:00
const shorthands = {
"α": ["al", "alpha"],
"β": ["be", "beta"],
"γ": ["ga", "gamma"],
"δ": ["de", "delta"],
"ε": ["ep", "epsilon"],
"ζ": ["ze", "zeta"],
"η": ["et", "eta"],
"θ": ["th", "theta"],
"κ": ["ka", "kappa"],
"λ": ["la", "lambda"],
"μ": ["mu"],
"ν": ["nu"],
"ξ": ["xi"],
"ρ": ["rh", "rho"],
"σ": ["si", "sigma"],
"τ": ["ta", "tau"],
"υ": ["up", "upsilon"],
"φ": ["ph", "phi"],
"χ": ["ch", "chi"],
"ψ": ["ps", "psi"],
"ω": ["om", "omega"],
"Γ": ["gga", "ggamma"],
"Δ": ["gde", "gdelta"],
"Θ": ["gth", "gtheta"],
"Λ": ["gla", "glambda"],
"Ξ": ["gxi"],
"Π": ["gpi"],
"Σ": ["gsi", "gsigma"],
"Φ": ["gph", "gphi"],
"Ψ": ["gps", "gpsi"],
"Ω": ["gom", "gomega"],
}
for(const [char, shorts] of Object.entries(shorthands)) {
expect(parseName(char)).to.equal(char)
for(const short of shorts)
expect(parseName(short)).to.equal(char)
}
})
2024-10-12 03:31:42 +00:00
it("parses array elements into sub", function() {
2024-10-12 01:22:49 +00:00
expect(parseName("u[n+1]")).to.equal("uₙ₊₁")
expect(parseName("u[(n+x)]")).to.equal("u₍ₙ₊ₓ₎")
expect(parseName("u[A]")).to.equal("uA")
})
2024-10-12 03:31:42 +00:00
it("removes disallowed characters when indicated", function() {
2024-10-12 01:22:49 +00:00
const disallowed = "xnπ\\∪∩[] ()^/^/÷*×+=1234567890¹²³⁴⁵⁶⁷⁸⁹⁰-"
expect(parseName(disallowed)).to.equal("")
expect(parseName("AA" + disallowed)).to.equal("AA")
expect(parseName(disallowed, false)).to.equal(disallowed)
})
2024-10-12 03:31:42 +00:00
it("is able to do all three at once", function() {
2024-10-12 01:22:49 +00:00
expect(parseName("al[n+1]+n")).to.equal("αₙ₊₁")
expect(parseName("al[n+1]+n", false)).to.equal("αₙ₊₁+n")
})
})
describe("#getRandomColor", function() {
2024-10-12 03:31:42 +00:00
it("provides a valid color", function() {
2024-10-12 01:22:49 +00:00
const colorReg = /^#[A-F\d]{6}$/
for(let i = 0; i < 50; i++)
expect(getRandomColor()).to.match(colorReg)
})
})
describe("#escapeHTML", function() {
2024-10-12 03:31:42 +00:00
it("escapes ampersands", function() {
2024-10-12 01:22:49 +00:00
expect(escapeHTML("&")).to.equal("&amp;")
expect(escapeHTML("High & Mighty")).to.equal("High &amp; Mighty")
})
2024-10-12 03:31:42 +00:00
it("escapes injected HTML tags", function() {
2024-10-12 01:22:49 +00:00
expect(escapeHTML("<script>alert('Injected!')</script>")).to.equal("&lt;script&gt;alert('Injected!')&lt;/script&gt;")
expect(escapeHTML('<a href="javascript:alert()">Link</a>')).to.equal('&lt;a href="javascript:alert()"&gt;Link&lt;/a&gt;')
})
})
describe("#exponentsToExpression", function() {
2024-10-12 03:31:42 +00:00
it("transforms exponents to power expression", function() {
2024-10-12 01:22:49 +00:00
expect(exponentsToExpression("x¹²³⁴⁵⁶⁷⁸⁹⁰")).to.equal("x^1234567890")
expect(exponentsToExpression("x¹²+y³⁴")).to.equal("x^12+y^34")
})
})
})