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
21 changes: 21 additions & 0 deletions golang-basic/latihan/01-casperwein.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "fmt"

func main() {
pol1 := "madam"
pol2 := "kuda"

fmt.Println(Polindrom(pol1)) // true
fmt.Println(Polindrom(pol2)) // false
}

func Polindrom(pol string) bool {
len := len(pol)
for i := 0; i < len/2; i++ {
if pol[i] != pol[len-i-1] {
return false
}
}
return true
}
16 changes: 16 additions & 0 deletions golang-basic/latihan/02-casperwein.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

func main() {
fmt.Println(isKonsonan("a"))
fmt.Println(isKonsonan("h"))
}

func isKonsonan(h string) string {
if h == "a" || h == "i" || h == "u" || h == "e" || h == "o" {
return "vokal"
} else {
return "konsonan"
}
}
1 change: 0 additions & 1 deletion golang-basic/latihan/test.go

This file was deleted.

7 changes: 7 additions & 0 deletions gomodule-unit-test/latihan/latihan-moq/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
8 changes: 8 additions & 0 deletions gomodule-unit-test/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=
65 changes: 65 additions & 0 deletions gomodule-unit-test/latihan/latihan-moq/konsonan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"strings"
"testing"
)

func isKonsonan(h string) string {
kons := "bcdfghjklmnpqrstvwxyz"
vokal := "aiueo"
if strings.ContainsAny(h, vokal) {
return "vokal"
} else if strings.ContainsAny(h, kons) {
return "konsonan"
} else {
return "masukan huruf konsonan atau vokal"
}
}

func Test_isKonsonan(t *testing.T) {
type args struct {
h string
}
tests := []struct {
name string
args args
want string
}{
{
name: "must_vokal",
args: args{
h: "a",
},
want: string("vokal"),
},
{
name: "must_konsonan",
args: args{
h: "b",
},
want: string("konsonan"),
},
{
name: "must_vokal_or_konsonan",
args: args{
h: "1",
},
want: string("masukan huruf konsonan atau vokal"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isKonsonan(tt.args.h); got != tt.want {
t.Errorf("isKonsonan() = %v, want %v", got, tt.want)
}
})
}
}

func BenchmarkIsKonsonan(b *testing.B) {
huruf := "a"
for i := 0; i < b.N; i++ {
isKonsonan(huruf)
}
}
19 changes: 10 additions & 9 deletions gomodule-unit-test/latihan/latihan-moq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -16,15 +17,6 @@ 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) {
Expand All @@ -36,6 +28,15 @@ func (r StudentRepository) GetAllStudents() ([]Student, error) {
return Students, nil
}

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

return Students, nil
}

func main() {
repository := StudentRepository{}
service := StudentService{repository}
Expand Down
67 changes: 67 additions & 0 deletions gomodule-unit-test/latihan/latihan-moq/main_mock_test.go

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

48 changes: 48 additions & 0 deletions gomodule-unit-test/latihan/latihan-moq/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"reflect"
"testing"
)

func TestStudentService_GetStudent(t *testing.T) {
tests := []struct {
name string
s StudentService
want []Student
wantErr bool
}{
{
name: "get student success",
s: StudentService{
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) {
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)
}
})
}
}
57 changes: 57 additions & 0 deletions gomodule-unit-test/latihan/latihan-moq/polindrom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import "testing"

func Polindrom(pol string) bool {
len := len(pol)
for i := 0; i < len/2; i++ {
if pol[i] != pol[len-i-1] {
return false
}
}
return true
}

func TestPolindrom(t *testing.T) {
type args struct {
pol string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "must_be_true",
args: args{
pol: "madam",
},
want: bool(true),
},
{
name: "must_be_false",
args: args{
pol: "cicak",
},
want: bool(false),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Polindrom(tt.args.pol); got != tt.want {
t.Errorf("Polindrom() = %v, want %v", got, tt.want)
}
})
}
}

func BenchmarkPolindrom(b *testing.B) {
kata := "madam"
kata1 := "cicak"
for i := 0; i < b.N; i++ {
Polindrom(kata)
}
for i := 0; i < b.N; i++ {
Polindrom(kata1)
}
}