- Distributed Computing with Go
- V.N. Nikhil Anurag
- 206字
- 2021-06-24 18:36:08
variadic_test.go
We would like to test the two valid functions, simpleVariadicToSlice and mixedVariadicToSlice, for various rules defined in the previous section. However, for the sake of brevity, we will test these:
- simpleVariadicToSlice: This is for no arguments, three arguments, and also to look at how to pass a slice to a variadic function
- mixedVariadicToSlice: This is to accept a simple argument and a variadic argument
Let's now look at the code to test these two functions:
// variadic_test.go package main import "testing" func TestSimpleVariadicToSlice(t *testing.T) { // Test for no arguments if val := simpleVariadicToSlice(); val != nil { t.Error("value should be nil", nil) } else { t.Log("simpleVariadicToSlice() -> nil") } // Test for random set of values vals := simpleVariadicToSlice(1, 2, 3) expected := []int{1, 2, 3} isErr := false for i := 0; i < 3; i++ { if vals[i] != expected[i] { isErr = true break } } if isErr { t.Error("value should be []int{1, 2, 3}", vals) } else { t.Log("simpleVariadicToSlice(1, 2, 3) -> []int{1, 2, 3}") } // Test for a slice vals = simpleVariadicToSlice(expected...) isErr = false for i := 0; i < 3; i++ { if vals[i] != expected[i] { isErr = true break } } if isErr { t.Error("value should be []int{1, 2, 3}", vals) } else { t.Log("simpleVariadicToSlice([]int{1, 2, 3}...) -> []int{1, 2, 3}") } } func TestMixedVariadicToSlice(t *testing.T) { // Test for simple argument & no variadic arguments name, numbers := mixedVariadicToSlice("Bob") if name == "Bob" && numbers == nil { t.Log("Recieved as expected: Bob, <nil slice>") } else { t.Errorf("Received unexpected values: %s, %s", name, numbers) } }
推薦閱讀
- Containerization with LXC
- Learning Windows Server Containers
- Ansible權威指南
- Linux網絡操作系統與實訓(第三版)
- 無蘋果不生活 OS X Mountain Lion隨身寶典
- 玩到極致 iPhone 4S完全攻略
- Mobile First Design with HTML5 and CSS3
- Mastering Reactive JavaScript
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- Linux服務器配置與管理
- 云原生落地:產品、架構與商業模式
- INSTANT Galleria Howto
- Cassandra 3.x High Availability(Second Edition)
- Linux操作系統案例教程(第2版)
- Microsoft Hyper-V Cluster Design