| 928 | // shared with the ender integration tests |
| 929 | |
| 930 | function createSerializeHelper(ok) { |
| 931 | var forms = document.forms |
| 932 | , foo = forms[0].getElementsByTagName('input')[1] |
| 933 | , bar = forms[0].getElementsByTagName('input')[2] |
| 934 | , choices = forms[0].getElementsByTagName('select')[0] |
| 935 | , BIND_ARGS = 'bind' |
| 936 | , PASS_ARGS = 'pass' |
| 937 | |
| 938 | function reset() { |
| 939 | forms[1].reset() |
| 940 | } |
| 941 | |
| 942 | function formElements(formIndex, tagName, elementIndex) { |
| 943 | return forms[formIndex].getElementsByTagName(tagName)[elementIndex] |
| 944 | } |
| 945 | |
| 946 | function isArray(a) { |
| 947 | return Object.prototype.toString.call(a) == '[object Array]' |
| 948 | } |
| 949 | |
| 950 | function sameValue(value, expected) { |
| 951 | if (expected == null) { |
| 952 | return value === null |
| 953 | } else if (isArray(expected)) { |
| 954 | if (value.length !== expected.length) return false |
| 955 | for (var i = 0; i < expected.length; i++) { |
| 956 | if (value[i] != expected[i]) return false |
| 957 | } |
| 958 | return true |
| 959 | } else return value == expected |
| 960 | } |
| 961 | |
| 962 | function testInput(input, name, value, str) { |
| 963 | var sa = ajax.serialize(input, { type: 'array' }) |
| 964 | , sh = ajax.serialize(input, { type: 'map' }) |
| 965 | , av, i |
| 966 | |
| 967 | if (value != null) { |
| 968 | av = isArray(value) ? value : [ value ] |
| 969 | |
| 970 | ok( |
| 971 | sa.length == av.length |
| 972 | , 'serialize(' + str + ', {type:\'array\'}) returns array ' |
| 973 | + '[{name,value}]' |
| 974 | ) |
| 975 | |
| 976 | for (i = 0; i < av.length; i++) { |
| 977 | ok( |
| 978 | name == sa[i].name |
| 979 | , 'serialize(' + str + ', {type:\'array\'})[' + i + '].name' |
| 980 | ) |
| 981 | ok( |
| 982 | av[i] == sa[i].value |
| 983 | , 'serialize(' + str + ', {type:\'array\'})[' + i + '].value' |
| 984 | ) |
| 985 | } |
| 986 | |
| 987 | ok(sameValue(sh[name], value), 'serialize(' + str + ', {type:\'map\'})') |