first commit

This commit is contained in:
CalvinLiu123
2025-11-10 12:27:02 +07:00
commit bec02173b8
7 changed files with 171 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
package validasi
import "fmt"
// IsUsiaProduktif returns true if age is between 18 and 60 inclusive.
func IsUsiaProduktif(age int) bool {
return age >= 18 && age <= 60
}
// ValidateUsia returns nil when usia produktif, otherwise an error explaining why.
func ValidateUsia(age int) error {
if age < 0 {
return fmt.Errorf("umur tidak boleh negatif")
}
if IsUsiaProduktif(age) {
return nil
}
return fmt.Errorf("umur %d bukan usia produktif (18-60)", age)
}
+38
View File
@@ -0,0 +1,38 @@
package validasi
import "testing"
func TestIsUsiaProduktif(t *testing.T) {
cases := []struct{
age int
want bool
}{
{17, false},
{18, true},
{30, true},
{60, true},
{61, false},
}
for _, c := range cases {
got := IsUsiaProduktif(c.age)
if got != c.want {
t.Fatalf("IsUsiaProduktif(%d) = %v; want %v", c.age, got, c.want)
}
}
}
func TestValidateUsia(t *testing.T) {
if err := ValidateUsia(-1); err == nil {
t.Fatalf("expected error for negative age")
}
if err := ValidateUsia(18); err != nil {
t.Fatalf("unexpected error for age 18: %v", err)
}
if err := ValidateUsia(60); err != nil {
t.Fatalf("unexpected error for age 60: %v", err)
}
if err := ValidateUsia(61); err == nil {
t.Fatalf("expected error for age 61")
}
}