cmd_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. package com
  15. import (
  16. "fmt"
  17. "runtime"
  18. "strings"
  19. "testing"
  20. )
  21. func TestColorLogS(t *testing.T) {
  22. if runtime.GOOS != "windows" {
  23. // Trace + path.
  24. cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere")
  25. clsR := fmt.Sprintf(
  26. "[\033[%dmTRAC%s] Trace level test with path(\033[%dm%s%s)",
  27. Blue, EndColor, Yellow, "/path/to/somethere", EndColor)
  28. if cls != clsR {
  29. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  30. }
  31. // Error + error.
  32. cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error")
  33. clsR = fmt.Sprintf(
  34. "[\033[%dmERRO%s] Error level test with error[\033[%dm%s%s]",
  35. Red, EndColor, Red, "test error", EndColor)
  36. if cls != clsR {
  37. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  38. }
  39. // Warning + highlight.
  40. cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!")
  41. clsR = fmt.Sprintf(
  42. "[\033[%dmWARN%s] Warnning level test with highlight \033[%dm%s%s",
  43. Magenta, EndColor, Gray, "special offer!", EndColor)
  44. if cls != clsR {
  45. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  46. }
  47. // Success.
  48. cls = ColorLogS("[SUCC] Success level test")
  49. clsR = fmt.Sprintf(
  50. "[\033[%dmSUCC%s] Success level test",
  51. Green, EndColor)
  52. if cls != clsR {
  53. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  54. }
  55. // Default.
  56. cls = ColorLogS("[INFO] Default level test")
  57. clsR = fmt.Sprintf(
  58. "[INFO] Default level test")
  59. if cls != clsR {
  60. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  61. }
  62. } else {
  63. // Trace + path.
  64. cls := ColorLogS("[TRAC] Trace level test with path( %s )", "/path/to/somethere")
  65. clsR := fmt.Sprintf(
  66. "[TRAC] Trace level test with path(%s)",
  67. "/path/to/somethere")
  68. if cls != clsR {
  69. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  70. }
  71. // Error + error.
  72. cls = ColorLogS("[ERRO] Error level test with error[ %s ]", "test error")
  73. clsR = fmt.Sprintf(
  74. "[ERRO] Error level test with error[%s]",
  75. "test error")
  76. if cls != clsR {
  77. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  78. }
  79. // Warning + highlight.
  80. cls = ColorLogS("[WARN] Warnning level test with highlight # %s #", "special offer!")
  81. clsR = fmt.Sprintf(
  82. "[WARN] Warnning level test with highlight %s",
  83. "special offer!")
  84. if cls != clsR {
  85. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  86. }
  87. // Success.
  88. cls = ColorLogS("[SUCC] Success level test")
  89. clsR = fmt.Sprintf(
  90. "[SUCC] Success level test")
  91. if cls != clsR {
  92. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  93. }
  94. // Default.
  95. cls = ColorLogS("[INFO] Default level test")
  96. clsR = fmt.Sprintf(
  97. "[INFO] Default level test")
  98. if cls != clsR {
  99. t.Errorf("ColorLogS:\n Expect => %s\n Got => %s\n", clsR, cls)
  100. }
  101. }
  102. }
  103. func TestExecCmd(t *testing.T) {
  104. stdout, stderr, err := ExecCmd("go", "help", "get")
  105. if err != nil {
  106. t.Errorf("ExecCmd:\n Expect => %v\n Got => %v\n", nil, err)
  107. } else if len(stderr) != 0 {
  108. t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "", stderr)
  109. } else if !strings.HasPrefix(stdout, "usage: go get") {
  110. t.Errorf("ExecCmd:\n Expect => %s\n Got => %s\n", "usage: go get", stdout)
  111. }
  112. }
  113. func BenchmarkColorLogS(b *testing.B) {
  114. log := fmt.Sprintf(
  115. "[WARN] This is a tesing log that should be colored, path( %s ),"+
  116. " highlight # %s #, error [ %s ].",
  117. "path to somewhere", "highlighted content", "tesing error")
  118. for i := 0; i < b.N; i++ {
  119. ColorLogS(log)
  120. }
  121. }
  122. func BenchmarkExecCmd(b *testing.B) {
  123. for i := 0; i < b.N; i++ {
  124. ExecCmd("go", "help", "get")
  125. }
  126. }