-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Iiqbal2000/go/chr
Feat-Go: add Chr function
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package pehape_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
PHP "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 := PHP.Chr(c.input) | ||
|
||
if c.wantOuput != got { | ||
t.Errorf("want: %q, got: %q", c.wantOuput, got) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("symbol", func(t *testing.T) { | ||
want := "🐘" | ||
|
||
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters