Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(p/ufmt): add ufmt.Errorf #1767

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion examples/gno.land/p/demo/ufmt/ufmt.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// (hence the name µfmt - micro fmt).
package ufmt

import "strconv"
import (
"bytes"
"io"
"strconv"
)
thehowl marked this conversation as resolved.
Show resolved Hide resolved

// Sprintf offers similar functionality to Go's fmt.Sprintf, or the sprintf
// equivalent available in many languages, including C/C++.
Expand Down Expand Up @@ -96,3 +100,35 @@ func Sprintf(format string, args ...interface{}) string {
}
return buf
}

// errMsg implements the error interface.
type errMsg struct {
msg string
}

// Error defines the requirements of the error interface.
// It functions similarly to Go's errors.New()
func (e *errMsg) Error() string {
return e.msg
}

// Errorf is a function that mirrors the functionality of fmt.Errorf.
//
// It takes a format string and arguments to create a formatted string,
// then sets this string as the 'msg' field of an errMsg struct and returns a pointer to this struct.
//
// This function operates in a similar manner to Go's fmt.Errorf,
// providing a way to create formatted error messages.
//
// The currently formatted verbs are the following:
//
// %s: places a string value directly.
// If the value implements the interface interface{ String() string },
// the String() method is called to retrieve the value.
// %d: formats an integer value using package "strconv".
// Currently supports only uint, uint64, int, int64.
// %t: formats a boolean value to "true" or "false".
// %%: outputs a literal %. Does not consume an argument.
func Errorf(format string, args ...interface{}) error {
return &errMsg{Sprintf(format, args...)}
}
53 changes: 51 additions & 2 deletions examples/gno.land/p/demo/ufmt/ufmt_test.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ufmt

import (
"fmt"
"bytes"
thehowl marked this conversation as resolved.
Show resolved Hide resolved
"testing"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func TestSprintf(t *testing.T) {
}

for _, tc := range cases {
name := fmt.Sprintf(tc.format, tc.values...)
name := Sprintf(tc.format, tc.values...)
thehowl marked this conversation as resolved.
Show resolved Hide resolved
t.Run(name, func(t *testing.T) {
got := Sprintf(tc.format, tc.values...)
if got != tc.expectedOutput {
Expand All @@ -44,3 +44,52 @@ func TestSprintf(t *testing.T) {
})
}
}

func TestErrorf(t *testing.T) {
tests := []struct {
name string
format string
args []interface{}
expected string
}{
{
name: "simple string",
format: "error: %s",
args: []interface{}{"something went wrong"},
expected: "error: something went wrong",
},
{
name: "integer value",
format: "value: %d",
args: []interface{}{42},
expected: "value: 42",
},
{
name: "boolean value",
format: "success: %t",
args: []interface{}{true},
expected: "success: true",
},
{
name: "multiple values",
format: "error %d: %s (success=%t)",
args: []interface{}{123, "failure occurred", false},
expected: "error 123: failure occurred (success=false)",
},
{
name: "literal percent",
format: "literal %%",
args: []interface{}{},
expected: "literal %",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Errorf(tt.format, tt.args...)
if err.Error() != tt.expected {
t.Errorf("Errorf(%q, %v) = %q, expected %q", tt.format, tt.args, err.Error(), tt.expected)
}
})
}
}
Loading