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

This commit is contained in:
Ad5001 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

25
.mocharc.jsonc Normal file
View file

@ -0,0 +1,25 @@
/**
* 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/>.
*/
{
"recursive": true,
"require": [
"esm",
"./tests/js/hooks.mjs"
]
}

View file

@ -130,7 +130,7 @@ def create_engine(helper: Helper, latex: Latex, dep_time: float) -> tuple[QQmlAp
global tmpfile
engine = QQmlApplicationEngine()
js_globals = PyJSValue(engine.globalObject())
js_globals.Modules = engine.newObject()
js_globals.globalThis = engine.globalObject()
js_globals.Helper = engine.newQObject(helper)
js_globals.Latex = engine.newQObject(latex)
engine.rootContext().setContextProperty("TestBuild", "--test-build" in argv)

View file

@ -259,4 +259,10 @@ ApplicationWindow {
function showUpdateMenu() {
appMenu.addMenu(updateMenu)
}
// Initializing modules
Component.onCompleted: {
Modules.IO.initialize({ root, settings, alert })
Modules.Latex.initialize({ latex: Latex, helper: Helper })
}
}

View file

@ -127,10 +127,6 @@ ScrollView {
*/
property string saveFilename: ""
Component.onCompleted: {
Modules.IO.initialize({ root, settings, alert })
}
Column {
spacing: 10
width: parent.width

View file

@ -18,15 +18,8 @@
import js from "./lib/polyfills/js.mjs"
// Loading modules in order
import * as Objects from "./module/objects.mjs"
import * as ExprParser from "./module/expreval.mjs"
import * as Modules from "./module/index.mjs"
import * as ObjsAutoload from "./objs/autoload.mjs"
import * as Latex from "./module/latex.mjs"
import * as History from "./module/history.mjs"
import * as CanvasAPI from "./module/canvas.mjs"
import * as IOAPI from "./module/io.mjs"
import * as PreferencesAPI from "./module/preferences.mjs"
export * as MathLib from "./math/index.mjs"
export * as HistoryLib from "./history/index.mjs"

View file

@ -44,32 +44,3 @@ const Qt = {
return {x: x, y: y, width: width, height: height};
}
}
/** Typehints for Helper. */
const Helper = {
/** @type {function(string): boolean} */
getSettingBool: (setting) => true,
/** @type {function(string): int} */
getSettingInt: (setting) => 0,
/** @type {function(string): string} */
getSetting: (setting) => '',
/** @type {function(string, boolean)} */
setSettingBool: (setting, value) => {},
/** @type {function(string, int)} */
setSettingInt: (setting, value) => 0,
/** @type {function(string, string)} */
setSetting: (setting, value) => '',
/** @type {function(string, string)} */
write: (filename, data) => {},
/** @type {function(string): string} */
load: (filename) => '',
}
const Latex = {
/** @type {function(string, number, string): string} */
render: (latex_markup, font_size, color) => '',
/** @type {function(string, number, string): string} */
findPrerendered: (latex_markup, font_size, color) => '',
/** @type {function(): boolean} */
checkLatexInstallation: () => true,
}

View file

@ -585,13 +585,13 @@ Domain.ZME = new SpecialDomain("ℤ⁻*", x => x % 1 === 0 && x < 0,
x => Math.min(Math.ceil(x) - 1, -1))
Domain.ZME.latexMarkup = "\\mathbb{Z}^{-*}"
Domain.NLog = new SpecialDomain("ℕˡᵒᵍ",
x => x / Math.pow(10, x.toString().length - 1) % 1 === 0 && x > 0,
x => x / Math.pow(10, Math.ceil(Math.log10(x))) % 1 === 0 && x > 0,
function(x) {
let x10pow = Math.pow(10, x.toString().length - 1)
let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
return Math.max(1, (Math.floor(x / x10pow) + 1) * x10pow)
},
function(x) {
let x10pow = Math.pow(10, x.toString().length - 1)
let x10pow = Math.pow(10, Math.ceil(Math.log10(x)))
return Math.max(1, (Math.ceil(x / x10pow) - 1) * x10pow)
})
Domain.NLog.latexMarkup = "\\mathbb{N}^{log}"

View file

@ -18,6 +18,9 @@
import { Interface } from "./interface.mjs"
// Define Modules interface before they are imported.
globalThis.Modules = globalThis.Modules || {}
/**
* Base class for global APIs in runtime.
*/
@ -33,6 +36,7 @@ export class Module {
this.__name = name
this.__initializationParameters = initializationParameters
this.initialized = false
}
/**
@ -42,6 +46,7 @@ export class Module {
initialize(options) {
if(this.initialized)
throw new Error(`Cannot reinitialize module ${this.__name}.`)
console.log(`Initializing ${this.__name}...`)
for(const [name, value] of Object.entries(this.__initializationParameters)) {
if(!options.hasOwnProperty(name))
throw new Error(`Option '${name}' of initialize of module ${this.__name} does not exist.`)

View file

@ -37,7 +37,6 @@ class HistoryAPI extends Module {
initialize({ historyObj, themeTextColor, imageDepth, fontSize }) {
super.initialize({ historyObj, themeTextColor, imageDepth, fontSize })
console.log("Initializing history...")
this.history = historyObj
this.themeTextColor = themeTextColor
this.imageDepth = imageDepth

View file

@ -0,0 +1,35 @@
/**
* 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 Objects from "./objects.mjs"
import ExprParser from "./expreval.mjs"
import Latex from "./latex.mjs"
import History from "./history.mjs"
import Canvas from "./canvas.mjs"
import IO from "./io.mjs"
import Preferences from "./preferences.mjs"
export default {
Objects,
ExprParser,
Latex,
History,
Canvas,
IO,
Preferences
}

View file

@ -25,6 +25,7 @@ export const STRING = "string"
export const BOOLEAN = true
export const OBJECT = {}
export const FUNCTION = () => {
throw new Error("Cannot call function of an interface.")
}
@ -42,7 +43,7 @@ export class Interface {
const toCheckName = classToCheck.constructor.name
for(const [property, value] of Object.entries(properties))
if(property !== "implement") {
if(!classToCheck.hasOwnProperty(property))
if(classToCheck[property] === undefined)
// Check if the property exist
throw new Error(`Property '${property}' (${typeof value}) is present in interface ${interfaceName}, but not in implementation ${toCheckName}.`)
else if((typeof value) !== (typeof classToCheck[property]))
@ -56,15 +57,6 @@ export class Interface {
throw new Error(`Property '${property}' of ${interfaceName} implementation ${toCheckName} is not '${value.constructor.name}'.`)
}
}
/**
* Decorator to automatically check if a class conforms to the current interface.
* @param {object} class_
*/
implement(class_) {
Interface.check_implementation(this, class_)
return class_
}
}
@ -120,3 +112,76 @@ export class HistoryInterface extends Interface {
unserialize = FUNCTION
serialize = FUNCTION
}
export class LatexInterface extends Interface {
/**
* @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 = FUNCTION
/**
* @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 = FUNCTION
/**
* Checks if the Latex installation is valid
* @returns {boolean}
*/
checkLatexInstallation = FUNCTION
}
export class HelperInterface extends Interface {
/**
* 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 = FUNCTION
/**
* 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 = FUNCTION
/**
* 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 = FUNCTION
/**
* 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 = FUNCTION
/**
* 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 = FUNCTION
/**
* 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 = FUNCTION
/**
* Sends data to be written
* @param {string} file
* @param {string} dataToWrite - just JSON encoded, requires the "LPFv1" mime to be added before writing
*/
write = FUNCTION
/**
* 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 = FUNCTION
}

View file

@ -19,6 +19,7 @@
import { Module } from "./common.mjs"
import * as Instruction from "../lib/expr-eval/instruction.mjs"
import { escapeValue } from "../lib/expr-eval/expression.mjs"
import { HelperInterface, LatexInterface } from "./interface.mjs"
const unicodechars = [
"α", "β", "γ", "δ", "ε", "ζ", "η",
@ -60,11 +61,25 @@ class LatexRenderResult {
class LatexAPI extends Module {
constructor() {
super("Latex")
super("Latex", {
latex: LatexInterface,
helper: HelperInterface
})
/**
* true if latex has been enabled by the user, false otherwise.
*/
this.enabled = Helper.getSettingBool("enable_latex")
this.enabled = false
}
/**
* @param {LatexInterface} latex
* @param {HelperInterface} helper
*/
initialize({ latex, helper }) {
super.initialize({ latex, helper })
this.latex = latex
this.helper = helper
this.enabled = helper.getSettingBool("enable_latex")
}
/**
@ -77,7 +92,8 @@ class LatexAPI extends Module {
* @returns {LatexRenderResult|null}
*/
findPrerendered(markup, fontSize, color) {
const data = Latex.findPrerendered(markup, fontSize, color)
if(!this.initialized) throw new Error("Attempting findPrerendered before initialize!")
const data = this.latex.findPrerendered(markup, fontSize, color)
let ret = null
if(data !== "")
ret = new LatexRenderResult(...data.split(","))
@ -93,7 +109,8 @@ class LatexAPI extends Module {
* @returns {Promise<LatexRenderResult>}
*/
async requestAsyncRender(markup, fontSize, color) {
let args = Latex.render(markup, fontSize, color).split(",")
if(!this.initialized) throw new Error("Attempting requestAsyncRender before initialize!")
let args = this.latex.render(markup, fontSize, color).split(",")
return new LatexRenderResult(...args)
}
@ -313,5 +330,5 @@ class LatexAPI extends Module {
/** @type {LatexAPI} */
Modules.Latex = Modules.Latex || new LatexAPI()
/** @type {LatexAPI} */
export default Modules.Latex

View file

@ -34,4 +34,4 @@ class PreferencesAPI extends Module {
/** @type {CanvasAPI} */
Modules.Preferences = Modules.Preferences || new PreferencesAPI()
export const API = Modules.Preferences
export default Modules.Preferences

View file

@ -120,11 +120,9 @@ def setSetting(namespace, data):
"""
names = namespace.split(".")
setting = current_config
for name in names:
if name != names[-1]:
for name in names[:-1]:
if name in setting:
setting = setting[name]
else:
raise UnknownNamespaceError(f"Setting {namespace} doesn't exist. Debug: {setting}, {name}")
else:
setting[name] = data
setting[names[-1]] = data

1044
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,8 @@
"description": "2D plotter software to make Bode plots, sequences and distribution functions.",
"main": "LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/autoload.mjs",
"scripts": {
"build": "rollup --config rollup.config.mjs"
"build": "rollup --config rollup.config.mjs",
"test": "mocha tests/js/**/*.mjs"
},
"repository": {
"type": "git",
@ -20,5 +21,13 @@
"install": "^0.13.0",
"rollup": "^4.22.4",
"rollup-plugin-cleanup": "^3.2.1"
},
"devDependencies": {
"@types/chai": "^5.0.0",
"@types/mocha": "^10.0.8",
"chai": "^5.1.1",
"chai-as-promised": "^8.0.0",
"esm": "^3.2.25",
"mocha": "^10.7.3"
}
}

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,
}