Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
226 lines (203 sloc) 5.62 KB
//
// The application that does not use this middleware, Request.Session is alwasy nil.
//
// Copyright (C) 2014 Yohei Sasaki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package wcg
import (
"testing"
)
type ShallowCopyTest struct {
Foo string
Bar int
Hoge *ShallowCopyTest
}
func TestShallowCopy(t *testing.T) {
src := new(ShallowCopyTest)
src.Foo = "foo"
src.Bar = 1
src.Hoge = new(ShallowCopyTest)
src.Hoge.Foo = "foo1"
src.Hoge.Bar = 2
dst := new(ShallowCopyTest)
ShallowCopy(dst, src)
if dst.Foo != "foo" {
t.Errorf("ShallowCopy error on Foo")
}
if dst.Bar != 1 {
t.Errorf("ShallowCopy error on Bar")
}
if dst.Hoge.Foo != "foo1" {
t.Errorf("ShallowCopy error on Hoge.Foo")
}
if dst.Hoge.Bar != 2 {
t.Errorf("ShallowCopy error on Hoge.Foo")
}
// src.Hoge and dst.Hoge should be the same pointer.
// if src.Hoge.Foo is changed, dst.Hoge.Foo should be also changed.
src.Hoge.Foo = "changed"
if dst.Hoge.Foo != "changed" {
t.Errorf("Shallow copy should share Hoge pointer, but not.")
}
}
func TestShallowCopyPanic(t *testing.T) {
(func() {
src := ShallowCopyTest{}
dst := ShallowCopyTest{}
defer func() {
err := recover()
if err == nil {
t.Errorf("ShallowCopy should raise panic if non-ptr values are passed.")
}
expected := "ShallowCopy error: not a pointer object but a struct"
if err != expected {
t.Errorf("ShallowCopy error wants '%s', but got '%s'", expected, err)
}
}()
ShallowCopy(dst, src)
})()
(func() {
dst := ShallowCopyTest{}
defer func() {
err := recover()
if err == nil {
t.Errorf("ShallowCopy should raise panic if non-ptr values are passed.")
}
expected := "ShallowCopy error: type mismatch wcg.ShallowCopyTest with *testing.T"
if err != expected {
t.Errorf("ShallowCopy error wants '%s', but got '%s'", expected, err)
}
}()
ShallowCopy(dst, t)
})()
}
func TestCompiledFormatter(t *testing.T) {
varnameMap := map[string]string{
"$IP": "value1",
"$FOO": "value2",
}
repl := func(varname string, v ...interface{}) []byte {
if len(v) != 1 {
t.Errorf("passed value list should have 1 element, but got %d element(s).", 1, len(v))
}
if v[0] != "foo" {
t.Errorf("passed value should be %q, but %q", "foo", v[0])
}
val, ok := varnameMap[varname]
if !ok {
t.Errorf("Unexpected variable name (%s) was found.", varname)
}
return []byte(val)
}
c := NewCompiledFormatter(" $IP $foo", repl)
if len(c.funcs) != 2 {
t.Errorf("NewCompiledFormattershould return 2 lengths of functions, but %d", len(c.funcs))
}
str := string(c.Format("foo"))
expected := " value1 value2"
if str != expected {
t.Errorf("Format function should return %q, but %q", expected, str)
}
c = NewCompiledFormatter(" $IP $foo ", repl)
if len(c.funcs) != 3 {
t.Errorf("NewCompiledFormattershould return 3 lengths of functions, but %d", len(c.funcs))
}
str = string(c.Format("foo"))
expected = " value1 value2 "
if str != expected {
t.Errorf("Format function should return %q, but %q", expected, str)
}
// no variables definition
c = NewCompiledFormatter(" abc ", repl)
if len(c.funcs) != 1 {
t.Errorf("NewCompiledFormattershould return 1 length of functions, but %d", len(c.funcs))
}
str = string(c.Format("foo"))
expected = " abc "
if str != expected {
t.Errorf("Format function should return %q, but %q", expected, str)
}
}
func BenchmarkCompiledFormatter(b *testing.B) {
varnameMap := map[string]string{
"$IP": "value1",
"$FOO": "value2",
}
repl := func(varname string, v ...interface{}) []byte {
val, ok := varnameMap[varname]
if ok {
return []byte(val)
} else {
return []byte(varname)
}
}
b.ResetTimer()
c := NewCompiledFormatter(" $IP $foo", repl)
// prepare context value
for i := 0; i < b.N; i++ {
c.Format("foo")
}
}
func BenchmarkReplaceAllFunc(b *testing.B) {
varnameMap := map[string]string{
"$IP": "value1",
"$FOO": "value2",
}
repl := func(varname []byte) []byte {
val, ok := varnameMap[string(varname)]
if ok {
return []byte(val)
} else {
return varname
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
formatvarre.ReplaceAllFunc([]byte(" $IP $foo"), repl)
}
}
func TestParseInt(t *testing.T) {
var got, expected int
got = ParseInt("15", 10, 3, 21)
expected = 15
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
got = ParseInt("a", 10, 3, 21)
expected = 10
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
got = ParseInt("21", 10, 3, 21)
expected = 21
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
got = ParseInt("23", 10, 3, 21)
expected = 21
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
got = ParseInt("3", 10, 3, 21)
expected = 3
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
got = ParseInt("1", 10, 3, 21)
expected = 3
if got != expected {
t.Errorf("ParseInt(\"a\", 10, 21, 3) should return %d but got %d", expected, got)
}
}