Skip to content

Commit eeb1b03

Browse files
committed
test: Adapt JSON tests for caching loader
1 parent 5911fd9 commit eeb1b03

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/files/json.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type assert, assertObjectMatch } from '@std/assert'
22
import type { BIDSFile } from '../types/filetree.ts'
33
import type { FileIgnoreRules } from './ignore.ts'
44

5+
import { pathsToTree } from '../files/filetree.ts'
56
import { loadJSON } from './json.ts'
67

78
function encodeUTF16(text: string) {
@@ -17,9 +18,12 @@ function encodeUTF16(text: string) {
1718
return buffer
1819
}
1920

20-
function makeFile(text: string, encoding: string): BIDSFile {
21+
function makeFile(path: string, text: string, encoding: string): BIDSFile {
2122
const bytes = encoding === 'utf-8' ? new TextEncoder().encode(text) : encodeUTF16(text)
23+
const file = pathsToTree([path]).get(path) as BIDSFile
2224
return {
25+
path: file.path,
26+
parent: file.parent,
2327
readBytes: async (size: number) => {
2428
return new Uint8Array(bytes)
2529
},
@@ -29,13 +33,13 @@ function makeFile(text: string, encoding: string): BIDSFile {
2933

3034
Deno.test('Test JSON error conditions', async (t) => {
3135
await t.step('Load valid JSON', async () => {
32-
const JSONfile = makeFile('{"a": 1}', 'utf-8')
36+
const JSONfile = makeFile('/valid-contents.json', '{"a": 1}', 'utf-8')
3337
const result = await loadJSON(JSONfile)
3438
assertObjectMatch(result, { a: 1 })
3539
})
3640

3741
await t.step('Error on BOM', async () => {
38-
const BOMfile = makeFile('\uFEFF{"a": 1}', 'utf-8')
42+
const BOMfile = makeFile('/BOM.json', '\uFEFF{"a": 1}', 'utf-8')
3943
let error: any = undefined
4044
await loadJSON(BOMfile).catch((e) => {
4145
error = e
@@ -44,7 +48,7 @@ Deno.test('Test JSON error conditions', async (t) => {
4448
})
4549

4650
await t.step('Error on UTF-16', async () => {
47-
const UTF16file = makeFile('{"a": 1}', 'utf-16')
51+
const UTF16file = makeFile('/utf16.json', '{"a": 1}', 'utf-16')
4852
let error: any = undefined
4953
await loadJSON(UTF16file).catch((e) => {
5054
error = e
@@ -53,11 +57,12 @@ Deno.test('Test JSON error conditions', async (t) => {
5357
})
5458

5559
await t.step('Error on invalid JSON syntax', async () => {
56-
const badJSON = makeFile('{"a": 1]', 'utf-8')
60+
const badJSON = makeFile('/bad-syntax.json', '{"a": 1]', 'utf-8')
5761
let error: any = undefined
5862
await loadJSON(badJSON).catch((e) => {
5963
error = e
6064
})
6165
assertObjectMatch(error, { code: 'JSON_INVALID' })
6266
})
67+
loadJSON.cache.clear()
6368
})

0 commit comments

Comments
 (0)