github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/internal/sh/builtin/print_test.go (about) 1 package builtin_test 2 3 import "testing" 4 5 func TestPrint(t *testing.T) { 6 type printDesc struct { 7 script string 8 output string 9 } 10 11 tests := map[string]printDesc{ 12 "textonly": { 13 script: `print("helloworld")`, 14 output: "helloworld", 15 }, 16 "nCallsRegresion": { 17 script: `print("%s:%s", "hello", "world")`, 18 output: "hello:world", 19 }, 20 "fmtstring": { 21 script: ` 22 print("%s:%s", "hello", "world") 23 print("%s:%s", "hello", "world") 24 `, 25 output: "hello:worldhello:world", 26 }, 27 "fmtlist": { 28 script: ` 29 var list = ("1" "2" "3") 30 print("%s:%s", "list", $list) 31 `, 32 output: "list:1 2 3", 33 }, 34 "funconly": { 35 script: ` 36 fn func() {} 37 print($func) 38 `, 39 output: "<fn func>", 40 }, 41 "funcfmt": { 42 script: ` 43 fn func() {} 44 print("calling:%s", $func) 45 `, 46 output: "calling:<fn func>", 47 }, 48 "listonly": { 49 script: ` 50 var list = ("1" "2" "3") 51 print($list) 52 `, 53 output: "1 2 3", 54 }, 55 "listoflists": { 56 script: ` 57 var list = (("1" "2" "3") ("4" "5" "6")) 58 print("%s:%s", "listoflists", $list) 59 `, 60 output: "listoflists:1 2 3 4 5 6", 61 }, 62 "listasfmt": { 63 script: ` 64 var list = ("%s" "%s") 65 print($list, "1", "2") 66 `, 67 output: "1 2", 68 }, 69 "invalidFmt": { 70 script: `print("%d%s", "invalid")`, 71 output: "%!d(string=invalid)%!s(MISSING)", 72 }, 73 } 74 75 for name, desc := range tests { 76 t.Run(name, func(t *testing.T) { 77 output := execSuccess(t, desc.script) 78 if output != desc.output { 79 t.Fatalf("got %q expected %q", output, desc.output) 80 } 81 }) 82 } 83 } 84 85 func TestPrintErrors(t *testing.T) { 86 type printDesc struct { 87 script string 88 } 89 90 tests := map[string]printDesc{ 91 "noParams": { 92 script: `print()`, 93 }, 94 } 95 96 for name, desc := range tests { 97 t.Run(name, func(t *testing.T) { 98 execFailure(t, desc.script) 99 }) 100 } 101 }