Testing!
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Adsooi 2024-09-28 03:32:51 +02:00
parent c806f09b10
commit 56a0817960
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
22 changed files with 1680 additions and 72 deletions

30
tests/js/hooks.mjs Normal file
View file

@ -0,0 +1,30 @@
/**
* 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 * as fs from "./mock/fs.mjs";
import Qt from "./mock/qt.mjs";
import { MockHelper } from "./mock/helper.mjs";
import { MockLatex } from "./mock/latex.mjs";
import Modules from "../../LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/index.mjs";
function setup() {
globalThis.Helper = new MockHelper()
globalThis.Latex = new MockLatex()
Modules.Latex.initialize({ latex: Latex, helper: Helper })
}
setup()

63
tests/js/math/domain.mjs Normal file
View file

@ -0,0 +1,63 @@
/**
* 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 { describe, it } from "mocha"
import { expect } from "chai"
import { Domain, parseDomainSimple } from "../../../LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/math/domain.mjs"
describe("math.domain", function() {
describe("#parseDomainSimple", function() {
it("returns predefined domains", function() {
const predefinedToCheck = [
// Real domains
{ domain: Domain.R, shortcuts: ["R", ""] },
// Zero exclusive real domains
{ domain: Domain.RE, shortcuts: ["RE", "R*", "*"] },
// Real positive domains
{ domain: Domain.RP, shortcuts: ["RP", "R+", "ℝ⁺", "+"] },
// Zero-exclusive real positive domains
{ domain: Domain.RPE, shortcuts: ["RPE", "REP", "R+*", "R*+", "*⁺", "ℝ⁺*", "*+", "+*"] },
// Real negative domain
{ domain: Domain.RM, shortcuts: ["RM", "R-", "ℝ⁻", "-"] },
// Zero-exclusive real negative domains
{ domain: Domain.RME, shortcuts: ["RME", "REM", "R-*", "R*-", "ℝ⁻*", "*⁻", "-*", "*-"] },
// Natural integers domain
{ domain: Domain.N, shortcuts: ["", "N", "ZP", "Z+", "ℤ⁺", "+"] },
// Zero-exclusive natural integers domain
{ domain: Domain.NE, shortcuts: ["NE", "NP", "N*", "N+", "*", "ℕ⁺", "+", "ZPE", "ZEP", "Z+*", "Z*+", "ℤ⁺*", "*⁺", "+*", "*+"] },
// Logarithmic natural domains
{ domain: Domain.NLog, shortcuts: ["NLOG", "ℕˡᵒᵍ", "LOG"] },
// All integers domains
{ domain: Domain.Z, shortcuts: ["Z", ""] },
// Zero-exclusive all integers domain
{ domain: Domain.ZE, shortcuts: ["ZE", "Z*", "*"] },
// Negative integers domain
{ domain: Domain.ZM, shortcuts: ["ZM", "Z-", "ℤ⁻", "-"] },
// Zero-exclusive negative integers domain
{ domain: Domain.ZME, shortcuts: ["ZME", "ZEM", "Z-*", "Z*-", "ℤ⁻*", "*⁻", "-*", "*-"] },
]
// Real domains
for(const { domain, shortcuts } of predefinedToCheck)
for(const shortcut of shortcuts)
expect(parseDomainSimple(shortcut)).to.be.equal(domain)
})
})
})

44
tests/js/mock/fs.mjs Normal file
View file

@ -0,0 +1,44 @@
/**
* 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 { readFileSync as readNode } from "node:fs"
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url))
export const HOME = "/home/user"
export const TMP = "/tmp"
const filesystem = {
[`${HOME}/test1.lpf`]: readNode(__dirname + "/../../../ci/test1.lpf")
}
export function existsSync(file) {
return filesystem[file] !== undefined
}
export function writeFileSync(file, data, encoding) {
filesystem[file] = Buffer.from(data, encoding)
}
export function readFileSync(file, encoding) {
return filesystem[file].toString(encoding)
}

158
tests/js/mock/helper.mjs Normal file
View file

@ -0,0 +1,158 @@
/**
* 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 { readFileSync, writeFileSync, existsSync } from "./fs.mjs"
const DEFAULT_SETTINGS = {
"check_for_updates": true,
"reset_redo_stack": true,
"last_install_greet": "0",
"enable_latex": false,
"expression_editor": {
"autoclose": true,
"colorize": true,
"color_scheme": 0
},
"autocompletion": {
"enabled": true
},
"default_graph": {
"xzoom": 100,
"yzoom": 10,
"xmin": 5 / 10,
"ymax": 25,
"xaxisstep": "4",
"yaxisstep": "4",
"xlabel": "",
"ylabel": "",
"linewidth": 1,
"textsize": 18,
"logscalex": true,
"showxgrad": true,
"showygrad": true
}
}
export class MockHelper {
constructor() {
this.__settings = { ...DEFAULT_SETTINGS }
}
__getSetting(settingName) {
const namespace = settingName.split(".")
let data = this.__settings
for(const name of namespace)
if(data.hasOwnProperty(name))
data = data[name]
else
throw new Error(`Setting ${namespace} does not exist.`)
return data
}
__setSetting(settingName, value) {
const namespace = settingName.split(".")
const finalName = namespace.pop()
let data = this.__settings
for(const name of namespace)
if(data.hasOwnProperty(name))
data = data[name]
else
throw new Error(`Setting ${namespace} does not exist.`)
data[finalName] = value
}
/**
* Gets a setting from the config
* @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin")
* @returns {boolean} Value of the setting
*/
getSettingBool(settingName) {
return this.__getSetting(settingName) === true
}
/**
* Gets a setting from the config
* @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin")
* @returns {number} Value of the setting
*/
getSettingInt(settingName) {
return +(this.__getSetting(settingName))
}
/**
* Gets a setting from the config
* @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin")
* @returns {string} Value of the setting
*/
getSetting(settingName) {
return this.__getSetting(settingName).toString()
}
/**
* Sets a setting in the config
* @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin")
* @param {boolean} value
*/
setSettingBool(settingName, value) {
return this.__setSetting(settingName, value === true)
}
/**
* Sets a setting in the config
* @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin")
* @param {number} value
*/
setSettingInt(settingName, value) {
return this.__setSetting(settingName, +(value))
}
/**
* Sets a setting in the config
* @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin")
* @param {string} value
*/
setSetting(settingName, value) {
return this.__setSetting(settingName, value.toString())
}
/**
* Sends data to be written
* @param {string} file
* @param {string} dataToWrite - just JSON encoded, requires the "LPFv1" mime to be added before writing
*/
write(file, dataToWrite) {
writeFileSync(file, "LPFv1" + dataToWrite)
}
/**
* Requests data to be read from a file
* @param {string} file
* @returns {string} the loaded data - just JSON encoded, requires the "LPFv1" mime to be stripped
*/
load(file) {
if(existsSync(file)) {
const data = readFileSync(file, "utf8")
if(data.startsWith("LPFv1"))
return data.substring(5)
else
throw new Error(`Invalid LogarithmPlotter file.`)
} else
throw new Error(`File not found.`)
}
}

90
tests/js/mock/latex.mjs Normal file
View file

@ -0,0 +1,90 @@
/**
* 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 { TMP, existsSync, writeFileSync } from "./fs.mjs"
const PIXEL = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR4AWNgAAAAAgABc3UBGAAAAABJRU5ErkJggg=="
export class MockLatex {
constructor() {
}
/**
* Creates a simple string hash.
* @param {string} string
* @return {number}
* @private
*/
__hash(string) {
let hash = 0
let i, chr
if(string.length === 0) return hash
for(i = 0; i < string.length; i++) {
chr = string.charCodeAt(i)
hash = ((hash << 5) - hash) + chr
hash |= 0 // Convert to 32bit integer
}
return hash
}
/**
*
* @param {string} markup
* @param {number} fontSize
* @param {string} color
* @return {string}
* @private
*/
__getFileName(markup, fontSize, color) {
const name = this.__hash(`${markup}_${fontSize}_${color}`)
return `${TMP}/${name}.png`
}
/**
* @param {string} markup - LaTeX markup to render
* @param {number} fontSize - Font size (in pt) to render
* @param {string} color - Color of the text to render
* @returns {string} - Comma separated data of the image (source, width, height)
*/
render(markup, fontSize, color) {
const file = this.__getFileName(markup, fontSize, color)
writeFileSync(file, PIXEL, "base64")
return `${file},1,1`
}
/**
* @param {string} markup - LaTeX markup to render
* @param {number} fontSize - Font size (in pt) to render
* @param {string} color - Color of the text to render
* @returns {string} - Comma separated data of the image (source, width, height)
*/
findPrerendered(markup, fontSize, color) {
const file = this.__getFileName(markup, fontSize, color)
if(existsSync(file))
return `${file},1,1`
return ""
}
/**
* Checks if the Latex installation is valid
* @returns {boolean}
*/
checkLatexInstallation() {
return true // We're not *actually* doing any latex.
}
}

60
tests/js/mock/qt.mjs Normal file
View file

@ -0,0 +1,60 @@
/**
* 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/>.
*/
// Mock qt methods.
/**
* Polyfill for Qt.rect.
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
* @returns {{x, width, y, height}}
*/
function rect(x, y, width, height) {
return { x, y, width, height }
}
/**
* Mock for QT_TRANSLATE_NOOP and qsTranslate
* @param {string} category
* @param {string} string
* @return {string}
*/
function QT_TRANSLATE_NOOP(category, string) {
return string
}
function setup() {
globalThis.Qt = {
rect
}
globalThis.QT_TRANSLATE_NOOP = QT_TRANSLATE_NOOP
globalThis.qsTranslate = QT_TRANSLATE_NOOP
String.prototype.arg = function() { return this; } // No need to reimplement it for now.
}
setup()
export default {
rect,
QT_TRANSLATE_NOOP,
qtTranslate: QT_TRANSLATE_NOOP,
}