refactor: use python instead of javascript

This commit is contained in:
Bjarke Sporring
2026-01-05 12:21:48 +01:00
parent e1b8d8418a
commit f11f5a4646
14 changed files with 550 additions and 735 deletions

View File

@@ -40,88 +40,78 @@ git commit -m "Initial commit" | Out-Null
# Commit 1: WIP user profile
$userProfile = @"
class UserProfile {
constructor(name, email) {
this.name = name;
this.email = email;
}
}
class UserProfile:
def __init__(self, name, email):
self.name = name
self.email = email
"@
Set-Content -Path "user-profile.js" -Value $userProfile
git add user-profile.js
Set-Content -Path "user_profile.py" -Value $userProfile
git add user_profile.py
git commit -m "WIP: user profile" | Out-Null
# Commit 2: Add validation (typo in message)
$userProfileWithValidation = @"
class UserProfile {
constructor(name, email) {
this.name = name;
this.email = email;
}
class UserProfile:
def __init__(self, name, email):
self.name = name
self.email = email
validate() {
if (!this.name || !this.email) {
throw new Error('Name and email are required');
}
return true;
}
}
def validate(self):
if not self.name or not self.email:
raise ValueError('Name and email are required')
return True
"@
Set-Content -Path "user-profile.js" -Value $userProfileWithValidation
git add user-profile.js
Set-Content -Path "user_profile.py" -Value $userProfileWithValidation
git add user_profile.py
git commit -m "add validaton" | Out-Null # Intentional typo
# Commit 3: Fix validation
$userProfileFixed = @"
class UserProfile {
constructor(name, email) {
this.name = name;
this.email = email;
}
class UserProfile:
def __init__(self, name, email):
self.name = name
self.email = email
validate() {
if (!this.name || !this.email) {
throw new Error('Name and email are required');
}
if (!this.email.includes('@')) {
throw new Error('Invalid email format');
}
return true;
}
}
def validate(self):
if not self.name or not self.email:
raise ValueError('Name and email are required')
if '@' not in self.email:
raise ValueError('Invalid email format')
return True
"@
Set-Content -Path "user-profile.js" -Value $userProfileFixed
git add user-profile.js
Set-Content -Path "user_profile.py" -Value $userProfileFixed
git add user_profile.py
git commit -m "fix validation bug" | Out-Null
# Commit 4: Add tests (another WIP commit)
$tests = @"
const assert = require('assert');
const UserProfile = require('./user-profile');
import unittest
from user_profile import UserProfile
describe('UserProfile', () => {
it('should validate correct user data', () => {
const user = new UserProfile('John', 'john@example.com');
assert.strictEqual(user.validate(), true);
});
class TestUserProfile(unittest.TestCase):
def test_validate_correct_user_data(self):
user = UserProfile('John', 'john@example.com')
self.assertTrue(user.validate())
it('should reject missing name', () => {
const user = new UserProfile('', 'john@example.com');
assert.throws(() => user.validate());
});
def test_reject_missing_name(self):
user = UserProfile('', 'john@example.com')
with self.assertRaises(ValueError):
user.validate()
it('should reject invalid email', () => {
const user = new UserProfile('John', 'invalid-email');
assert.throws(() => user.validate());
});
});
def test_reject_invalid_email(self):
user = UserProfile('John', 'invalid-email')
with self.assertRaises(ValueError):
user.validate()
if __name__ == '__main__':
unittest.main()
"@
Set-Content -Path "user-profile.test.js" -Value $tests
git add user-profile.test.js
Set-Content -Path "test_user_profile.py" -Value $tests
git add test_user_profile.py
git commit -m "WIP tests" | Out-Null
# Return to module directory

View File

@@ -86,70 +86,70 @@ if ($featureCommit.Length -lt 10) {
}
# Check that required files exist
if (-not (Test-Path "user-profile.js")) {
Write-Host "[FAIL] user-profile.js not found." -ForegroundColor Red
if (-not (Test-Path "user_profile.py")) {
Write-Host "[FAIL] user_profile.py not found." -ForegroundColor Red
Set-Location ..
exit 1
}
if (-not (Test-Path "user-profile.test.js")) {
Write-Host "[FAIL] user-profile.test.js not found." -ForegroundColor Red
if (-not (Test-Path "test_user_profile.py")) {
Write-Host "[FAIL] test_user_profile.py not found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check that user-profile.js contains all expected features
$userProfileContent = Get-Content "user-profile.js" -Raw
# Check that user_profile.py contains all expected features
$userProfileContent = Get-Content "user_profile.py" -Raw
# Should have the class
if ($userProfileContent -notmatch "class UserProfile") {
Write-Host "[FAIL] user-profile.js should contain UserProfile class." -ForegroundColor Red
Write-Host "[FAIL] user_profile.py should contain UserProfile class." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should have validation method
if ($userProfileContent -notmatch "validate\(\)") {
Write-Host "[FAIL] user-profile.js should contain validate() method." -ForegroundColor Red
if ($userProfileContent -notmatch "def validate\(") {
Write-Host "[FAIL] user_profile.py should contain validate() method." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should have email format validation (the final fix from commit 3)
if ($userProfileContent -notmatch "includes\('@'\)") {
Write-Host "[FAIL] user-profile.js should contain email format validation." -ForegroundColor Red
if ($userProfileContent -notmatch "'@'.*in.*email|email.*in.*'@'") {
Write-Host "[FAIL] user_profile.py should contain email format validation." -ForegroundColor Red
Write-Host "Hint: Make sure all changes from all 4 commits are included." -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check that test file has content
$testContent = Get-Content "user-profile.test.js" -Raw
$testContent = Get-Content "test_user_profile.py" -Raw
if ($testContent -notmatch "describe.*UserProfile") {
Write-Host "[FAIL] user-profile.test.js should contain UserProfile tests." -ForegroundColor Red
if ($testContent -notmatch "class.*TestUserProfile") {
Write-Host "[FAIL] test_user_profile.py should contain TestUserProfile tests." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check that we have at least 3 test cases
$testMatches = ([regex]::Matches($testContent, "it\(")).Count
$testMatches = ([regex]::Matches($testContent, "def test_")).Count
if ($testMatches -lt 3) {
Write-Host "[FAIL] user-profile.test.js should contain at least 3 test cases." -ForegroundColor Red
Write-Host "[FAIL] test_user_profile.py should contain at least 3 test cases." -ForegroundColor Red
Set-Location ..
exit 1
}
# Verify that the latest commit contains changes to both files
$filesInLastCommit = git diff-tree --no-commit-id --name-only -r HEAD 2>$null
if ($filesInLastCommit -notcontains "user-profile.js") {
Write-Host "[FAIL] The feature commit should include user-profile.js" -ForegroundColor Red
if ($filesInLastCommit -notcontains "user_profile.py") {
Write-Host "[FAIL] The feature commit should include user_profile.py" -ForegroundColor Red
Set-Location ..
exit 1
}
if ($filesInLastCommit -notcontains "user-profile.test.js") {
Write-Host "[FAIL] The feature commit should include user-profile.test.js" -ForegroundColor Red
if ($filesInLastCommit -notcontains "test_user_profile.py") {
Write-Host "[FAIL] The feature commit should include test_user_profile.py" -ForegroundColor Red
Set-Location ..
exit 1
}