more tests

This commit is contained in:
Moritz Jung 2026-07-03 11:38:15 +02:00
parent 84a82f521a
commit 80adf4aff7
10 changed files with 436 additions and 10 deletions

View file

@ -1,5 +1,41 @@
import { mock } from 'bun:test';
function parseScalar(value: string): unknown {
if (value === 'true') return true;
if (value === 'false') return false;
if (/^-?\d+(\.\d+)?$/.test(value)) return Number(value);
return value.replace(/^['"]|['"]$/g, '');
}
function parseSimpleYaml(yaml: string): Record<string, unknown> {
const result: Record<string, unknown> = {};
for (const line of yaml.split('\n')) {
const match = /^([^:#][^:]*):\s*(.*)$/.exec(line.trim());
if (!match) continue;
const [, key, value] = match;
result[key] =
value.startsWith('[') && value.endsWith(']')
? value
.slice(1, -1)
.split(',')
.map(item => parseScalar(item.trim()))
: parseScalar(value);
}
return result;
}
function stringifySimpleYaml(value: unknown): string {
if (!value || typeof value !== 'object') return String(value);
return Object.entries(value as Record<string, unknown>)
.map(([key, entry]) => `${key}: ${Array.isArray(entry) ? `[${entry.join(', ')}]` : String(entry)}`)
.join('\n')
.concat('\n');
}
mock.module('obsidian', () => ({
AbstractInputSuggest: class {},
Component: class {
@ -22,14 +58,14 @@ mock.module('obsidian', () => ({
Notice: class {},
normalizePath: (path: string): string => path,
moment: Object.assign((value?: unknown): unknown => value, { locale: (): void => {} }),
parseYaml: (): unknown => ({}),
parseYaml: parseSimpleYaml,
Plugin: class {},
PluginSettingTab: class {},
requestUrl: async (): Promise<unknown> => ({}),
SecretComponent: class {},
Setting: class {},
SettingGroup: class {},
stringifyYaml: (value: unknown): string => String(value),
stringifyYaml: stringifySimpleYaml,
TFile: class {},
TFolder: class {},
TextComponent: class {},