diff --git a/golang-basic/latihan/latihan-1.go b/golang-basic/latihan/latihan-1.go new file mode 100644 index 0000000..2c16abf --- /dev/null +++ b/golang-basic/latihan/latihan-1.go @@ -0,0 +1,55 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +func isPalindrome(s string) bool { + for i := 0; i < len(s)/2; i++ { + if s[i] != s[len(s)-i-1] { + return false + } + } + return true +} + +func main() { + fmt.Print("Welcome to Palindrome check, please input text to check is it a palindrome or not. to exit the program please type 'end' in the input field \n") + + // for is to make a loop so the program will always running until it stop by the user + for { + // scan input from terminal + scanner := bufio.NewScanner(os.Stdin) + + fmt.Print("Enter a string: ") + + scanner.Scan() + + input := scanner.Text() + + // type end to end the program + if input == "end" { + break + } + + // empty string validation + inputNotEmpty := true + + if input == "" { + fmt.Println("Input cannot be an empty string") + inputNotEmpty = false + } + + if inputNotEmpty { + palindromeValidation := isPalindrome(input) + + if palindromeValidation { + fmt.Println("Palindrome") + } else { + fmt.Println("Not a Palindrome") + } + } + } +} diff --git a/golang-basic/latihan/latihan-2.go b/golang-basic/latihan/latihan-2.go new file mode 100644 index 0000000..979e471 --- /dev/null +++ b/golang-basic/latihan/latihan-2.go @@ -0,0 +1,63 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "regexp" + "strings" +) + +func isVowel(s string) bool { + input := strings.ToLower(s) + if(input == "a" || input == "i" || input == "u" || input == "e" || input == "o") { + return true + } + return false +} + +func isAlphabet(s string) bool { + re := regexp.MustCompile(`^[a-zA-Z]*$`) + + return re.MatchString(s) +} + +func main() { + fmt.Print("Welcome to Vowel check, please input an alphabet to check is it a vowel or not. to exit the program please type 'end' in the input field \n") + + // for is to make a loop so the program will always running until it stop by the user + for { + // scan input from terminal + scanner := bufio.NewScanner(os.Stdin) + + fmt.Print("Enter a single string: ") + + scanner.Scan() + + input := scanner.Text() + + // type end to end the program + if input == "end" { + break + } + + // empty string validation & character validation + inputValidationOk := true + + if input == "" || len(input) > 1 || !isAlphabet(input){ + fmt.Println("Input only can an alphabet and cannot be empty") + inputValidationOk = false + } + + + if inputValidationOk { + characterCheck := isVowel(input) + + if characterCheck { + fmt.Println("Vowel") + } else { + fmt.Println("Not a Vowel") + } + } + } +} \ No newline at end of file diff --git a/gomodule-unit-test/latihan/latihan-moq/go.mod b/gomodule-unit-test/latihan/latihan-moq/go.mod index 3aec68f..193af7b 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 +) 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..66dd48c --- /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= diff --git a/gomodule-unit-test/latihan/latihan-moq/main.go b/gomodule-unit-test/latihan/latihan-moq/main.go index 0cbd3d4..ec4e897 100644 --- a/gomodule-unit-test/latihan/latihan-moq/main.go +++ b/gomodule-unit-test/latihan/latihan-moq/main.go @@ -8,6 +8,7 @@ type Student struct { Class int `json:"class"` } +//go:generate moq -out main_mock_test.go . StudentRepositoryInterface type StudentRepositoryInterface interface { GetAllStudents() ([]Student, error) } 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..0e3811d --- /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 +} 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..4d06421 --- /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: "Tono", + Grade: "A", + Class: 2, + }, + { + FullName: "Andi", + Grade: "C", + Class: 3, + }, + }, nil + }, + }, + }, + wantErr: false, + want: []Student{ + {FullName: "Ihsan Arif", Grade: "B", Class: 1}, + {FullName: "Tono", Grade: "A", Class: 2}, + {FullName: "Andi", 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) + } + }) + } +}