Example #1
0
// Extract get value at key in objecrt.
func (object Object) Extract(key string) optional.Value {
	value, ok := object[key]
	if !ok {
		return optional.NewValue(nil)
	}
	return optional.NewValue(value)
}
Example #2
0
func TestNewValue(t *testing.T) {
	actual := optional.NewValue(nil)
	if actual.Raw != nil {
		t.Errorf("actual must be nil\n")
	}

	testData := []interface{}{
		1,
		"1",
		1.0,
	}

	for _, expected := range testData {
		actual = optional.NewValue(expected)
		if actual.Raw != expected {
			t.Errorf("actual must be %#v\n", expected)
		}
	}

	testArray := []int{1, 2, 3}
	actual = optional.NewValue(testArray)
	actualArray, ok := actual.Raw.([]int)
	if !ok {
		t.Errorf("actual must be an array of integer\n")
	}
	if len(testArray) != len(actualArray) {
		t.Errorf("expected and actual array must have same size\n")
	}
	for index, expectedItem := range testArray {
		actualItem := actualArray[index]
		if expectedItem != actualItem {
			t.Errorf("item at %d must be %#v\n", index, expectedItem)
		}
	}

	testMap := map[string]int{"1": 1, "2": 2, "3": 3}
	actual = optional.NewValue(testMap)
	actualMap, ok := actual.Raw.(map[string]int)
	if !ok {
		t.Errorf("actual must be a map[string]int\n")
	}
	if len(actualMap) != len(testMap) {
		t.Errorf("acual and expected must have same size\n")
	}
	for key, expectedItem := range testMap {
		actualItem := actualMap[key]
		if expectedItem != actualItem {
			t.Errorf("item at %s must be %#v\n", key, expectedItem)
		}
	}
}
Example #3
0
func TestIsArray(t *testing.T) {
	testCaseList := []map[string]interface{}{
		{"data": nil, "expected": false},
		{"data": int(-1), "expected": false},
		{"data": int8(1), "expected": false},
		{"data": int16(3), "expected": false},
		{"data": int32(5), "expected": false},
		{"data": int64(7), "expected": false},
		{"data": float32(-1.5), "expected": false},
		{"data": float32(-1.4), "expected": false},
		{"data": float32(-1.0), "expected": false},
		{"data": float32(1.0), "expected": false},
		{"data": float32(1.3), "expected": false},
		{"data": float32(1.51), "expected": false},
		{"data": float64(3), "expected": false},
		{"data": float32(5.0), "expected": false},
		{"data": float64(7.0), "expected": false},
		{"data": "1", "expected": false},
		{"data": "abcdefg", "expected": false},
		{"data": "abc\ndefg", "expected": false},
		{"data": []interface{}{1, 2, 3}, "expected": true},
		{"data": []interface{}{"1", "2", "3"}, "expected": true},
		{"data": map[int]int{1: 1, 2: 2}, "expected": false},
	}
	for index, testCase := range testCaseList {
		value := optional.NewValue(testCase["data"])
		actual := value.IsArray()
		if actual != testCase["expected"] {
			t.Errorf("testCase[%d] must be %#v, actual: %#v", index, testCase["expected"], actual)
		}
	}
}
Example #4
0
func TestSeemsInteger(t *testing.T) {
	testCaseList := []map[string]interface{}{
		{"data": nil, "expected": false},
		{"data": 0, "expected": true},
		{"data": -1, "expected": true},
		{"data": 1, "expected": true},
		{"data": -99999, "expected": true},
		{"data": 99999, "expected": true},
		{"data": .0, "expected": true},
		{"data": 1.1, "expected": true},
		{"data": -20.5, "expected": true},
		{"data": int8(10), "expected": true},
		{"data": int16(10), "expected": true},
		{"data": "string", "expected": false},
		{"data": true, "expected": false},
		{"data": []int{1, 2, 3}, "expected": false},
		{"data": []string{"1"}, "expected": false},
		{"data": map[int]int{1: 1}, "expected": false},
	}
	for index, testCase := range testCaseList {
		value := optional.NewValue(testCase["data"])
		if value.SeemsInteger() != testCase["expected"] {
			t.Errorf("%d must be %#v, actual: %#v", index, testCase["expected"], value)
		}
	}
}
Example #5
0
func TestGetInteger(t *testing.T) {
	testData := []map[string]interface{}{
		{"expected": nil, "ok": false},
		{"expected": int(-1), "ok": true, "actual": -1},
		{"expected": int8(1), "ok": true, "actual": 1},
		{"expected": int16(3), "ok": true, "actual": 3},
		{"expected": int32(5), "ok": true, "actual": 5},
		{"expected": int64(7), "ok": true, "actual": 7},
		{"expected": float32(-1.5), "ok": true, "actual": -2},
		{"expected": float32(-1.4), "ok": true, "actual": -1},
		{"expected": float32(-1.0), "ok": true, "actual": -1},
		{"expected": float32(1.0), "ok": true, "actual": 1},
		{"expected": float32(1.3), "ok": true, "actual": 1},
		{"expected": float32(1.51), "ok": true, "actual": 2},
		{"expected": float64(3), "ok": true, "actual": 3},
		{"expected": float32(5.0), "ok": true, "actual": 5},
		{"expected": float64(7.0), "ok": true, "actual": 7},
		{"expected": "1", "ok": false},
		{"expected": []int{1, 2, 3}, "ok": false},
		{"expected": map[int]int{1: 1, 2: 2}, "ok": false},
	}
	for index, expected := range testData {
		actual := optional.NewValue(expected["expected"])
		intValue, ok := actual.GetInteger()
		if ok != expected["ok"] {
			t.Errorf("item at %d must be %#v \n", index, expected["ok"])
		}
		expectedResult, ok := expected["actual"]
		if ok {
			if intValue != expectedResult {
				t.Errorf("item at %d must be %#v, actual:%#v\n", index, expectedResult, intValue)
			}
		}
	}
}
Example #6
0
func TestGetStringForece(t *testing.T) {
	testCaseList := []map[string]interface{}{
		{"data": nil, "expected": "<nil>"},
		{"data": 0, "expected": "0"},
		{"data": -1, "expected": "-1"},
		{"data": 1, "expected": "1"},
		{"data": -99999, "expected": "-99999"},
		{"data": 99999, "expected": "99999"},
		{"data": .0, "expected": "0"},
		{"data": 1.1, "expected": "1.1"},
		{"data": -20.5, "expected": "-20.5"},
		{"data": int8(10), "expected": "10"},
		{"data": int16(10), "expected": "10"},
		{"data": "string", "expected": "string"},
		{"data": true, "expected": "true"},
		{"data": []int{1, 2, 3}, "expected": "[1 2 3]"},
		{"data": []string{"1"}, "expected": "[1]"},
		{"data": map[int]int{1: 1}, "expected": "map[1:1]"},
	}
	for index, testCase := range testCaseList {
		value := optional.NewValue(testCase["data"])
		actual := value.GetStringForce()
		if actual != testCase["expected"] {
			t.Errorf("%d must be %#v, actual: %#v", index, testCase["expected"], actual)
		}
	}

}
Example #7
0
func TestHasValue(t *testing.T) {
	actual := optional.NewValue(nil)
	if actual.HasValue() {
		t.Errorf("actual must be empty\n")
	}
	testData := []interface{}{
		1,
		"1",
		1.0,
		[]string{},
		[]string{""},
		[]string{"1", "2"},
		map[string]int{},
		map[string]int{"1": 1},
	}

	for index, expected := range testData {
		actual := optional.NewValue(expected)
		if !actual.HasValue() {
			t.Errorf("actual at %d must have value.", index)
		}
	}
}
Example #8
0
func TestGetArray(t *testing.T) {
	testData := []map[string]interface{}{
		{"expected": nil, "ok": false},
		{"expected": int(-1), "ok": false},
		{"expected": int8(1), "ok": false},
		{"expected": int16(3), "ok": false},
		{"expected": int32(5), "ok": false},
		{"expected": int64(7), "ok": false},
		{"expected": float32(-1.5), "ok": false},
		{"expected": float32(-1.4), "ok": false},
		{"expected": float32(-1.0), "ok": false},
		{"expected": float32(1.0), "ok": false},
		{"expected": float32(1.3), "ok": false},
		{"expected": float32(1.51), "ok": false},
		{"expected": float64(3), "ok": false},
		{"expected": float32(5.0), "ok": false},
		{"expected": float64(7.0), "ok": false},
		{"expected": "1", "ok": false},
		{"expected": "abcdefg", "ok": false},
		{"expected": "abc\ndefg", "ok": false},
		{"expected": []interface{}{1, 2, 3}, "ok": true},
		{"expected": []interface{}{"1", "2", "3"}, "ok": true, "actual": []interface{}{"1", "2", "3"}},
		{"expected": map[int]int{1: 1, 2: 2}, "ok": false},
	}
	for index, expected := range testData {
		actual := optional.NewValue(expected["expected"])
		arrayValue, ok := actual.GetArray()
		if ok != expected["ok"] {
			t.Errorf("item at %d must be %#v \n", index, expected["ok"])
		}
		expectedResult, ok := expected["actual"]
		if ok {
			for index, expectedItem := range expectedResult.([]interface{}) {
				actualItem := arrayValue[index]
				if actualItem != expectedItem {
					t.Errorf("item at %d must be %#v, actual:%#v\n", index, expectedItem, actualItem)
				}
			}
		}
	}
}
Example #9
0
func TestIsBoolean(t *testing.T) {
	testCaseList := []map[string]interface{}{
		{"data": 0, "expected": false},
		{"data": -1, "expected": false},
		{"data": 1, "expected": false},
		{"data": -99999, "expected": false},
		{"data": 99999, "expected": false},
		{"data": "string", "expected": false},
		{"data": true, "expected": true},
		{"data": false, "expected": true},
		{"data": []int{1, 2, 3}, "expected": false},
		{"data": []string{"1"}, "expected": false},
		{"data": map[int]int{1: 1}, "expected": false},
	}
	for index, testCase := range testCaseList {
		value := optional.NewValue(testCase["data"])
		if value.IsBoolean() != testCase["expected"] {
			t.Errorf("%d must be %#v, actual: %#v", index, testCase["expected"], value)
		}
	}
}