File: string-startend.js

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2018 froglogic GmbH */

///////////// String.prototype.startsWith() ////////////////

shouldBe("String.prototype.startsWith.length", "1");

// simple
shouldBeTrue("'abc'.startsWith('a')");
shouldBeTrue("'abc'.startsWith('ab')");
shouldBeFalse("'abc'.startsWith('b')");
shouldBeFalse("'abc'.startsWith('x')");
shouldBeFalse("'abc'.startsWith('abcd')");

// with start position
shouldBeTrue("'abc'.startsWith('a', 0)");
shouldBeTrue("'abc'.startsWith('a', -1)");
shouldBeTrue("'abc'.startsWith('b', 1)");
shouldBeFalse("'abc'.startsWith('c', 4)");
shouldBeTrue("'abc'.startsWith('a', -Infinity)");
shouldBeFalse("'abc'.startsWith('a', +Infinity)");

// non-numerical position parameter
shouldBeTrue("'abc'.startsWith('a', undefined)");
shouldBeTrue("'abc'.startsWith('a', null)");
shouldBeTrue("'abc'.startsWith('a', NaN)");
shouldBeTrue("'abc'.startsWith('a', '0')");
shouldBeFalse("'abc'.startsWith('a', '1')");

// non-string objects
shouldBeTrue("'123'.startsWith(1)");
shouldBeFalse("'123'.startsWith(4)");
var arr = ['a', 'b'];
arr.startsWith = String.prototype.startsWith;
shouldBeTrue("arr.startsWith('a')");
var obj = { toString: function() { return "xyz"; } };
obj.startsWith = String.prototype.startsWith;
shouldBeTrue("obj.startsWith('xyz')");

// invalid
shouldThrow("'abc'.startsWith(/a/)");

///////////// String.prototype.endsWith() ////////////////

shouldBe("String.prototype.endsWith.length", "1");

// simple
shouldBeTrue("'abc'.endsWith('c')");
shouldBeTrue("'abc'.endsWith('bc')");
shouldBeFalse("'abc'.endsWith('b')");
shouldBeFalse("'abc'.endsWith('x')");
shouldBeFalse("'abc'.endsWith('abcd')");

// with start position
shouldBeTrue("'abc'.endsWith('c', 3)");
shouldBeTrue("'abc'.endsWith('c', 4)");
shouldBeTrue("'abc'.endsWith('b', 2)");
shouldBeFalse("'abc'.endsWith('a', 0)");
shouldBeFalse("'abc'.endsWith('c', -1)");
shouldBeFalse("'abc'.endsWith('c', -Infinity)");
shouldBeTrue("'abc'.endsWith('c', +Infinity)");

// non-numerical position parameter
shouldBeTrue("'abc'.endsWith('c', undefined)");
shouldBeFalse("'abc'.endsWith('a', null)");
shouldBeFalse("'abc'.endsWith('c', null)");
shouldBeFalse("'abc'.endsWith('a', NaN)");
shouldBeFalse("'abc'.endsWith('c', NaN)");
shouldBeTrue("'abc'.endsWith('c', '4')");
shouldBeFalse("'abc'.endsWith('b', '1')");

// non-string objects
shouldBeTrue("'123'.endsWith(3)");
shouldBeFalse("'123'.endsWith(4)");
var arr = ['a', 'b'];
arr.endsWith = String.prototype.endsWith;
shouldBeTrue("arr.endsWith('b')");
var obj = { toString: function() { return "xyz"; } };
obj.endsWith = String.prototype.endsWith;
shouldBeTrue("obj.endsWith('xyz')");

// invalid
shouldThrow("'abc'.endsWith(/a/)");