Skip to content

Commit

Permalink
Merge pull request #55 from jameschensmith/chore/share-common-tests
Browse files Browse the repository at this point in the history
Share common tests
  • Loading branch information
NonlinearFruit authored Aug 18, 2023
2 parents 9ed3cb5 + 9b83c5e commit 9ace5d8
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 503 deletions.
107 changes: 6 additions & 101 deletions tests/canon.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
import { resolve } from "path";
import { readdirSync, readFileSync } from "fs"
import { describe, test, expect } from "vitest"
import { Validator } from "jsonschema"
import TJS from "typescript-json-schema"
import { describe } from "vitest"
import { testData, testDocument, testProofs } from "./common.ts"

const validator = new Validator()
describe.each(testData.Canon)('$filename', ({filepath, creed}) => {

const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
}
const program = TJS.getProgramFromFiles(
[resolve("tests/types.ts")],
compilerOptions
)
const settings: TJS.PartialArgs = {
required: true,
}

const type = "Canon"
const repoPath = resolve(__dirname, '..')
const creedFolder = `${repoPath}/creeds`
const files = readdirSync(creedFolder)
const testData = files
.map(filename => ({
filename,
creed: require(`${creedFolder}/${filename}`)
}))
.filter(testData => testData.creed.Metadata.CreedFormat == type)

describe.each(testData)('$filename', ({filename, creed}) => {

test('has metadata', async () => {
const schema = TJS.generateSchema(program, "Metadata", settings)

const result = validator.validate(creed.Metadata, schema)

expect(result.valid, result).toBeTruthy()
})

test(`matches ${creed.Metadata.CreedFormat} schema`, async () => {
const schema = TJS.generateSchema(program, creed.Metadata.CreedFormat, settings)

const result = validator.validate(creed, schema)

expect(result.valid, result).toBeTruthy()
})

test('is ascii', async () => {
let buf = readFileSync(`${creedFolder}/${filename}`)
const len=buf.length
for (let i=0; i<len; i++)
expect(buf[i]).toBeLessThan(127)
})
testDocument(creed, filepath)

let data = creed.Data
if (data instanceof Array)
Expand All @@ -64,54 +16,7 @@ describe.each(testData)('$filename', ({filename, creed}) => {
title: "Content",
item: data
}]
describe.each(data)('$title', ({title, item}) => {

test('each footnote has a proof text', () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, textWithProofs]) => textWithProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
const proofTextIds = item
.Proofs
.map((proof) => proof.Id.toString())
if (footnoteIds)
for (const footnoteId of footnoteIds)
expect(proofTextIds).toContain(footnoteId)
})

test('each proof text has a footnote', async () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const text = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, value]) => value)
.reduce((a, b) => a + b, '')
for (const proof of item.Proofs)
expect(text).toContain(`[${proof.Id}]`)
})

test('each proof reference is valid', async () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
for (const proof of item.Proofs) {
expect(proof.References).toBeInstanceOf(Array)
}
})

test('no footnotes in non-WithProofs strings', async () => {
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => !key.endsWith('WithProofs'))
.filter(([_, obj]) => typeof obj == "string")
.map(([_, textWithoutProofs]) => textWithoutProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
expect(footnoteIds).toBeNull()
})
describe.each(data)('$title', ({item}) => {
testProofs(item)
})
})
107 changes: 6 additions & 101 deletions tests/catechism.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
import { resolve } from "path";
import { readdirSync, readFileSync } from "fs"
import { describe, test, expect } from "vitest"
import { Validator } from "jsonschema"
import TJS from "typescript-json-schema"
import { describe } from "vitest"
import { testData, testDocument, testProofs } from "./common.ts"

const validator = new Validator()
describe.each(testData.Catechism)('$filename', ({filepath, creed}) => {

const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
}
const program = TJS.getProgramFromFiles(
[resolve("tests/types.ts")],
compilerOptions
)
const settings: TJS.PartialArgs = {
required: true,
}

const type = "Catechism"
const repoPath = resolve(__dirname, '..')
const creedFolder = `${repoPath}/creeds`
const files = readdirSync(creedFolder)
const testData = files
.map(filename => ({
filename,
creed: require(`${creedFolder}/${filename}`)
}))
.filter(testData => testData.creed.Metadata.CreedFormat == type)

describe.each(testData)('$filename', ({filename, creed}) => {

test('has metadata', async () => {
const schema = TJS.generateSchema(program, "Metadata", settings)

const result = validator.validate(creed.Metadata, schema)

expect(result.valid, result).toBeTruthy()
})

test(`matches ${creed.Metadata.CreedFormat} schema`, async () => {
const schema = TJS.generateSchema(program, creed.Metadata.CreedFormat, settings)

const result = validator.validate(creed, schema)

expect(result.valid, result).toBeTruthy()
})

test('is ascii', async () => {
let buf = readFileSync(`${creedFolder}/${filename}`)
const len=buf.length
for (let i=0; i<len; i++)
expect(buf[i]).toBeLessThan(127)
})
testDocument(creed, filepath)

let data = creed.Data
if (data instanceof Array)
Expand All @@ -64,54 +16,7 @@ describe.each(testData)('$filename', ({filename, creed}) => {
title: "Content",
item: data
}]
describe.each(data)('$title', ({title, item}) => {

test('each footnote has a proof text', () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, textWithProofs]) => textWithProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
const proofTextIds = item
.Proofs
.map((proof) => proof.Id.toString())
if (footnoteIds)
for (const footnoteId of footnoteIds)
expect(proofTextIds).toContain(footnoteId)
})

test('each proof text has a footnote', async () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const text = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, value]) => value)
.reduce((a, b) => a + b, '')
for (const proof of item.Proofs)
expect(text).toContain(`[${proof.Id}]`)
})

test('each proof reference is valid', async () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
for (const proof of item.Proofs) {
expect(proof.References).toBeInstanceOf(Array)
}
})

test('no footnotes in non-WithProofs strings', async () => {
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => !key.endsWith('WithProofs'))
.filter(([_, obj]) => typeof obj == "string")
.map(([_, textWithoutProofs]) => textWithoutProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
expect(footnoteIds).toBeNull()
})
describe.each(data)('$title', ({item}) => {
testProofs(item)
})
})
112 changes: 112 additions & 0 deletions tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { test, expect } from "vitest"
import { resolve } from "path";
import { readdirSync, readFileSync } from "fs"
import { Validator } from "jsonschema"
import TJS from "typescript-json-schema"
import type { CreedDocument, Proof } from "./types.ts"

const repoPath = resolve(__dirname, '..')
const creedFolder = `${repoPath}/creeds`
const files = readdirSync(creedFolder)
export const testData = files.reduce((acc, filename) => {
const filepath = `${creedFolder}/${filename}`
const creed = require(filepath)
const data = {
filename,
filepath,
creed,
}
acc[creed.Metadata.CreedFormat] ??= []
acc[creed.Metadata.CreedFormat].push(data)
return acc
}, {})

const validator = new Validator()

const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
}
const program = TJS.getProgramFromFiles(
[resolve("tests/types.ts")],
compilerOptions
)
const settings: TJS.PartialArgs = {
required: true,
}

const validateSchema = (typeName, document) => {
test(`matches ${typeName} schema`, async () => {
const schema = TJS.generateSchema(program, typeName, settings)

const result = validator.validate(document, schema)

expect(result.valid, result).toBeTruthy()
})
}

export const testDocument = (document: CreedDocument<any>, filename: string) => {
validateSchema("Metadata", document.Metadata)

validateSchema(document.Metadata.CreedFormat, document)

test('is ascii', async () => {
let buf = readFileSync(filename)
const len=buf.length
for (let i=0; i<len; i++)
expect(buf[i]).toBeLessThan(127)
})
}

const testReferences = (proofs: Proof[]) => {
test('each proof reference is valid', async () => {
if (!proofs || !(proofs instanceof Array))
return
for (const proof of proofs) {
expect(proof.References).toBeInstanceOf(Array)
}
})
}

export const testProofs = (item: any) => {
testReferences(item.Proofs)

test('each footnote has a proof text', () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, textWithProofs]) => textWithProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
const proofTextIds = item
.Proofs
.map((proof) => proof.Id.toString())
if (footnoteIds)
for (const footnoteId of footnoteIds)
expect(proofTextIds).toContain(footnoteId)
})

test('each proof text has a footnote', async () => {
if (!item.Proofs || !(item.Proofs instanceof Array))
return
const text = Object
.entries(item)
.filter(([key, _]) => key.endsWith('WithProofs'))
.map(([_, value]) => value)
.reduce((a, b) => a + b, '')
for (const proof of item.Proofs)
expect(text).toContain(`[${proof.Id}]`)
})

test('no footnotes in non-WithProofs strings', async () => {
const footnoteIds = Object
.entries(item)
.filter(([key, _]) => !key.endsWith('WithProofs'))
.filter(([_, obj]) => typeof obj == "string")
.map(([_, textWithoutProofs]) => textWithoutProofs)
.reduce((a, b) => a + b, '')
.match(/(?<=\[)\d+(?=\])/g)
expect(footnoteIds).toBeNull()
})
}
Loading

0 comments on commit 9ace5d8

Please sign in to comment.