summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/template-literals.js
blob: 07d81509a8ca4d577345299e813d1af48e82493d (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
test("plain literals with expression-like characters", () => {
    expect(`foo`).toBe("foo");
    expect(`foo{`).toBe("foo{");
    expect(`foo}`).toBe("foo}");
    expect(`foo$`).toBe("foo$");
});

test("plain literals with escaped special characters", () => {
    expect(`foo\``).toBe("foo`");
    expect(`foo\$`).toBe("foo$");
    expect(`foo \${"bar"}`).toBe('foo ${"bar"}');
});

test("literals in expressions", () => {
    expect(`foo ${undefined}`).toBe("foo undefined");
    expect(`foo ${null}`).toBe("foo null");
    expect(`foo ${5}`).toBe("foo 5");
    expect(`foo ${true}`).toBe("foo true");
    expect(`foo ${"bar"}`).toBe("foo bar");
});

test("objects in expressions", () => {
    expect(`foo ${{}}`).toBe("foo [object Object]");
    expect(`foo ${{ bar: { baz: "qux" } }}`).toBe("foo [object Object]");
});

test("expressions at beginning of template literal", () => {
    expect(`${"foo"} bar baz`).toBe("foo bar baz");
    expect(`${"foo bar baz"}`).toBe("foo bar baz");
});

test("multiple template literals", () => {
    expect(`foo ${"bar"} ${"baz"}`).toBe("foo bar baz");
});

test("variables in expressions", () => {
    let a = 27;
    expect(`${a}`).toBe("27");
    expect(`foo ${a}`).toBe("foo 27");
    expect(`foo ${a ? "bar" : "baz"}`).toBe("foo bar");
    expect(`foo ${(() => a)()}`).toBe("foo 27");
});

test("template literals in expressions", () => {
    expect(`foo ${`bar`}`).toBe("foo bar");
    expect(`${`${`${`${"foo"}`} bar`}`}`).toBe("foo bar");
});

test("newline literals (not characters)", () => {
    expect(
        `foo
    bar`
    ).toBe("foo\n    bar");
});

test("line continuation in literals (not characters)", () => {
    expect(
        `foo\
    bar`
    ).toBe("foo    bar");
});

test("reference error from expressions", () => {
    expect(() => `${b}`).toThrowWithMessage(ReferenceError, "'b' is not defined");
});