github.com/agiledragon/gomonkey/v2@v2.11.1-0.20240427155748-d56c6823ec17/test/fake/fake.go (about)

     1  package fake
     2  
     3  import (
     4      "errors"
     5      "fmt"
     6      "os/exec"
     7      "strings"
     8  )
     9  
    10  var (
    11      ErrActual       = errors.New("actual")
    12      ErrElemExsit    = errors.New("elem already exist")
    13      ErrElemNotExsit = errors.New("elem not exist")
    14  )
    15  
    16  func Exec(cmd string, args ...string) (string, error) {
    17      cmdPath, err := exec.LookPath(cmd)
    18      if err != nil {
    19          fmt.Errorf("exec.LookPath err: %v, cmd: %s", err, cmd)
    20          return "", errors.New("any")
    21      }
    22  
    23      var output []byte
    24      output, err = exec.Command(cmdPath, args...).CombinedOutput()
    25      if err != nil {
    26          fmt.Errorf("exec.Command.CombinedOutput err: %v, cmd: %s", err, cmd)
    27          return "", errors.New("any")
    28      }
    29      fmt.Println("CMD[", cmdPath, "]ARGS[", args, "]OUT[", string(output), "]")
    30      return string(output), nil
    31  }
    32  
    33  func Belong(points string, lines []string) bool {
    34      flag := false
    35      for _, line := range lines {
    36          flag = true
    37          for _, r := range points {
    38              if !strings.ContainsRune(line, r) {
    39                  flag = false
    40                  break
    41              }
    42          }
    43          if flag {
    44              return true
    45          }
    46      }
    47      return false
    48  }
    49  
    50  
    51  type Slice []int
    52  
    53  func NewSlice() Slice {
    54      return make(Slice, 0)
    55  }
    56  
    57  func (this* Slice) Add(elem int) error {
    58      for _, v := range *this {
    59          if v == elem {
    60              fmt.Printf("Slice: Add elem: %v already exist\n", elem)
    61              return ErrElemExsit
    62          }
    63      }
    64      *this = append(*this, elem)
    65      fmt.Printf("Slice: Add elem: %v succ\n", elem)
    66      return nil
    67  }
    68  
    69  func (this* Slice) Remove(elem int) error {
    70      found := false
    71      for i, v := range *this {
    72          if v == elem {
    73              if i == len(*this) - 1 {
    74                  *this = (*this)[:i]
    75  
    76              } else {
    77                  *this = append((*this)[:i], (*this)[i+1:]...)
    78              }
    79              found = true
    80              break
    81          }
    82      }
    83      if !found {
    84          fmt.Printf("Slice: Remove elem: %v not exist\n", elem)
    85          return ErrElemNotExsit
    86      }
    87      fmt.Printf("Slice: Remove elem: %v succ\n", elem)
    88      return nil
    89  }
    90  
    91  func (this *Slice) Append(elems ...int) int {
    92      *this = append(*this, elems...)
    93      fmt.Printf("Slice: Append elem: %v succ\n", elems)
    94      return len(elems)
    95  }
    96  
    97  func ReadLeaf(url string) (string, error) {
    98      output := fmt.Sprintf("%s, %s!", "Hello", "World")
    99      return output, nil
   100  }
   101  
   102  type Etcd struct {
   103  
   104  }
   105  
   106  func (this *Etcd) Retrieve(url string) (string, error) {
   107      output := fmt.Sprintf("%s, %s!", "Hello", "Etcd")
   108      return output, nil
   109  }
   110  
   111  var Marshal = func(v interface{}) ([]byte, error) {
   112      return nil, nil
   113  }
   114  
   115  type Db interface {
   116      Retrieve(url string) (string, error)
   117  }
   118  
   119  type Mysql struct {
   120  
   121  }
   122  
   123  func (this *Mysql) Retrieve(url string) (string, error) {
   124      output := fmt.Sprintf("%s, %s!", "Hello", "Mysql")
   125      return output, nil
   126  }
   127  
   128  func NewDb(style string) Db {
   129      if style == "etcd" {
   130          return new(Etcd)
   131      } else {
   132          return new(Mysql)
   133      }
   134  }
   135  
   136  type PrivateMethodStruct struct {
   137  
   138  }
   139  
   140  func (this *PrivateMethodStruct) ok() bool {
   141      return this != nil
   142  }
   143  
   144  func (this *PrivateMethodStruct) Happy() string {
   145      if this.ok() {
   146          return "happy"
   147      }
   148      return "unhappy"
   149  }
   150  
   151  func (this PrivateMethodStruct) haveEaten() bool {
   152      return this != PrivateMethodStruct{}
   153  }
   154  
   155  func (this PrivateMethodStruct) AreYouHungry() string {
   156      if this.haveEaten() {
   157          return "I am full"
   158      }
   159  
   160      return "I am hungry"
   161  }