From be80e821988da088f42a8379f6bd9c6d13ff2d1d Mon Sep 17 00:00:00 2001 From: Iqbal H <52209787+Iiqbal2000@users.noreply.github.com> Date: Sun, 2 Oct 2022 07:03:33 +0000 Subject: [PATCH 1/4] Feat-Go: add Chr function --- go/chr.go | 15 +++++++++++++ go/chr_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ go/readme.md | 16 +++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 go/chr.go create mode 100644 go/chr_test.go diff --git a/go/chr.go b/go/chr.go new file mode 100644 index 0000000..f695ded --- /dev/null +++ b/go/chr.go @@ -0,0 +1,15 @@ +package pehape + +import "bytes" + +// Chr is a function that returns a character from a number. +// Parameter : +// - codepoint => it can be in decimal, octal, or hex values. +// Return : +// - single-byte string +func Chr(codepoint int) string { + var result bytes.Buffer + result.WriteByte(byte(codepoint)) + + return result.String() +} diff --git a/go/chr_test.go b/go/chr_test.go new file mode 100644 index 0000000..578aadc --- /dev/null +++ b/go/chr_test.go @@ -0,0 +1,61 @@ +package pehape_test + +import ( + "bytes" + "fmt" + "testing" + + pehape "github.com/teknologi-umum/pehape/go" +) + +func TestChr(t *testing.T) { + testcases := []struct { + input int + wantOuput string + }{ + { + input: 65, + wantOuput: "A", + }, + { + input: 053, + wantOuput: "+", + }, + { + input: 0x52, + wantOuput: "R", + }, + { + input: -159, + wantOuput: "a", + }, + { + input: 833, + wantOuput: "A", + }, + } + + for i, c := range testcases { + t.Run(fmt.Sprint(i+1), func(t *testing.T) { + got := pehape.Chr(c.input) + + if c.wantOuput != got { + t.Errorf("want: %q, got: %q", c.wantOuput, got) + } + }) + } + + t.Run("symbol", func(t *testing.T) { + want := []byte("🐘") + + var got bytes.Buffer + got.WriteString(pehape.Chr(240)) + got.WriteString(pehape.Chr(159)) + got.WriteString(pehape.Chr(144)) + got.WriteString(pehape.Chr(152)) + + if !bytes.EqualFold(want, got.Bytes()) { + t.Errorf("want: %q, got: %q", want, got.Bytes()) + } + }) +} diff --git a/go/readme.md b/go/readme.md index 54c3a14..ee0a35a 100644 --- a/go/readme.md +++ b/go/readme.md @@ -76,3 +76,19 @@ fmt.Println(pehape.Ucwords(bar)) //result : "kasur rusak" ``` + * `Chr` + ```go + var AInASCII = pehape.Chr(65) + fmt.Println(AInASCII) + var charFromNumber = pehape.Chr(65) + fmt.Println(charFromNumber) + //result : "A" + var charFromOctal = pehape.Chr(053) + fmt.Println(charFromOctal) + //result: "+" + var charFromHex = pehape.Chr(0x52) + fmt.Println(charFromHex) + //result: "R" + var symbol = pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152) + fmt.Println(symbol) + ``` From 985a929d431d30d1987687840ad8d0160e7059ca Mon Sep 17 00:00:00 2001 From: Iqbal H <52209787+Iiqbal2000@users.noreply.github.com> Date: Sun, 2 Oct 2022 07:09:01 +0000 Subject: [PATCH 2/4] refactor: chr test --- go/chr_test.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/go/chr_test.go b/go/chr_test.go index 578aadc..f2df854 100644 --- a/go/chr_test.go +++ b/go/chr_test.go @@ -1,8 +1,8 @@ package pehape_test import ( - "bytes" "fmt" + "strings" "testing" pehape "github.com/teknologi-umum/pehape/go" @@ -46,16 +46,12 @@ func TestChr(t *testing.T) { } t.Run("symbol", func(t *testing.T) { - want := []byte("🐘") - - var got bytes.Buffer - got.WriteString(pehape.Chr(240)) - got.WriteString(pehape.Chr(159)) - got.WriteString(pehape.Chr(144)) - got.WriteString(pehape.Chr(152)) - - if !bytes.EqualFold(want, got.Bytes()) { - t.Errorf("want: %q, got: %q", want, got.Bytes()) + want := "🐘" + + got := pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152) + + if !strings.EqualFold(want, got) { + t.Errorf("want: %q, got: %q", want, got) } }) } From acf4f77dc6dc7e115075f09eb22f774c2a7d39fc Mon Sep 17 00:00:00 2001 From: Iqbal H <52209787+Iiqbal2000@users.noreply.github.com> Date: Mon, 3 Oct 2022 15:38:26 +0000 Subject: [PATCH 3/4] change alias of a package name --- go/chr_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go/chr_test.go b/go/chr_test.go index f2df854..2fac013 100644 --- a/go/chr_test.go +++ b/go/chr_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - pehape "github.com/teknologi-umum/pehape/go" + PHP "github.com/teknologi-umum/pehape/go" ) func TestChr(t *testing.T) { @@ -37,7 +37,7 @@ func TestChr(t *testing.T) { for i, c := range testcases { t.Run(fmt.Sprint(i+1), func(t *testing.T) { - got := pehape.Chr(c.input) + got := PHP.Chr(c.input) if c.wantOuput != got { t.Errorf("want: %q, got: %q", c.wantOuput, got) @@ -48,7 +48,7 @@ func TestChr(t *testing.T) { t.Run("symbol", func(t *testing.T) { want := "🐘" - got := pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152) + got := PHP.Chr(240) + PHP.Chr(159) + PHP.Chr(144) + PHP.Chr(152) if !strings.EqualFold(want, got) { t.Errorf("want: %q, got: %q", want, got) From 7dd2538f861146defa475727d3eaed06bacea721 Mon Sep 17 00:00:00 2001 From: Iqbal H <52209787+Iiqbal2000@users.noreply.github.com> Date: Tue, 4 Oct 2022 16:19:51 +0000 Subject: [PATCH 4/4] fix Chr usage --- go/readme.md | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/go/readme.md b/go/readme.md index ee0a35a..4335023 100644 --- a/go/readme.md +++ b/go/readme.md @@ -75,20 +75,22 @@ fmt.Println(pehape.Ucwords(bar)) fmt.Println(pehape.Strrev(bar)) //result : "kasur rusak" ``` - - * `Chr` - ```go - var AInASCII = pehape.Chr(65) - fmt.Println(AInASCII) - var charFromNumber = pehape.Chr(65) - fmt.Println(charFromNumber) - //result : "A" - var charFromOctal = pehape.Chr(053) - fmt.Println(charFromOctal) - //result: "+" - var charFromHex = pehape.Chr(0x52) - fmt.Println(charFromHex) - //result: "R" - var symbol = pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152) - fmt.Println(symbol) - ``` + + * `Chr` +```go +var charFromNumber = pehape.Chr(65) +fmt.Println(charFromNumber) +//result : "A" + +var charFromOctal = pehape.Chr(053) +fmt.Println(charFromOctal) +//result: "+" + +var charFromHex = pehape.Chr(0x52) +fmt.Println(charFromHex) +//result: "R" + +var symbol = pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152) +fmt.Println(symbol) +//result : "🐘" +```