diff --git a/.cargo/config.toml b/.cargo/config.toml index df87c4f8..e66776ac 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -4,3 +4,6 @@ [build] rustflags = ["--cfg=unreleased_changelogs"] rustdocflags = ["--cfg=unreleased_changelogs"] + +[target.wasm32-unknown-unknown] +runner = "wasm-bindgen-test-runner" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02390b98..d09f505b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,20 +60,26 @@ jobs: - { name: 'iOS Aarch64', target: aarch64-apple-ios, os: macos-latest, } - { name: 'Web', target: wasm32-unknown-unknown, os: ubuntu-latest, } exclude: + # Web on nightly needs extra arguments + - toolchain: nightly + platform: { name: 'Web', target: wasm32-unknown-unknown, os: ubuntu-latest } # Android is tested on stable-3 - toolchain: '1.73' platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package=winit --features=android-native-activity', cmd: 'apk --' } include: - toolchain: '1.73' platform: { name: 'Android', target: aarch64-linux-android, os: ubuntu-latest, options: '--package=winit --features=android-native-activity', cmd: 'apk --' } + - toolchain: 'nightly' + platform: { name: 'Web', target: wasm32-unknown-unknown, os: ubuntu-latest, test-options: -Zdoctest-xcompile } - toolchain: 'nightly' platform: { - name: 'Web Atomic', - target: wasm32-unknown-unknown, - os: ubuntu-latest, - options: '-Zbuild-std=panic_abort,std', - rustflags: '-Ctarget-feature=+atomics,+bulk-memory', - components: rust-src, + name: 'Web Atomic', + target: wasm32-unknown-unknown, + os: ubuntu-latest, + options: '-Zbuild-std=panic_abort,std', + test-options: -Zdoctest-xcompile, + rustflags: '-Ctarget-feature=+atomics,+bulk-memory', + components: rust-src, } env: @@ -83,8 +89,10 @@ jobs: # Faster compilation and error on warnings RUSTFLAGS: '--codegen=debuginfo=0 --deny=warnings ${{ matrix.platform.rustflags }}' + RUSTDOCFLAGS: ${{ matrix.platform.rustflags }} OPTIONS: --target=${{ matrix.platform.target }} ${{ matrix.platform.options }} + TEST_OPTIONS: ${{ matrix.platform.test-options }} CMD: ${{ matrix.platform.cmd }} steps: @@ -131,6 +139,11 @@ jobs: if: contains(matrix.platform.target, 'android') && (steps.cargo-apk-cache.outputs.cache-hit != 'true') run: cargo install cargo-apk --version=^0.9.7 --locked + - uses: taiki-e/cache-cargo-install-action@v2 + if: contains(matrix.platform.target, 'wasm32') && matrix.toolchain == 'nightly' + with: + tool: wasm-bindgen-cli + - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.toolchain }}${{ matrix.platform.host }} @@ -162,29 +175,29 @@ jobs: if: > !contains(matrix.platform.target, 'android') && !contains(matrix.platform.target, 'ios') && - !contains(matrix.platform.target, 'wasm32') && + (!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') && !contains(matrix.platform.target, 'redox') && matrix.toolchain != '1.73' run: cargo $CMD test $OPTIONS - name: Lint with clippy if: (matrix.toolchain == 'stable') && !contains(matrix.platform.options, '--no-default-features') - run: cargo clippy --all-targets $OPTIONS -- -Dwarnings + run: cargo clippy --all-targets $OPTIONS $TEST_OPTIONS -- -Dwarnings - name: Build tests with serde enabled if: > !contains(matrix.platform.target, 'redox') && matrix.toolchain != '1.73' - run: cargo $CMD test --no-run $OPTIONS --features serde + run: cargo $CMD test --no-run $OPTIONS $TEST_OPTIONS --features serde - name: Run tests with serde enabled if: > !contains(matrix.platform.target, 'android') && !contains(matrix.platform.target, 'ios') && - !contains(matrix.platform.target, 'wasm32') && + (!contains(matrix.platform.target, 'wasm32') || matrix.toolchain == 'nightly') && !contains(matrix.platform.target, 'redox') && matrix.toolchain != '1.73' - run: cargo $CMD test $OPTIONS --features serde + run: cargo $CMD test $OPTIONS $TEST_OPTIONS --features serde - name: Check docs.rs documentation if: matrix.toolchain == 'nightly' diff --git a/Cargo.toml b/Cargo.toml index 3238d34a..4ffa20d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -331,6 +331,7 @@ concurrent-queue = { version = "2", default-features = false } [target.'cfg(target_family = "wasm")'.dev-dependencies] console_error_panic_hook = "0.1" tracing-web = "0.1" +wasm-bindgen-test = "0.3" [[example]] doc-scrape-examples = true diff --git a/src/event.rs b/src/event.rs index e0a6d437..5cb80379 100644 --- a/src/event.rs +++ b/src/event.rs @@ -616,7 +616,7 @@ pub struct KeyEvent { /// In games, you often want to ignore repated key events - this can be /// done by ignoring events where this property is set. /// - /// ``` + /// ```no_run /// use winit::event::{ElementState, KeyEvent, WindowEvent}; /// use winit::keyboard::{KeyCode, PhysicalKey}; /// # let window_event = WindowEvent::RedrawRequested; // To make the example compile diff --git a/src/keyboard.rs b/src/keyboard.rs index 6039e92c..f8171d20 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -1568,10 +1568,15 @@ impl NamedKey { /// # Examples /// /// ``` + /// # #[cfg(web_platform)] + /// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + /// # #[cfg_attr(web_platform, wasm_bindgen_test::wasm_bindgen_test)] + /// # fn main() { /// use winit::keyboard::NamedKey; /// /// assert_eq!(NamedKey::Enter.to_text(), Some("\r")); /// assert_eq!(NamedKey::F20.to_text(), None); + /// # } /// ``` pub fn to_text(&self) -> Option<&str> { match self { @@ -1591,11 +1596,16 @@ impl Key { /// # Examples /// /// ``` + /// # #[cfg(web_platform)] + /// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + /// # #[cfg_attr(web_platform, wasm_bindgen_test::wasm_bindgen_test)] + /// # fn main() { /// use winit::keyboard::{Key, NamedKey}; /// /// assert_eq!(Key::Character("a".into()).to_text(), Some("a")); /// assert_eq!(Key::Named(NamedKey::Enter).to_text(), Some("\r")); /// assert_eq!(Key::Named(NamedKey::F20).to_text(), None); + /// # } /// ``` pub fn to_text(&self) -> Option<&str> { match self {