From e303f4c5877160b919364cd231afe089460aab81 Mon Sep 17 00:00:00 2001 From: muhamadisro27 Date: Sun, 8 Jan 2023 21:07:26 +0800 Subject: [PATCH 1/3] feat: push tugas latihan 1 --- golang-basic/latihan/go.mod | 3 +++ golang-basic/latihan/main.go | 17 +++++++++++++++++ .../latihan/tasks/01_muhamad_isro_sabanur.go | 14 ++++++++++++++ .../latihan/tasks/02_muhamad_isro_sabanur.go | 14 ++++++++++++++ golang-basic/latihan/test.go | 1 - 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 golang-basic/latihan/go.mod create mode 100644 golang-basic/latihan/main.go create mode 100644 golang-basic/latihan/tasks/01_muhamad_isro_sabanur.go create mode 100644 golang-basic/latihan/tasks/02_muhamad_isro_sabanur.go delete mode 100644 golang-basic/latihan/test.go diff --git a/golang-basic/latihan/go.mod b/golang-basic/latihan/go.mod new file mode 100644 index 0000000..ba4c49a --- /dev/null +++ b/golang-basic/latihan/go.mod @@ -0,0 +1,3 @@ +module github.com/santekno/tutorial-go + +go 1.17 diff --git a/golang-basic/latihan/main.go b/golang-basic/latihan/main.go new file mode 100644 index 0000000..04716a4 --- /dev/null +++ b/golang-basic/latihan/main.go @@ -0,0 +1,17 @@ +package main + +import( + "github.com/santekno/tutorial-go/tasks" +) + +func main() { + // isPalindrome := tasks.Palindrome("isro"); + + // if isPalindrome { + // fmt.Println(true) + // } else { + // fmt.Println(false) + // } + + tasks.IsVocalOrConsonant("c"); +} \ No newline at end of file diff --git a/golang-basic/latihan/tasks/01_muhamad_isro_sabanur.go b/golang-basic/latihan/tasks/01_muhamad_isro_sabanur.go new file mode 100644 index 0000000..bf50d38 --- /dev/null +++ b/golang-basic/latihan/tasks/01_muhamad_isro_sabanur.go @@ -0,0 +1,14 @@ +package tasks + +func Palindrome(name string) bool { + + for i :=0; i < len(name)/2; i ++ { + if name[i] != name[len(name) - i - 1] { + return false + } + + } + + return true + +} \ No newline at end of file diff --git a/golang-basic/latihan/tasks/02_muhamad_isro_sabanur.go b/golang-basic/latihan/tasks/02_muhamad_isro_sabanur.go new file mode 100644 index 0000000..10e0896 --- /dev/null +++ b/golang-basic/latihan/tasks/02_muhamad_isro_sabanur.go @@ -0,0 +1,14 @@ +package tasks + +import( + "fmt" +) + +func IsVocalOrConsonant(str string) { + + if str == "a" || str == "e" || str == "i" || str == "o" || str == "u" { + fmt.Println("Vokal") + } else { + fmt.Println("Konsonan") + } +} \ No newline at end of file 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 From 857c1c1c0d68bc9487fa8e248c3cc8b5d4471daa Mon Sep 17 00:00:00 2001 From: muhamadisro27 Date: Mon, 16 Jan 2023 21:12:45 +0800 Subject: [PATCH 2/3] feat: commit tugas unit test palindrome & vocal --- .../palindrome/01_muhamad_isro_sabanur.go | 14 ++ .../01_muhamad_isro_sabanur_test.go | 43 +++++ .../test/vokal/02_muhamad_isro_sabanur.go | 38 +++++ .../vokal/02_muhamad_isro_sabanur_test.go | 152 ++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur.go create mode 100644 gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur_test.go create mode 100644 gomodule-unit-test/latihan/test/vokal/02_muhamad_isro_sabanur.go create mode 100644 gomodule-unit-test/latihan/test/vokal/02_muhamad_isro_sabanur_test.go diff --git a/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur.go b/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur.go new file mode 100644 index 0000000..ad89743 --- /dev/null +++ b/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur.go @@ -0,0 +1,14 @@ +package main + +func Palindrome(name string) bool { + + for i :=0; i < len(name)/2; i ++ { + if name[i] != name[len(name) - i - 1] { + return false + } + + } + + return true + +} \ No newline at end of file diff --git a/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur_test.go b/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur_test.go new file mode 100644 index 0000000..034e704 --- /dev/null +++ b/gomodule-unit-test/latihan/test/palindrome/01_muhamad_isro_sabanur_test.go @@ -0,0 +1,43 @@ +package test + +import "testing" + + +func Test_palindrome(t *testing.T) { + type args struct { + input string + } + tests := []struct { + name string + args args + want bool + } { + { + name : "palindrome ?", + args : args{ + input : "madam" + }, + want : true + }, + { + name : "palindrome ?", + args : args{ + input : "abab" + }, + want : false + }, + } + for _,tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if gotIsPalindrome := isPalindrome(tt.args.kata); gotIsPalindrome != tt.want { + t.Errorf("isPalindrome() = %v, want %v", gotIsPalindrome, tt.want) + } + }) + } +} + +func BenchmarkIsPalindrome(b *testing.B) { + for i := 0; i Date: Mon, 16 Jan 2023 21:13:11 +0800 Subject: [PATCH 3/3] feat: commit tugas mock --- gomodule-unit-test/latihan/latihan-moq/go.mod | 7 ++ gomodule-unit-test/latihan/latihan-moq/go.sum | 8 +++ .../latihan/latihan-moq/main_mock_test.go | 67 +++++++++++++++++++ .../latihan/latihan-moq/main_test.go | 66 ++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 gomodule-unit-test/latihan/latihan-moq/go.sum create mode 100644 gomodule-unit-test/latihan/latihan-moq/main_mock_test.go create mode 100644 gomodule-unit-test/latihan/latihan-moq/main_test.go diff --git a/gomodule-unit-test/latihan/latihan-moq/go.mod b/gomodule-unit-test/latihan/latihan-moq/go.mod index 3aec68f..c94ccbf 100644 --- a/gomodule-unit-test/latihan/latihan-moq/go.mod +++ b/gomodule-unit-test/latihan/latihan-moq/go.mod @@ -1,3 +1,10 @@ module github.com/santekno/latihanmoq go 1.17 + +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 +) \ No newline at end of file diff --git a/gomodule-unit-test/latihan/latihan-moq/go.sum b/gomodule-unit-test/latihan/latihan-moq/go.sum new file mode 100644 index 0000000..976317f --- /dev/null +++ b/gomodule-unit-test/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= \ No newline at end of file diff --git a/gomodule-unit-test/latihan/latihan-moq/main_mock_test.go b/gomodule-unit-test/latihan/latihan-moq/main_mock_test.go new file mode 100644 index 0000000..613e18d --- /dev/null +++ b/gomodule-unit-test/latihan/latihan-moq/main_mock_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 +} \ No newline at end of file diff --git a/gomodule-unit-test/latihan/latihan-moq/main_test.go b/gomodule-unit-test/latihan/latihan-moq/main_test.go new file mode 100644 index 0000000..278fc21 --- /dev/null +++ b/gomodule-unit-test/latihan/latihan-moq/main_test.go @@ -0,0 +1,66 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestStudentService_GetStudent(t *testing.T) { + type fields struct { + StudentRepositoryInterface StudentRepositoryInterface + } + tests := []struct { + name string + fields fields + want []Student + wantErr bool + }{ + { + name: "Success get user", + fields: fields{ + StudentRepositoryInterface: &StudentRepositoryInterfaceMock{ + GetAllStudentsFunc: func() ([]Student, error){ + return []Student{ + { + FullName: "Ihsan Arif", + Grade: "B", + Class: 1, + }, + { + FullName: "Isro", + Grade: "A", + Class: 2, + }, + { + FullName: "Roozy", + Grade: "C", + Class: 3, + }, + }, nil + }, + }, + }, + wantErr: false, + want: []Student{ + {FullName: "Ihsan Arif", Grade: "B", Class: 1}, + {FullName: "Isro", Grade: "A", Class: 2}, + {FullName: "Roozy", Grade: "C", Class: 3}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := StudentService{ + StudentRepositoryInterface: tt.fields.StudentRepositoryInterface, + } + got, err := 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) + } + }) + } +} \ No newline at end of file