Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christianp committed Nov 15, 2024
1 parent de47990 commit 65238c7
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
59 changes: 57 additions & 2 deletions tests/jme-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15991,6 +15991,23 @@ newBuiltin('separate', [TList, TLambda], TList, null, {
}
});

newBuiltin('groups_of', [TList, TNum], TList, null, {
evaluate: function(args, scope) {
var list = args[0].value;
var n = args[1].value;

var out = [];
for(let i=0; i<list.length; i+=n) {
const row = list.slice(i, i+n);
if(row.length) {
out.push(new TList(row));
}
}

return new TList(out);
}
});

newBuiltin('enumerate',[TList],TList,function(list) {
return list.map(function(v,i) {
return new TList([new TInt(i),v]);
Expand Down Expand Up @@ -16547,7 +16564,44 @@ function set_html_content(element,tok,scope) {
element.appendChild(tok.value);
}
}
newBuiltin('table',[TList,TList],THTML, null, {
newBuiltin('table',['list of list','list', 'list'],THTML, null, {
evaluate: function(args, scope) {
var data = args[0].value;
var col_headers = args[1].value;
var row_headers = args[2].value;
var table = document.createElement('table');
var thead = document.createElement('thead');
table.appendChild(thead);
thead.appendChild(document.createElement('th'));
for(var i=0;i<col_headers.length;i++) {
var th = document.createElement('th');
th.setAttribute('scope','col');
set_html_content(th,col_headers[i],scope);
thead.appendChild(th);
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for(var i=0;i<data.length;i++) {
var row = document.createElement('tr');
tbody.appendChild(row);

var th = document.createElement('th');
th.setAttribute('scope','row');
set_html_content(th,row_headers[i],scope);
row.appendChild(th);

for(var j=0;j<data[i].value.length;j++) {
var cell = data[i].value[j];
var td = document.createElement('td');
set_html_content(td,data[i].value[j],scope);
row.appendChild(td);
}
}
table.setAttribute('data-interactive','false');
return new THTML(table);
}
});
newBuiltin('table',['list of list', 'list'],THTML, null, {
evaluate: function(args, scope) {
var data = args[0].value;
var headers = args[1].value;
Expand All @@ -16556,6 +16610,7 @@ newBuiltin('table',[TList,TList],THTML, null, {
table.appendChild(thead);
for(var i=0;i<headers.length;i++) {
var th = document.createElement('th');
th.setAttribute('scope','col');
set_html_content(th,headers[i],scope);
thead.appendChild(th);
}
Expand All @@ -16575,7 +16630,7 @@ newBuiltin('table',[TList,TList],THTML, null, {
return new THTML(table);
}
});
newBuiltin('table',[TList],THTML, null, {
newBuiltin('table',['list of list'],THTML, null, {
evaluate: function(args,scope) {
var data = args[0].value;
var table = document.createElement('table');
Expand Down
23 changes: 22 additions & 1 deletion tests/jme/doc-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4013,6 +4013,26 @@ export default
}
]
},
{
"name": "groups_of",
"keywords": [
""
],
"noexamples": false,
"calling_patterns": [
"groups_of(list, size)"
],
"examples": [
{
"in": "groups_of(0..5, 2)",
"out": "[ [0,1], [2,3], [4,5] ]"
},
{
"in": "groups_of([\"A\", \"B\", \"C\", \"D\", \"E\"], 3)",
"out": "[ [\"A\", \"B\", \"C\"], [\"D\", \"E\"] ]"
}
]
},
{
"name": "flatten",
"keywords": [
Expand Down Expand Up @@ -4930,7 +4950,8 @@ export default
"noexamples": true,
"calling_patterns": [
"table(data)",
"table(data,headers)"
"table(data, headers)",
"table(data, column_headers, row_headers)"
],
"examples": []
},
Expand Down
2 changes: 1 addition & 1 deletion tests/jme/jme-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal

QUnit.test('HTML',function(assert) {
assert.equal(evaluate('table([["x","y"],["3",1]])').html,"<table data-interactive=\"false\"><tbody><tr><td>x</td><td>y</td></tr><tr><td>3</td><td>1</td></tr></tbody></table>",'table');
assert.equal(evaluate('table([["x","y"],["3",1]],["a","b"])').html,"<table data-interactive=\"false\"><thead><th>a</th><th>b</th></thead><tbody><tr><td>x</td><td>y</td></tr><tr><td>3</td><td>1</td></tr></tbody></table>",'table with headers');
assert.equal(evaluate('table([["x","y"],["3",1]],["a","b"])').html,"<table data-interactive=\"false\"><thead><th scope=\"col\">a</th><th scope=\"col\">b</th></thead><tbody><tr><td>x</td><td>y</td></tr><tr><td>3</td><td>1</td></tr></tbody></table>",'table with headers');
});

QUnit.test('Calculus', function(assert) {
Expand Down
59 changes: 57 additions & 2 deletions tests/numbas-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15582,6 +15582,23 @@ newBuiltin('separate', [TList, TLambda], TList, null, {
}
});

newBuiltin('groups_of', [TList, TNum], TList, null, {
evaluate: function(args, scope) {
var list = args[0].value;
var n = args[1].value;

var out = [];
for(let i=0; i<list.length; i+=n) {
const row = list.slice(i, i+n);
if(row.length) {
out.push(new TList(row));
}
}

return new TList(out);
}
});

newBuiltin('enumerate',[TList],TList,function(list) {
return list.map(function(v,i) {
return new TList([new TInt(i),v]);
Expand Down Expand Up @@ -16138,7 +16155,44 @@ function set_html_content(element,tok,scope) {
element.appendChild(tok.value);
}
}
newBuiltin('table',[TList,TList],THTML, null, {
newBuiltin('table',['list of list','list', 'list'],THTML, null, {
evaluate: function(args, scope) {
var data = args[0].value;
var col_headers = args[1].value;
var row_headers = args[2].value;
var table = document.createElement('table');
var thead = document.createElement('thead');
table.appendChild(thead);
thead.appendChild(document.createElement('th'));
for(var i=0;i<col_headers.length;i++) {
var th = document.createElement('th');
th.setAttribute('scope','col');
set_html_content(th,col_headers[i],scope);
thead.appendChild(th);
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for(var i=0;i<data.length;i++) {
var row = document.createElement('tr');
tbody.appendChild(row);

var th = document.createElement('th');
th.setAttribute('scope','row');
set_html_content(th,row_headers[i],scope);
row.appendChild(th);

for(var j=0;j<data[i].value.length;j++) {
var cell = data[i].value[j];
var td = document.createElement('td');
set_html_content(td,data[i].value[j],scope);
row.appendChild(td);
}
}
table.setAttribute('data-interactive','false');
return new THTML(table);
}
});
newBuiltin('table',['list of list', 'list'],THTML, null, {
evaluate: function(args, scope) {
var data = args[0].value;
var headers = args[1].value;
Expand All @@ -16147,6 +16201,7 @@ newBuiltin('table',[TList,TList],THTML, null, {
table.appendChild(thead);
for(var i=0;i<headers.length;i++) {
var th = document.createElement('th');
th.setAttribute('scope','col');
set_html_content(th,headers[i],scope);
thead.appendChild(th);
}
Expand All @@ -16166,7 +16221,7 @@ newBuiltin('table',[TList,TList],THTML, null, {
return new THTML(table);
}
});
newBuiltin('table',[TList],THTML, null, {
newBuiltin('table',['list of list'],THTML, null, {
evaluate: function(args,scope) {
var data = args[0].value;
var table = document.createElement('table');
Expand Down

0 comments on commit 65238c7

Please sign in to comment.