Skip to content
Permalink
Browse files
Create simple unit tests for most easily testable code. Add CI for ru…
…nning the unit tests. Closes issue #21
  • Loading branch information
Shanedell authored and stevedlawrence committed Oct 27, 2021
1 parent 7ce383f commit f5f5fffc8b036975a141bac0b035beccfe6f4070
Showing 6 changed files with 339 additions and 1 deletion.
@@ -0,0 +1,39 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
name: Unit Tests
on:
push:
branches-ignore: [ 'dependabot/**' ]
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: Unit Tests
strategy:
matrix:
os: [macos-11, ubuntu-20.04, windows-2019]
node: [ '10', '12', '14', '16' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2.3.5
- name: Install Node.js
uses: actions/setup-node@v1.4.6
with:
node-version: ${{ matrix.node}}
- run: npm install
- run: npm test
@@ -31,7 +31,9 @@
"watch2": "tsc -watch -p ./",
"package": "vsce package",
"publish": "vsce publish",
"package-ext": "webpack --mode production --config ./build/extension.webpack.config.js"
"package-ext": "webpack --mode production --config ./build/extension.webpack.config.js",
"pretest": "yarn run compile && yarn run lint",
"test": "node node_modules/mocha/bin/_mocha -u tdd --timeout 999999 --colors ./out/tests"
},
"dependencies": {
"await-notify": "1.0.1",
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert'
import * as path from 'path'
import { DebugClient } from 'vscode-debugadapter-testsupport'

suite('Daffodil Debug Adapter', () => {
const DEBUG_ADAPTER = './out/adapter/debugAdapter.js'
const PROJECT_ROOT = path.join(__dirname, '../../')
const DATA_ROOT = path.join(PROJECT_ROOT, 'src/tests/data/')

let client: DebugClient

setup(() => {
client = new DebugClient('node', DEBUG_ADAPTER, 'dfdl')
return client.start()
})

teardown(() => client.stop())

suite('basic', () => {
test('unknown request should produce error', (done) => {
client
.send('illegal_request')
.then(() => {
done(new Error('does not report error on unknown request'))
})
.catch(() => {
done()
})
})
})

suite('initialize', () => {
test('should return supported features', () => {
return client.initializeRequest().then((response) => {
response.body = response.body || {}
assert.strictEqual(response.body.supportsConfigurationDoneRequest, true)
})
})

test("should produce error for invalid 'pathFormat'", (done) => {
client
.initializeRequest({
adapterID: 'dfdl',
linesStartAt1: true,
columnsStartAt1: true,
pathFormat: 'url',
})
.then((response) => {
done(
new Error("does not report error on invalid 'pathFormat' attribute")
)
})
.catch((err) => {
// error expected
done()
})
})
})

suite('launch', () => {
test('should run program to the end', () => {
const PROGRAM = path.join(DATA_ROOT, 'test.dfdl.xsd')

return Promise.all([
client.configurationSequence(),
client.launch({ program: PROGRAM }),
client.waitForEvent('terminated'),
])
})

test('should stop on entry', () => {
const PROGRAM = path.join(DATA_ROOT, 'test.dfdl.xsd')
const ENTRY_LINE = 1

return Promise.all([
client.configurationSequence(),
client.launch({ program: PROGRAM, stopOnEntry: true }),
client.assertStoppedLocation('entry', { line: ENTRY_LINE }),
])
})
})
})
@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert'
import * as daffodil from '../daffodil'

suite('Daffodfil', () => {
// suite to test all functions work properly
suite('interfaces', () => {
test('DaffodilData functions properly', () => {
let daffodilData: daffodil.DaffodilData = {
bytePos1b: 100,
}

assert.strictEqual(100, daffodilData.bytePos1b)
})

test('InfosetEvent functions properly', () => {
let infosetEvent: daffodil.InfosetEvent = {
content: 'This is content',
mimeType: 'xml',
}

assert.strictEqual('This is content', infosetEvent.content)
assert.strictEqual('xml', infosetEvent.mimeType)
})

test('InfosetOutput functions properly', () => {
let infosetOutput: daffodil.InfosetOutput = {
type: 'console',
}

assert.strictEqual('console', infosetOutput.type)
})

test('BuildInfo functions properly', () => {
let allVersions = '1.0.0'
let buildInfo: daffodil.BuildInfo = {
version: allVersions,
daffodilVersion: allVersions,
scalaVersion: allVersions,
}

assert.strictEqual(allVersions, buildInfo.version)
assert.strictEqual(allVersions, buildInfo.daffodilVersion)
assert.strictEqual(allVersions, buildInfo.scalaVersion)
})

test('LaunchArgs functions properly', () => {
let infosetOutput: daffodil.InfosetOutput = {
type: 'console',
}

let launchArgs: daffodil.LaunchArgs = {
schemaPath: '/path/to/schema.xsd.xml',
dataPath: '/path/to/data.jpg',
stopOnEntry: true,
infosetOutput: infosetOutput,
}

assert.strictEqual('/path/to/schema.xsd.xml', launchArgs.schemaPath)
assert.strictEqual('/path/to/data.jpg', launchArgs.dataPath)
assert.strictEqual(true, launchArgs.stopOnEntry)
assert.strictEqual(infosetOutput, launchArgs.infosetOutput)
})

test('ConfigEvent functions properly', () => {
let allVersions = '1.0.0'
let infosetOutput: daffodil.InfosetOutput = {
type: 'console',
}
let buildInfo: daffodil.BuildInfo = {
version: allVersions,
daffodilVersion: allVersions,
scalaVersion: allVersions,
}
let launchArgs: daffodil.LaunchArgs = {
schemaPath: '/path/to/schema.xsd.xml',
dataPath: '/path/to/data.jpg',
stopOnEntry: true,
infosetOutput: infosetOutput,
}

let configEvent: daffodil.ConfigEvent = {
buildInfo: buildInfo,
launchArgs: launchArgs,
}

assert.strictEqual(buildInfo, configEvent.buildInfo)
assert.strictEqual(launchArgs, configEvent.launchArgs)
})
})

// suite to test all constants get set properly
suite('constants', () => {
test('dataEvent set properly', () => {
assert.strictEqual(daffodil.dataEvent, 'daffodil.data')
})

test('infosetEvent set properly', () => {
assert.strictEqual(daffodil.infosetEvent, 'daffodil.infoset')
})

test('configEvent set properly', () => {
assert.strictEqual(daffodil.configEvent, 'daffodil.config')
})
})
})
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<xs:schema xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="25"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

</xs:schema>
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert'
import * as fs from 'fs'
import * as path from 'path'

suite('Daffodil Version', () => {
const PROJECT_ROOT = path.join(__dirname, '../../')
const versionFile = path.join(PROJECT_ROOT, 'src/version.ts')
const packageMapped = JSON.parse(
fs.readFileSync(path.join(PROJECT_ROOT, 'package.json')).toString()
)

suite('version', () => {
test('version.ts should exist', (done) => {
if (fs.existsSync(versionFile)) {
done()
} else {
new Error('version.ts not created')
}
})

test('version.ts version should be same as package.json', () => {
let version = fs.readFileSync(versionFile).toString().trim()
assert.strictEqual(
version,
`export const LIB_VERSION = "${packageMapped.version}";`
)
})
})
})

0 comments on commit f5f5fff

Please sign in to comment.