@@ -31,6 +31,115 @@ fn char_at() {
31
31
. test ( )
32
32
}
33
33
34
+ #[ test]
35
+ fn starts_with ( ) {
36
+ project ( )
37
+ . file ( "src/lib.rs" , r#"
38
+ #![feature(proc_macro, wasm_custom_section)]
39
+
40
+ extern crate wasm_bindgen;
41
+ use wasm_bindgen::prelude::*;
42
+ use wasm_bindgen::js;
43
+
44
+ #[wasm_bindgen]
45
+ pub fn string_starts_with(this: &js::JsString, search_string: &js::JsString, position: u32) -> bool {
46
+ this.starts_with(search_string, position)
47
+ }
48
+ "# )
49
+ . file ( "test.ts" , r#"
50
+ import * as assert from "assert";
51
+ import * as wasm from "./out";
52
+
53
+ export function test() {
54
+ let str = "To be, or not to be, that is the question.";
55
+
56
+ // TODO: remove second parameter for both assertions once we have optional parameters
57
+ assert.ok(wasm.string_starts_with(str, 'To be', 0));
58
+ assert.ok(!wasm.string_starts_with(str, 'not to be', 0));
59
+ assert.ok(wasm.string_starts_with(str, 'not to be', 10));
60
+ }
61
+ "# )
62
+ . test ( )
63
+ }
64
+
65
+ #[ test]
66
+ fn substring ( ) {
67
+ project ( )
68
+ . file ( "src/lib.rs" , r#"
69
+ #![feature(proc_macro, wasm_custom_section)]
70
+
71
+ extern crate wasm_bindgen;
72
+ use wasm_bindgen::prelude::*;
73
+ use wasm_bindgen::js;
74
+
75
+ #[wasm_bindgen]
76
+ pub fn string_substring(this: &js::JsString, index_start: u32, index_end: u32) -> js::JsString {
77
+ this.substring(index_start, index_end)
78
+ }
79
+ "# )
80
+ . file ( "test.ts" , r#"
81
+ import * as assert from "assert";
82
+ import * as wasm from "./out";
83
+
84
+ export function test() {
85
+ let anyString = "Mozilla";
86
+
87
+ assert.equal(wasm.string_substring(anyString, 0, 1), 'M');
88
+ assert.equal(wasm.string_substring(anyString, 1, 0), 'M');
89
+
90
+ assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill');
91
+
92
+ // TODO: Add test once we have optional parameters
93
+ // assert.equal(wasm.string_substring(anyString, 4), 'lla');
94
+ assert.equal(wasm.string_substring(anyString, 4, 7), 'lla');
95
+ assert.equal(wasm.string_substring(anyString, 7, 4), 'lla');
96
+
97
+ assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla');
98
+ assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla');
99
+ }
100
+ "# )
101
+ . test ( )
102
+ }
103
+
104
+ #[ test]
105
+ fn index_of ( ) {
106
+ project ( )
107
+ . file ( "src/lib.rs" , r#"
108
+ #![feature(proc_macro, wasm_custom_section)]
109
+
110
+ extern crate wasm_bindgen;
111
+ use wasm_bindgen::prelude::*;
112
+ use wasm_bindgen::js;
113
+
114
+ #[wasm_bindgen]
115
+ pub fn string_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
116
+ this.index_of(search_value, from_index)
117
+ }
118
+ "# )
119
+ . file ( "test.ts" , r#"
120
+ import * as assert from "assert";
121
+ import * as wasm from "./out";
122
+
123
+ export function test() {
124
+ let str = "Blue Whale";
125
+
126
+ // TODO: remove second parameter once we have optional parameters
127
+ assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
128
+ // TODO: remove second parameter once we have optional parameters
129
+ assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
130
+ assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
131
+ assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
132
+ assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
133
+ // TODO: remove second parameter once we have optional parameters
134
+ assert.equal(wasm.string_index_of(str, '', 0), 0);
135
+ assert.equal(wasm.string_index_of(str, '', 9), 9);
136
+ assert.equal(wasm.string_index_of(str, '', 10), 10);
137
+ assert.equal(wasm.string_index_of(str, '', 11), 10);
138
+ }
139
+ "# )
140
+ . test ( )
141
+ }
142
+
34
143
#[ test]
35
144
fn slice ( ) {
36
145
project ( )
@@ -94,4 +203,4 @@ fn substr() {
94
203
}
95
204
"# )
96
205
. test ( )
97
- }
206
+ }
0 commit comments