1package mdform_test23import (4 "strings"5 "testing"67 "gno.land/p/jeronimoalbi/mdform/v0"8 "gno.land/p/nt/uassert/v0"9 "gno.land/p/nt/urequire/v0"10)1112// cur is a zero-value realm used as a placeholder when forwarding to13// uassert/urequire dispatch helpers that gained an `rlm realm` param.14// These tests pass `func()` callbacks (no crossing inside the callback),15// so rlm is ignored — a nil realm here is safe.16var cur realm1718func TestNew(cur realm, t *testing.T) {19 tests := []struct {20 name string21 attrs []string22 markdown string23 err string24 }{25 {26 name: "ok",27 attrs: []string{"exec", "FunctionName"},28 markdown: `<gno-form exec="FunctionName"></gno-form>`,29 },30 {31 name: "no attributes",32 markdown: `<gno-form></gno-form>`,33 },34 {35 name: "uneven attributes",36 attrs: []string{"exec"},37 err: "expected an even number of attribute arguments",38 },39 {40 name: "invalid attribute",41 attrs: []string{"foo", ""},42 err: "invalid attribute: foo",43 },44 }4546 for _, tc := range tests {47 t.Run(tc.name, func(t *testing.T) {48 var markdown string49 fn := func() {50 f := mdform.New(tc.attrs...)51 markdown = strings.ReplaceAll(f.String(), "\n", "")52 }5354 if tc.err != "" {55 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form to fail")56 return57 }5859 urequire.NotPanics(t, cur, fn, "expect form to be created")60 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")61 })62 }63}6465func TestFormInput(cur realm, t *testing.T) {66 tests := []struct {67 name string68 inputName string69 attrs []string70 markdown string71 err string72 }{73 {74 name: "ok",75 inputName: "test",76 attrs: []string{"value", "foo"},77 markdown: `<gno-form><gno-input name="test" value="foo" /></gno-form>`,78 },79 {80 name: "no attributes",81 inputName: "test",82 markdown: `<gno-form><gno-input name="test" /></gno-form>`,83 },84 {85 name: "empty name",86 inputName: " ",87 err: "form input name is required",88 },89 {90 name: "radio type",91 inputName: "test",92 attrs: []string{"type", "radio"},93 err: "use form.Radio() to create inputs of type radio",94 },95 {96 name: "checkbox type",97 inputName: "test",98 attrs: []string{"type", "checkbox"},99 err: "use form.Checkbox() to create inputs of type checkbox",100 },101 {102 name: "uneven attributes",103 inputName: "test",104 attrs: []string{"value"},105 err: "expected an even number of attribute arguments",106 },107 {108 name: "invalid attribute",109 inputName: "test",110 attrs: []string{"foo", ""},111 err: "invalid attribute: foo",112 },113 }114115 for _, tc := range tests {116 t.Run(tc.name, func(t *testing.T) {117 var markdown string118 fn := func() {119 f := mdform.New()120 f.Input(tc.inputName, tc.attrs...)121 markdown = strings.ReplaceAll(f.String(), "\n", "")122 }123124 if tc.err != "" {125 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form input to fail")126 return127 }128129 urequire.NotPanics(t, cur, fn, "expect form input to be created")130 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")131 })132 }133}134135func TestFormRadio(cur realm, t *testing.T) {136 tests := []struct {137 name string138 inputName string139 value string140 attrs []string141 markdown string142 err string143 }{144 {145 name: "ok",146 inputName: "test",147 value: "foo",148 attrs: []string{"readonly", "true"},149 markdown: `<gno-form><gno-input type="radio" name="test" value="foo" readonly="true" /></gno-form>`,150 },151 {152 name: "no attributes",153 inputName: "test",154 value: "foo",155 markdown: `<gno-form><gno-input type="radio" name="test" value="foo" /></gno-form>`,156 },157 {158 name: "empty name",159 inputName: " ",160 err: "form radio input name is required",161 },162 {163 name: "ignore type attribute",164 inputName: "test",165 attrs: []string{"type", "text"},166 markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,167 },168 {169 name: "ignore value attribute",170 inputName: "test",171 attrs: []string{"value", "foo"},172 markdown: `<gno-form><gno-input type="radio" name="test" value="" /></gno-form>`,173 },174 {175 name: "uneven attributes",176 inputName: "test",177 attrs: []string{"readonly"},178 err: "expected an even number of attribute arguments",179 },180 {181 name: "invalid attribute",182 inputName: "test",183 attrs: []string{"foo", ""},184 err: "invalid attribute: foo",185 },186 }187188 for _, tc := range tests {189 t.Run(tc.name, func(t *testing.T) {190 var markdown string191 fn := func() {192 f := mdform.New()193 f.Radio(tc.inputName, tc.value, tc.attrs...)194 markdown = strings.ReplaceAll(f.String(), "\n", "")195 }196197 if tc.err != "" {198 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form radio input to fail")199 return200 }201202 urequire.NotPanics(t, cur, fn, "expect form radio input to be created")203 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")204 })205 }206}207208func TestFormCheckbox(cur realm, t *testing.T) {209 tests := []struct {210 name string211 inputName string212 value string213 attrs []string214 markdown string215 err string216 }{217 {218 name: "ok",219 inputName: "test",220 value: "foo",221 attrs: []string{"readonly", "true"},222 markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" readonly="true" /></gno-form>`,223 },224 {225 name: "no attributes",226 inputName: "test",227 value: "foo",228 markdown: `<gno-form><gno-input type="checkbox" name="test" value="foo" /></gno-form>`,229 },230 {231 name: "empty name",232 inputName: " ",233 err: "form checkbox input name is required",234 },235 {236 name: "ignore type attribute",237 inputName: "test",238 attrs: []string{"type", "text"},239 markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,240 },241 {242 name: "ignore value attribute",243 inputName: "test",244 attrs: []string{"value", "foo"},245 markdown: `<gno-form><gno-input type="checkbox" name="test" value="" /></gno-form>`,246 },247 {248 name: "uneven attributes",249 inputName: "test",250 attrs: []string{"readonly"},251 err: "expected an even number of attribute arguments",252 },253 {254 name: "invalid attribute",255 inputName: "test",256 attrs: []string{"foo", ""},257 err: "invalid attribute: foo",258 },259 }260261 for _, tc := range tests {262 t.Run(tc.name, func(t *testing.T) {263 var markdown string264 fn := func() {265 f := mdform.New()266 f.Checkbox(tc.inputName, tc.value, tc.attrs...)267 markdown = strings.ReplaceAll(f.String(), "\n", "")268 }269270 if tc.err != "" {271 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form checkbox input to fail")272 return273 }274275 urequire.NotPanics(t, cur, fn, "expect form checkbox input to be created")276 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")277 })278 }279}280281func TestFormTextarea(cur realm, t *testing.T) {282 tests := []struct {283 name string284 inputName string285 attrs []string286 markdown string287 err string288 }{289 {290 name: "ok",291 inputName: "test",292 attrs: []string{"value", "foo"},293 markdown: `<gno-form><gno-textarea name="test" value="foo" /></gno-form>`,294 },295 {296 name: "no attributes",297 inputName: "test",298 markdown: `<gno-form><gno-textarea name="test" /></gno-form>`,299 },300 {301 name: "empty name",302 inputName: " ",303 err: "form textarea name is required",304 },305 {306 name: "uneven attributes",307 inputName: "test",308 attrs: []string{"value"},309 err: "expected an even number of attribute arguments",310 },311 {312 name: "invalid attribute",313 inputName: "test",314 attrs: []string{"foo", ""},315 err: "invalid attribute: foo",316 },317 }318319 for _, tc := range tests {320 t.Run(tc.name, func(t *testing.T) {321 var markdown string322 fn := func() {323 f := mdform.New()324 f.Textarea(tc.inputName, tc.attrs...)325 markdown = strings.ReplaceAll(f.String(), "\n", "")326 }327328 if tc.err != "" {329 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")330 return331 }332333 urequire.NotPanics(t, cur, fn, "expect form textarea to be created")334 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")335 })336 }337}338339func TestFormSelect(cur realm, t *testing.T) {340 tests := []struct {341 name string342 inputName string343 value string344 attrs []string345 markdown string346 err string347 }{348 {349 name: "ok",350 inputName: "test",351 value: "foo",352 attrs: []string{"readonly", "true"},353 markdown: `<gno-form><gno-select name="test" value="foo" readonly="true" /></gno-form>`,354 },355 {356 name: "no attributes",357 inputName: "test",358 value: "foo",359 markdown: `<gno-form><gno-select name="test" value="foo" /></gno-form>`,360 },361 {362 name: "empty name",363 inputName: " ",364 err: "form select name is required",365 },366 {367 name: "uneven attributes",368 inputName: "test",369 attrs: []string{"value"},370 err: "expected an even number of attribute arguments",371 },372 {373 name: "invalid attribute",374 inputName: "test",375 attrs: []string{"foo", ""},376 err: "invalid attribute: foo",377 },378 }379380 for _, tc := range tests {381 t.Run(tc.name, func(t *testing.T) {382 var markdown string383 fn := func() {384 f := mdform.New()385 f.Select(tc.inputName, tc.value, tc.attrs...)386 markdown = strings.ReplaceAll(f.String(), "\n", "")387 }388389 if tc.err != "" {390 urequire.PanicsWithMessage(t, cur, tc.err, fn, "expect form textarea to fail")391 return392 }393394 urequire.NotPanics(t, cur, fn, "expect form textarea to be created")395 uassert.Equal(t, tc.markdown, markdown, "expected markdown to match")396 })397 }398}399Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.