summaryrefslogtreecommitdiffhomepage
path: root/tests/01_arithmetic/00_value_conversion
blob: c44ad00453cfbc99c42360444cef0e06196b2c9e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Performing unary plus or minus, or performing arithmetic operations may
implicitly convert non-numeric values to numeric ones.

If an addition is performed and either operand is a string, the other
operand is converted into a string as well and the addition result is a
concatenated string consisting of the two operand values.

-- Expect stdout --
HelloWorld
12
34
true
false
null
{ "some": "object" }
[ "some", "array" ]
function() { ... }
1.2
Infinity
124
123
123
NaN
NaN
NaN
124.2
Infinity
1
0
0
NaN
NaN
NaN
1.2
Infinity
123
12.3
NaN
-1
0
0
NaN
NaN
NaN
-1.2
-Infinity
-123
-12.3
NaN
4.2
9.6
-- End --

-- Testcase --
{%
	print(join("\n", [

		// Adding two strings concatenates them:
		"Hello" + "World",

		// Adding a number to a string results in a string:
		"1" + 2,

		// Adding a string to a number results in a string:
		3 + "4",

		// Adding any non-string value to a string or vice versa will
		// force stringification of the non-string value
		"" + true,
		"" + false,
		"" + null,
		"" + { some: "object" },
		"" + [ "some", "array" ],
		"" + function() {},
		"" + 1.2,
		"" + (1 / 0),

		// Adding a numeric value to a non-string, non-numeric value
		// or vice versa will convert the non-numeric argument to a
		// number
		123 + true,
		123 + false,
		123 + null,
		123 + { some: "object" },
		123 + [ "some", "array" ],
		123 + function() {},
		123 + 1.2,
		123 + (1 / 0),

		// The unary "+" operator follows the same logic as adding a
		// non-numeric, non-string value to a numeric one. Additionally
		// the unary plus forces conversion of string values into numbers
		+true,
		+false,
		+null,
		+{ some: "object" },
		+[ "some", "array" ],
		+function() {},
		+1.2,
		+(1 / 0),
		+"123",
		+"12.3",
		+"this is not a number",

		// The unary "-" operator functions like the unary "+" one and
		// it additionally returns the negation of the numeric value
		-true,
		-false,
		-null,
		-{ some: "object" },
		-[ "some", "array" ],
		-function() {},
		-1.2,
		-(1 / 0),
		-"123",
		-"12.3",
		-"this is not a number",

		// Adding a double to an integer or vice versa will force the
		// result to a double as well
		1.2 + 3,
		4 + 5.6,

	"" ]));
-- End --