github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/cmd_test.go (about) 1 // Copyright 2013 com authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package com 16 17 import ( 18 "fmt" 19 "runtime" 20 "strings" 21 "testing" 22 ) 23 24 func TestColorLogS(t *testing.T) { 25 if runtime.GOOS != "windows" { 26 // Trace + path. 27 cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere") 28 clsR := fmt.Sprintf( 29 "[\033[%dmTRAC%s] Trace level test with path(\033[%dm%s%s)", 30 Blue, EndColor, Yellow, "/path/to/somethere", EndColor) 31 if cls != clsR { 32 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 33 } 34 35 // Error + error. 36 cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error") 37 clsR = fmt.Sprintf( 38 "[\033[%dmERRO%s] Error level test with error[\033[%dm%s%s]", 39 Red, EndColor, Red, "test error", EndColor) 40 if cls != clsR { 41 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 42 } 43 44 // Warning + highlight. 45 cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!") 46 clsR = fmt.Sprintf( 47 "[\033[%dmWARN%s] Warnning level test with highlight \033[%dm%s%s", 48 Magenta, EndColor, Gray, "special offer!", EndColor) 49 if cls != clsR { 50 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 51 } 52 53 // Success. 54 cls = ColorLogS("[SUCC] Success level test") 55 clsR = fmt.Sprintf( 56 "[\033[%dmSUCC%s] Success level test", 57 Green, EndColor) 58 if cls != clsR { 59 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 60 } 61 62 // Default. 63 cls = ColorLogS("[INFO] Default level test") 64 clsR = fmt.Sprintf( 65 "[INFO] Default level test") 66 if cls != clsR { 67 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 68 } 69 } else { 70 // Trace + path. 71 cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere") 72 clsR := fmt.Sprintf( 73 "[TRAC] Trace level test with path(%s)", 74 "/path/to/somethere") 75 if cls != clsR { 76 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 77 } 78 79 // Error + error. 80 cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error") 81 clsR = fmt.Sprintf( 82 "[ERRO] Error level test with error[%s]", 83 "test error") 84 if cls != clsR { 85 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 86 } 87 88 // Warning + highlight. 89 cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!") 90 clsR = fmt.Sprintf( 91 "[WARN] Warnning level test with highlight %s", 92 "special offer!") 93 if cls != clsR { 94 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 95 } 96 97 // Success. 98 cls = ColorLogS("[SUCC] Success level test") 99 clsR = fmt.Sprintf( 100 "[SUCC] Success level test") 101 if cls != clsR { 102 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 103 } 104 105 // Default. 106 cls = ColorLogS("[INFO] Default level test") 107 clsR = fmt.Sprintf( 108 "[INFO] Default level test") 109 if cls != clsR { 110 t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls) 111 } 112 } 113 } 114 115 func TestExecCmd(t *testing.T) { 116 stdout, stderr, err := ExecCmd("go", "help", "get") 117 if err != nil { 118 t.Errorf("ExecCmd:\n Expect => %v\n Got => %v\n", nil, err) 119 } else if len(stderr) != 0 { 120 t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "", stderr) 121 } else if !strings.HasPrefix(stdout, "usage: go get") { 122 t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "usage: go get", stdout) 123 } 124 } 125 126 func BenchmarkColorLogS(b *testing.B) { 127 log := fmt.Sprintf( 128 "[WARN] This is a tesing log that should be colored, path( %s ),"+ 129 " highlight # %s #, error [ %s ].", 130 "path to somewhere", "highlighted content", "tesing error") 131 for i := 0; i < b.N; i++ { 132 ColorLogS(log) 133 } 134 } 135 136 func BenchmarkExecCmd(b *testing.B) { 137 for i := 0; i < b.N; i++ { 138 ExecCmd("go", "help", "get") 139 } 140 }