Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions golang-basic/latihan/latihan-moq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
10 changes: 10 additions & 0 deletions golang-basic/latihan/latihan-moq/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module latihan

go 1.19

require (
github.com/matryer/moq v0.3.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/tools v0.3.0 // indirect
)
8 changes: 8 additions & 0 deletions golang-basic/latihan/latihan-moq/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/matryer/moq v0.3.0 h1:4j0goF/XK3pMTc7fJB3fveuTJoQNdavRX/78vlK3Xb4=
github.com/matryer/moq v0.3.0/go.mod h1:RJ75ZZZD71hejp39j4crZLsEDszGk6iH4v4YsWFKH4s=
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.3.0 h1:SrNbZl6ECOS1qFzgTdQfWXZM9XBkiA6tkFrH9YSTPHM=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
45 changes: 45 additions & 0 deletions golang-basic/latihan/latihan-moq/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import "fmt"

//go:generate moq -out myinterface_moq_test.go . StudentRepositoryInterface
type Student struct {
FullName string `json:"fullname"`
Grade string `json:"grade"`
Class int `json:"class"`
}

type StudentRepositoryInterface interface {
GetAllStudents() ([]Student, error)
}

type StudentService struct {
StudentRepositoryInterface
}

func (s StudentService) GetStudent() ([]Student, error) {
Students, err := s.StudentRepositoryInterface.GetAllStudents()
if err != nil {
return nil, err
}

return Students, nil
}

type StudentRepository struct{}

func (r StudentRepository) GetAllStudents() ([]Student, error) {
Students := []Student{
{FullName: "Ihsan Arif", Grade: "B", Class: 1},
{FullName: "Tono", Grade: "A", Class: 2},
{FullName: "Andi", Grade: "C", Class: 3},
}
return Students, nil
}

func main() {
repository := StudentRepository{}
service := StudentService{repository}
Students, _ := service.GetStudent()
fmt.Println(Students)
}
62 changes: 62 additions & 0 deletions golang-basic/latihan/latihan-moq/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"reflect"
"testing"
)

func TestStudentService_GetStudent(t *testing.T) {
tests := []struct {
name string
s StudentService
want []Student
wantErr bool
}{
{
name: "shoulbe success get student",
s: StudentService{
StudentRepositoryInterface: &StudentRepositoryInterfaceMock{
GetAllStudentsFunc: func() ([]Student, error) {
return []Student{
{
FullName: "Irvan",
Grade: "A",
Class: 12,
},
{
FullName: "Taufik",
Grade: "C",
Class: 11,
},
}, nil
},
},
},
wantErr: false,
want: []Student{
{
FullName: "Irvan",
Grade: "A",
Class: 12,
},
{
FullName: "Taufik",
Grade: "C",
Class: 11,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.s.GetStudent()
if (err != nil) != tt.wantErr {
t.Errorf("StudentService.GetStudent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("StudentService.GetStudent() = %v, want %v", got, tt.want)
}
})
}
}
67 changes: 67 additions & 0 deletions golang-basic/latihan/latihan-moq/myinterface_moq_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions golang-basic/latihan/latihan-unitest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module quis

go 1.19
20 changes: 20 additions & 0 deletions golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package isPalindrom

import (
"strings"
)

// Needed to use Split

func IsPalindrom(str string) bool {
arrayStr := []string{}
strSplit := strings.Split(str, "")

for i := len(strSplit) - 1; i >= 0; i-- {
arrayStr = append(arrayStr, strSplit[i])
}
revesedStr := strings.Join(arrayStr, "")

return revesedStr == str

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package isPalindrom

import "testing"

func TestIsPalindrom(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "should be isPalindrom = true",
args: args{
str: "katak",
},
want: true,
},
{
name: "should be isPalindrom = false",
args: args{
str: "golang",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsPalindrom(tt.args.str); got != tt.want {
t.Errorf("IsPalindrom() = %v, want %v", got, tt.want)
}
})
}
}
24 changes: 24 additions & 0 deletions golang-basic/latihan/latihan-unitest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"quis/isPalindrom"
"quis/vocal"
)

func main() {
isPalindromTrue := isPalindrom.IsPalindrom("kodok")
isPalindromFalse := isPalindrom.IsPalindrom("batman")

isvocalTrue := vocal.Vocal("a")
isvocalFalse := vocal.Vocal("B")

fmt.Println("Soal nomor satu")
fmt.Println("kodok = ", isPalindromTrue)
fmt.Println("batman = ", isPalindromFalse)

fmt.Println("-----------------------------")
fmt.Println("Soal nomor Dua")
fmt.Println("Huruf A adalah ", isvocalTrue)
fmt.Println("Huruf B adalah ", isvocalFalse)
}
5 changes: 5 additions & 0 deletions golang-basic/latihan/latihan-unitest/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
how to run
cmd go run main.go

soal 01-irvan-taufik = folder isPalindrom
soal 02-irvan-taufik = folder vocal
18 changes: 18 additions & 0 deletions golang-basic/latihan/latihan-unitest/vocal/vocal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package vocal

import (
"strings"
)

func Vocal(letter string) string {
letterTolower := strings.ToLower(letter)
vocal := [...]string{"a", "i", "u", "e", "o"}

for i := 0; i < len(vocal); i++ {
if letterTolower == vocal[i] {
return "vocal"
}
}
return "consonant"

}
36 changes: 36 additions & 0 deletions golang-basic/latihan/latihan-unitest/vocal/vocal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package vocal

import "testing"

func TestVocal(t *testing.T) {
type args struct {
letter string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should be vocal",
args: args{
letter: "A",
},
want: "vocal",
},
{
name: "should be consonant",
args: args{
letter: "B",
},
want: "consonant",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Vocal(tt.args.letter); got != tt.want {
t.Errorf("Vocal() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 0 additions & 1 deletion golang-basic/latihan/test.go

This file was deleted.