Source

lang-js / test / test_array.py

from test.test_interp import assertv, assertp


def test_sort(capsys):
    assertp("var x = [5,2]; print(x.sort());", '2,5', capsys)
    assertp("var x = [1,2,3]; print(x.sort());", '1,2,3', capsys)
    assertp("var x = [4,3,2,1]; print(x.sort());", '1,2,3,4', capsys)


def test_array_push(capsys):
    assertv("var x = []; x.push(42); x.length;", 1)
    assertv("var x = []; x.push(42); x[0];", 42)
    assertv("var x = [1,2,3]; x.push(42); x[3];", 42)
    assertp("var x = []; x.push(4); x.push(3); x.push(2); x.push(1); print(x)", '4,3,2,1', capsys)


def test_array_pop():
    assertv("var x = [4,3,2,1]; x.pop(); x.length;", 3)
    assertv("var x = [4,3,2,1]; x.pop();", 1)
    assertv("var x = [4,3,2,1]; x.pop(); x.pop(); x.pop(); x.pop();", 4)
    assertv("var x = [4,3,2,1]; x.pop(); x.pop(); x.pop(); x.pop(); x.length", 0)


def test_array_length():
    assertv("var x = []; x.length;", 0)
    assertv("var x = [1,2,3]; x.length;", 3)
    assertv("var x = []; x[0] = 1; x[1] = 2; x[2] = 3; x.length;", 3)
    assertv("var x = []; x[2] = 3; x.length;", 3)


def test_make_array_index():
    from js.jsobj import make_array_index, NOT_ARRAY_INDEX
    assert make_array_index('12345') == 12345
    assert make_array_index(u'12345') == 12345
    assert make_array_index('12a45') == NOT_ARRAY_INDEX
    assert make_array_index('012345') == 12345
    assert make_array_index('') == NOT_ARRAY_INDEX
    assert make_array_index(' ') == NOT_ARRAY_INDEX
    assert make_array_index('x') == NOT_ARRAY_INDEX
    assert make_array_index('abc123') == NOT_ARRAY_INDEX
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.