Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
Note: fail_tests do not get tested right now
  • Loading branch information
SOF3 committed Aug 13, 2019
1 parent 78fa5ee commit d629eec
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 5 deletions.
Binary file added assets/ascii-control.txt
Binary file not shown.
2 changes: 2 additions & 0 deletions assets/ascii-printable.txt
@@ -0,0 +1,2 @@
abcdef
012345
26 changes: 23 additions & 3 deletions codegen/src/lib.rs
Expand Up @@ -19,6 +19,7 @@ extern crate proc_macro;

use std::fs::File;
use std::path::PathBuf;
use std::str::from_utf8;

use libflate::deflate::Encoder;
use proc_macro::{TokenStream};
Expand All @@ -28,13 +29,21 @@ use syn::{Error, LitStr, Result};

#[proc_macro]
pub fn deflate_file(ts: TokenStream) -> TokenStream {
match inner(ts) {
match inner(ts, false) {
Ok(ts) => ts.into(),
Err(err) => err.to_compile_error().into(),
}
}

fn inner(ts: TokenStream) -> Result<impl Into<TokenStream>> {
#[proc_macro]
pub fn deflate_utf8_file(ts: TokenStream) -> TokenStream {
match inner(ts, true) {
Ok(ts) => ts.into(),
Err(err) => err.to_compile_error().into(),
}
}

fn inner(ts: TokenStream, utf8: bool) -> Result<impl Into<TokenStream>> {
let dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let lit = syn::parse::<LitStr>(ts)?;
Expand All @@ -51,8 +60,19 @@ fn inner(ts: TokenStream) -> Result<impl Into<TokenStream>> {
}

let mut file = File::open(target).map_err(emap)?;

let mut encoder = Encoder::new(Vec::<u8>::new());
std::io::copy(&mut file, &mut encoder).map_err(emap)?;
if utf8 {
use std::io::Write;

let mut vec = Vec::<u8>::new();
std::io::copy(&mut file, &mut vec).map_err(emap)?;
from_utf8(&vec).map_err(emap)?;
encoder.write_all(&vec).map_err(emap)?;
} else {
// no need to store the raw buffer; let's avoid storing two buffers
std::io::copy(&mut file, &mut encoder).map_err(emap)?;
}
let bytes = encoder.finish().into_result().map_err(emap)?;

let bytes = bytes.into_iter().map(|byte| Literal::u8_suffixed(byte));
Expand Down
28 changes: 28 additions & 0 deletions fail_tests/009f-str.rs
@@ -0,0 +1,28 @@
// include-flate
// Copyright (C) SOFe
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(proc_macro_hygiene)]

include!("../test_util.rs");

use include_flate::flate;

flate!(pub static DATA: str from "assets/009f.dat");

#[test]
fn test() {
println!("Tests raw");
verify_str("009f.dat", &DATA);
}
34 changes: 34 additions & 0 deletions test_util.rs
@@ -0,0 +1,34 @@
// include-flate
// Copyright (C) SOFe
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs::File;
use std::path::PathBuf;
use std::str::from_utf8;

pub fn read_file(name: &str) -> Vec<u8> {
let path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("assets").join(name);
let mut vec = Vec::<u8>::new();
std::io::copy(&mut File::open(path).unwrap(), &mut vec).unwrap();
vec
}

pub fn verify(name: &str, data: &[u8]) {
assert_eq!(read_file(name), data);
}

pub fn verify_str(name: &str, data: &str) {
// But the file should not have compiled in the first place
assert_eq!(from_utf8(&read_file(name)).expect("File is not encoded"), data);
}
6 changes: 4 additions & 2 deletions tests/raw.rs → tests/009f.rs
Expand Up @@ -15,12 +15,14 @@

#![feature(proc_macro_hygiene)]

include!("../test_util.rs");

use include_flate::flate;

flate!(pub static FFFE: [u8] from "assets/009f.dat");
flate!(pub static DATA: [u8] from "assets/009f.dat");

#[test]
fn test() {
println!("Tests raw");
assert_eq!(*FFFE, &[0x00u8, 0x9Fu8]);
verify("009f.dat", &DATA);
}
27 changes: 27 additions & 0 deletions tests/ascii-control.rs
@@ -0,0 +1,27 @@
// include-flate
// Copyright (C) SOFe
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(proc_macro_hygiene)]

include!("../test_util.rs");

use include_flate::flate;

flate!(pub static DATA: str from "assets/ascii-printable.txt");

#[test]
fn test() {
verify_str("ascii-printable.txt", &DATA);
}
27 changes: 27 additions & 0 deletions tests/ascii-printable.rs
@@ -0,0 +1,27 @@
// include-flate
// Copyright (C) SOFe
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(proc_macro_hygiene)]

include!("../test_util.rs");

use include_flate::flate;

flate!(pub static DATA: str from "assets/ascii-printable.txt");

#[test]
fn test() {
verify_str("ascii-printable.txt", &DATA);
}

0 comments on commit d629eec

Please sign in to comment.