From 8cb444fa3f7b15d657dc305ec436407f33482acd Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 3 Jan 2022 11:36:54 -0400 Subject: [PATCH] test: examples --- .dockerignore | 48 ++++++++++++++++++++++++++++++++ Dockerfile | 2 +- examples/chaining.go | 8 ++---- examples/chaining_test.go | 11 ++++++++ examples/filterMapReduce.go | 6 ++-- examples/filterMapReduce_test.go | 11 ++++++++ 6 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 .dockerignore create mode 100644 examples/chaining_test.go create mode 100644 examples/filterMapReduce_test.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ae27e71 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,48 @@ +*~ +.fuse_hidden* +.directory +.Trash-* +.nfs* +/public/ +/resources/_gen/ +hugo.exe +hugo.darwin +hugo.linux +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.test +*.out +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db +*.stackdump +[Dd]esktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msix +*.msm +*.msp +*.lnk +.DS_Store +.AppleDouble +.LSOverride +Icon +._* +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk +/docs \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d7c77b6..6973cc4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,6 @@ WORKDIR /app COPY go.* ./ RUN go mod download -COPY *.go ./ +COPY . ./ RUN go test ./... -cover \ No newline at end of file diff --git a/examples/chaining.go b/examples/chaining.go index bd60193..f4b5a9d 100644 --- a/examples/chaining.go +++ b/examples/chaining.go @@ -1,19 +1,15 @@ package examples import ( - "fmt" - "github.com/rjNemo/underscore/chain" ) -func chaining() { - sum := chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). +func chaining() int { + return chain.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). // filter even numbers from the slice Filter(func(n int) bool { return n%2 == 0 }). // square every number in the slice Map(func(n int) int { return n * n }). // reduce to the sum Reduce(func(n, acc int) int { return n + acc }, 0) - - fmt.Println(sum) } diff --git a/examples/chaining_test.go b/examples/chaining_test.go new file mode 100644 index 0000000..1dc5127 --- /dev/null +++ b/examples/chaining_test.go @@ -0,0 +1,11 @@ +package examples + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestChainingExample(t *testing.T) { + assert.Equal(t, 120, chaining()) +} diff --git a/examples/filterMapReduce.go b/examples/filterMapReduce.go index d0d1b64..f295eb4 100644 --- a/examples/filterMapReduce.go +++ b/examples/filterMapReduce.go @@ -1,12 +1,10 @@ package examples import ( - "fmt" - u "github.com/rjNemo/underscore" ) -func filterMapReduce() { +func filterMapReduce() int { numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // filter even numbers from the slice evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) @@ -15,5 +13,5 @@ func filterMapReduce() { // reduce to the sum res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) - fmt.Println(res) + return res } diff --git a/examples/filterMapReduce_test.go b/examples/filterMapReduce_test.go new file mode 100644 index 0000000..4951a8a --- /dev/null +++ b/examples/filterMapReduce_test.go @@ -0,0 +1,11 @@ +package examples + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFilterMapReduceExample(t *testing.T) { + assert.Equal(t, 120, filterMapReduce()) +}