|
| 1 | +#![allow(non_snake_case)] |
| 2 | + |
| 3 | +use super::project; |
| 4 | + |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn to_locale_string() { |
| 8 | + project() |
| 9 | + .file("src/lib.rs", r#" |
| 10 | + #![feature(proc_macro, wasm_custom_section)] |
| 11 | +
|
| 12 | + extern crate wasm_bindgen; |
| 13 | + use wasm_bindgen::prelude::*; |
| 14 | + use wasm_bindgen::js; |
| 15 | +
|
| 16 | + #[wasm_bindgen] |
| 17 | + pub fn to_locale_string(this: &js::Number, locale: String) -> String { |
| 18 | + this.to_locale_string(locale) |
| 19 | + } |
| 20 | + "#) |
| 21 | + .file("test.ts", r#" |
| 22 | + import * as assert from "assert"; |
| 23 | + import * as wasm from "./out"; |
| 24 | +
|
| 25 | + export function test() { |
| 26 | + let number = 1234.45; |
| 27 | + assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45"); |
| 28 | + assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45"); |
| 29 | + assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45"); |
| 30 | + } |
| 31 | + "#) |
| 32 | + .test() |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn to_precision() { |
| 37 | + project() |
| 38 | + .file("src/lib.rs", r#" |
| 39 | + #![feature(proc_macro, wasm_custom_section)] |
| 40 | +
|
| 41 | + extern crate wasm_bindgen; |
| 42 | + use wasm_bindgen::prelude::*; |
| 43 | + use wasm_bindgen::js; |
| 44 | +
|
| 45 | + #[wasm_bindgen] |
| 46 | + pub fn to_precision(this: &js::Number, precision: u8) -> String { |
| 47 | + let result = this.to_precision(precision); |
| 48 | + let result = match result { |
| 49 | + Ok(num) => num, |
| 50 | + Err(_err) => "RangeError".to_string() |
| 51 | + }; |
| 52 | + result |
| 53 | + } |
| 54 | + "#) |
| 55 | + .file("test.ts", r#" |
| 56 | + import * as assert from "assert"; |
| 57 | + import * as wasm from "./out"; |
| 58 | +
|
| 59 | + export function test() { |
| 60 | + assert.equal(wasm.to_precision(0.1, 3), "0.100"); |
| 61 | + assert.equal(wasm.to_precision(10, 101), "RangeError"); |
| 62 | + } |
| 63 | + "#) |
| 64 | + .test() |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn to_string() { |
| 69 | + project() |
| 70 | + .file("src/lib.rs", r#" |
| 71 | + #![feature(proc_macro, wasm_custom_section)] |
| 72 | +
|
| 73 | + extern crate wasm_bindgen; |
| 74 | + use wasm_bindgen::prelude::*; |
| 75 | + use wasm_bindgen::js; |
| 76 | +
|
| 77 | + #[wasm_bindgen] |
| 78 | + pub fn to_string(this: &js::Number, radix: u8) -> String { |
| 79 | + let result = this.to_string(radix); |
| 80 | + let result = match result { |
| 81 | + Ok(num) => num, |
| 82 | + Err(_err) => "RangeError".to_string() |
| 83 | + }; |
| 84 | + result |
| 85 | + } |
| 86 | + "#) |
| 87 | + .file("test.ts", r#" |
| 88 | + import * as assert from "assert"; |
| 89 | + import * as wasm from "./out"; |
| 90 | +
|
| 91 | + export function test() { |
| 92 | + let number = 42; |
| 93 | + assert.equal(wasm.to_string(number, 10), "42"); |
| 94 | + assert.equal(wasm.to_string(233, 16), "e9"); |
| 95 | + assert.equal(wasm.to_string(number, 100), "RangeError"); |
| 96 | + } |
| 97 | + "#) |
| 98 | + .test() |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn value_of() { |
| 103 | + project() |
| 104 | + .file("src/lib.rs", r#" |
| 105 | + #![feature(proc_macro, wasm_custom_section)] |
| 106 | +
|
| 107 | + extern crate wasm_bindgen; |
| 108 | + use wasm_bindgen::prelude::*; |
| 109 | + use wasm_bindgen::js; |
| 110 | +
|
| 111 | + #[wasm_bindgen] |
| 112 | + pub fn js_value_of(this: &js::Number) -> js::Number { |
| 113 | + this.value_of() |
| 114 | + } |
| 115 | + "#) |
| 116 | + .file("test.ts", r#" |
| 117 | + import * as assert from "assert"; |
| 118 | + import * as wasm from "./out"; |
| 119 | +
|
| 120 | + export function test() { |
| 121 | + let number = 42; |
| 122 | + assert.equal(wasm.js_value_of(number), 42); |
| 123 | + assert.equal(typeof wasm.js_value_of(number), "number"); |
| 124 | + } |
| 125 | + "#) |
| 126 | + .test() |
| 127 | +} |
0 commit comments