Example #1
0
func TestEditTemplate(t *testing.T) {
	b, err := form.Marshal(new(models.Task), models.TaskKind.String())
	if err != nil {
		t.Fatalf("form.Marshal(task, string(models.TaskKind)) error: %v", err)
	}

	if err := records.EditTemplate.Execute(
		ioutil.Discard,
		&records.EditData{
			Flash:    "flash message",
			FormHTML: template.HTML(string(b)),
		}); err != nil {
		t.Fatalf("records.EditTemplate.Execute error: %v", err)
	}
}
Example #2
0
func TestMarshalTask(t *testing.T) {
	t.Skip()
	db := mem.NewDB()
	user, _, err := user.Create(db, "username", "password")
	if err != nil {
		t.Fatalf("user.Create error: %v", err)
	}

	bytes, err := form.Marshal(user, "user")
	if err != nil {
		t.Fatalf("form.Marshal error: %v", err)
	}

	if got, want := string(bytes), ""; got != want {
		t.Fatalf("form.Marshal: got\n%s\nwant,\n%s", got, want)
	}
}
Example #3
0
func TestRecordsEditTemplate(t *testing.T) {
	e := &models.Event{
		Name: "this is the name",
	}

	b, err := form.Marshal(e, e.Kind().String())
	if err != nil {
		t.Fatalf("form.Marshal error: %v", err)
	}

	cd := &records.EditData{
		Flash:    "this is the flash",
		FormHTML: template.HTML(string(b)),
	}

	buf := new(bytes.Buffer)
	if err := records.EditTemplate.Execute(buf, cd); err != nil {
		t.Fatalf("records.EditTemplate.Execute error: %s", err)
	}

	o := buf.String()
	t.Logf("Output:\n%s", o)

	contains := map[string]bool{
		"this is the flash": true,
		"Name":              true,
		"this is the name":  true,
		"credential":        false,
	}

	for c, want := range contains {
		if got := strings.Contains(o, c); got != want {
			t.Errorf("strings.Contains(%q): got %b, want %b", c, got, want)
		}

	}
}
Example #4
0
// --- TestMarshal {{{
func TestMarshal(t *testing.T) {
	cases := []struct {
		name      string
		structure interface{}
		output    string
	}{
		// Named
		// time.Time
		{
			name:      "time.Time",
			structure: time.Unix(1136214245, 0),
			output:    `<label for="time.Time">time.Time</label><input name="time.Time" type="datetime-local" value="2006-01-02T07:04:05-08:00" />`,
		},
		// Primitives
		// Byte (reflect.Kind == Uint8)
		{
			name:      "byte",
			structure: byte(2),
			output:    `<label for="byte">byte</label><input name="byte" type="number" value="2" />`,
		},
		// Bool
		{
			name:      "bool_true",
			structure: true,
			output:    `<label for="bool_true">bool_true</label><input name="bool_true" type="checkbox" checked />`,
		},
		{
			name:      "bool_false",
			structure: false,
			output:    `<label for="bool_false">bool_false</label><input name="bool_false" type="checkbox" />`,
		},
		// Int
		{
			name:      "int",
			structure: int(5),
			output:    `<label for="int">int</label><input name="int" type="number" value="5" />`,
		},
		{
			name:      "int8",
			structure: int8(5),
			output:    `<label for="int8">int8</label><input name="int8" type="number" value="5" />`,
		},
		{
			name:      "int16",
			structure: int16(5),
			output:    `<label for="int16">int16</label><input name="int16" type="number" value="5" />`,
		},
		{
			name:      "int32",
			structure: int32(5),
			output:    `<label for="int32">int32</label><input name="int32" type="number" value="5" />`,
		},
		{
			name:      "int64",
			structure: int64(5),
			output:    `<label for="int64">int64</label><input name="int64" type="number" value="5" />`,
		},
		// Uint
		{
			name:      "uint",
			structure: uint(5),
			output:    `<label for="uint">uint</label><input name="uint" type="number" value="5" />`,
		},
		{
			name:      "uint8",
			structure: uint8(5),
			output:    `<label for="uint8">uint8</label><input name="uint8" type="number" value="5" />`,
		},
		{
			name:      "uint16",
			structure: uint16(5),
			output:    `<label for="uint16">uint16</label><input name="uint16" type="number" value="5" />`,
		},
		{
			name:      "uint32",
			structure: uint32(5),
			output:    `<label for="uint32">uint32</label><input name="uint32" type="number" value="5" />`,
		},
		{
			name:      "uint64",
			structure: uint64(5),
			output:    `<label for="uint64">uint64</label><input name="uint64" type="number" value="5" />`,
		},
		// Floats
		{
			name:      "float32",
			structure: float32(123.0),
			output:    `<label for="float32">float32</label><input name="float32" type="number" value="123.000000" />`,
		},
		{
			name:      "float64",
			structure: float32(123.0),
			output:    `<label for="float64">float64</label><input name="float64" type="number" value="123.000000" />`,
		},
		// String
		{
			name:      "string",
			structure: "here is a string",
			output:    `<label for="string">string</label><input name="string" type="text" value="here is a string" />`,
		},
		// Interface
		{
			name:      "interface{}(string)",
			structure: interface{}("string"),
			output:    `<label for="interface{}(string)">interface{}(string)</label><input name="interface{}(string)" type="text" value="string" />`,
		},

		// Composites
		// Slices
		{
			name:      "[]int",
			structure: []int{1, 2, 3},
			output: strings.TrimSpace(`
<label for="[]int">[]int</label><textarea name="[]int">[
	1,
	2,
	3
]</textarea>
			`),
		},
		{
			name:      "[]string",
			structure: []string{"foo", "bar", "tod"},
			output: strings.TrimSpace(`
<label for="[]string">[]string</label><textarea name="[]string">[
	"foo",
	"bar",
	"tod"
]</textarea>
			`),
		},
		{
			name:      "[]byte",
			structure: []byte{1, 2, 3, 4, 5},
			output:    `<label for="[]byte">[]byte</label><textarea name="[]byte">"AQIDBAU="</textarea>`,
		},
		// Maps
		{
			name: "map[string]interface{}",
			structure: map[string]interface{}{
				"this": map[string]interface{}{
					"is": 1,
					"json": map[string]interface{}{
						"crazy": "stuff",
					},
				},
			},
			output: strings.TrimSpace(`
<label for="map[string]interface{}">map[string]interface{}</label><textarea name="map[string]interface{}">{
	"this": {
		"is": 1,
		"json": {
			"crazy": "stuff"
		}
	}
}</textarea>
			`),
		},

		// Structures
		{
			name: "bool_field_true",
			structure: struct {
				B bool
			}{
				B: true,
			},
			output: `<fieldset><legend>bool_field_true</legend><label for="bool_field_true/B">B</label><input name="bool_field_true/B" type="checkbox" checked /><br></fieldset>`,
		},
		{
			name: "bool_field_false",
			structure: struct {
				B bool
			}{
				B: false,
			},
			output: `<fieldset><legend>bool_field_false</legend><label for="bool_field_false/B">B</label><input name="bool_field_false/B" type="checkbox" /><br></fieldset>`,
		},
		{
			name: "integer_field",
			structure: struct {
				I int
			}{
				I: 45,
			},
			output: `<fieldset><legend>integer_field</legend><label for="integer_field/I">I</label><input name="integer_field/I" type="number" value="45" /><br></fieldset>`,
		},
		{
			name: "float_field",
			structure: struct {
				F float64
			}{
				F: 54.3,
			},
			output: `<fieldset><legend>float_field</legend><label for="float_field/F">F</label><input name="float_field/F" type="number" value="54.300000" /><br></fieldset>`,
		},
		{
			name: "string_field",
			structure: struct {
				S string
			}{
				S: "foo bar",
			},
			output: `<fieldset><legend>string_field</legend><label for="string_field/S">S</label><input name="string_field/S" type="text" value="foo bar" /><br></fieldset>`,
		},
		{
			name: "nested_structure",
			structure: struct {
				S struct {
					I int
				}
			}{
				S: struct{ I int }{
					I: 5,
				},
			},
			output: `<fieldset><legend>nested_structure</legend><fieldset><legend>S</legend><label for="nested_structure/S/I">I</label><input name="nested_structure/S/I" type="number" value="5" /><br></fieldset><br></fieldset>`,
		},
		{
			name: "ignore_unexported",
			structure: struct {
				unexp int
			}{
				unexp: 5,
			},
			output: `<fieldset><legend>ignore_unexported</legend></fieldset>`,
		},
		{
			name: "field override",
			structure: &Composite{
				N: NamedPrimitive(1),
			},
			output: `<fieldset><legend>field override</legend>barfoobar<br></fieldset>`,
		},

		// Form struct
		{
			name: "form_struct",
			structure: &form.Form{
				Action: "/action/",
				Method: "post",
			},
			output: `<form action="/action/" method="post"></form>`,
		},
		{
			name: "form_struct/with_value",
			structure: &form.Form{
				Action: "/action/",
				Method: "post",
				Value: struct {
					Foo string
					Bar int
				}{
					Foo: "foo",
					Bar: 8,
				},
				Name: "Structure",
			},
			output: `<form action="/action/" method="post" name="Structure"><fieldset><legend>Structure</legend><label for="Structure/Foo">Foo</label><input name="Structure/Foo" type="text" value="foo" /><br><label for="Structure/Bar">Bar</label><input name="Structure/Bar" type="number" value="8" /><br></fieldset></form>`,
		},

		// Nullity
		{
			name: "nil_pointer",
			structure: struct {
				S *struct {
					val string
				}
			}{
				S: nil,
			},
			output: `<fieldset><legend>nil_pointer</legend></fieldset>`,
		},
		{
			name: "nil_interface",
			structure: struct {
				Val interface{}
			}{
				Val: nil,
			},
			output: `<fieldset><legend>nil_interface</legend></fieldset>`,
		},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			bytes, err := form.Marshal(c.structure, c.name)
			if err != nil {
				t.Fatalf("form.Marshal error: %v", err)
			}

			out := string(bytes)
			if got, want := out, c.output; got != want {
				t.Errorf("output: got,\n%s\nwant,\n%s", got, want)
			}
		})
	}
}