From 6866e6a886705b79189187c9f3fd0a5cde2bf70f Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:42:15 +0530 Subject: [PATCH] tests: finished porting all tests --- Cargo.lock | 252 ++++----- swhkd/src/config.rs | 15 +- swhkd/src/tests.rs | 1231 ------------------------------------------- 3 files changed, 96 insertions(+), 1402 deletions(-) delete mode 100644 swhkd/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index ec1453ec..379d082f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -48,9 +48,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.14" +version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" dependencies = [ "anstyle", "anstyle-parse", @@ -63,36 +63,36 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] name = "anstyle-wincon" -version = "3.0.3" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -168,15 +168,15 @@ dependencies = [ [[package]] name = "bytes" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" [[package]] name = "cc" -version = "1.0.102" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "779e6b7d17797c0b42023d417228c02889300190e700cb074c3438d9c541d332" +checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" [[package]] name = "cfg-if" @@ -186,9 +186,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.8" +version = "4.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" +checksum = "35723e6a11662c2afb578bcf0b88bf6ea8e21282a953428f240574fcc3a2b5b3" dependencies = [ "clap_builder", "clap_derive", @@ -196,9 +196,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.8" +version = "4.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +checksum = "49eb96cbfa7cfa35017b7cd548c75b14c3118c98b423041d70562665e07fb0fa" dependencies = [ "anstream", "anstyle", @@ -208,9 +208,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.8" +version = "4.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" +checksum = "5d029b67f89d30bbb547c89fd5161293c0aec155fc691d7924b64550662db93e" dependencies = [ "heck", "proc-macro2", @@ -220,15 +220,15 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" [[package]] name = "colorchoice" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" [[package]] name = "core-foundation-sys" @@ -400,9 +400,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "is_terminal_polyfill" -version = "1.70.0" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itertools" @@ -480,13 +480,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.11" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" +checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" dependencies = [ + "hermit-abi 0.3.9", "libc", "wasi", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] @@ -511,21 +512,11 @@ dependencies = [ "winapi", ] -[[package]] -name = "num_cpus" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" -dependencies = [ - "hermit-abi 0.3.9", - "libc", -] - [[package]] name = "object" -version = "0.36.1" +version = "0.36.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "081b846d1d56ddfc18fdf1a922e4f6e07a11768ea1b92dec44e42b72712ccfce" +checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e" dependencies = [ "memchr", ] @@ -556,14 +547,14 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.5", + "windows-targets", ] [[package]] name = "pest" -version = "2.7.10" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "560131c633294438da9f7c4b08189194b20946c8274c6b9e38881a7874dc8ee8" +checksum = "cd53dff83f26735fdc1ca837098ccf133605d794cdae66acfc2bfac3ec809d95" dependencies = [ "memchr", "thiserror", @@ -572,9 +563,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.10" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26293c9193fbca7b1a3bf9b79dc1e388e927e6cacaa78b4a3ab705a1d3d41459" +checksum = "2a548d2beca6773b1c244554d36fcf8548a8a58e74156968211567250e48e49a" dependencies = [ "pest", "pest_generator", @@ -582,9 +573,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.10" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ec22af7d3fb470a85dd2ca96b7c577a1eb4ef6f1683a9fe9a8c16e136c04687" +checksum = "3c93a82e8d145725dcbaf44e5ea887c8a869efdcc28706df2d08c69e17077183" dependencies = [ "pest", "pest_meta", @@ -595,9 +586,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.7.10" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7a240022f37c361ec1878d646fc5b7d7c4d28d5946e1a80ad5a7a4f4ca0bdcd" +checksum = "a941429fea7e08bedec25e4f6785b6ffaacc6b755da98df5ef3e7dcf4a124c4f" dependencies = [ "once_cell", "pest", @@ -662,9 +653,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" dependencies = [ "bitflags 2.6.0", ] @@ -765,7 +756,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -776,8 +767,8 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "sweet" -version = "0.1.0" -source = "git+https://github.com/lavafroth/sweet.git#30e44f4f1a765507203a9b5e869893d7422f3130" +version = "0.3.0" +source = "git+https://github.com/lavafroth/sweet.git#6146627ee75be8bda87dc443c2f1d153590d3610" dependencies = [ "anyhow", "bitflags 2.6.0", @@ -801,9 +792,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.68" +version = "2.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" dependencies = [ "proc-macro2", "quote", @@ -842,18 +833,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.61" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", @@ -862,28 +853,27 @@ dependencies = [ [[package]] name = "tokio" -version = "1.38.0" +version = "1.39.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" dependencies = [ "backtrace", "bytes", "libc", "mio", - "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.48.0", + "windows-sys", ] [[package]] name = "tokio-macros" -version = "2.3.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", @@ -949,9 +939,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "version_check" -version = "0.9.4" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "wasi" @@ -981,7 +971,7 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "windows-sys 0.52.0", + "windows-sys", ] [[package]] @@ -990,144 +980,78 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets", ] [[package]] name = "windows-targets" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", -] - -[[package]] -name = "windows-targets" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" -dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "wyz" diff --git a/swhkd/src/config.rs b/swhkd/src/config.rs index 15b5f910..2c87db1f 100644 --- a/swhkd/src/config.rs +++ b/swhkd/src/config.rs @@ -197,13 +197,14 @@ fn sweet_def_to_kb(def: &Definition) -> KeyBinding { let modifiers = def .modifiers .iter() - .map(|m| match m { - sweet::token::Modifier::Super => Modifier::Super, - sweet::token::Modifier::Any => Modifier::Any, - sweet::token::Modifier::Control => Modifier::Control, - sweet::token::Modifier::Alt => Modifier::Alt, - sweet::token::Modifier::Altgr => Modifier::Altgr, - sweet::token::Modifier::Shift => Modifier::Shift, + .filter_map(|m| match m { + sweet::token::Modifier::Super => Some(Modifier::Super), + sweet::token::Modifier::Any => Some(Modifier::Any), + sweet::token::Modifier::Control => Some(Modifier::Control), + sweet::token::Modifier::Alt => Some(Modifier::Alt), + sweet::token::Modifier::Altgr => Some(Modifier::Altgr), + sweet::token::Modifier::Shift => Some(Modifier::Shift), + sweet::token::Modifier::Omission => None, }) .collect(); diff --git a/swhkd/src/tests.rs b/swhkd/src/tests.rs deleted file mode 100644 index fe7d93aa..00000000 --- a/swhkd/src/tests.rs +++ /dev/null @@ -1,1231 +0,0 @@ -mod test_config { - use crate::config::{ - extract_curly_brace, load, parse_contents, Error, Hotkey, Modifier, ParseError, Prefix, - }; - use std::fs; - use std::io::Write; - use std::{fs::File, path::PathBuf}; - - // Implement a struct for a path used in tests - // so that the test file will be automatically removed - // no matter how the test goes - struct TestPath { - path: PathBuf, - } - - impl TestPath { - fn new(path: &str) -> Self { - TestPath { path: PathBuf::from(path) } - } - - // Create a path method for a more succinct way - // to deal with borrowing the path value - fn path(&self) -> PathBuf { - self.path.clone() - } - } - - impl Drop for TestPath { - fn drop(self: &mut TestPath) { - if self.path.exists() { - fs::remove_file(self.path()).unwrap(); - } - } - } - - // Wrapper for config tests - fn eval_config_test(contents: &str, expected_hotkeys: Vec) -> std::io::Result<()> { - let result = parse_contents(PathBuf::new(), contents.to_string()); - - let mut expected_hotkeys_mut = expected_hotkeys; - - if result.is_err() { - panic!("Expected Ok config, found Err {:?}", result.unwrap_err()); - } - - let result = &result.unwrap()[0]; - let actual_hotkeys = &result.hotkeys; - - assert_eq!(actual_hotkeys.len(), expected_hotkeys_mut.len()); - - // Go through each actual hotkey, and pop a corresponding - // hotkey from the expected hotkeys - // to make sure that order does not matter - for hotkey in actual_hotkeys { - if let Some(index) = expected_hotkeys_mut.iter().position(|key| { - key.keybinding == hotkey.keybinding && key.command == hotkey.command - }) { - expected_hotkeys_mut.remove(index); - } else { - panic!( - "unexpected hotkey {:#?} found in result\nExpected result:\n{:#?}", - hotkey, expected_hotkeys_mut - ); - } - } - - if !expected_hotkeys_mut.is_empty() { - panic!( - "Some hotkeys were not returned by the actual result:\n{:#?}", - expected_hotkeys_mut - ); - } - - Ok(()) - } - - // Wrapper for the many error tests - fn eval_invalid_config_test( - contents: &str, - parse_error_type: ParseError, - ) -> std::io::Result<()> { - let result = parse_contents(PathBuf::new(), contents.to_string()); - - assert!(result.is_err()); - let result = result.unwrap_err(); - - // Check if the Error type is InvalidConfig - let result = match result { - Error::InvalidConfig(parse_err) => parse_err, - _ => panic!(), - }; - - // Check the ParseError enum type - if result != parse_error_type { - panic!("ParseError: Expected `{:?}`, found `{:?}`", parse_error_type, result); - } - - Ok(()) - } - - #[test] - fn test_existing_file() -> std::io::Result<()> { - let setup = TestPath::new("/tmp/swhkd-test-file1"); - // Build a dummy file in /tmp - let mut f = File::create(setup.path())?; - f.write_all( - b" -x - dmenu_run - -q - bspc node -q", - )?; - - let result = fs::read_to_string(&setup.path()); - assert!(result.is_ok()); - Ok(()) - } - - #[test] - fn test_load_multiple_config() -> std::io::Result<()> { - let setup = TestPath::new("/tmp/swhkd-test-file2"); - let mut f = File::create(setup.path())?; - f.write_all( - b" -include /tmp/swhkd-test-file3 -super + b - firefox", - )?; - - let setup2 = TestPath::new("/tmp/swhkd-test-file3"); - let mut f2 = File::create(setup2.path())?; - f2.write_all( - b" -super + c - hello", - )?; - - let hotkeys = &load(&setup.path()).unwrap()[0].hotkeys; - assert_eq!( - *hotkeys, - vec!( - Hotkey::new(evdev::Key::KEY_C, vec![Modifier::Super], String::from("hello")), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], String::from("firefox")) - ) - ); - Ok(()) - } - - #[test] - fn test_relative_import() -> std::io::Result<()> { - let setup = TestPath::new("/tmp/swhkd-relative-file1"); - let mut f = File::create(setup.path())?; - f.write_all( - b" -include swhkd-relative-file2 -super + b - firefox", - )?; - - let setup2 = TestPath::new("swhkd-relative-file2"); - let mut f2 = File::create(setup2.path())?; - f2.write_all( - b" -super + c - hello", - )?; - - let hotkeys = &load(&setup.path()).unwrap()[0].hotkeys; - assert_eq!( - *hotkeys, - vec!( - Hotkey::new(evdev::Key::KEY_C, vec![Modifier::Super], String::from("hello")), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], String::from("firefox")) - ) - ); - Ok(()) - } - - #[test] - fn test_more_multiple_configs() -> std::io::Result<()> { - let setup = TestPath::new("/tmp/swhkd-test-file4"); - let mut f = File::create(setup.path())?; - f.write_all( - b" -a - a", - )?; - - let setup2 = TestPath::new("/tmp/swhkd-test-file5"); - let mut f2 = File::create(setup2.path())?; - f2.write_all( - b" -include /tmp/swhkd-test-file4 -b - b", - )?; - let setup3 = TestPath::new("/tmp/swhkd-test-file6"); - let mut f3 = File::create(setup3.path())?; - f3.write_all( - b" -include /tmp/swhkd-test-file4 -include /tmp/swhkd-test-file5 -include /tmp/swhkd-test-file6 -include /tmp/swhkd-test-file7 -c - c", - )?; - let setup4 = TestPath::new("/tmp/swhkd-test-file7"); - let mut f4 = File::create(setup4.path())?; - f4.write_all( - b" -include /tmp/swhkd-test-file6 -d - d", - )?; - - let hotkeys = &load(&setup4.path()).unwrap()[0].hotkeys; - assert_eq!( - *hotkeys, - vec!( - Hotkey::new(evdev::Key::KEY_C, vec![], String::from("c")), - Hotkey::new(evdev::Key::KEY_A, vec![], String::from("a")), - Hotkey::new(evdev::Key::KEY_B, vec![], String::from("b")), - Hotkey::new(evdev::Key::KEY_D, vec![], String::from("d")), - ) - ); - Ok(()) - } - #[test] - fn test_include_and_unbind() -> std::io::Result<()> { - let setup = TestPath::new("/tmp/swhkd-test-file8"); - let mut f = File::create(setup.path())?; - f.write_all( - b" -include /tmp/swhkd-test-file9 -super + b - firefox -ignore super + d", - )?; - - let setup2 = TestPath::new("/tmp/swhkd-test-file9"); - let mut f2 = File::create(setup2.path())?; - f2.write_all( - b" -super + c - hello -super + d - world", - )?; - - let hotkeys = &load(&setup.path()).unwrap()[0].hotkeys; - assert_eq!( - *hotkeys, - vec!( - Hotkey::new(evdev::Key::KEY_C, vec![Modifier::Super], String::from("hello")), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], String::from("firefox")) - ) - ); - Ok(()) - } - - #[test] - fn test_basic_keybind() -> std::io::Result<()> { - let contents = " -r - alacritty - "; - - eval_config_test( - contents, - vec![Hotkey::new(evdev::Key::KEY_R, vec![], String::from("alacritty"))], - ) - } - - #[test] - fn test_multiple_keybinds() -> std::io::Result<()> { - let contents = " -r - alacritty - -w - kitty - -t - /bin/firefox - "; - - let hotkey_1 = Hotkey::new(evdev::Key::KEY_R, vec![], String::from("alacritty")); - let hotkey_2 = Hotkey::new(evdev::Key::KEY_W, vec![], String::from("kitty")); - let hotkey_3 = Hotkey::new(evdev::Key::KEY_T, vec![], String::from("/bin/firefox")); - - eval_config_test(contents, vec![hotkey_1, hotkey_2, hotkey_3]) - } - - #[test] - fn test_comments() -> std::io::Result<()> { - let contents = " -r - alacritty - -w - kitty - -#t - #/bin/firefox - "; - - let expected_keybinds = vec![ - Hotkey::new(evdev::Key::KEY_R, vec![], String::from("alacritty")), - Hotkey::new(evdev::Key::KEY_W, vec![], String::from("kitty")), - ]; - - eval_config_test(contents, expected_keybinds) - } - - #[test] - fn test_multiple_keypress() -> std::io::Result<()> { - let contents = " -super + 5 - alacritty - "; - - let expected_keybinds = - vec![Hotkey::new(evdev::Key::KEY_5, vec![Modifier::Super], String::from("alacritty"))]; - - eval_config_test(contents, expected_keybinds) - } - - #[test] - fn test_keysym_instead_of_modifier() -> std::io::Result<()> { - let contents = " -shift + k + m - notify-send 'Hello world!' - "; - - eval_invalid_config_test(contents, ParseError::InvalidModifier(PathBuf::new(), 2)) - } - - #[test] - fn test_modifier_instead_of_keysym() -> std::io::Result<()> { - let contents = " -shift + k + alt - notify-send 'Hello world!' - "; - - eval_invalid_config_test(contents, ParseError::InvalidModifier(PathBuf::new(), 2)) - } - - #[test] - fn test_unfinished_plus_sign() -> std::io::Result<()> { - let contents = " - - -shift + alt + - notify-send 'Hello world!' - "; - - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 4)) - } - - #[test] - fn test_plus_sign_at_start() -> std::io::Result<()> { - let contents = " -+ shift + k - notify-send 'Hello world!' - "; - - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 2)) - } - - #[test] - fn test_common_modifiers() -> std::io::Result<()> { - let contents = " -shift + k - notify-send 'Hello world!' - -control + 5 - notify-send 'Hello world!' - -alt + 2 - notify-send 'Hello world!' - -altgr + i - notify-send 'Hello world!' - -super + z - notify-send 'Hello world!' - "; - - let expected_hotkeys = vec![ - Hotkey::new( - evdev::Key::KEY_K, - vec![Modifier::Shift], - "notify-send 'Hello world!'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_5, - vec![Modifier::Control], - "notify-send 'Hello world!'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Alt], - "notify-send 'Hello world!'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_I, - vec![Modifier::Altgr], - "notify-send 'Hello world!'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_Z, - vec![Modifier::Super], - "notify-send 'Hello world!'".to_string(), - ), - ]; - - eval_config_test(contents, expected_hotkeys) - } - - #[test] - fn test_command_with_many_spaces() -> std::io::Result<()> { - let contents = " -p - xbacklight -inc 10 -fps 30 -time 200 - "; - - let expected_keybinds = vec![Hotkey::new( - evdev::Key::KEY_P, - vec![], - String::from("xbacklight -inc 10 -fps 30 -time 200"), - )]; - - eval_config_test(contents, expected_keybinds) - } - - #[test] - fn test_invalid_keybinding() -> std::io::Result<()> { - let contents = " -p - xbacklight -inc 10 -fps 30 -time 200 - -pesto - xterm - "; - - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 5)) - } - - #[test] - // keysyms not followed by command should be ignored - fn test_no_command() -> std::io::Result<()> { - let contents = " -k - xbacklight -inc 10 -fps 30 -time 200 - -w - - "; - - eval_config_test( - contents, - vec![Hotkey::new( - evdev::Key::KEY_K, - vec![], - "xbacklight -inc 10 -fps 30 -time 200".to_string(), - )], - ) - } - - #[test] - fn test_real_config_snippet() -> std::io::Result<()> { - let contents = " -# reloads sxhkd configuration: -super + Escape - pkill -USR1 -x sxhkd ; sxhkd & - -# Launch Terminal -super + Return - alacritty -t \"Terminal\" -e \"$HOME/.config/sxhkd/new_tmux_terminal.sh\" - -# terminal emulator (no tmux) -super + shift + Return - alacritty -t \"Terminal\" - -# terminal emulator (new tmux session) -alt + Return - alacritty -t \"Terminal\" -e \"tmux\" - -ctrl + 0 - play-song.sh - -super + minus - play-song.sh album - "; - - let expected_result: Vec = vec![ - Hotkey::new( - evdev::Key::KEY_ESC, - vec![Modifier::Super], - String::from("pkill -USR1 -x sxhkd ; sxhkd &"), - ), - Hotkey::new( - evdev::Key::KEY_ENTER, - vec![Modifier::Super], - String::from( - "alacritty -t \"Terminal\" -e \"$HOME/.config/sxhkd/new_tmux_terminal.sh\"", - ), - ), - Hotkey::new( - evdev::Key::KEY_ENTER, - vec![Modifier::Super, Modifier::Shift], - String::from("alacritty -t \"Terminal\""), - ), - Hotkey::new( - evdev::Key::KEY_ENTER, - vec![Modifier::Alt], - String::from("alacritty -t \"Terminal\" -e \"tmux\""), - ), - Hotkey::new(evdev::Key::KEY_0, vec![Modifier::Control], String::from("play-song.sh")), - Hotkey::new( - evdev::Key::KEY_MINUS, - vec![Modifier::Super], - String::from("play-song.sh album"), - ), - ]; - - eval_config_test(contents, expected_result) - } - - #[test] - fn test_multiline_command() -> std::io::Result<()> { - let contents = " -k - mpc ls | dmenu | \\ - sed -i 's/foo/bar/g' - "; - - let expected_keybind = Hotkey::new( - evdev::Key::KEY_K, - vec![], - String::from("mpc ls | dmenu | sed -i 's/foo/bar/g'"), - ); - - eval_config_test(contents, vec![expected_keybind]) - } - - #[test] - fn test_commented_out_keybind() -> std::io::Result<()> { - let contents = " -#w - gimp - "; - - eval_config_test(contents, vec![]) - } - - // TODO: Write these tests as needed. - - #[test] - fn test_all_alphanumeric() -> std::io::Result<()> { - let symbols: [&str; 36] = [ - "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", - "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", - "8", "9", - ]; - let keysyms: [evdev::Key; 36] = [ - evdev::Key::KEY_A, - evdev::Key::KEY_B, - evdev::Key::KEY_C, - evdev::Key::KEY_D, - evdev::Key::KEY_E, - evdev::Key::KEY_F, - evdev::Key::KEY_G, - evdev::Key::KEY_H, - evdev::Key::KEY_I, - evdev::Key::KEY_J, - evdev::Key::KEY_K, - evdev::Key::KEY_L, - evdev::Key::KEY_M, - evdev::Key::KEY_N, - evdev::Key::KEY_O, - evdev::Key::KEY_P, - evdev::Key::KEY_Q, - evdev::Key::KEY_R, - evdev::Key::KEY_S, - evdev::Key::KEY_T, - evdev::Key::KEY_U, - evdev::Key::KEY_V, - evdev::Key::KEY_W, - evdev::Key::KEY_X, - evdev::Key::KEY_Y, - evdev::Key::KEY_Z, - evdev::Key::KEY_0, - evdev::Key::KEY_1, - evdev::Key::KEY_2, - evdev::Key::KEY_3, - evdev::Key::KEY_4, - evdev::Key::KEY_5, - evdev::Key::KEY_6, - evdev::Key::KEY_7, - evdev::Key::KEY_8, - evdev::Key::KEY_9, - ]; - - let mut contents = String::new(); - for symbol in &symbols { - contents.push_str(&format!("{}\n st\n", symbol)); - } - let contents = &contents; - - let expected_result: Vec = - keysyms.iter().map(|keysym| Hotkey::new(*keysym, vec![], "st".to_string())).collect(); - - eval_config_test(contents, expected_result) - } - - #[test] - fn test_homerow_special_keys_top() -> std::io::Result<()> { - let symbols: [&str; 7] = - ["Escape", "BackSpace", "Return", "Tab", "minus", "equal", "grave"]; - - let keysyms: [evdev::Key; 7] = [ - evdev::Key::KEY_ESC, - evdev::Key::KEY_BACKSPACE, - evdev::Key::KEY_ENTER, - evdev::Key::KEY_TAB, - evdev::Key::KEY_MINUS, - evdev::Key::KEY_EQUAL, - evdev::Key::KEY_GRAVE, - ]; - - let mut contents = String::new(); - for symbol in &symbols { - contents.push_str(&format!("{}\n st\n", symbol)); - } - let contents = &contents; - - let expected_result: Vec = - keysyms.iter().map(|keysym| Hotkey::new(*keysym, vec![], "st".to_string())).collect(); - - eval_config_test(contents, expected_result) - } - - #[test] - fn test_case_insensitive() -> std::io::Result<()> { - let contents = " -Super + SHIFT + alt + a - st -ReTurn - ts - "; - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_A, - vec![Modifier::Super, Modifier::Shift, Modifier::Alt], - "st".to_string(), - ), - Hotkey::new(evdev::Key::KEY_ENTER, vec![], "ts".to_string()), - ], - ) - } - - #[test] - fn test_duplicate_hotkeys() -> std::io::Result<()> { - let contents = " -super + shift + a - st -shift + suPer + A - ts -b - st -B - ts -"; - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_A, - vec![Modifier::Super, Modifier::Shift], - "ts".to_string(), - ), - Hotkey::new(evdev::Key::KEY_B, vec![], "ts".to_string()), - ], - ) - } - - #[test] - fn test_inline_comment() -> std::io::Result<()> { - let contents = " -super + a #comment and comment super - st -super + shift + b - ts #this comment should be handled by shell -" - .to_string(); - eval_config_test( - &contents, - vec![ - Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "st".to_string()), - Hotkey::new( - evdev::Key::KEY_B, - vec![Modifier::Super, Modifier::Shift], - "ts #this comment should be handled by shell".to_string(), - ), - ], - ) - } - - #[test] - fn test_blank_config() -> std::io::Result<()> { - let contents = ""; - - eval_config_test(contents, vec![]) - } - - #[test] - fn test_blank_config_with_whitespace() -> std::io::Result<()> { - let contents = " - - - "; - - eval_config_test(contents, vec![]) - } - - #[test] - fn test_extract_curly_brace() -> std::io::Result<()> { - let keybind_with_curly_brace = "super + {a,b,c}"; - assert_eq!( - extract_curly_brace(keybind_with_curly_brace), - vec!["super + a", "super + b", "super + c",] - ); - let command_with_curly_brace = "bspc node -p {west,south,north,west}"; - assert_eq!( - extract_curly_brace(command_with_curly_brace), - vec![ - "bspc node -p west", - "bspc node -p south", - "bspc node -p north", - "bspc node -p west", - ] - ); - let wrong_format = "super + }a, b, c{"; - assert_eq!(extract_curly_brace(wrong_format), vec![wrong_format]); - let single_sym = "super + {a}"; - assert_eq!(extract_curly_brace(single_sym), vec!["super + a"]); - Ok(()) - } - - #[test] - fn test_curly_brace() -> std::io::Result<()> { - let contents = " -super + {a,b,c} - {firefox, brave, chrome}"; - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "firefox".to_string()), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], "brave".to_string()), - Hotkey::new(evdev::Key::KEY_C, vec![Modifier::Super], "chrome".to_string()), - ], - ) - } - - #[test] - fn test_curly_brace_less_commands() -> std::io::Result<()> { - let contents = " -super + {a,b,c} - {firefox, brave}"; - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "firefox".to_string()), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], "brave".to_string()), - ], - ) - } - - #[test] - fn test_curly_brace_less_keysyms() -> std::io::Result<()> { - let contents = " -super + {a, b} - {firefox, brave, chrome}"; - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "firefox".to_string()), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], "brave".to_string()), - ], - ) - } - - #[test] - fn test_range_syntax() -> std::io::Result<()> { - let contents = " -super + {1-9,0} - bspc desktop -f '{1-9,0}'"; - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Super], - "bspc desktop -f '1'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Super], - "bspc desktop -f '2'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_3, - vec![Modifier::Super], - "bspc desktop -f '3'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_4, - vec![Modifier::Super], - "bspc desktop -f '4'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_5, - vec![Modifier::Super], - "bspc desktop -f '5'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_6, - vec![Modifier::Super], - "bspc desktop -f '6'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_7, - vec![Modifier::Super], - "bspc desktop -f '7'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_8, - vec![Modifier::Super], - "bspc desktop -f '8'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_9, - vec![Modifier::Super], - "bspc desktop -f '9'".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_0, - vec![Modifier::Super], - "bspc desktop -f '0'".to_string(), - ), - ], - ) - } - - #[test] - fn test_range_syntax_ascii_character() -> std::io::Result<()> { - let contents = " -super + {a-c} - {firefox, brave, chrome}"; - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "firefox".to_string()), - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], "brave".to_string()), - Hotkey::new(evdev::Key::KEY_C, vec![Modifier::Super], "chrome".to_string()), - ], - ) - } - - #[test] - fn test_range_syntax_not_ascii() -> std::io::Result<()> { - let contents = " -super + {a-是} - {firefox, brave} - "; - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 2)) - } - - #[test] - fn test_range_syntax_invalid_range() -> std::io::Result<()> { - let contents = " -super + {bc-ad} - {firefox, brave} - "; - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 2)) - } - - #[test] - fn test_ranger_syntax_not_full_range() -> std::io::Result<()> { - let contents = " -super + {a-} - {firefox, brave}"; - eval_invalid_config_test(contents, ParseError::UnknownSymbol(PathBuf::new(), 2)) - } - - #[test] - fn test_none() -> std::io::Result<()> { - let contents = " -super + {_, shift} + b - {firefox, brave}"; - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_B, vec![Modifier::Super], "firefox".to_string()), - Hotkey::new( - evdev::Key::KEY_B, - vec![Modifier::Super, Modifier::Shift], - "brave".to_string(), - ), - ], - ) - } - - #[test] - fn test_multiple_ranges() -> std::io::Result<()> { - let contents = " -super + {shift,alt} + {c,d} - {librewolf, firefox} {--sync, --help} - "; - - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_C, - vec![Modifier::Super, Modifier::Shift], - "librewolf --sync".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_D, - vec![Modifier::Super, Modifier::Shift], - "librewolf --help".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_C, - vec![Modifier::Super, Modifier::Alt], - "firefox --sync".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_D, - vec![Modifier::Super, Modifier::Alt], - "firefox --help".to_string(), - ), - ], - ) - } - - #[test] - fn test_multiple_ranges_numbers() -> std::io::Result<()> { - let contents = " -{control,super} + {1-3} - {notify-send, echo} {hello,how,are} - "; - - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Control], - "notify-send hello".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Control], - "notify-send how".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_3, - vec![Modifier::Control], - "notify-send are".to_string(), - ), - Hotkey::new(evdev::Key::KEY_1, vec![Modifier::Super], "echo hello".to_string()), - Hotkey::new(evdev::Key::KEY_2, vec![Modifier::Super], "echo how".to_string()), - Hotkey::new(evdev::Key::KEY_3, vec![Modifier::Super], "echo are".to_string()), - ], - ) - } - - #[test] - fn test_bspwm_multiple_curly_brace() -> std::io::Result<()> { - let contents = " -super + {_,shift + }{h,j,k,l} - bspc node -{f,s} {west,south,north,east}"; - - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_H, - vec![Modifier::Super], - "bspc node -f west".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_J, - vec![Modifier::Super], - "bspc node -f south".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_K, - vec![Modifier::Super], - "bspc node -f north".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_L, - vec![Modifier::Super], - "bspc node -f east".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_H, - vec![Modifier::Super, Modifier::Shift], - "bspc node -s west".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_J, - vec![Modifier::Super, Modifier::Shift], - "bspc node -s south".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_K, - vec![Modifier::Super, Modifier::Shift], - "bspc node -s north".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_L, - vec![Modifier::Super, Modifier::Shift], - "bspc node -s east".to_string(), - ), - ], - ) - } - - #[test] - fn test_longer_multiple_curly_brace() -> std::io::Result<()> { - let contents = " -super + {_, ctrl +} {_, shift +} {1-2} - riverctl {set, toggle}-{focused, view}-tags {1-2}"; - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Super], - "riverctl set-focused-tags 1".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Super], - "riverctl set-focused-tags 2".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Super, Modifier::Control], - "riverctl toggle-focused-tags 1".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Super, Modifier::Control], - "riverctl toggle-focused-tags 2".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Super, Modifier::Shift], - "riverctl set-view-tags 1".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Super, Modifier::Shift], - "riverctl set-view-tags 2".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_1, - vec![Modifier::Super, Modifier::Control, Modifier::Shift], - "riverctl toggle-view-tags 1".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_2, - vec![Modifier::Super, Modifier::Control, Modifier::Shift], - "riverctl toggle-view-tags 2".to_string(), - ), - ], - ) - } - - #[test] - fn test_period_binding() -> std::io::Result<()> { - let contents = " -super + {comma, period} - riverctl focus-output {previous, next}"; - - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_COMMA, - vec![Modifier::Super], - "riverctl focus-output previous".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_DOT, - vec![Modifier::Super], - "riverctl focus-output next".to_string(), - ), - ], - ) - } - - #[test] - fn test_period_escape_binding() -> std::io::Result<()> { - let contents = " -super + {\\,, .} - riverctl focus-output {previous, next}"; - - eval_config_test( - contents, - vec![ - Hotkey::new( - evdev::Key::KEY_COMMA, - vec![Modifier::Super], - "riverctl focus-output previous".to_string(), - ), - Hotkey::new( - evdev::Key::KEY_DOT, - vec![Modifier::Super], - "riverctl focus-output next".to_string(), - ), - ], - ) - } - - #[test] - fn test_prefix() -> std::io::Result<()> { - let contents = " -super + @1 - 1 -super + ~2 - 2 -super + ~@3 - 3 -super + @~4 - 4"; - - eval_config_test( - contents, - vec![ - Hotkey::new(evdev::Key::KEY_1, vec![Modifier::Super], "1".to_string()).on_release(), - Hotkey::new(evdev::Key::KEY_2, vec![Modifier::Super], "2".to_string()).send(), - Hotkey::new(evdev::Key::KEY_3, vec![Modifier::Super], "3".to_string()) - .on_release() - .send(), - Hotkey::new(evdev::Key::KEY_4, vec![Modifier::Super], "4".to_string()) - .on_release() - .send(), - ], - ) - } - - #[test] - fn test_override() -> std::io::Result<()> { - let contents = " -super + a - 1 -super + a - 2"; - eval_config_test( - contents, - vec![Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Super], "2".to_string())], - ) - } - - #[test] - fn test_any_modifier() -> std::io::Result<()> { - let contents = " -any + a - 1"; - eval_config_test( - contents, - vec![Hotkey::new(evdev::Key::KEY_A, vec![Modifier::Any], "1".to_string())], - ) - } -} - -mod test_config_display { - use crate::config::{Error, ParseError}; - use std::io; - use std::path::PathBuf; - - #[test] - fn test_display_io_error() { - let error = Error::Io(io::Error::from(io::ErrorKind::UnexpectedEof)); - - if !format!("{}", error).contains("unexpected end of file") { - panic!("Error message was '{}", error); - } - } - - #[test] - fn test_display_unknown_symbol_error() { - let error = Error::InvalidConfig(ParseError::UnknownSymbol(PathBuf::new(), 10)); - - assert_eq!( - format!("{}", error), - "Error parsing config file \"\". Unknown symbol at line 10." - ); - } - - #[test] - fn test_display_invalid_modifier_error() { - let error = Error::InvalidConfig(ParseError::InvalidModifier(PathBuf::new(), 25)); - - assert_eq!( - format!("{}", error), - "Error parsing config file \"\". Invalid modifier at line 25." - ); - } - - #[test] - fn test_invalid_keysm_error() { - let error = Error::InvalidConfig(ParseError::InvalidKeysym(PathBuf::new(), 7)); - - assert_eq!( - format!("{}", error), - "Error parsing config file \"\". Invalid keysym at line 7." - ); - } -}