2015-03-25 07:54:43 +01:00
|
|
|
/*
|
2015-03-25 21:23:07 +01:00
|
|
|
* Iodine, (C) 2015 Minio, Inc.
|
2015-03-25 07:54:43 +01:00
|
|
|
*
|
|
|
|
* 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 iodine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-04-09 02:13:25 +02:00
|
|
|
"fmt"
|
2015-03-25 07:54:43 +01:00
|
|
|
"testing"
|
2015-04-09 02:13:25 +02:00
|
|
|
|
|
|
|
"encoding/json"
|
2015-04-09 04:32:37 +02:00
|
|
|
"os"
|
2015-03-25 07:54:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIodine(t *testing.T) {
|
2015-03-26 23:55:06 +01:00
|
|
|
iodineError := New(errors.New("Hello"), nil)
|
|
|
|
iodineError = New(iodineError, nil)
|
|
|
|
iodineError = New(iodineError, nil)
|
|
|
|
iodineError = New(iodineError, nil)
|
2015-04-09 02:13:25 +02:00
|
|
|
|
2015-03-26 03:12:39 +01:00
|
|
|
switch typedError := iodineError.(type) {
|
2015-03-26 23:55:06 +01:00
|
|
|
case Error:
|
2015-03-26 03:12:39 +01:00
|
|
|
{
|
2015-04-09 02:13:25 +02:00
|
|
|
// Visually watch out for formating errors
|
|
|
|
fmt.Println(typedError.EmitHumanReadable())
|
|
|
|
|
2015-03-26 03:12:39 +01:00
|
|
|
if len(typedError.Stack) != 4 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2015-03-27 06:48:09 +01:00
|
|
|
_, err := json.MarshalIndent(typedError, "", " ")
|
2015-03-26 03:12:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
t.Fail()
|
|
|
|
}
|
2015-03-25 21:23:07 +01:00
|
|
|
}
|
2015-03-25 18:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestState(t *testing.T) {
|
|
|
|
SetGlobalState("hello", "world")
|
2015-03-25 21:23:07 +01:00
|
|
|
result := GetGlobalStateKey("hello")
|
|
|
|
if result != "world" {
|
|
|
|
t.Error("global state not set: hello->world")
|
2015-03-25 18:37:01 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
ClearGlobalState()
|
|
|
|
if len(GetGlobalState()) != 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
SetGlobalState("foo", "bar")
|
2015-03-26 23:55:06 +01:00
|
|
|
err := New(errors.New("a simple error"), nil)
|
2015-03-26 03:12:39 +01:00
|
|
|
switch typedError := err.(type) {
|
2015-03-26 23:55:06 +01:00
|
|
|
case Error:
|
2015-03-26 03:12:39 +01:00
|
|
|
{
|
|
|
|
if res, ok := typedError.Stack[0].Data["foo"]; ok {
|
|
|
|
if res != "bar" {
|
|
|
|
t.Error("global state not set: foo->bar")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2015-03-26 23:55:06 +01:00
|
|
|
typedError = New(typedError, map[string]string{"foo2": "bar2"}).(Error)
|
2015-03-26 03:12:39 +01:00
|
|
|
if res, ok := typedError.Stack[0].Data["foo"]; ok {
|
|
|
|
if res != "bar" {
|
|
|
|
t.Error("annotate should not modify previous data entries")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("annotate should not remove previous data entries")
|
|
|
|
}
|
|
|
|
if res, ok := typedError.Stack[1].Data["foo"]; ok {
|
|
|
|
if res != "bar" {
|
|
|
|
t.Error("global state should set value properly in annotate")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("global state should set key properly in annotate")
|
|
|
|
}
|
|
|
|
if res, ok := typedError.Stack[1].Data["foo2"]; ok {
|
|
|
|
if res != "bar2" {
|
|
|
|
// typedError = Error(typedError, nil).(WrappedError)
|
|
|
|
t.Error("foo2 -> bar should be set")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// typedError = Error(typedError, nil).(WrappedError)
|
|
|
|
t.Error("foo2 should be set")
|
|
|
|
}
|
2015-03-25 18:37:01 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 07:54:43 +01:00
|
|
|
}
|
2015-04-09 04:32:37 +02:00
|
|
|
|
|
|
|
func TestToError(t *testing.T) {
|
|
|
|
_, err := os.Stat("hello")
|
|
|
|
ierr := New(err, nil)
|
|
|
|
if ToError(ierr) != err {
|
|
|
|
t.Error("Error is not the same")
|
|
|
|
}
|
|
|
|
ierr = New(ierr, nil)
|
|
|
|
if ToError(ierr) != err {
|
|
|
|
t.Error("Stacked Error is not the same")
|
|
|
|
}
|
|
|
|
}
|