diff --git a/golang-basic/latihan/latihan-moq/.gitignore b/golang-basic/latihan/latihan-moq/.gitignore new file mode 100644 index 0000000..a725465 --- /dev/null +++ b/golang-basic/latihan/latihan-moq/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/golang-basic/latihan/latihan-moq/go.mod b/golang-basic/latihan/latihan-moq/go.mod new file mode 100644 index 0000000..488293b --- /dev/null +++ b/golang-basic/latihan/latihan-moq/go.mod @@ -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 +) diff --git a/golang-basic/latihan/latihan-moq/go.sum b/golang-basic/latihan/latihan-moq/go.sum new file mode 100644 index 0000000..66dd48c --- /dev/null +++ b/golang-basic/latihan/latihan-moq/go.sum @@ -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= diff --git a/golang-basic/latihan/latihan-moq/main.go b/golang-basic/latihan/latihan-moq/main.go new file mode 100644 index 0000000..c1dbfc1 --- /dev/null +++ b/golang-basic/latihan/latihan-moq/main.go @@ -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) +} diff --git a/golang-basic/latihan/latihan-moq/main_test.go b/golang-basic/latihan/latihan-moq/main_test.go new file mode 100644 index 0000000..ac270fd --- /dev/null +++ b/golang-basic/latihan/latihan-moq/main_test.go @@ -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) + } + }) + } +} diff --git a/golang-basic/latihan/latihan-moq/myinterface_moq_test.go b/golang-basic/latihan/latihan-moq/myinterface_moq_test.go new file mode 100644 index 0000000..0e3811d --- /dev/null +++ b/golang-basic/latihan/latihan-moq/myinterface_moq_test.go @@ -0,0 +1,67 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package main + +import ( + "sync" +) + +// Ensure, that StudentRepositoryInterfaceMock does implement StudentRepositoryInterface. +// If this is not the case, regenerate this file with moq. +var _ StudentRepositoryInterface = &StudentRepositoryInterfaceMock{} + +// StudentRepositoryInterfaceMock is a mock implementation of StudentRepositoryInterface. +// +// func TestSomethingThatUsesStudentRepositoryInterface(t *testing.T) { +// +// // make and configure a mocked StudentRepositoryInterface +// mockedStudentRepositoryInterface := &StudentRepositoryInterfaceMock{ +// GetAllStudentsFunc: func() ([]Student, error) { +// panic("mock out the GetAllStudents method") +// }, +// } +// +// // use mockedStudentRepositoryInterface in code that requires StudentRepositoryInterface +// // and then make assertions. +// +// } +type StudentRepositoryInterfaceMock struct { + // GetAllStudentsFunc mocks the GetAllStudents method. + GetAllStudentsFunc func() ([]Student, error) + + // calls tracks calls to the methods. + calls struct { + // GetAllStudents holds details about calls to the GetAllStudents method. + GetAllStudents []struct { + } + } + lockGetAllStudents sync.RWMutex +} + +// GetAllStudents calls GetAllStudentsFunc. +func (mock *StudentRepositoryInterfaceMock) GetAllStudents() ([]Student, error) { + if mock.GetAllStudentsFunc == nil { + panic("StudentRepositoryInterfaceMock.GetAllStudentsFunc: method is nil but StudentRepositoryInterface.GetAllStudents was just called") + } + callInfo := struct { + }{} + mock.lockGetAllStudents.Lock() + mock.calls.GetAllStudents = append(mock.calls.GetAllStudents, callInfo) + mock.lockGetAllStudents.Unlock() + return mock.GetAllStudentsFunc() +} + +// GetAllStudentsCalls gets all the calls that were made to GetAllStudents. +// Check the length with: +// +// len(mockedStudentRepositoryInterface.GetAllStudentsCalls()) +func (mock *StudentRepositoryInterfaceMock) GetAllStudentsCalls() []struct { +} { + var calls []struct { + } + mock.lockGetAllStudents.RLock() + calls = mock.calls.GetAllStudents + mock.lockGetAllStudents.RUnlock() + return calls +} diff --git a/golang-basic/latihan/latihan-unitest/go.mod b/golang-basic/latihan/latihan-unitest/go.mod new file mode 100644 index 0000000..d413206 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/go.mod @@ -0,0 +1,3 @@ +module quis + +go 1.19 diff --git a/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom.go b/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom.go new file mode 100644 index 0000000..54df304 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom.go @@ -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 + +} diff --git a/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom_test.go b/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom_test.go new file mode 100644 index 0000000..a5883f8 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/isPalindrom/isPalindrom_test.go @@ -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) + } + }) + } +} diff --git a/golang-basic/latihan/latihan-unitest/main.go b/golang-basic/latihan/latihan-unitest/main.go new file mode 100644 index 0000000..c952a5e --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/main.go @@ -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) +} diff --git a/golang-basic/latihan/latihan-unitest/readme.md b/golang-basic/latihan/latihan-unitest/readme.md new file mode 100644 index 0000000..2e23e47 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/readme.md @@ -0,0 +1,5 @@ +how to run +cmd go run main.go + +soal 01-irvan-taufik = folder isPalindrom +soal 02-irvan-taufik = folder vocal \ No newline at end of file diff --git a/golang-basic/latihan/latihan-unitest/vocal/vocal.go b/golang-basic/latihan/latihan-unitest/vocal/vocal.go new file mode 100644 index 0000000..1c6f4a4 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/vocal/vocal.go @@ -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" + +} diff --git a/golang-basic/latihan/latihan-unitest/vocal/vocal_test.go b/golang-basic/latihan/latihan-unitest/vocal/vocal_test.go new file mode 100644 index 0000000..a3a4cf0 --- /dev/null +++ b/golang-basic/latihan/latihan-unitest/vocal/vocal_test.go @@ -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) + } + }) + } +} diff --git a/golang-basic/latihan/test.go b/golang-basic/latihan/test.go deleted file mode 100644 index 06ab7d0..0000000 --- a/golang-basic/latihan/test.go +++ /dev/null @@ -1 +0,0 @@ -package main