@@ -59,3 +59,112 @@ fn slice() {
59
59
"# )
60
60
. test ( )
61
61
}
62
+
63
+ #[ test]
64
+ fn starts_with ( ) {
65
+ project ( )
66
+ . file ( "src/lib.rs" , r#"
67
+ #![feature(proc_macro, wasm_custom_section)]
68
+
69
+ extern crate wasm_bindgen;
70
+ use wasm_bindgen::prelude::*;
71
+ use wasm_bindgen::js;
72
+
73
+ #[wasm_bindgen]
74
+ pub fn string_starts_with(this: &js::JsString, search_string: &js::JsString, position: u32) -> bool {
75
+ this.starts_with(search_string, position)
76
+ }
77
+ "# )
78
+ . file ( "test.ts" , r#"
79
+ import * as assert from "assert";
80
+ import * as wasm from "./out";
81
+
82
+ export function test() {
83
+ let str = "To be, or not to be, that is the question.";
84
+
85
+ // TODO: remove second parameter for both assertions once we have optional parameters
86
+ assert.ok(wasm.string_starts_with(str, 'To be', 0));
87
+ assert.ok(!wasm.string_starts_with(str, 'not to be', 0));
88
+ assert.ok(wasm.string_starts_with(str, 'not to be', 10));
89
+ }
90
+ "# )
91
+ . test ( )
92
+ }
93
+
94
+ #[ test]
95
+ fn substring ( ) {
96
+ project ( )
97
+ . file ( "src/lib.rs" , r#"
98
+ #![feature(proc_macro, wasm_custom_section)]
99
+
100
+ extern crate wasm_bindgen;
101
+ use wasm_bindgen::prelude::*;
102
+ use wasm_bindgen::js;
103
+
104
+ #[wasm_bindgen]
105
+ pub fn string_substring(this: &js::JsString, index_start: u32, index_end: u32) -> js::JsString {
106
+ this.substring(index_start, index_end)
107
+ }
108
+ "# )
109
+ . file ( "test.ts" , r#"
110
+ import * as assert from "assert";
111
+ import * as wasm from "./out";
112
+
113
+ export function test() {
114
+ let anyString = "Mozilla";
115
+
116
+ assert.equal(wasm.string_substring(anyString, 0, 1), 'M');
117
+ assert.equal(wasm.string_substring(anyString, 1, 0), 'M');
118
+
119
+ assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill');
120
+
121
+ // TODO: Add test once we have optional parameters
122
+ // assert.equal(wasm.string_substring(anyString, 4), 'lla');
123
+ assert.equal(wasm.string_substring(anyString, 4, 7), 'lla');
124
+ assert.equal(wasm.string_substring(anyString, 7, 4), 'lla');
125
+
126
+ assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla');
127
+ assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla');
128
+ }
129
+ "# )
130
+ . test ( )
131
+ }
132
+
133
+ #[ test]
134
+ fn index_of ( ) {
135
+ project ( )
136
+ . file ( "src/lib.rs" , r#"
137
+ #![feature(proc_macro, wasm_custom_section)]
138
+
139
+ extern crate wasm_bindgen;
140
+ use wasm_bindgen::prelude::*;
141
+ use wasm_bindgen::js;
142
+
143
+ #[wasm_bindgen]
144
+ pub fn string_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
145
+ this.index_of(search_value, from_index)
146
+ }
147
+ "# )
148
+ . file ( "test.ts" , r#"
149
+ import * as assert from "assert";
150
+ import * as wasm from "./out";
151
+
152
+ export function test() {
153
+ let str = "Blue Whale";
154
+
155
+ // TODO: remove second parameter once we have optional parameters
156
+ assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
157
+ // TODO: remove second parameter once we have optional parameters
158
+ assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
159
+ assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
160
+ assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
161
+ assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
162
+ // TODO: remove second parameter once we have optional parameters
163
+ assert.equal(wasm.string_index_of(str, '', 0), 0);
164
+ assert.equal(wasm.string_index_of(str, '', 9), 9);
165
+ assert.equal(wasm.string_index_of(str, '', 10), 10);
166
+ assert.equal(wasm.string_index_of(str, '', 11), 10);
167
+ }
168
+ "# )
169
+ . test ( )
170
+ }
0 commit comments