-
Notifications
You must be signed in to change notification settings - Fork 1
feat: update to latest code #252
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
Open
robaone
wants to merge
2
commits into
develop
Choose a base branch
from
feat/update-to-latest-code
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,92 +1,136 @@ | ||
| function Test(testClass) { | ||
| if (!testClass || typeof testClass !== 'object') { | ||
| throw new Error('Test class must be a valid object'); | ||
| } | ||
| this.tests = testClass; | ||
| this.run = function(){ | ||
| var objectKeys = Object.keys(this.tests); | ||
| var propertyNames = Object.getOwnPropertyNames(Object.getPrototypeOf(this.tests)); | ||
| const tests = testClass; | ||
| this.run = function () { | ||
| const results = { passed: 0, failed: 0, errors: [] }; | ||
| const objectKeys = Object.keys(tests); | ||
| const propertyNames = Object.getOwnPropertyNames(Object.getPrototypeOf(tests)); | ||
|
|
||
| const keys = [...new Set([...objectKeys, ...propertyNames])]; | ||
| const keys = [...new Set([...objectKeys, ...propertyNames])]; | ||
|
|
||
| keys.filter(key => key.startsWith('test_')).forEach(k => { | ||
| const before_index = keys.findIndex(key => key == 'before'); | ||
| if(before_index > -1) this.tests['before'](); | ||
| Logger.log(`Running test: ${k.substring(5)}`); | ||
| this.tests[k](); | ||
| const after_index = keys.findIndex(key => key == 'after'); | ||
| if(after_index > -1) this.tests['after'](); | ||
| }); | ||
| } | ||
| keys.filter(key => key.startsWith('test_')).forEach(k => { | ||
| try { | ||
| if (typeof tests.before === 'function') tests.before(); | ||
| Logger.log(`Running test: ${k.substring(5)}`); | ||
| tests[k](); | ||
| results.passed++; | ||
| } catch (error) { | ||
| results.failed++; | ||
| results.errors.push(`${k}: ${error.message || error}`); | ||
| Logger.log(`Test failed: ${k} - ${error.message || error}`); | ||
| } finally { | ||
| try { | ||
| if (typeof tests.after === 'function') tests.after(); | ||
| } catch (error) { | ||
| Logger.log(`After hook failed: ${error.message || error}`); | ||
| } | ||
| } | ||
| }); | ||
| Logger.log(`${results.passed} tests passed`); | ||
| Logger.log(`${results.failed} tests failed`); | ||
| results.errors.forEach((error) => Logger.log(error)); | ||
| return results; | ||
| } | ||
| }; | ||
|
|
||
| var Assert = { | ||
| 'isTrue': function(value){ | ||
| if(value != true){ | ||
| throw 'Value is not true'; | ||
| } | ||
| }, | ||
| 'match': function(expected,actual){ | ||
| if(expected != actual){ | ||
| throw 'Expected value is (' + JSON.stringify(expected) + ') but is (' + JSON.stringify(actual) + ')'; | ||
| } | ||
| }, | ||
| 'deepEquals': function(array1, array2) { | ||
| const result = (() => { | ||
| if (array1.length !== array2.length) { | ||
| return false; | ||
| isTrue(value) { | ||
| if (value !== true) { | ||
| throw new Error('Value is not true'); | ||
| } | ||
| }, | ||
| match(expected, actual) { | ||
| if (expected != actual) { | ||
| throw new Error(`Expected value is (${JSON.stringify(expected)}) but is (${JSON.stringify(actual)})`); | ||
| } | ||
| }, | ||
| equals(expected, actual) { | ||
| if (expected !== actual) { | ||
| throw new Error(`Assertion failed: expected ${expected}, but got ${actual}`); | ||
| } | ||
| }, | ||
| deepEquals(obj1, obj2) { | ||
| // Check if both arguments are of the same type | ||
| if (typeof obj1 !== typeof obj2) { | ||
| throw new Error(`Objects are not the same type: ${typeof obj1} != ${typeof obj2}`); | ||
| } | ||
|
|
||
| for (var i = 0; i < array1.length; i++) { | ||
| if (Array.isArray(array1[i]) && Array.isArray(array2[i])) { | ||
| if (!Assert.deepEquals(array1[i], array2[i])) { | ||
| return false; | ||
| // Handle arrays | ||
| if (Array.isArray(obj1) && Array.isArray(obj2)) { | ||
| if (obj1.length !== obj2.length) { | ||
| throw new Error(`Arrays have different lengths: ${obj1.length} !== ${obj2.length}`); | ||
| } | ||
| for (let i = 0; i < obj1.length; i++) { | ||
| try{ | ||
| this.deepEquals(obj1[i], obj2[i]); | ||
| }catch(error) { | ||
| throw new Error(`Arrays differ at index ${i}: ${error.message}`); | ||
| } | ||
| } else { | ||
| const array1_str = JSON.stringify(array1[i]); | ||
| const array2_str = JSON.stringify(array2[i]); | ||
| if (array1_str !== array2_str) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // Handle objects | ||
| if (typeof obj1 === 'object' && obj1 !== null && typeof obj2 === 'object' && obj2 !== null) { | ||
| const keys1 = Object.keys(obj1); | ||
| const keys2 = Object.keys(obj2); | ||
|
|
||
| if (keys1.length !== keys2.length) { | ||
| throw new Error(`Objects have different number of keys: ${keys1.length} !== ${keys2.length}`); | ||
| } | ||
|
|
||
| for (const key of keys1) { | ||
| if (!keys2.includes(key)) { | ||
| throw new Error(`Key "${key}" missing from second object`); | ||
| } | ||
| try { | ||
| this.deepEquals(obj1[key], obj2[key]); | ||
| } catch (error) { | ||
| throw new Error(`Objects differ at key "${key}": ${error.message}`); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| return true; | ||
| })(); | ||
| if(!result){ | ||
| throw `Expected value is (${JSON.stringify(array1)}) but is (${JSON.stringify(array2)})`; | ||
| // Handle primitive values | ||
| return obj1 === obj2; | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * Copy this class and test_Example function to your implementation file. | ||
| * Change the class name and test name to match the class that you are testing. | ||
| */ | ||
| class ExampleTest { | ||
| before() { | ||
| // This happens before every test | ||
| Logger.log('before()'); | ||
| } | ||
| after() { | ||
| // This happens after every test | ||
| Logger.log('after()'); | ||
| } | ||
| test_testName1() { | ||
| // This is a test | ||
| Logger.log('GIVEN'); | ||
| Logger.log('WHEN'); | ||
| Logger.log('THEN'); | ||
| Assert.isTrue(true); | ||
| } | ||
| test_testName2() { | ||
| // This is a test | ||
| Logger.log('GIVEN'); | ||
| Logger.log('WHEN'); | ||
| Logger.log('THEN'); | ||
| Assert.isTrue(true); | ||
| } | ||
| before() { | ||
| // This happens before every test | ||
| Logger.log('before()'); | ||
| } | ||
| after() { | ||
| // This happens after every test | ||
| Logger.log('after()'); | ||
| } | ||
| test_testName1() { | ||
| // This is a test | ||
| Logger.log('GIVEN'); | ||
| Logger.log('WHEN'); | ||
| Logger.log('THEN'); | ||
| Assert.equals(true,false); | ||
| } | ||
| test_testName2() { | ||
| // This is a test | ||
| Logger.log('GIVEN'); | ||
| Logger.log('WHEN'); | ||
| Logger.log('THEN'); | ||
| Assert.isTrue(true); | ||
| } | ||
| test_testName3() { | ||
| const expected = {this: "that", the: "other"}; | ||
| const actual = {this: "that", the: "other"}; | ||
| Assert.deepEquals(expected,actual); | ||
| } | ||
| } | ||
|
|
||
| function test_ExampleTest() { | ||
| const object = new ExampleTest(); | ||
| new Test(object).run(); | ||
| } | ||
| function test_Example() { | ||
| const object = new ExampleTest(); | ||
| new Test(object).run(); | ||
| } | ||
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.