示例#1
0
func TestExtractInteger(t *testing.T) {
	testData := `
	{
		"intValue": 1,
		"stringValue": "testString",
		"stringArrayValue": ["testString", "testString"]
	}
	`
	actual := object.Object{}
	json.Unmarshal([]byte(testData), &actual)
	actualIntValue := actual.Extract("intValue")

	if !actualIntValue.HasValue() {
		t.Error("intValue must be exists")
	}
	result, ok := actualIntValue.GetInteger()
	if !ok {
		t.Error("intValue must be integer")
	}
	if result != 1 {
		t.Error("intValue must be 1")
	}

	unknownValue := actual.Extract("unknown")
	if unknownValue.HasValue() {
		t.Error("unknown value must not have value")
	}
}
示例#2
0
func TestExtractStringArray(t *testing.T) {
	testData := `
	{
		"intValue": 1,
		"stringValue": "testString",
		"stringArrayValue": ["testString", "testString"]
	}
	`
	actual := object.Object{}
	json.Unmarshal([]byte(testData), &actual)
	actualStringArrayValue := actual.Extract("stringArrayValue")

	if !actualStringArrayValue.HasValue() {
		t.Error("stringArrayValue must be exists")
	}
	resultArray, ok := actualStringArrayValue.GetArray()
	if !ok {
		t.Error("stringArray must be an array")
	}
	if len(resultArray) != 2 {
		t.Error("stringArray must have 2 elements")
	}
	for _, item := range resultArray {
		if item != "testString" {
			t.Error("item of stringArray must be testString")
		}
	}
	unknownValue := actual.Extract("unknown")

	if unknownValue.HasValue() {
		t.Error("unknown value must not have value")
	}
}