v/vlib/v/gen/js/tests/interp.js

261 lines
5.1 KiB
JavaScript

// V_COMMIT_HASH 8a24d7d
// V_CURRENT_COMMIT_HASH 123d788
// Generated by the V compiler
"use strict";
/** @namespace builtin */
const builtin = (function () {
/**
* @function
* @param {any} s
* @returns {void}
*/
function println(s) {
console.log(s);
}
/**
* @function
* @param {any} s
* @returns {void}
*/
function print(s) {
process.stdout.write(s);
}
/* module exports */
return {
println,
print
};
})();
/** @namespace main */
const main = (function () {
/**
* @function
* @param {string} s1
* @param {string} s2
* @returns {void}
*/
function test_fn(s1, s2) {
builtin.print((s1 === s2 ? "true" : "false"));
builtin.print("\t=> ");
builtin.println(`"${s1}", "${s2}"`);
}
/**
* @function
* @returns {void}
*/
function simple_string_interpolation() {
/** @type {string} */
const a = "Hello";
/** @type {string} */
const b = "World";
/** @type {string} */
const res = `${a} ${b}`;
test_fn(res, "Hello World");
}
/**
* @function
* @returns {void}
*/
function mixed_string_interpolation() {
/** @type {number} */
const num = 7;
/** @type {string} */
const str = "abc";
/** @type {string} */
const s1 = `number=${num}`;
test_fn(s1, "number=7");
/** @type {string} */
const s2 = `string=${str}`;
test_fn(s2, "string=abc");
/** @type {string} */
const s3 = `a: ${num} | b: ${str}`;
test_fn(s3, "a: 7 | b: abc");
}
/**
* @function
* @returns {void}
*/
function formatted_string_interpolation() {
/** @type {string} */
const x = "abc";
/** @type {string} */
const axb = `a:${x}:b`;
test_fn(axb, "a:abc:b");
/** @type {string} */
const x_10 = `a:${x}:b`;
/** @type {string} */
const x10_ = `a:${x}:b`;
test_fn(x_10, "a: abc:b");
test_fn(x10_, "a:abc :b");
/** @type {number} */
const i = 23;
/** @type {string} */
const si_right = `${i}`;
/** @type {string} */
const si__left = `${i}`;
test_fn(si_right, " 23");
test_fn(si__left, "23 ");
}
/**
* @function
* @returns {void}
*/
function implicit_str() {
/** @type {number} */
const i = 42;
test_fn(`int ${i}`, "int 42");
test_fn(`${i}`, "42");
/** @type {boolean} */
const check = `${i}` === "42";
/** @type {string} */
const text = `${i}` + "42";
test_fn(text, "4242");
}
/**
* @function
* @returns {void}
*/
function string_interpolation_percent_escaping() {
/** @type {string} */
const test = "hello";
/** @type {string} */
const hello = "world";
/** @type {string} */
const x = `%.*s${hello}${test} |${hello}|`;
test_fn(x, "%.*sworldhello |world |");
}
/**
* @function
* @returns {void}
*/
function string_interpolation_string_prefix() {
/** @type {string} */
const r = "r";
/** @type {string} */
const rr = `${r}${r}`;
test_fn(rr, "rr");
/** @type {string} */
const c = "c";
/** @type {string} */
const cc = `${c}${c}`;
test_fn(cc, "cc");
/** @type {string} */
const js = "js";
/** @type {string} */
const jsjs = `${js}${js}`;
test_fn(jsjs, "jsjs");
}
/**
* @function
* @returns {void}
*/
function interpolation_string_prefix_expr() {
/** @type {number} */
const r = 1;
/** @type {number} */
const c = 2;
/** @type {number} */
const js = 1;
test_fn(`>${3 + r}<`, ">4<");
test_fn(`${r === js} ${js}`, "true 1");
test_fn(`>${js + c} ${js + r === c}<`, ">3 true<");
}
/**
* @function
* @returns {void}
*/
function utf8_string_interpolation() {
/** @type {string} */
const a = "à-côté";
/** @type {string} */
const st = "Sträßle";
/** @type {string} */
const m = "10€";
test_fn(`${a} ${st} ${m}`, "à-côté Sträßle 10€");
/** @type {string} */
const zz = `>${a}< >${st}< >${m}<-`;
/** @type {string} */
const zz_expected = "> à-côté< >Sträßle < > 10€<-";
test_fn(zz, zz_expected);
/** @type {string} */
const e = "€";
test_fn(`100.00 ${e}`, "100.00 €");
/** @type {string} */
const m2 = "Москва́";
/** @type {string} */
const d = "Antonín Dvořák";
test_fn(`:${m2}:${d}:`, ": Москва́:Antonín Dvořák :");
/** @type {string} */
const g = "Πελοπόννησος";
test_fn(`>${g}<`, ">Πελοπόννησος <");
}
/**
* @constructor
* @param {{v1?: number, v2?: number}} init
*/
function Sss({ v1 = 0, v2 = 0 }) {
this.v1 = v1
this.v2 = v2
};
Sss.prototype = {
/** @type {number} */
v1: 0,
/** @type {number} */
v2: 0,
/**
* @function
* @returns {string}
*/
str() {
const s = this;
return `[${s.v1}, ${s.v2}]`;
}
};
/**
* @function
* @returns {void}
*/
function string_interpolation_str_evaluation() {
/** @type {Sss} */
let x = new Sss({
v1: 17,
v2: 13.455893
});
test_fn(`${x.str()}`, "[17, 13.456]");
}
/* program entry point */
(function() {
simple_string_interpolation();
mixed_string_interpolation();
formatted_string_interpolation();
implicit_str();
string_interpolation_percent_escaping();
string_interpolation_string_prefix();
interpolation_string_prefix_expr();
utf8_string_interpolation();
string_interpolation_str_evaluation();
})();
/* module exports */
return {};
})();