-
Notifications
You must be signed in to change notification settings - Fork 31
Add module for writing unit tests #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d8e8c79
initial commit
podocarp b8bee5b
Merge branch 'master' of https://github.com/source-academy/modules in…
podocarp d9b1127
fixes and add test
podocarp 3ce2b22
add more test
podocarp feed8a1
remove redundant asserts and make a general one
podocarp 3bd5575
Merge branch 'master' of https://github.com/source-academy/modules in…
RichDom2185 a67e302
Fix auto-fixable lint problems
RichDom2185 fbf05c9
Fix type import
RichDom2185 028d724
Merge branch 'master' of https://github.com/source-academy/modules in…
RichDom2185 08c30ec
Add react ESLint plugin
RichDom2185 43ce719
Add assert_not_equals
RichDom2185 1304ed8
Remove yet-to-be implemented exports
RichDom2185 ed11646
Fix compile error post-merge
RichDom2185 267a2d8
Commit updated ESLint config
RichDom2185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,5 +116,10 @@ | |
"tabs": [ | ||
"Nbody" | ||
] | ||
}, | ||
"testing": { | ||
"tabs": [ | ||
"Testing" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ | |
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-jest": "^27.9.0", | ||
"eslint-plugin-jsx-a11y": "^6.5.1", | ||
"eslint-plugin-react": "^7.37.4", | ||
"eslint-plugin-react-hooks": "^4.4.0", | ||
"globals": "^15.11.0", | ||
"http-server": "^0.12.3", | ||
|
@@ -134,5 +135,6 @@ | |
}, | ||
"resolutions": { | ||
"**/gl": "^8.0.2" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as asserts from '../asserts'; | ||
import * as testing from '../functions'; | ||
import { list } from '../list'; | ||
|
||
beforeAll(() => { | ||
testing.context.suiteResults = { | ||
name: '', | ||
results: [], | ||
total: 0, | ||
passed: 0, | ||
}; | ||
testing.context.allResults.results = []; | ||
testing.context.runtime = 0; | ||
}); | ||
|
||
test('context is created correctly', () => { | ||
const mockTestFn = jest.fn(); | ||
testing.describe('Testing 321', () => { | ||
testing.it('Testing 123', mockTestFn); | ||
}); | ||
expect(testing.context.suiteResults.passed).toEqual(1); | ||
expect(mockTestFn).toHaveBeenCalled(); | ||
}); | ||
|
||
test('context fails correctly', () => { | ||
testing.describe('Testing 123', () => { | ||
testing.it('This test fails!', () => asserts.assert_equals(0, 1)); | ||
}); | ||
expect(testing.context.suiteResults.passed).toEqual(0); | ||
expect(testing.context.suiteResults.total).toEqual(1); | ||
}); | ||
|
||
test('assert works', () => { | ||
expect(() => asserts.assert(() => true)).not.toThrow(); | ||
expect(() => asserts.assert(() => false)).toThrow('Assert failed'); | ||
}); | ||
|
||
test('assert_equals works', () => { | ||
expect(() => asserts.assert_equals(1, 1)).not.toThrow(); | ||
expect(() => asserts.assert_equals(0, 1)).toThrow('Expected'); | ||
expect(() => asserts.assert_equals(1.00000000001, 1)).not.toThrow(); | ||
}); | ||
|
||
test('assert_contains works', () => { | ||
const list1 = list(1, 2, 3); | ||
expect(() => asserts.assert_contains(list1, 2)).not.toThrow(); | ||
expect(() => asserts.assert_contains(list1, 10)).toThrow(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { is_pair, head, tail, is_list, is_null, member, length } from './list'; | ||
|
||
/** | ||
* Asserts that a predicate returns true. | ||
* @param pred An predicate function that returns true/false. | ||
* @returns | ||
*/ | ||
export function assert(pred: () => boolean) { | ||
if (!pred()) { | ||
throw new Error('Assert failed!'); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts the equality (===) of two parameters. | ||
* @param expected The expected value. | ||
* @param received The given value. | ||
* @returns | ||
*/ | ||
export function assert_equals(expected: any, received: any) { | ||
const fail = () => { | ||
throw new Error(`Expected \`${expected}\`, got \`${received}\`!`); | ||
}; | ||
if (typeof expected !== typeof received) { | ||
fail(); | ||
} | ||
// approx checking for floats | ||
if (typeof expected === 'number' && !Number.isInteger(expected)) { | ||
if (Math.abs(expected - received) > 0.001) { | ||
fail(); | ||
} else { | ||
return; | ||
} | ||
} | ||
if (expected !== received) { | ||
fail(); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that two parameters are not equal (!==). | ||
* @param expected The expected value. | ||
* @param received The given value. | ||
* @returns | ||
*/ | ||
export function assert_not_equals(expected: any, received: any) { | ||
if (expected === received) { | ||
throw new Error(`Expected \`${expected}\` to not equal \`${received}\`!`); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that `xs` contains `toContain`. | ||
* @param xs The list to assert. | ||
* @param toContain The element that `xs` is expected to contain. | ||
*/ | ||
export function assert_contains(xs: any, toContain: any) { | ||
const fail = () => { | ||
throw new Error(`Expected \`${xs}\` to contain \`${toContain}\`.`); | ||
}; | ||
|
||
if (is_null(xs)) { | ||
fail(); | ||
} else if (is_list(xs)) { | ||
if (is_null(member(toContain, xs))) { | ||
fail(); | ||
} | ||
} else if (is_pair(xs)) { | ||
if (head(xs) === toContain || tail(xs) === toContain) { | ||
return; | ||
} | ||
|
||
// check the head, if it fails, checks the tail, if that fails, fail. | ||
try { | ||
assert_contains(head(xs), toContain); | ||
return; | ||
} catch (_) { | ||
try { | ||
assert_contains(tail(xs), toContain); | ||
return; | ||
} catch (__) { | ||
fail(); | ||
} | ||
} | ||
} else { | ||
throw new Error(`First argument must be a list or a pair, got \`${xs}\`.`); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that the given list has length `len`. | ||
* @param list The list to assert. | ||
* @param len The expected length of the list. | ||
*/ | ||
export function assert_length(list: any, len: number) { | ||
assert_equals(length(list), len); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import type { TestContext, TestSuite, Test } from './types'; | ||
|
||
const handleErr = (err: any) => { | ||
if (err.error && err.error.message) { | ||
return (err.error as Error).message; | ||
} | ||
if (err.message) { | ||
return (err as Error).message; | ||
} | ||
throw err; | ||
}; | ||
|
||
export const context: TestContext = { | ||
describe: (msg: string, suite: TestSuite) => { | ||
const starttime = performance.now(); | ||
context.suiteResults = { | ||
name: msg, | ||
results: [], | ||
total: 0, | ||
passed: 0, | ||
}; | ||
|
||
suite(); | ||
|
||
context.allResults.results.push(context.suiteResults); | ||
|
||
const endtime = performance.now(); | ||
context.runtime += endtime - starttime; | ||
return context.allResults; | ||
}, | ||
|
||
it: (msg: string, test: Test) => { | ||
const name = `${msg}`; | ||
let error = ''; | ||
context.suiteResults.total += 1; | ||
|
||
try { | ||
test(); | ||
context.suiteResults.passed += 1; | ||
} catch (err: any) { | ||
error = handleErr(err); | ||
} | ||
|
||
context.suiteResults.results.push({ | ||
name, | ||
error, | ||
}); | ||
}, | ||
|
||
suiteResults: { | ||
name: '', | ||
results: [], | ||
total: 0, | ||
passed: 0, | ||
}, | ||
|
||
allResults: { | ||
results: [], | ||
toReplString: () => | ||
`${context.allResults.results.length} suites completed in ${context.runtime} ms.`, | ||
}, | ||
|
||
runtime: 0, | ||
}; | ||
|
||
/** | ||
* Defines a single test. | ||
* @param str Description for this test. | ||
* @param func Function containing tests. | ||
*/ | ||
export function it(msg: string, func: Test) { | ||
context.it(msg, func); | ||
} | ||
|
||
/** | ||
* Describes a test suite. | ||
* @param str Description for this test. | ||
* @param func Function containing tests. | ||
*/ | ||
export function describe(msg: string, func: TestSuite) { | ||
return context.describe(msg, func); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
assert_equals, | ||
assert_not_equals, | ||
assert_contains, | ||
assert_length, | ||
} from './asserts'; | ||
import { it, describe } from './functions'; | ||
import { mock_fn } from './mocks'; | ||
|
||
/** | ||
* Collection of unit-testing tools for Source. | ||
* @author Jia Xiaodong | ||
*/ | ||
|
||
/** | ||
* Increment a number by a value of 1. | ||
* @param x the number to be incremented | ||
* @returns the incremented value of the number | ||
*/ | ||
function sample_function(x: number) { | ||
return x + 1; | ||
} | ||
|
||
// Un-comment the next line if your bundle requires the use of variables | ||
// declared in cadet-frontend or js-slang. | ||
export default () => ({ | ||
sample_function, | ||
it, | ||
describe, | ||
assert_equals, | ||
assert_not_equals, | ||
assert_contains, | ||
assert_length, | ||
mock_fn, | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.