Skip to content

Commit

Permalink
Refactor Tests (#165)
Browse files Browse the repository at this point in the history
* Cleanup tests

* Fix output test
  • Loading branch information
daogrady authored Feb 22, 2024
1 parent 5729077 commit b095b87
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 406 deletions.
4 changes: 2 additions & 2 deletions test/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class JSASTWrapper {
}

constructor(code) {
this.programm = acorn.parse(code, { ecmaVersion: 'latest'})
this.program = acorn.parse(code, { ecmaVersion: 'latest'})
}

exportsAre(expected) {
Expand All @@ -402,7 +402,7 @@ class JSASTWrapper {
}

getExports() {
return this.exports ??= this.programm.body.filter(node => {
return this.exports ??= this.program.body.filter(node => {
if (node.type !== 'ExpressionStatement') return false
if (node.expression.left.type !== 'MemberExpression') return false
const { object, property } = node.expression.left.object
Expand Down
35 changes: 16 additions & 19 deletions test/unit/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const { ASTWrapper, checkFunction, check } = require('../ast')
const { locations, cds2ts } = require('../util')

const dir = locations.testOutput('actions_test')
const { checkFunction, check, ASTWrapper } = require('../ast')
const { locations, prepareUnitTest } = require('../util')

describe('Actions', () => {
beforeEach(async () => await fs.unlink(dir).catch(() => {}))
let paths
let astwBound
let astwUnbound

beforeAll(async () => {
paths = (await prepareUnitTest('actions/model.cds', locations.testOutput('actions_test'))).paths
astwBound = new ASTWrapper(path.join(paths[1], 'index.ts'))
astwUnbound = new ASTWrapper(path.join(paths[2], 'index.ts'))
})

test('Bound', async () => {
const paths = await cds2ts('actions/model.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
const actions = astw.getAspectProperty('_EAspect', 'actions')
const actions = astwBound.getAspectProperty('_EAspect', 'actions')
expect(actions.modifiers.some(check.isStatic)).toBeTruthy()
checkFunction(actions.type.members.find(fn => fn.name === 'f'), {
parameterCheck: ({members: [fst]}) => fst.name === 'x' && check.isNullable(fst.type, [check.isString])
Expand All @@ -31,8 +35,7 @@ describe('Actions', () => {
})

test('Unbound', async () => {
const paths = await cds2ts('actions/model.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const ast = new ASTWrapper(path.join(paths[2], 'index.ts')).tree
const ast = astwUnbound.tree
checkFunction(ast.find(node => node.name === 'free'), {
modifiersCheck: (modifiers = []) => !modifiers.some(check.isStatic),
callCheck: type => check.isNullable(type)
Expand All @@ -50,9 +53,7 @@ describe('Actions', () => {
})

test('Bound Returning External Type', async () => {
const paths = await cds2ts('actions/model.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
const actions = astw.getAspectProperty('_EAspect', 'actions')
const actions = astwBound.getAspectProperty('_EAspect', 'actions')
expect(actions.modifiers.some(check.isStatic)).toBeTruthy()
checkFunction(actions.type.members.find(fn => fn.name === 'f'), {
callCheck: signature => check.isAny(signature),
Expand All @@ -72,8 +73,7 @@ describe('Actions', () => {
})

test('Unbound Returning External Type', async () => {
const paths = await cds2ts('actions/model.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const ast = new ASTWrapper(path.join(paths[2], 'index.ts')).tree
const ast = astwUnbound.tree

checkFunction(ast.find(node => node.name === 'free2'), {
modifiersCheck: (modifiers = []) => !modifiers.some(check.isStatic),
Expand All @@ -89,11 +89,8 @@ describe('Actions', () => {
})

test('Bound Expecting $self Arguments', async () => {
const paths = await cds2ts('actions/model.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
const actions = astw.getAspectProperty('_EAspect', 'actions')
const actions = astwBound.getAspectProperty('_EAspect', 'actions')
expect(actions.modifiers.some(check.isStatic)).toBeTruthy()


// mainly make sure $self parameter is not present at all
checkFunction(actions.type.members.find(fn => fn.name === 's1'), {
Expand Down
25 changes: 6 additions & 19 deletions test/unit/arrayof.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper, checkFunction, check } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('arrayof_test')

const { checkFunction, check } = require('../ast')
const { locations, prepareUnitTest } = require('../util')

describe('array of', () => {
let ast
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('arrayof/model.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
// eslint-disable-next-line no-console
.catch((err) => console.error(err))
ast = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
beforeAll(async () => astw = (await prepareUnitTest('arrayof/model.cds', locations.testOutput('arrayof_test'))).astw)

describe('Entity Properties', () => {
let aspect
beforeAll(async () => aspect = ast.tree.find(n => n.name === '_EAspect').body[0])
beforeAll(async () => aspect = astw.tree.find(n => n.name === '_EAspect').body[0])

test('array of String', async () => {
expect(aspect.members.find(m => m.name === 'stringz'
Expand Down Expand Up @@ -59,7 +46,7 @@ describe('array of', () => {

describe('Function', () => {
let func
beforeAll(async () => func = ast.tree.find(n => n.name === 'fn'))
beforeAll(async () => func = astw.tree.find(n => n.name === 'fn'))

test('Returning array of String', async () => {
//expect(func.type.type.full === 'Array' && func.type.type.args[0].keyword === 'string').toBeTruthy()
Expand Down
17 changes: 5 additions & 12 deletions test/unit/autoexpose.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const { ASTWrapper } = require('../ast')
const { locations, cds2ts } = require('../util')

const dir = locations.testOutput('autoexpose_test')
const { locations, prepareUnitTest } = require('../util')

describe('Autoexpose', () => {
beforeEach(async () => await fs.unlink(dir).catch(() => {}))
let ast

beforeAll(async () => ast = (await prepareUnitTest('autoexpose/service.cds', locations.testOutput('autoexpose_test'))).astw.tree)

test('Autoexposed Composition Target Present in Service', async () => {
const paths = await cds2ts('autoexpose/service.cds', { outputDirectory: dir, inlineDeclarations: 'structured' })
const ast = new ASTWrapper(path.join(paths[1], 'index.ts')).tree
expect(ast.find(n => n.name === 'Books')).toBeTruthy()
})
test('Autoexposed Composition Target Present in Service', async () => expect(ast.find(n => n.name === 'Books')).toBeTruthy())
})
15 changes: 2 additions & 13 deletions test/unit/delimited.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('enums_test')
const { locations, prepareUnitTest } = require('../util')

describe('Delimited Identifiers', () => {
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('delimident/model.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
beforeAll(async () => astw = (await prepareUnitTest('delimident/model.cds', locations.testOutput('delimident_test'))).astw)

test('Properties in Aspect Present', () => {
expect(astw.getAspectProperty('_FooAspect', 'sap-icon://a')).toBeTruthy()
Expand Down
14 changes: 3 additions & 11 deletions test/unit/draft.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper } = require('../ast')
const { locations } = require('../util')
const { locations, prepareUnitTest } = require('../util')

const dir = locations.testOutput('draft_test')
const draftable_ = (entity, ast) => ast.find(n => n.name === entity && n.members.find(({name}) => name === 'drafts'))
const draftable = (entity, ast, plural = e => `${e}_`) => draftable_(entity, ast) && draftable_(plural(entity), ast)

describe('bookshop', () => {
test('Projections Up and Down', async () => {
const paths = await cds2ts.compileFromFile(locations.unit.files('draft/catalog-service.cds'), { outputDirectory: dir })
const paths = (await prepareUnitTest('draft/catalog-service.cds', locations.testOutput('bookshop_projection'))).paths
const service = new ASTWrapper(path.join(paths[1], 'index.ts')).tree
const model = new ASTWrapper(path.join(paths[2], 'index.ts')).tree

Expand All @@ -26,12 +23,7 @@ describe('bookshop', () => {
describe('@odata.draft.enabled', () => {
let ast

beforeAll(async () => {
await fs.unlink(dir).catch(() => {})
const paths = await cds2ts
.compileFromFile(locations.unit.files('draft/model.cds'), { outputDirectory: dir })
ast = new ASTWrapper(path.join(paths[1], 'index.ts')).tree
})
beforeAll(async () => ast = (await prepareUnitTest('draft/model.cds', locations.testOutput('draft_test'))).astw.tree)

test('Direct Annotation', async () => expect(draftable('A', ast)).toBeTruthy())

Expand Down
28 changes: 6 additions & 22 deletions test/unit/enum.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper, check, JSASTWrapper, checkFunction } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('enums_test')
const { check, JSASTWrapper, checkFunction } = require('../ast')
const { locations, prepareUnitTest } = require('../util')

// FIXME: missing: inline enums (entity Foo { bar: String enum { ... }})
describe('Enum Action Parameters', () => {
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('enums/actions.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
beforeAll(async () => astw = (await prepareUnitTest('enums/actions.cds', locations.testOutput('enums_test_actions'))).astw)

test('Coalescing Assignment Present', () => {
const actions = astw.getAspectProperty('_FoobarAspect', 'actions')
Expand All @@ -43,15 +34,13 @@ describe('Enum Action Parameters', () => {
describe('Nested Enums', () => {
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('enums/nested.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
const paths = (await prepareUnitTest('enums/nested.cds', locations.testOutput('enums_test_nested'))).paths
astw = await JSASTWrapper.initialise(path.join(paths[1], 'index.js'))
})

test('Coalescing Assignment Present', () => {
const stmts = astw.programm.body
const stmts = astw.program.body
const enm = stmts.find(n => n.type === 'ExpressionStatement' && n.expression.type === 'AssignmentExpression' && n.expression.operator === '??=')
expect(enm).toBeTruthy()
const { left } = enm.expression
Expand All @@ -64,12 +53,7 @@ describe('Nested Enums', () => {
describe('Enum Types', () => {
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('enums/model.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
astw = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
beforeAll(async () => astw = (await prepareUnitTest('enums/model.cds', locations.testOutput('enums_test_model'))).astw)

describe('Anonymous', () => {
describe('Within type Definition', () => {
Expand Down
25 changes: 6 additions & 19 deletions test/unit/events.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper, check } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('events_test')

const { check } = require('../ast')
const { locations, prepareUnitTest } = require('../util')

describe('events', () => {
let ast

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('events/model.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
// eslint-disable-next-line no-console
.catch((err) => console.error(err))
ast = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
let astw

beforeAll(async () => astw = (await prepareUnitTest('events/model.cds', locations.testOutput('events_test'))).astw)

describe('Event Type Present', () => {
test('Top Level Event', async () => {
expect(ast.tree.find(cls => cls.name === 'Bar'
expect(astw.tree.find(cls => cls.name === 'Bar'
&& cls.members.length === 2
&& cls.members[0].name === 'id' && check.isNullable(cls.members[0].type, [check.isNumber])
&& cls.members[1].name === 'name' && check.isNullable(cls.members[1].type, [check.isIndexedAccessType])
Expand Down
11 changes: 2 additions & 9 deletions test/unit/excluding.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper } = require('../ast')
const { locations } = require('../util')
const dir = locations.testOutput('excluding_test')
const { locations, prepareUnitTest } = require('../util')

describe('Excluding Clause', () => {
let paths

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
paths = await cds2ts
.compileFromFile(locations.unit.files('excluding/model.cds'), { outputDirectory: dir })
})
beforeAll(async () => paths = (await prepareUnitTest('excluding/model.cds', locations.testOutput('excluding_test'))).paths)

test('Element Present in Original', async () =>
expect(new ASTWrapper(path.join(paths[1], 'index.ts')).exists('_TestObjectAspect', 'dependencies')).toBeTruthy())
Expand Down
31 changes: 10 additions & 21 deletions test/unit/foreignkeys.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
'use strict'

const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('foreign_keys')
const { locations, prepareUnitTest } = require('../util')

describe('Foreign Keys', () => {
let ast
beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('foreignkeys/model.cds'), { outputDirectory: dir })
ast = new ASTWrapper(path.join(paths[1], 'index.ts'))
})
let astw
beforeAll(async () => astw = (await prepareUnitTest('foreignkeys/model.cds', locations.testOutput('foreign_keys'))).astw)

test('One Level Deep', async () => {
expect(ast.exists('_BAspect', 'c_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(ast.exists('_BAspect', 'd_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(ast.exists('_CAspect', 'e_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_BAspect', 'c_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_BAspect', 'd_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_CAspect', 'e_ID', m => m.type.keyword === 'string')).toBeTruthy()
})

test('Two Levels Deep', async () => {
expect(ast.exists('_AAspect', 'b_c_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(ast.exists('_AAspect', 'b_d_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(ast.exists('_BAspect', 'c_e_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_AAspect', 'b_c_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_AAspect', 'b_d_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_BAspect', 'c_e_ID', m => m.type.keyword === 'string')).toBeTruthy()
})

test('Three Levels Deep', async () => {
expect(ast.exists('_AAspect', 'b_c_e_ID', m => m.type.keyword === 'string')).toBeTruthy()
expect(astw.exists('_AAspect', 'b_c_e_ID', m => m.type.keyword === 'string')).toBeTruthy()
})
})
Loading

0 comments on commit b095b87

Please sign in to comment.