Skip to content

Commit ce87fed

Browse files
committed
Convert pre-modules exercises to stdout-based testing
1 parent 3b08e3d commit ce87fed

File tree

62 files changed

+741
-196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+741
-196
lines changed

exercises/01.expressions-and-output/01.solution.hello-world/index.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import { execSync } from 'node:child_process'
33
import { test } from 'node:test'
44

55
await test('should print Hello, World!', () => {
6-
const output = execSync('node index.ts', { encoding: 'utf8' })
76
assert.ok(
8-
output.includes('Hello, World!'),
7+
execSync('npm start --silent', { encoding: 'utf8' }).includes(
8+
'Hello, World!',
9+
),
910
'🚨 Output should include "Hello, World!" - make sure you used console.log() with that exact string',
1011
)
1112
})
1213

1314
await test('should print at least two lines', () => {
14-
const output = execSync('node index.ts', { encoding: 'utf8' })
15+
const output = execSync('npm start --silent', { encoding: 'utf8' })
1516
const lines = output.trim().split('\n').filter(Boolean)
1617
assert.ok(
1718
lines.length >= 2,

exercises/01.expressions-and-output/02.problem.escaping-strings/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@
1212

1313
// 🐨 Log tab-separated data: Name: [tab] Age: [tab] City: (like column headers)
1414
// 💰 Use \t to create tabs between words
15+
16+
// 🐨 When you're done, uncomment this and update the values:
17+
// const results = {
18+
// apostrophe: "It's working!",
19+
// quotes: 'She said "Hi"',
20+
// newlines: 'Hello\nWorld',
21+
// tabs: 'Name:\tAge:\tCity:',
22+
// }
23+
// console.log('Results JSON:', JSON.stringify(results))

exercises/01.expressions-and-output/02.solution.escaping-strings/index.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@ import assert from 'node:assert/strict'
22
import { execSync } from 'node:child_process'
33
import { test } from 'node:test'
44

5+
const output = execSync('npm start --silent', { encoding: 'utf8' })
6+
const jsonLine = output
7+
.split('\n')
8+
.find((line) => line.startsWith('Results JSON:'))
9+
assert.ok(jsonLine, '🚨 Missing "Results JSON:" output line')
10+
const { apostrophe, quotes, newlines, tabs } = JSON.parse(
11+
jsonLine.replace('Results JSON:', '').trim(),
12+
)
13+
514
await test('should print string with apostrophe', () => {
6-
const output = execSync('node index.ts', { encoding: 'utf8' })
715
assert.ok(
8-
output.includes("It's working!"),
16+
apostrophe === "It's working!",
917
'🚨 Output should include "It\'s working!" - make sure you escaped the apostrophe',
1018
)
1119
})
1220

1321
await test('should print string with quotes', () => {
14-
const output = execSync('node index.ts', { encoding: 'utf8' })
1522
assert.ok(
16-
output.includes('She said "Hi"'),
23+
quotes === 'She said "Hi"',
1724
'🚨 Output should include: She said "Hi" - make sure you escaped the double quotes',
1825
)
1926
})
2027

2128
await test('should print Hello and World on separate lines', () => {
22-
const output = execSync('node index.ts', { encoding: 'utf8' })
2329
assert.ok(
24-
output.includes('Hello\nWorld'),
30+
newlines === 'Hello\nWorld',
2531
'🚨 Output should include "Hello" and "World" on separate lines - use \\n in a single string',
2632
)
2733
})
2834

2935
await test('should print tab-separated values', () => {
30-
const output = execSync('node index.ts', { encoding: 'utf8' })
3136
assert.ok(
32-
output.includes('\t'),
37+
tabs.includes('\t'),
3338
'🚨 Output should include tab characters - make sure you used \\t for tabs',
3439
)
3540
})

exercises/01.expressions-and-output/02.solution.escaping-strings/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
// Learn to include special characters in strings
33

44
// prettier-ignore
5-
console.log('It\'s working!')
5+
const apostrophe = 'It\'s working!'
6+
console.log(apostrophe)
67

78
// prettier-ignore
8-
console.log("She said \"Hi\"")
9+
const quotes = "She said \"Hi\""
10+
console.log(quotes)
911

1012
// prettier-ignore
11-
console.log('Hello\nWorld')
13+
const newlines = 'Hello\nWorld'
14+
console.log(newlines)
1215

1316
// prettier-ignore
14-
console.log('Name:\tAge:\tCity:')
17+
const tabs = 'Name:\tAge:\tCity:'
18+
console.log(tabs)
19+
20+
console.log(
21+
'Results JSON:',
22+
JSON.stringify({ apostrophe, quotes, newlines, tabs }),
23+
)

exercises/01.expressions-and-output/03.problem.strings/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@
77

88
// 🐨 Log a sentence by concatenating multiple strings
99
// 💰 Example: "I" + " " + "am" + " " + "learning" + " " + "to" + " " + "code"
10+
11+
// 🐨 When you're done, uncomment this and update the values:
12+
// const results = {
13+
// greeting: 'Hello TypeScript',
14+
// fullName: 'Your Name',
15+
// sentence: 'I am learning to code',
16+
// }
17+
// console.log('Results JSON:', JSON.stringify(results))

exercises/01.expressions-and-output/03.solution.strings/index.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import { execSync } from 'node:child_process'
33
import { readFileSync } from 'node:fs'
44
import { test } from 'node:test'
55

6+
const output = execSync('npm start --silent', { encoding: 'utf8' })
7+
const jsonLine = output
8+
.split('\n')
9+
.find((line) => line.startsWith('Results JSON:'))
10+
assert.ok(jsonLine, '🚨 Missing "Results JSON:" output line')
11+
const { greeting } = JSON.parse(jsonLine.replace('Results JSON:', '').trim())
12+
613
await test('should print Hello TypeScript', () => {
7-
const output = execSync('node index.ts', { encoding: 'utf8' })
814
assert.ok(
9-
output.includes('Hello TypeScript'),
15+
greeting === 'Hello TypeScript',
1016
'🚨 Output should include "Hello TypeScript" - concatenate "Hello" + " " + "TypeScript"',
1117
)
1218
})
1319

1420
await test('should print at least three lines', () => {
15-
const output = execSync('node index.ts', { encoding: 'utf8' })
1621
const lines = output.trim().split('\n').filter(Boolean)
1722
assert.ok(
1823
lines.length >= 3,
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// String Expressions
22
// Learn to work with text data
33

4-
console.log('Hello' + ' ' + 'TypeScript')
4+
const greeting = 'Hello' + ' ' + 'TypeScript'
5+
const fullName = 'Kody' + ' ' + 'Koala'
6+
const sentence = 'I' + ' ' + 'am' + ' ' + 'learning' + ' ' + 'to' + ' ' + 'code'
57

6-
console.log('Kody' + ' ' + 'Koala')
7-
8-
console.log('I' + ' ' + 'am' + ' ' + 'learning' + ' ' + 'to' + ' ' + 'code')
8+
console.log(greeting)
9+
console.log(fullName)
10+
console.log(sentence)
11+
console.log('Results JSON:', JSON.stringify({ greeting, fullName, sentence }))

exercises/01.expressions-and-output/04.problem.numbers/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@
99

1010
// 🐨 Use console.log to show the result of (10 + 5) multiplied by 2
1111
// 💰 Use parentheses to control order of operations
12+
13+
// 🐨 When you're done, uncomment this and update the values:
14+
// const results = {
15+
// addResult: 42,
16+
// multiplyResult: 48,
17+
// divideResult: 25,
18+
// groupedResult: 30,
19+
// }
20+
// console.log('Results JSON:', JSON.stringify(results))

exercises/01.expressions-and-output/04.solution.numbers/index.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@ import assert from 'node:assert/strict'
22
import { execSync } from 'node:child_process'
33
import { test } from 'node:test'
44

5-
await test('should print correct arithmetic results', () => {
6-
const output = execSync('node index.ts', { encoding: 'utf8' })
5+
const output = execSync('npm start --silent', { encoding: 'utf8' })
6+
const jsonLine = output
7+
.split('\n')
8+
.find((line) => line.startsWith('Results JSON:'))
9+
assert.ok(jsonLine, '🚨 Missing "Results JSON:" output line')
10+
const { addResult, multiplyResult, divideResult, groupedResult } = JSON.parse(
11+
jsonLine.replace('Results JSON:', '').trim(),
12+
)
713

14+
await test('should print correct arithmetic results', () => {
815
assert.ok(
9-
output.includes('42'),
16+
addResult === 42,
1017
'🚨 Output should include 42 (the result of 25 + 17)',
1118
)
1219
assert.ok(
13-
output.includes('48'),
20+
multiplyResult === 48,
1421
'🚨 Output should include 48 (the result of 8 * 6)',
1522
)
1623
assert.ok(
17-
output.includes('25'),
24+
divideResult === 25,
1825
'🚨 Output should include 25 (the result of 100 / 4)',
1926
)
2027
assert.ok(
21-
output.includes('30'),
28+
groupedResult === 30,
2229
'🚨 Output should include 30 (the result of (10 + 5) * 2)',
2330
)
2431
})
2532

2633
await test('should print at least four lines', () => {
27-
const output = execSync('node index.ts', { encoding: 'utf8' })
2834
const lines = output.trim().split('\n').filter(Boolean)
2935
assert.ok(
3036
lines.length >= 4,
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// Number Expressions
22
// Learn arithmetic operations
33

4-
console.log(25 + 17)
4+
const addResult = 25 + 17
5+
const multiplyResult = 8 * 6
6+
const divideResult = 100 / 4
7+
const groupedResult = (10 + 5) * 2
58

6-
console.log(8 * 6)
7-
8-
console.log(100 / 4)
9-
10-
console.log((10 + 5) * 2)
9+
console.log(addResult)
10+
console.log(multiplyResult)
11+
console.log(divideResult)
12+
console.log(groupedResult)
13+
console.log(
14+
'Results JSON:',
15+
JSON.stringify({ addResult, multiplyResult, divideResult, groupedResult }),
16+
)

0 commit comments

Comments
 (0)