Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.2k
feat: add runnable examples with test harness#628
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
itzshikharofficial12 wants to merge1 commit intoleonardomso:masterChoose a base branch fromitzshikharofficial12:add-runnable-examples-and-tests
base:master
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.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletionsREADME.md
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
25 changes: 25 additions & 0 deletionsexamples/call-stack.js
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,25 @@ | ||
| // Example: Call Stack (LIFO order) | ||
| function a() { | ||
| console.log('a start'); | ||
| b(); | ||
| console.log('a end'); | ||
| } | ||
| function b() { | ||
| console.log('b start'); | ||
| c(); | ||
| console.log('b end'); | ||
| } | ||
| function c() { | ||
| console.log('c'); | ||
| } | ||
| a(); | ||
| // Expected output: | ||
| // a start | ||
| // b start | ||
| // c | ||
| // b end | ||
| // a end |
15 changes: 15 additions & 0 deletionsexamples/call-stack.test.js
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,15 @@ | ||
| const { expectOutput } = require('./test-utils'); | ||
| function testCallStack() { | ||
| return expectOutput(() => { | ||
| require('./call-stack'); | ||
| }, [ | ||
| 'a start', | ||
| 'b start', | ||
| 'c', | ||
| 'b end', | ||
| 'a end' | ||
| ]); | ||
| } | ||
| module.exports = testCallStack; |
12 changes: 12 additions & 0 deletionsexamples/closures.js
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,12 @@ | ||
| // Example: Closures — a function that remembers its lexical scope | ||
| function makeCounter() { | ||
| let count = 0; | ||
| return function () { | ||
| count += 1; | ||
| return count; | ||
| }; | ||
| } | ||
| const counter = makeCounter(); | ||
| console.log('closure:', counter()); // 1 | ||
| console.log('closure:', counter()); // 2 |
12 changes: 12 additions & 0 deletionsexamples/closures.test.js
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,12 @@ | ||
| const { expectOutput } = require('./test-utils'); | ||
| function testClosures() { | ||
| return expectOutput(() => { | ||
| require('./closures'); | ||
| }, [ | ||
| 'closure: 1', | ||
| 'closure: 2' | ||
| ]); | ||
| } | ||
| module.exports = testClosures; |
14 changes: 14 additions & 0 deletionsexamples/event-loop.js
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,14 @@ | ||
| // Example: Event Loop ordering (macrotasks vs microtasks) | ||
| console.log('script start'); | ||
| setTimeout(() => console.log('timeout callback (macrotask)'), 0); | ||
| Promise.resolve().then(() => console.log('promise callback (microtask)')); | ||
| console.log('script end'); | ||
| // Expected order: | ||
| // script start | ||
| // script end | ||
| // promise callback (microtask) | ||
| // timeout callback (macrotask) |
14 changes: 14 additions & 0 deletionsexamples/event-loop.test.js
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,14 @@ | ||
| const { expectOutputAsync } = require('./test-utils'); | ||
| function testEventLoop() { | ||
| return expectOutputAsync(() => { | ||
| require('./event-loop'); | ||
| }, [ | ||
| 'script start', | ||
| 'script end', | ||
| 'promise callback (microtask)', | ||
| 'timeout callback (macrotask)' | ||
| ], 100); | ||
| } | ||
| module.exports = testEventLoop; |
10 changes: 10 additions & 0 deletionsexamples/map-filter-reduce.js
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,10 @@ | ||
| // Example: map, filter and reduce | ||
| const arr = [1, 2, 3, 4, 5]; | ||
| const squares = arr.map(n => n * n); | ||
| const evens = arr.filter(n => n % 2 === 0); | ||
| const sum = arr.reduce((acc, n) => acc + n, 0); | ||
| console.log('map -> squares:', squares); // [1,4,9,16,25] | ||
| console.log('filter -> evens:', evens); // [2,4] | ||
| console.log('reduce -> sum:', sum); // 15 |
13 changes: 13 additions & 0 deletionsexamples/map-filter-reduce.test.js
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,13 @@ | ||
| const { expectOutput } = require('./test-utils'); | ||
| function testMapFilterReduce() { | ||
| return expectOutput(() => { | ||
| require('./map-filter-reduce'); | ||
| }, [ | ||
| 'map -> squares: [1,4,9,16,25]', | ||
| 'filter -> evens: [2,4]', | ||
| 'reduce -> sum: 15' | ||
| ]); | ||
| } | ||
| module.exports = testMapFilterReduce; |
12 changes: 12 additions & 0 deletionsexamples/primitives-vs-references.js
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,12 @@ | ||
| // Example: Primitive vs Reference types | ||
| // Primitives are copied by value | ||
| let a = 1; | ||
| let b = a; | ||
| b = 2; | ||
| console.log('primitives:', { a, b }); // a stays 1, b is 2 | ||
| // Objects are copied by reference | ||
| const obj1 = { x: 1 }; | ||
| const obj2 = obj1; | ||
| obj2.x = 2; | ||
| console.log('references:', { obj1, obj2 }); // both reflect the change |
12 changes: 12 additions & 0 deletionsexamples/primitives-vs-references.test.js
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,12 @@ | ||
| const { expectOutput } = require('./test-utils'); | ||
| function testPrimitivesVsReferences() { | ||
| return expectOutput(() => { | ||
| require('./primitives-vs-references'); | ||
| }, [ | ||
| 'primitives: {"a":1,"b":2}', | ||
| 'references: {"obj1":{"x":2},"obj2":{"x":2}}' | ||
| ]); | ||
| } | ||
| module.exports = testPrimitivesVsReferences; |
12 changes: 12 additions & 0 deletionsexamples/promises-async-await.js
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,12 @@ | ||
| // Example: Promises and async/await | ||
| function wait(ms) { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
| async function run() { | ||
| console.log('before await'); | ||
| await wait(50); | ||
| console.log('after await'); | ||
| } | ||
| run(); |
12 changes: 12 additions & 0 deletionsexamples/promises-async-await.test.js
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,12 @@ | ||
| const { expectOutputAsync } = require('./test-utils'); | ||
| function testPromisesAsyncAwait() { | ||
| return expectOutputAsync(() => { | ||
| require('./promises-async-await'); | ||
| }, [ | ||
| 'before await', | ||
| 'after await' | ||
| ], 100); | ||
| } | ||
| module.exports = testPromisesAsyncAwait; |
31 changes: 31 additions & 0 deletionsexamples/run-tests.js
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,31 @@ | ||
| // Test runner | ||
| const { expectOutput } = require('./test-utils'); | ||
| // Run all example tests | ||
| async function runTests() { | ||
| const tests = [ | ||
| require('./call-stack.test.js'), | ||
| require('./primitives-vs-references.test.js'), | ||
| require('./closures.test.js'), | ||
| require('./map-filter-reduce.test.js'), | ||
| require('./event-loop.test.js'), | ||
| require('./this-call-bind.test.js'), | ||
| require('./promises-async-await.test.js'), | ||
| ]; | ||
| console.log('\nRunning tests...\n'); | ||
| let passed = 0; | ||
| let failed = 0; | ||
| for (const test of tests) { | ||
| const result = await test(); | ||
| if (result) passed++; | ||
| else failed++; | ||
| } | ||
| console.log(`\nResults: ${passed} passed, ${failed} failed\n`); | ||
| process.exit(failed > 0 ? 1 : 0); | ||
| } | ||
| runTests().catch(console.error); |
81 changes: 81 additions & 0 deletionsexamples/test-utils.js
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,81 @@ | ||
| // Simple test harness | ||
| function expectOutput(fn, expectedLines) { | ||
| const originalLog = console.log; | ||
| const actualOutput = []; | ||
| console.log = (...args) => { | ||
| // Handle object stringification consistently | ||
| const formatted = args.map(arg => | ||
| typeof arg === 'object' ? JSON.stringify(arg) : String(arg) | ||
| ).join(' '); | ||
| actualOutput.push(formatted); | ||
| }; | ||
| try { | ||
| fn(); | ||
| console.log = originalLog; | ||
| const passed = expectedLines.every((expected, i) => { | ||
| if (!actualOutput[i]) return false; | ||
| // Normalize JSON strings and arrays for comparison | ||
| const normalizedExpected = expected.replace(/[\s{}[\]]/g, ''); | ||
| const normalizedActual = actualOutput[i].replace(/[\s{}[\]]/g, ''); | ||
| return normalizedActual.includes(normalizedExpected); | ||
| }); | ||
| if (passed) { | ||
| console.log('\x1b[32m✓\x1b[0m', fn.name || 'Test passed'); | ||
| return true; | ||
| } else { | ||
| console.log('\x1b[31m✗\x1b[0m', fn.name || 'Test failed'); | ||
| console.log('Expected:', expectedLines); | ||
| console.log('Got:', actualOutput); | ||
| return false; | ||
| } | ||
| } catch (err) { | ||
| console.log = originalLog; | ||
| console.error('\x1b[31m✗\x1b[0m Test error:', err); | ||
| return false; | ||
| } | ||
| } | ||
| // For async tests | ||
| function expectOutputAsync(fn, expectedLines, timeout = 1000) { | ||
| return new Promise((resolve) => { | ||
| const originalLog = console.log; | ||
| const actualOutput = []; | ||
| console.log = (...args) => { | ||
| const formatted = args.map(arg => | ||
| typeof arg === 'object' ? JSON.stringify(arg) : String(arg) | ||
| ).join(' '); | ||
| actualOutput.push(formatted); | ||
| }; | ||
| fn(); | ||
| // Wait for all async operations to complete | ||
| setTimeout(() => { | ||
| console.log = originalLog; | ||
| const passed = expectedLines.every((expected, i) => { | ||
| if (!actualOutput[i]) return false; | ||
| const normalizedExpected = expected.replace(/[\s{}[\]]/g, ''); | ||
| const normalizedActual = actualOutput[i].replace(/[\s{}[\]]/g, ''); | ||
| return normalizedActual.includes(normalizedExpected); | ||
| }); | ||
| if (passed) { | ||
| console.log('\x1b[32m✓\x1b[0m', fn.name || 'Test passed'); | ||
| resolve(true); | ||
| } else { | ||
| console.log('\x1b[31m✗\x1b[0m', fn.name || 'Test failed'); | ||
| console.log('Expected:', expectedLines); | ||
| console.log('Got:', actualOutput); | ||
| resolve(false); | ||
| } | ||
| }, timeout); | ||
| }); | ||
| } | ||
| module.exports = { expectOutput, expectOutputAsync }; |
13 changes: 13 additions & 0 deletionsexamples/this-call-bind.js
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,13 @@ | ||
| // Example: this, call, apply and bind | ||
| const person = { | ||
| name: 'Alice', | ||
| greet() { | ||
| return `Hello ${this.name}`; | ||
| } | ||
| }; | ||
| const greet = person.greet; | ||
| console.log('direct method:', person.greet()); // Hello Alice | ||
| console.log('extracted function (this lost):', greet()); // undefined or global | ||
| console.log('call ->', greet.call({ name: 'Bob' })); // Hello Bob | ||
| console.log('bind ->', greet.bind({ name: 'Carol' })()); // Hello Carol |
14 changes: 14 additions & 0 deletionsexamples/this-call-bind.test.js
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,14 @@ | ||
| const { expectOutput } = require('./test-utils'); | ||
| function testThisCallBind() { | ||
| return expectOutput(() => { | ||
| require('./this-call-bind'); | ||
| }, [ | ||
| 'direct method: Hello Alice', | ||
| 'extracted function (this lost): Hello undefined', | ||
| 'call -> Hello Bob', | ||
| 'bind -> Hello Carol' | ||
| ]); | ||
| } | ||
| module.exports = testThisCallBind; |
4 changes: 4 additions & 0 deletionsindex.js
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
13 changes: 13 additions & 0 deletionspackage-lock.json
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
3 changes: 2 additions & 1 deletionpackage.json
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
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.