Skip to content

Commit

Permalink
Merge pull request #2 from lbeder/decode-tests
Browse files Browse the repository at this point in the history
Add decode tests
  • Loading branch information
lbeder authored Mar 2, 2019
2 parents 5dbb66c + 148cb27 commit b91e7fd
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
16 changes: 13 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
language: rust
rust:
- stable
- nightly
- nightly-2019-02-08
cache: cargo
before_install:
- sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa
- sudo apt-get update -qq
- sudo apt-get install qt5-default qttools5-dev-tools
before_script:
- rustup component add clippy
- |
if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then
rustup toolchain add nightly-2019-02-08
rustup component add --toolchain nightly-2019-02-08 clippy
else
rustup component add clippy
fi
script:
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test --release
- QT_DIR=/usr/local/opt/qt cargo test --release
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.3.2 (2019-03-02)

- Add decoding tests (but only for tests without text embedding, since resvg is unable to render SVG with text).
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qrcode-cli"
version = "0.3.1"
version = "0.3.2"
authors = ["Leonid Beder <[email protected]>"]
edition = "2018"

Expand All @@ -11,3 +11,12 @@ hex = "0.3.1"
strum = "0.13.0"
strum_macros = "0.13.0"
svgdom = "0.16.0"

[dev-dependencies]
quirc = "0.0.1"
tempfile = "3.0.7"
image = "0.19.0"

[dev-dependencies.resvg]
version = "0.5.0"
features = ["qt-backend"]
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ Now you can build it:
CROSS_COMPILE=x86_64-linux-musl- cargo build --target=x86_64-unknown-linux-musl
```

## Test

To run the tests, you'd need to have the `qt` backend installed:

For Mac OS:

```bash
brew install qt
```

For Linux (Ubuntu):

```bash
sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa
sudo apt-get update -qq
sudo apt-get install qt5-default qttools5-dev-tools
```

You would need to pass `qt` root directory via the `QT_DIR` environment variable. For example:

```bash
QT_DIR=/usr/local/opt/qt cargo test
```

## Example

Let's see few QR code generation examples:
Expand Down
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ echo "Running clippy..."
cargo clippy --all-targets --all-features -- -D warnings

echo "Running tests..."
cargo test --release
QT_DIR=/usr/local/opt/qt cargo test --release

echo "Building v${VERSION} for Mac OS..."
APPLE_RELEASE="target/qrcode-cli-${VERSION}-osx.tgz"
Expand Down
46 changes: 46 additions & 0 deletions src/qrcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,25 @@ impl<'a> QRCode<'a> {

#[cfg(test)]
mod tests {
extern crate image;
extern crate quirc;
extern crate tempfile;

use super::*;
use quirc::{EccLevel, QrCoder};
use resvg::usvg;
use tempfile::Builder;

impl From<ECLevel> for EccLevel {
fn from(ec: ECLevel) -> Self {
match ec {
ECLevel::L => EccLevel::L,
ECLevel::M => EccLevel::M,
ECLevel::Q => EccLevel::Q,
ECLevel::H => EccLevel::H,
}
}
}

#[test]
fn test_default_options() {
Expand All @@ -157,9 +175,37 @@ mod tests {
#[test]
fn $name() {
let (options, data, expected) = $value;

// Encode the data to an SVG and check its contents.
let qr = QRCode::new(&options);
let encoded = qr.encode_svg(data.as_bytes());
assert_eq!(encoded, expected);

// TODO: At the moment, resvg is unable to render SVG with text, so we'd have to skip extended tests for
// this case.
if options.embed {
return;
}

// Convert the SVG to a PNG.
let temp_file = Builder::new()
.suffix(".png")
.tempfile().unwrap();
let opt = resvg::Options::default();
let rtree = usvg::Tree::from_data(encoded.as_bytes(), &opt.usvg).unwrap();
let temp_image = resvg::backend_qt::render_to_image(&rtree, &opt).unwrap();
let temp_path = temp_file.path().to_str().unwrap();
temp_image.save(temp_path);

// Use quirc to decode and verify that the data was properly encoded.
let image = image::open(temp_path).unwrap().to_luma();
let mut quirc = QrCoder::new().unwrap();

let mut codes = quirc.codes(&image, image.width(), image.height()).unwrap();
let code = codes.next().unwrap().unwrap();
assert_eq!(code.payload, data.as_bytes());
assert_eq!(code.ecc_level, EccLevel::from(options.ec_level));
assert!(codes.next().is_none());
}
)*
}
Expand Down

0 comments on commit b91e7fd

Please sign in to comment.