Skip to content

Commit 4b8faed

Browse files
author
eliastor
committed
added unit 2
1 parent 2e7d80e commit 4b8faed

File tree

13 files changed

+733
-1
lines changed

13 files changed

+733
-1
lines changed

.ci/unit2/e1_main_test.go.tpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"golang.org/x/tour/tree"
9+
)
10+
11+
func TestMain(t *testing.T) {
12+
assert.False(t, Same(tree.New(1), tree.New(2)))
13+
assert.True(t, Same(tree.New(2), tree.New(2)))
14+
ch := make(chan int)
15+
nums := []int{}
16+
tree2 := []int{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
17+
go Walk(tree.New(2), ch)
18+
for num := range ch {
19+
t.Log(num)
20+
nums = append(nums, num)
21+
}
22+
sort.Ints(nums)
23+
assert.Equal(t, tree2, nums, "Tree walk function produces corrupted data for tree.New(2)")
24+
}

.ci/unit2/e2_main_test.go.tpl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCrawl(t *testing.T) {
14+
15+
}
16+
17+
func Test_main(t *testing.T) {
18+
old := os.Stdout
19+
r, w, _ := os.Pipe()
20+
os.Stdout = w
21+
outCh := make(chan string)
22+
23+
go func() {
24+
var buf bytes.Buffer
25+
io.Copy(&buf, r)
26+
outCh <- buf.String()
27+
}()
28+
29+
Crawl("https://golang.org/", 4, fetcher)
30+
w.Close()
31+
os.Stdout = old
32+
output := <-outCh
33+
entries := []string{
34+
`found: https://golang.org/ "The Go Programming Language"`,
35+
`found: https://golang.org/pkg/ "Packages"`,
36+
`found: https://golang.org/pkg/os/ "Package os"`,
37+
`not found: https://golang.org/cmd/`,
38+
`found: https://golang.org/pkg/fmt/ "Package fmt"`,
39+
}
40+
41+
for _, entry := range entries {
42+
assert.Contains(t, output, entry)
43+
output = strings.Replace(output, entry, "", 1)
44+
assert.NotContains(t, output, entry, "There must be no duplicates")
45+
}
46+
47+
}

.ci/unit2/exercise.test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
cp unit2/e$1_main_test.go.tpl ../unit2/exercises/e$1/main_test.go
4+
5+
cd ..
6+
7+
go mod init course || true
8+
9+
go get github.com/stretchr/testify/assert
10+
go get golang.org/x/tour/tree
11+
12+
CGO_ENABLED=0 go test ./unit2/exercises/e$1/...

.github/workflows/unit2.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Unit2 Test
2+
on:
3+
pull_request:
4+
types: [opened, reopened, synchronize, edited]
5+
paths: ["unit2/exercises/e**"]
6+
# branches-ignore: ["master", "main"]
7+
jobs:
8+
test:
9+
name: "Test exercises"
10+
runs-on: "ubuntu-latest"
11+
strategy:
12+
matrix:
13+
excercise: [0,1,2,3]
14+
steps:
15+
- uses: actions/setup-go@v3
16+
with:
17+
go-version: '>1.18.0'
18+
- uses: actions/checkout@v3
19+
- name: Exercise ${{ matrix.excercise }}
20+
run: ./unit2/exercise.test.sh ${{ matrix.excercise }}
21+
env:
22+
CGO_ENABLED: 0
23+
working-directory: .ci

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go.work
2+
**/__debug_bin
3+

unit1/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Unit 1: A tour of Go
2-
Please finish all material on https://go.dev/tour/ or https://go.dev/tour/list except "Generics" and "Concurrency"
2+
Please finish all materials on https://go.dev/tour/ or https://go.dev/tour/list except "Generics" and "Concurrency"
33

44
Note: if you stuck with module try to read https://go.dev/blog/using-go-modules.
55

6+
## FAQ
7+
8+
TBA
9+
610
## Quiz
711

812
#### Q1. How will you add the number 3 to the right side?

0 commit comments

Comments
 (0)