forked from GitHub/gf-core
Add bootNGF. Organise tests better.
This commit is contained in:
1
.github/workflows/build-majestic.yml
vendored
1
.github/workflows/build-majestic.yml
vendored
@@ -61,6 +61,7 @@ jobs:
|
|||||||
working-directory: ./src/runtime/javascript
|
working-directory: ./src/runtime/javascript
|
||||||
env:
|
env:
|
||||||
LD_LIBRARY_PATH: /usr/local/lib
|
LD_LIBRARY_PATH: /usr/local/lib
|
||||||
|
PYTHON: /usr/bin/python2
|
||||||
run: |
|
run: |
|
||||||
npm ci
|
npm ci
|
||||||
npm run test
|
npm run test
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ interface Pointer extends Buffer {
|
|||||||
class PGFError extends Error {
|
class PGFError extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message)
|
super(message)
|
||||||
|
// https://stackoverflow.com/a/65243177/98600
|
||||||
|
Object.setPrototypeOf(this, PGFError.prototype)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,17 +149,17 @@ function PgfText_AsString (txtPtr: Pointer) {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// PGF grammar object
|
// PGF grammar object
|
||||||
|
|
||||||
class PGF {
|
export class PGFGrammar {
|
||||||
db: Pointer
|
readonly db: Pointer
|
||||||
revision: number
|
readonly revision: number
|
||||||
|
|
||||||
constructor () {
|
constructor (db: Pointer, revision: number) {
|
||||||
this.db = ref.NULL_POINTER as Pointer
|
this.db = db
|
||||||
this.revision = 0
|
this.revision = revision
|
||||||
}
|
}
|
||||||
|
|
||||||
// NB the library user is responsible for calling this
|
// NB the library user is responsible for calling this
|
||||||
destructor () {
|
release () {
|
||||||
runtime.pgf_free_revision(this.db, this.revision)
|
runtime.pgf_free_revision(this.db, this.revision)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,23 +201,29 @@ class PGF {
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// PGF module functions
|
// PGF module functions
|
||||||
|
|
||||||
function readPGF (path: string): PGF {
|
function readPGF (path: string): PGFGrammar {
|
||||||
const rev = ref.alloc(PgfRevision) as Pointer
|
const rev = ref.alloc(PgfRevision) as Pointer
|
||||||
const err = ref.alloc(PgfExn) as Pointer
|
const err = ref.alloc(PgfExn) as Pointer
|
||||||
const db = runtime.pgf_read_pgf(path, rev, err)
|
const db = runtime.pgf_read_pgf(path, rev, err)
|
||||||
handleError(err)
|
handleError(err)
|
||||||
|
return new PGFGrammar(db, rev.deref())
|
||||||
|
}
|
||||||
|
|
||||||
const state = new PGF()
|
function bootNGF (pgf_path: string, ngf_path: string): PGFGrammar {
|
||||||
state.db = db
|
const rev = ref.alloc(PgfRevision) as Pointer
|
||||||
state.revision = rev.deref()
|
const err = ref.alloc(PgfExn) as Pointer
|
||||||
return state
|
const db = runtime.pgf_boot_ngf(pgf_path, ngf_path, rev, err)
|
||||||
|
handleError(err)
|
||||||
|
return new PGFGrammar(db, rev.deref())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Exposed library API
|
// Exposed library API
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
readPGF
|
PGFError,
|
||||||
|
readPGF,
|
||||||
|
bootNGF
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -1,30 +1,86 @@
|
|||||||
import PGF from '../index'
|
import PGF, { PGFGrammar } from '../index'
|
||||||
|
import fs from 'fs'
|
||||||
|
|
||||||
// readPGF
|
describe('readPGF', () => {
|
||||||
|
test('valid', () => {
|
||||||
|
PGF.readPGF('../haskell/tests/basic.pgf')
|
||||||
|
})
|
||||||
|
|
||||||
test('readPGF successful', () => {
|
test('non-existent', () => {
|
||||||
let gr = PGF.readPGF('../haskell/tests/basic.pgf')
|
expect(() => {
|
||||||
|
PGF.readPGF('../haskell/tests/abc.pgf')
|
||||||
|
}).toThrow(Error)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GF', () => {
|
||||||
|
expect(() => {
|
||||||
|
PGF.readPGF('../haskell/tests/basic.gf')
|
||||||
|
}).toThrow(PGF.PGFError)
|
||||||
|
})
|
||||||
|
|
||||||
|
// test('NGF', () => {
|
||||||
|
// expect(() => {
|
||||||
|
// PGF.readPGF('basic.ngf')
|
||||||
|
// }).toThrow(PGF.PGFError)
|
||||||
|
// })
|
||||||
})
|
})
|
||||||
|
|
||||||
test('readPGF missing file', () => {
|
describe('bootNGF', () => {
|
||||||
expect(() => {
|
beforeAll(() => {
|
||||||
PGF.readPGF('abc.pgf')
|
try {
|
||||||
}).toThrow()
|
fs.unlinkSync('./basic.ngf')
|
||||||
|
} catch { }
|
||||||
|
})
|
||||||
|
|
||||||
|
test('valid', () => {
|
||||||
|
PGF.bootNGF('../haskell/tests/basic.pgf', './basic.ngf')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('non-existent', () => {
|
||||||
|
expect(() => {
|
||||||
|
PGF.bootNGF('../haskell/tests/abc.pgf', './abc.ngf')
|
||||||
|
}).toThrow(Error)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GF', () => {
|
||||||
|
expect(() => {
|
||||||
|
PGF.bootNGF('../haskell/tests/basic.gf', './abc.ngf')
|
||||||
|
}).toThrow(PGF.PGFError)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('NGF', () => {
|
||||||
|
expect(() => {
|
||||||
|
PGF.bootNGF('./basic.ngf', './abc.ngf')
|
||||||
|
}).toThrow(PGF.PGFError)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('existing', () => {
|
||||||
|
expect(() => {
|
||||||
|
PGF.bootNGF('../haskell/tests/basic.pgf', './basic.ngf')
|
||||||
|
}).toThrow(Error)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// abstract syntax
|
describe('abstract syntax', () => {
|
||||||
|
let gr: PGFGrammar
|
||||||
|
|
||||||
test('abstract name', () => {
|
beforeAll(() => {
|
||||||
let gr = PGF.readPGF('../haskell/tests/basic.pgf')
|
gr = PGF.readPGF('../haskell/tests/basic.pgf')
|
||||||
expect(gr.getAbstractName()).toBe('basic')
|
})
|
||||||
})
|
|
||||||
|
|
||||||
test('categories', () => {
|
afterAll(() => {
|
||||||
let gr = PGF.readPGF('../haskell/tests/basic.pgf')
|
gr.release()
|
||||||
expect(gr.getCategories()).toEqual(['Float','Int','N','P','S','String'])
|
})
|
||||||
})
|
|
||||||
|
|
||||||
test('functions', () => {
|
test('abstract name', () => {
|
||||||
let gr = PGF.readPGF('../haskell/tests/basic.pgf')
|
expect(gr.getAbstractName()).toBe('basic')
|
||||||
expect(gr.getFunctions()).toEqual(['c','ind','s','z'])
|
})
|
||||||
|
|
||||||
|
test('categories', () => {
|
||||||
|
expect(gr.getCategories()).toEqual(['Float','Int','N','P','S','String'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('functions', () => {
|
||||||
|
expect(gr.getFunctions()).toEqual(['c','ind','s','z'])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from pgf import *
|
|||||||
def PGF():
|
def PGF():
|
||||||
return readPGF("../haskell/tests/basic.pgf")
|
return readPGF("../haskell/tests/basic.pgf")
|
||||||
|
|
||||||
def test_readPGF_non_existant():
|
def test_readPGF_non_existent():
|
||||||
with pytest.raises(FileNotFoundError):
|
with pytest.raises(FileNotFoundError):
|
||||||
readPGF("../haskell/tests/abc.pgf")
|
readPGF("../haskell/tests/abc.pgf")
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ def NGF():
|
|||||||
yield ngf
|
yield ngf
|
||||||
os.remove("./basic.ngf")
|
os.remove("./basic.ngf")
|
||||||
|
|
||||||
def test_bootNGF_non_existant():
|
def test_bootNGF_non_existent():
|
||||||
with pytest.raises(FileNotFoundError):
|
with pytest.raises(FileNotFoundError):
|
||||||
bootNGF("../haskell/tests/abc.pgf", "./abc.ngf")
|
bootNGF("../haskell/tests/abc.pgf", "./abc.ngf")
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ def test_bootNGF_existing(NGF):
|
|||||||
|
|
||||||
# readNGF
|
# readNGF
|
||||||
|
|
||||||
def test_readNGF_non_existant():
|
def test_readNGF_non_existent():
|
||||||
with pytest.raises(FileNotFoundError):
|
with pytest.raises(FileNotFoundError):
|
||||||
readNGF("./abc.ngf")
|
readNGF("./abc.ngf")
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ def test_functionsByCat_1(PGF):
|
|||||||
def test_functionsByCat_2(PGF):
|
def test_functionsByCat_2(PGF):
|
||||||
assert PGF.functionsByCat("S") == ["c"]
|
assert PGF.functionsByCat("S") == ["c"]
|
||||||
|
|
||||||
def test_functionsByCat_non_existant(PGF):
|
def test_functionsByCat_non_existent(PGF):
|
||||||
assert PGF.functionsByCat("X") == []
|
assert PGF.functionsByCat("X") == []
|
||||||
|
|
||||||
def test_categoryContext_1(PGF):
|
def test_categoryContext_1(PGF):
|
||||||
@@ -169,7 +169,7 @@ def test_functionType_2(PGF):
|
|||||||
def test_functionType_3(PGF):
|
def test_functionType_3(PGF):
|
||||||
assert PGF.functionType("c") == readType("N -> S")
|
assert PGF.functionType("c") == readType("N -> S")
|
||||||
|
|
||||||
def test_functionType_non_existant(PGF):
|
def test_functionType_non_existent(PGF):
|
||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
assert PGF.functionType("cbx")
|
assert PGF.functionType("cbx")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user