Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
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,13 @@ | ||
package closer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func Closer(a io.Closer) { | ||
err := a.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
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,95 @@ | ||
package closer | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
type closerSuccess struct { | ||
} | ||
|
||
func (c closerSuccess) Close() (err error) { | ||
return | ||
} | ||
|
||
type closerError struct { | ||
} | ||
|
||
func (c closerError) Close() (err error) { | ||
err = errors.New("closer error") | ||
return | ||
} | ||
|
||
func TestCloser(t *testing.T) { | ||
|
||
getStdout := func(obj io.Closer) (out []byte, err error) { | ||
rescueStdout := os.Stdout | ||
defer func() { os.Stdout = rescueStdout }() | ||
r, w, err := os.Pipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
os.Stdout = w | ||
|
||
Closer(obj) | ||
|
||
err = w.Close() | ||
if err != nil { | ||
return | ||
} | ||
out, err = ioutil.ReadAll(r) | ||
return | ||
} | ||
|
||
cs := closerSuccess{} | ||
ce := closerError{} | ||
|
||
type args struct { | ||
body io.Closer | ||
} | ||
type expected struct { | ||
err bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want expected | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
body: cs, | ||
}, | ||
want: expected{ | ||
err: false, | ||
}, | ||
}, | ||
{ | ||
name: "error", | ||
args: args{ | ||
body: ce, | ||
}, | ||
want: expected{ | ||
err: true, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out, err := getStdout(tt.args.body) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
if (len(out) > 0) != tt.want.err { | ||
fmt.Printf("out: %q\n", string(out)) | ||
t.Errorf("closer() unexpected log %q", string(out)) | ||
} | ||
}) | ||
} | ||
} |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
func closer(a io.Closer) { | ||
err := a.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func main() { | ||
file, err := os.Open("nome.txt") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer closer(file) | ||
|
||
file2, err := os.Open("nome.txt") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
defer closer(file2) | ||
// .... | ||
} |