예제 #1
0
파일: params_test.go 프로젝트: vadv/ostent
func TestLinks(t *testing.T) {
	req, err := http.NewRequest("GET", "http://localhost/index?df=mp", nil)
	if err != nil {
		t.Fatal(err)
	}
	req.ParseForm()
	df := NewParamsENUM(nil)["df"]
	err = df.Decode(req.Form)
	if err != nil {
		t.Fatal(err)
	}
	if num := df.Number; num.Negative || num.Uint != enums.Uint(enums.MP) {
		t.Errorf("Decode failed: %+v\n", num)
	}

	if total := df.EncodeUint("df", enums.TOTAL); total.Href != "?df=total" || total.Class != "state" || total.CaretClass != "" {
		t.Fatalf("Encode failed: total: %+v", total)
	}
	if mp := df.EncodeUint("df", enums.MP); mp.Href != "?df=-mp" || mp.Class != "state current dropup" || mp.CaretClass != "caret" {
		t.Fatalf("Encode failed: mp: %+v", mp)
	}

	if true {
		req, err := http.NewRequest("GET", "http://localhost/index?df=size", nil)
		if err != nil {
			t.Fatal(err)
		}
		req.ParseForm()
		params := NewParams(TestPeriodFlag)
		err = params.ENUM["df"].Decode(req.Form)
		if err == nil || err.Error() != "" {
			t.Fatalf("Error expected (%q)", err)
		}
		if s := params.Query.Encode(); s != "df=total" {
			t.Fatalf("Expected Encode: %q", s)
		}
	}
	CheckRedirect(t, NewForm(t, "df=fs&ps=pid"), []string{"df"}, "df=-fs")
	CheckRedirect(t, NewForm(t, "df=fs&ps=pid"), []string{"df", "ps"}, "df=-fs&ps=-pid")
	CheckRedirect(t, NewForm(t, "df=fs&ps=pid"), []string{"ps"}, "ps=-pid")

	form := NewForm(t, "df=fs&ps=pid")
	CheckRedirect(t, form, []string{"ps"}, "ps=-pid")
	if err := form.Params.ENUM["df"].Decode(url.Values{"df": []string{"mp"}}); err != nil {
		t.Fatalf("Decoding errd unexpectedly: %s", err)
	}
	if s, moved := form.Params.Query.Encode(), "df=mp&ps=-pid"; s != moved {
		t.Fatalf("Redirect mismatch (%q): %q", moved, s)
	}
}
예제 #2
0
파일: params.go 프로젝트: vadv/ostent
			return n
		}
		for _, f := range fs {
			s = f(s)
		}
		return s
	}
}

// TODO Enum* now handle sorting AND tabs:
// - tabs don't need .Alphas
// - tabs don't have negatives

var EnumDecodecs = map[string]EnumDecodec{
	"df": {
		Default: Number{Uint: enums.Uint(enums.FS)},
		Alphas:  []enums.Uint{enums.Uint(enums.FS), enums.Uint(enums.MP)},
		Unew:    func() (string, Upointer) { return "df", new(enums.UintDF) },
		Text:    TextFunc(map[string]string{"FS": "Device", "MP": "Mounted"}, strings.ToLower, strings.Title),
	},
	"dft": {
		Default:      Number{Uint: enums.Uint(enums.DFBYTES)},
		Unew:         func() (string, Upointer) { return "dft", new(enums.UintDFT) },
		Text:         TextFunc(map[string]string{"DFBYTES": "Bytes"}, strings.ToLower, strings.Title),
		OnlyPositive: true,
		TitlePrefix:  "Disks",
	},
	"ift": {
		Default:      Number{Uint: enums.Uint(enums.IFBYTES)},
		Unew:         func() (string, Upointer) { return "ift", new(enums.UintIFT) },
		Text:         TextFunc(map[string]string{"IFBYTES": "Bytes"}, strings.ToLower, strings.Title),