forked from senjun-team/senjun-courses
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add chapters 2, 3 of golang course (without tests) * cosmetic fix of golang chapter 1 * add tests for task 1 of chapter 1
- Loading branch information
Showing
6 changed files
with
573 additions
and
24 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 @@ | ||
.vscode |
1 change: 1 addition & 0 deletions
1
golang/golang_chapter_0010/tasks/golang_chapter_0010_task_0010/wrapper_run
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 @@ | ||
#INJECT-b585472fa |
58 changes: 58 additions & 0 deletions
58
golang/golang_chapter_0010/tasks/golang_chapter_0010_task_0010/wrapper_test
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSwap(t *testing.T) { | ||
cases := []struct { | ||
firstString, secondString string | ||
}{ | ||
{"val1", "val2"}, | ||
{"val1", "val1"}, | ||
{"", ""}, | ||
} | ||
|
||
for _, c := range cases { | ||
oldFirst := c.firstString | ||
oldSecond := c.secondString | ||
|
||
swap(&c.firstString, &c.secondString) | ||
|
||
if oldFirst != c.secondString || oldSecond != c.firstString { | ||
t.Errorf("swap(&s, &s2) when s = %q and s2 = %q made s = %q and s2 = %q, wants s = %q and s2 = %q", | ||
oldFirst, oldSecond, c.firstString, c.secondString, oldSecond, oldFirst) | ||
} | ||
} | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
old := os.Stdout // keep backup of the real stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
outC := make(chan string) | ||
// copy the output in a separate goroutine so printing can't block indefinitely | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
outC <- buf.String() | ||
}() | ||
|
||
main() | ||
|
||
// back to normal state | ||
w.Close() | ||
os.Stdout = old // restoring the real stdout | ||
out := <-outC | ||
|
||
// reading our temp stdout | ||
if strings.TrimSpace(out) != "Hello, gophers!" { | ||
t.Fatalf("main() prints \"" + strings.TrimSpace(out) + "\", not \"Hello, gophers!\"") | ||
} | ||
|
||
} |
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
Oops, something went wrong.