summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/template-literals.js
blob: d3f07e86b5db9d821a0872ff858420a54dec0abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
load("test-common.js");

try {
    assert(`foo` === "foo");
    assert(`foo{` === "foo{");
    assert(`foo}` === "foo}");
    assert(`foo$` === "foo$");
    assert(`foo\`` === "foo`")
    assert(`foo\$` === "foo$");
    
    assert(`foo ${undefined}` === "foo undefined");
    assert(`foo ${null}` === "foo null");
    assert(`foo ${5}` === "foo 5");
    assert(`foo ${true}` === "foo true");
    assert(`foo ${"bar"}` === "foo bar");
    assert(`foo \${"bar"}` === 'foo ${"bar"}');

    assert(`foo ${{}}` === "foo [object Object]");
    assert(`foo ${{ bar: { baz: "qux" }}}` === "foo [object Object]");
    assert(`foo ${"bar"} ${"baz"}` === "foo bar baz");
    assert(`${"foo"} bar baz` === "foo bar baz");
    assert(`${"foo bar baz"}` === "foo bar baz");

    let a = 27;
    assert(`${a}` === "27");
    assert(`foo ${a}` === "foo 27");
    assert(`foo ${a ? "bar" : "baz"}` === "foo bar");
    assert(`foo ${(() => a)()}` === "foo 27");

    assert(`foo ${`bar`}` === "foo bar");
    assert(`${`${`${`${"foo"}`} bar`}`}` === "foo bar");
    assert(`foo
    bar` === "foo\n    bar");
    
    assertThrowsError(() => {
        `${b}`;
    }, {
        error: ReferenceError,
        message: "'b' is not defined",
    })

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}