PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/google.golang.org/appengine/internal/aetesting/fake.go

https://github.com/ironcladlou/kubernetes
Go | 88 lines | 70 code | 10 blank | 8 comment | 17 complexity | f4115840f602c605b99a013b37cdd54e MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, LGPL-3.0, Apache-2.0, JSON, BSD-2-Clause
  1. // Copyright 2011 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // Package aetesting provides utilities for testing App Engine packages.
  5. // This is not for testing user applications.
  6. package aetesting
  7. import (
  8. "fmt"
  9. "reflect"
  10. "testing"
  11. "github.com/golang/protobuf/proto"
  12. "google.golang.org/appengine"
  13. "google.golang.org/appengine/internal"
  14. )
  15. // FakeSingleContext returns a context whose Call invocations will be serviced
  16. // by f, which should be a function that has two arguments of the input and output
  17. // protocol buffer type, and one error return.
  18. func FakeSingleContext(t *testing.T, service, method string, f interface{}) appengine.Context {
  19. fv := reflect.ValueOf(f)
  20. if fv.Kind() != reflect.Func {
  21. t.Fatal("not a function")
  22. }
  23. ft := fv.Type()
  24. if ft.NumIn() != 2 || ft.NumOut() != 1 {
  25. t.Fatalf("f has %d in and %d out, want 2 in and 1 out", ft.NumIn(), ft.NumOut())
  26. }
  27. for i := 0; i < 2; i++ {
  28. at := ft.In(i)
  29. if !at.Implements(protoMessageType) {
  30. t.Fatalf("arg %d does not implement proto.Message", i)
  31. }
  32. }
  33. if ft.Out(0) != errorType {
  34. t.Fatalf("f's return is %v, want error", ft.Out(0))
  35. }
  36. return &single{
  37. t: t,
  38. service: service,
  39. method: method,
  40. f: fv,
  41. }
  42. }
  43. var (
  44. protoMessageType = reflect.TypeOf((*proto.Message)(nil)).Elem()
  45. errorType = reflect.TypeOf((*error)(nil)).Elem()
  46. )
  47. type single struct {
  48. t *testing.T
  49. service, method string
  50. f reflect.Value
  51. }
  52. func (s *single) logf(level, format string, args ...interface{}) {
  53. s.t.Logf(level+": "+format, args...)
  54. }
  55. func (s *single) Debugf(format string, args ...interface{}) { s.logf("DEBUG", format, args...) }
  56. func (s *single) Infof(format string, args ...interface{}) { s.logf("INFO", format, args...) }
  57. func (s *single) Warningf(format string, args ...interface{}) { s.logf("WARNING", format, args...) }
  58. func (s *single) Errorf(format string, args ...interface{}) { s.logf("ERROR", format, args...) }
  59. func (s *single) Criticalf(format string, args ...interface{}) { s.logf("CRITICAL", format, args...) }
  60. func (*single) FullyQualifiedAppID() string { return "dev~fake-app" }
  61. func (*single) Request() interface{} { return nil }
  62. func (s *single) Call(service, method string, in, out proto.Message, opts *internal.CallOptions) error {
  63. if service == "__go__" {
  64. return fmt.Errorf("Unknown API call /%s.%s", service, method)
  65. }
  66. if service != s.service || method != s.method {
  67. s.t.Fatalf("Unexpected call to /%s.%s", service, method)
  68. }
  69. ins := []reflect.Value{
  70. reflect.ValueOf(in),
  71. reflect.ValueOf(out),
  72. }
  73. outs := s.f.Call(ins)
  74. if outs[0].IsNil() {
  75. return nil
  76. }
  77. return outs[0].Interface().(error)
  78. }