예제 #1
0
func TestParseHash(t *testing.T) {
	hash := `
	{				
		int:	-1024	
		float:	6.4		
		bool:	true	
		string:	string	
		text: 	"
				I'm not a great programmer,
				I'm a pretty good programmer with great habits.
				"
	} 
	`
	n, err := kson.Parse([]byte(hash))

	if err != nil {
		t.Error("hash parse error", err)
		return
	}

	expect := map[string]string{
		"int":    "-1024",
		"float":  "6.4",
		"bool":   "true",
		"string": "string",
		"text": `
				I'm not a great programmer,
				I'm a pretty good programmer with great habits.
				`,
	}

	for key, v := range expect {
		child, ok := n.Child(key)
		if !ok {
			t.Errorf("can not find key %s of hash", key)
			continue
		}
		actual, err := child.String()
		if err != nil {
			t.Errorf("get string value of child %s error", key)
			continue
		}

		ktest.Equal(t, "hash parse", v, actual)

	}

	newMap := map[string]string{}
	err = n.Value(newMap)
	if err != nil {
		t.Error("hash unmarshal error")
		return
	}
	for key, v := range newMap {
		ktest.Equal(t, "hask unmarshal "+key, v, expect[key])
	}

}
예제 #2
0
func TestParseList(t *testing.T) {
	list := `
	[
		line one
		"[line two]"
		"

		line three

		"
	]
	`

	n, err := kson.Parse([]byte(list))
	if err != nil {
		t.Error("list parse error", err)
		return
	}

	expect := []string{
		"line one",
		"[line two]",
		`

		line three

		`,
	}

	for i, v := range expect {
		actual := string(n.List[i].Literal)
		ktest.Equal(t, "list parse", v, actual)
	}

	newList := []string{}
	err = n.Value(&newList)
	if err != nil {
		t.Error("list unmarshal error", err)
		return
	}
	for i, v := range expect {
		actual := newList[i]
		ktest.Equal(t, "list unmarshal", v, actual)
	}
}
예제 #3
0
func TestUnmarshalStruct(t *testing.T) {
	data := `	
		 {
			T1_bool:true
			T1_int:10			
			T1_map:{
				a:{
					A_array_T3:[
						{
							T3_int:2031
							T3_string:T3_string_2031
						}		
						{
							T3_int:2032
							T3_string:T3_string_2032
						}		
					]			
					T2_array_uint8:[
						2001
						2002
						2003
					]
					T2_float:2000
				}
		
				b:{
					A_array_T3:[
						{
							T3_int:2131
							T3_string:T3_string_2131
						}
		
						{
							T3_int:2132
							T3_string:T3_string_2132
						}
		
					]					
					T2_array_uint8:[
						2101
						2102
						2103
					]
					T2_float:2100
				}		
			}
		
			T1_t3: {
					T3_int:31
					T3_string:T3_string_31
				}
			T1_string:T1_string
		}
	`
	var v T1
	err := kson.Unmarshal([]byte(data), &v)
	if err != nil {
		t.Error("Unmarshal struct", err)
		return
	}

	ktest.Equal(t, "T1_bool", true, v.T1_bool)
	ktest.Equal(t, "T1_int", 10, v.T1_int)
	ktest.Equal(t, "T1_string", "T1_string", v.T1_string)
	ktest.Equal(t, "T1_t3.T3_int", 31, v.T1_t3.T3_int)
	ktest.Equal(t, "T1_t3.T3_string", "T3_string_31", v.T1_t3.T3_string)
	ktest.Equal(t, "T1_map_a.T2_float", float32(2000), v.T1_map["a"].T2_float)
	ktest.Equal(t, "T1_map_a.T2_array_uint8", [3]uint{2001, 2002, 2003}, v.T1_map["a"].T2_array_uint8)
	ktest.Equal(t, "T1_map_a.A_array_T3.T3_int", 2031, v.T1_map["a"].A_array_T3[0].T3_int)
	ktest.Equal(t, "T1_map_a.A_array_T3.T3_string", "T3_string_2031", v.T1_map["a"].A_array_T3[0].T3_string)

}
예제 #4
0
func TestParseConfig(t *testing.T) {
	config := defaultConfigString

	n, err := kson.Parse([]byte(config))
	t.Log(n)
	if err != nil {
		t.Error("config parse", err)
		return
	}

	ktest.Equal(t, "Log_Level", "debug", n.ChildString("Log_Level"))
	ktest.Equal(t, "Listen", 8000, n.ChildInt("Listen"))
	ktest.Equal(t, "role-user", "user", n.MustChild("Roles").List[0].ChildString("Name"))
	ktest.Equal(t, "role-user-allow", "/user", n.MustChild("Roles").List[0].MustChild("Allow").List[0].Literal)
	ktest.Equal(t, "dblog-host", "127.0.0.1", n.MustChild("Db_Log").ChildString("Host"))
	ktest.Equal(t, "env-auth", "http://auth.io", n.MustChild("Env").Hash["auth"].Literal)

	var newConfig Config
	err = kson.Unmarshal([]byte(config), &newConfig)
	if err != nil {
		t.Error("config unmarshal error", err)
		return
	}

	ktest.Equal(t, "Log_Level", "debug", newConfig.Log_Level)
	ktest.Equal(t, "Listen", 8000, newConfig.Listen)
	ktest.Equal(t, "role-user", "user", newConfig.Roles[0].Name)
	ktest.Equal(t, "role-user-allow", "/user", newConfig.Roles[0].Allow[0])
	ktest.Equal(t, "dblog-host", "127.0.0.1", newConfig.Db_Log.Host)
	ktest.Equal(t, "env-auth", "http://auth.io", newConfig.Env["auth"])
}