Skip to content

Commit 6e12ee5

Browse files
committed
chore: improve src/learn.sh creating examples in test dir
1 parent 7a8ed5f commit 6e12ee5

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

src/learn.sh

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare -r LEARN_PROGRESS_FILE="$HOME/.bashunit_learn_progress"
1313
##
1414
function learn::init() {
1515
mkdir -p "$LEARN_TEMP_DIR"
16+
mkdir -p test
1617
}
1718

1819
##
@@ -210,7 +211,7 @@ assertions to verify behavior.
210211
211212
TASK: Create a test file that checks if two values are equal.
212213
213-
File: first_test.sh
214+
File: test/first_test.sh
214215
───────────────────────────────────────────────────────────────
215216
#!/usr/bin/env bash
216217
@@ -225,9 +226,10 @@ TIPS:
225226
assert_same "expected" "actual"
226227
• Test functions must start with "test_" prefix
227228
• Always quote your strings to avoid word splitting
229+
• Keep test files in a test/ directory for better organization
228230
EOF
229231

230-
local default_file="first_test.sh"
232+
local default_file="test/first_test.sh"
231233
echo ""
232234
printf "When ready, enter file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
233235
read -r test_file
@@ -275,7 +277,7 @@ CONCEPT: bashunit provides many assertion functions for different checks:
275277
276278
TASK: Write a test file with 3 different assertions.
277279
278-
File: assertions_test.sh
280+
File: test/assertions_test.sh
279281
───────────────────────────────────────────────────────────────
280282
#!/usr/bin/env bash
281283
@@ -300,7 +302,7 @@ TIPS:
300302
• Explore more: assert_empty, assert_true, assert_false
301303
EOF
302304

303-
local default_file="assertions_test.sh"
305+
local default_file="test/assertions_test.sh"
304306
echo ""
305307
printf "When ready, enter file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
306308
read -r test_file
@@ -355,7 +357,7 @@ CONCEPT: Tests often need preparation and cleanup. bashunit provides:
355357
356358
TASK: Create a test that uses setup and teardown to manage files.
357359
358-
File: lifecycle_test.sh
360+
File: test/lifecycle_test.sh
359361
───────────────────────────────────────────────────────────────
360362
#!/usr/bin/env bash
361363
@@ -386,7 +388,7 @@ TIPS:
386388
• Use $$ for unique temp file names to avoid conflicts
387389
EOF
388390

389-
local default_file="lifecycle_test.sh"
391+
local default_file="test/lifecycle_test.sh"
390392
echo ""
391393
printf "When ready, enter file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
392394
read -r test_file
@@ -443,7 +445,7 @@ call them in your tests.
443445
444446
TASK: Create a script with a function, then test it.
445447
446-
File: calculator.sh
448+
File: calculator.sh (source code)
447449
───────────────────────────────────────────────────────────────
448450
#!/usr/bin/env bash
449451
@@ -452,13 +454,13 @@ function add() {
452454
}
453455
───────────────────────────────────────────────────────────────
454456
455-
File: calculator_test.sh
457+
File: test/calculator_test.sh (test file)
456458
───────────────────────────────────────────────────────────────
457459
#!/usr/bin/env bash
458460
459461
function set_up() {
460-
# TODO: Source calculator.sh
461-
# Hint: source ./calculator.sh
462+
# TODO: Source calculator.sh from parent directory
463+
# Hint: source ../calculator.sh
462464
}
463465
464466
function test_add_positive_numbers() {
@@ -478,9 +480,10 @@ TIPS:
478480
• Source files in set_up() to reload them fresh for each test
479481
• Capture function output with: result=$(function_name args)
480482
• Test edge cases: positive, negative, zero, large numbers
483+
• Source files from parent directory: source ../file.sh
481484
EOF
482485

483-
local default_file="calculator_test.sh"
486+
local default_file="test/calculator_test.sh"
484487
echo ""
485488
printf "When ready, enter TEST file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
486489
read -r test_file
@@ -490,8 +493,8 @@ EOF
490493
local template='#!/usr/bin/env bash
491494
492495
function set_up() {
493-
# TODO: Source calculator.sh
494-
# Hint: source ./calculator.sh
496+
# TODO: Source calculator.sh from parent directory
497+
# Hint: source ../calculator.sh
495498
}
496499
497500
function test_add_positive_numbers() {
@@ -534,28 +537,28 @@ Run them and capture their output.
534537
535538
TASK: Create a script and test its output.
536539
537-
File: greeter.sh
540+
File: greeter.sh (source code)
538541
───────────────────────────────────────────────────────────────
539542
#!/usr/bin/env bash
540543
name=${1:-World}
541544
echo "Hello, $name!"
542545
───────────────────────────────────────────────────────────────
543546
544-
File: greeter_test.sh
547+
File: test/greeter_test.sh (test file)
545548
───────────────────────────────────────────────────────────────
546549
#!/usr/bin/env bash
547550
548551
function test_default_greeting() {
549-
# TODO: Run greeter.sh and capture output
550-
# Hint: output=$(./greeter.sh)
552+
# TODO: Run greeter.sh from parent directory and capture output
553+
# Hint: output=$(../greeter.sh)
551554
552555
# TODO: Assert output contains "Hello, World!"
553556
# Hint: assert_contains "Hello, World!" "$output"
554557
}
555558
556559
function test_custom_greeting() {
557560
# TODO: Run greeter.sh with argument "Alice"
558-
# Hint: output=$(./greeter.sh "Alice")
561+
# Hint: output=$(../greeter.sh "Alice")
559562
560563
# TODO: Assert output contains "Hello, Alice!"
561564
# Hint: assert_contains "Hello, Alice!" "$output"
@@ -567,9 +570,10 @@ TIPS:
567570
• Make scripts executable: chmod +x script.sh
568571
• Test both default behavior and with various arguments
569572
• Scripts run in subshells, so they can't modify parent environment
573+
• Run scripts from parent directory: ../script.sh
570574
EOF
571575

572-
local default_file="greeter_test.sh"
576+
local default_file="test/greeter_test.sh"
573577
echo ""
574578
printf "When ready, enter TEST file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
575579
read -r test_file
@@ -579,16 +583,16 @@ EOF
579583
local template='#!/usr/bin/env bash
580584
581585
function test_default_greeting() {
582-
# TODO: Run greeter.sh and capture output
583-
# Hint: output=$(./greeter.sh)
586+
# TODO: Run greeter.sh from parent directory and capture output
587+
# Hint: output=$(../greeter.sh)
584588
585589
# TODO: Assert output contains "Hello, World!"
586590
# Hint: assert_contains "Hello, World!" "$output"
587591
}
588592
589593
function test_custom_greeting() {
590594
# TODO: Run greeter.sh with argument "Alice"
591-
# Hint: output=$(./greeter.sh "Alice")
595+
# Hint: output=$(../greeter.sh "Alice")
592596
593597
# TODO: Assert output contains "Hello, Alice!"
594598
# Hint: assert_contains "Hello, Alice!" "$output"
@@ -616,7 +620,7 @@ control their behavior in tests.
616620
617621
TASK: Test a function that uses external commands.
618622
619-
File: system_info.sh
623+
File: system_info.sh (source code)
620624
───────────────────────────────────────────────────────────────
621625
#!/usr/bin/env bash
622626
@@ -625,12 +629,12 @@ function get_system_info() {
625629
}
626630
───────────────────────────────────────────────────────────────
627631
628-
File: system_info_test.sh
632+
File: test/system_info_test.sh (test file)
629633
───────────────────────────────────────────────────────────────
630634
#!/usr/bin/env bash
631635
632636
function set_up() {
633-
source system_info.sh
637+
source ../system_info.sh
634638
}
635639
636640
function test_system_info_on_linux() {
@@ -660,7 +664,7 @@ TIPS:
660664
• Use mocks to avoid calling expensive external commands
661665
EOF
662666

663-
local default_file="system_info_test.sh"
667+
local default_file="test/system_info_test.sh"
664668
echo ""
665669
printf "When ready, enter TEST file path %s[%s]%s: " "${_COLOR_FAINT}" "$default_file" "${_COLOR_DEFAULT}"
666670
read -r test_file
@@ -670,7 +674,7 @@ EOF
670674
local template='#!/usr/bin/env bash
671675
672676
function set_up() {
673-
source system_info.sh
677+
source ../system_info.sh
674678
}
675679
676680
function test_system_info_on_linux() {

0 commit comments

Comments
 (0)