Fixed tests

This commit is contained in:
Aaronepower 2016-06-10 22:33:27 +01:00
parent 9a3dbf1a4a
commit b46bf60ab8
14 changed files with 632 additions and 477 deletions

1
Cargo.lock generated
View file

@ -238,7 +238,6 @@ name = "toml"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -3,7 +3,7 @@
# found in the LICENCE-{APACHE, MIT} file.
[package]
name = "tokei"
version = "2.1.3"
version = "3.0.0"
authors = ["Aaronepower <theaaronepower@gmail.com>"]
repository = "https://github.com/Aaronepower/tokei.git"
homepage = "https://aaronepower.github.io/tokei/"
@ -50,5 +50,9 @@ serde_cbor = "0.3.3"
serde_json = "0.7.0"
serde_yaml = "0.2.4"
serializable_enum = "0.3.0"
toml = {version = "0.1.30", features = ["serde"]}
walkdir = "0.1.5"
[dependencies.toml]
version = "0.1.30"
default-features = false
features = ["serde"]

View file

@ -1,7 +1,8 @@
use std::path::PathBuf;
use std::ops::AddAssign;
use utils::*;
use sort::Sort;
use sort::Sort::*;
use stats::Stats;
/// Struct representing a single Language.
@ -29,14 +30,13 @@ pub struct Language {
/// Whether the language supports nested multi line comments or not.
#[serde(skip_deserializing, skip_serializing)]
pub nested: bool,
/// The total number of files from `stats`.
pub total_files: usize,
}
impl Language {
/// Constructs a new empty Language with the comments provided.
///
/// ```
/// # use tokei::*;
/// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]);
/// ```
pub fn new(line_comment: Vec<&'static str>,
@ -53,9 +53,10 @@ impl Language {
/// Convience constructor for creating a language that has no commenting syntax.
///
/// ```
/// # use tokei::*;
/// let json = Language::new_blank();
///
/// assert_eq!(json.line_comment, vec![]);
/// let blank_vec: Vec<&str> = vec![];
/// assert_eq!(json.line_comment, blank_vec);
/// ```
pub fn new_blank() -> Self {
Self::default()
@ -64,6 +65,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as C like languages.
///
/// ```
/// # use tokei::*;
/// let rust = Language::new(vec!["//"], vec![("/*", "*/")]);
/// let c = Language::new_c();
///
@ -81,6 +83,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as ML like languages.
///
/// ```
/// # use tokei::*;
/// let ocaml = Language::new_multi(vec![("(*", "*)")]);
/// let coq = Language::new_func();
///
@ -94,6 +97,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as HTML like languages.
///
/// ```
/// # use tokei::*;
/// let xml = Language::new_multi(vec![("<!--", "-->")]);
/// let html = Language::new_html();
///
@ -107,6 +111,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as Bash.
///
/// ```
/// # use tokei::*;
/// let bash = Language::new_single(vec!["#"]);
/// let yaml = Language::new_hash();
///
@ -120,6 +125,7 @@ impl Language {
/// Convience constructor for creating a language that only has multi line comments.
///
/// ```
/// # use tokei::*;
/// let mustache = Language::new_multi(vec![("{{!", "}}")]);
/// ```
pub fn new_multi(multi_line: Vec<(&'static str, &'static str)>) -> Self {
@ -129,6 +135,7 @@ impl Language {
/// Convience constructor for creating a language that has the same commenting syntax as Prolog.
///
/// ```
/// # use tokei::*;
/// let prolog = Language::new(vec!["%"], vec![("/*", "*/")]);
/// let oz = Language::new_pro();
///
@ -146,6 +153,7 @@ impl Language {
/// Convience constructor for creating a language that only has single line comments.
///
/// ```
/// # use tokei::*;
/// let haskell = Language::new_single(vec!["--"]);
/// ```
pub fn new_single(line_comment: Vec<&'static str>) -> Self {
@ -155,6 +163,7 @@ impl Language {
/// Checks if the language is empty. Empty meaning it doesn't have any statistics.
///
/// ```
/// # use tokei::*;
/// let rust = Language::new_c();
///
/// assert!(rust.is_empty());
@ -166,6 +175,7 @@ impl Language {
/// Checks if the language doesn't contain any comments.
///
/// ```
/// # use tokei::*;
/// let json = Language::new_blank();
///
/// assert!(json.is_blank());
@ -177,6 +187,7 @@ impl Language {
/// Specify if the the language supports nested multi line comments.
///
/// ```
/// # use tokei::*;
/// let mut rust = Language::new(vec!["//"], vec![("/*", "*/")]).nested();
/// assert!(rust.nested);
/// ```
@ -189,9 +200,10 @@ impl Language {
/// panic!'s if given the wrong category.
///
/// ```
/// # use tokei::*;
/// let mut rust = Language::new_c();
/// let foo_stats = Stats::new();
/// let bar_stats = Stats::new();
/// let mut foo_stats = Stats::new("foo");
/// let mut bar_stats = Stats::new("bar");
///
/// foo_stats.code += 20;
/// bar_stats.code += 10;
@ -201,26 +213,24 @@ impl Language {
///
/// assert_eq!(rust.stats, vec![bar_stats.clone(), foo_stats.clone()]);
///
/// rust.sort_by(CODE);
/// rust.sort_by(Sort::Code);
///
/// assert_eq!(rust.stats, vec![foo_stats, bar_stats]);
///
/// ```
pub fn sort_by(&mut self, category: &str) {
pub fn sort_by(&mut self, category: Sort) {
match category {
BLANKS => self.stats.sort_by(|a, b| b.blanks.cmp(&a.blanks)),
COMMENTS => self.stats.sort_by(|a, b| b.comments.cmp(&a.comments)),
CODE => self.stats.sort_by(|a, b| b.code.cmp(&a.code)),
FILES => self.stats.sort_by(|a, b| a.name.cmp(&b.name)),
LINES => self.stats.sort_by(|a, b| b.lines.cmp(&a.lines)),
_ => unreachable!(),
Blanks => self.stats.sort_by(|a, b| b.blanks.cmp(&a.blanks)),
Comments => self.stats.sort_by(|a, b| b.comments.cmp(&a.comments)),
Code => self.stats.sort_by(|a, b| b.code.cmp(&a.code)),
Files => self.stats.sort_by(|a, b| a.name.cmp(&b.name)),
Lines => self.stats.sort_by(|a, b| b.lines.cmp(&a.lines)),
}
}
}
impl AddAssign for Language {
fn add_assign(&mut self, rhs: Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
@ -231,7 +241,6 @@ impl AddAssign for Language {
impl<'a> AddAssign<&'a Language> for Language {
fn add_assign(&mut self, rhs: &'a Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;
@ -242,7 +251,6 @@ impl<'a> AddAssign<&'a Language> for Language {
impl<'a> AddAssign<&'a mut Language> for Language {
fn add_assign(&mut self, rhs: &mut Self) {
self.total_files += rhs.total_files;
self.lines += rhs.lines;
self.comments += rhs.comments;
self.blanks += rhs.blanks;

View file

@ -161,8 +161,6 @@ serializable_enum! {
Yaml,
/// Zsh
Zsh,
#[doc(hidden)]
__Total,
}
LanguageTypeVisitor
}
@ -254,7 +252,6 @@ impl_as_ref_from_str! {
Xml => "XML",
Yaml => "YAML",
Zsh => "Zsh",
__Total => "Total",
}
Error::Parse
}
@ -264,6 +261,7 @@ impl LanguageType {
/// Returns the display name of a language.
///
/// ```
/// # use tokei::*;
/// let bash = LanguageType::Bash;
///
/// assert_eq!(bash.name(), "BASH");
@ -344,16 +342,16 @@ impl LanguageType {
Xml => "XML",
Yaml => "YAML",
Zsh => "Zsh",
__Total => "Total",
}
}
/// Get language from it's file extension.
///
/// ```
/// let rust = LanguageType::from_extension("rs");
/// ```no_run
/// # use tokei::*;
/// let rust = LanguageType::from_extension("./main.rs");
///
/// assert_eq!(rust, LanguageType::Rust);
/// assert_eq!(rust, Some(LanguageType::Rust));
/// ```
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
if let Some(extension) = get_extension(entry) {

View file

@ -12,6 +12,7 @@ use std::ops::{AddAssign, Deref, DerefMut};
use serde_cbor;
use serde_json;
use serde_yaml;
use toml;
use rayon::prelude::*;
use utils::*;
@ -28,6 +29,22 @@ pub struct Languages {
impl Languages {
/// Creates a `Languages` struct from cbor.
///
/// ```
/// # extern crate tokei;
/// # use tokei::*;
/// # extern crate rustc_serialize;
/// # use rustc_serialize::hex::FromHex;
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e7473\
/// 0065737461747381a566626c616e6b730564636f64650c68636f6d6d656e74730065\
/// 6c696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e\
/// 6573116b746f74616c5f66696c657301";
///
/// let mut languages = Languages::from_cbor(&*cbor.from_hex().unwrap()).unwrap();
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// # }
/// ```
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
let map = try!(serde_cbor::from_slice(cbor.into()));
@ -38,9 +55,26 @@ impl Languages {
/// Creates a `Languages` struct from json.
///
/// ```
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#;
/// let languages = Languages::from_json(&json);
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12)
/// # use tokei::*;
/// let json = r#"{
/// "Rust": {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "stats": [
/// {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "lines": 17,
/// "name": ".\\src\\lib\\build.rs"
/// }
/// ],
/// "lines": 17
/// }
/// }"#;
/// let mut languages = Languages::from_json(json.as_bytes()).unwrap();
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// ```
pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> serde_json::Result<Self> {
let map = try!(serde_json::from_slice(json.into()));
@ -50,27 +84,27 @@ impl Languages {
/// Creates a `Languages` struct from json.
///
/// ```
/// let yaml = r#"
/// ```no_run
/// # use tokei::*;
/// let yaml = r#"\
/// ---
/// "Rust":
/// "blanks": 5
/// "code": 12
/// "comments": 0
/// "lines": 17
/// "stats":
/// Rust:
/// blanks: 5
/// code: 12
/// comments: 0
/// lines: 17
/// stats:
/// -
/// "blanks": 5
/// "code": 12
/// "comments": 0
/// "lines": 17
/// "name": ".\\src\\lib\\build.rs"
/// "total_files": 1
/// blanks: 5
/// code: 12
/// comments: 0
/// lines: 17
/// name: .\src\lib\build.rs
/// "#;
///
/// let languages = Languages::from_yaml(&yaml);
/// let mut languages = Languages::from_yaml(yaml.as_bytes()).unwrap();
///
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12)
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// ```
pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> serde_yaml::Result<Self> {
let map = try!(serde_yaml::from_slice(yaml.into()));
@ -93,8 +127,9 @@ impl Languages {
/// to ignore paths containing them.
///
/// ```no_run
/// let languages = Languages::new();
/// languages.get_statistics(&vec!["."], &vec![".git", "target"]);
/// # use tokei::*;
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["."], &*vec![".git", "target"]);
///
/// println!("{:?}", languages);
/// ```
@ -111,7 +146,6 @@ impl Languages {
return;
}
language.total_files = language.files.len();
let is_fortran = name == &FortranModern || name == &FortranLegacy;
let files: Vec<_> = language.files.drain(..).collect();
@ -202,6 +236,7 @@ impl Languages {
/// Constructs a new, blank `Languages`.
///
/// ```
/// # use tokei::*;
/// let languages = Languages::new();
/// ```
pub fn new() -> Self {
@ -293,7 +328,21 @@ impl Languages {
Languages { inner: map }
}
fn remove_empty(&self) -> BTreeMap<LanguageType, Language> {
/// Creates a new map that only contains non empty languages.
///
/// ```
/// use tokei::*;
/// use std::collections::BTreeMap;
///
/// let mut languages = Languages::new();
/// languages.get_statistics(vec!["doesnt/exist"], vec![".git"]);
///
/// let empty_map = languages.remove_empty();
/// let new_map: BTreeMap<LanguageType, Language> = BTreeMap::new();
///
/// assert_eq!(empty_map, new_map);
/// ```
pub fn remove_empty(&self) -> BTreeMap<LanguageType, Language> {
let mut map = BTreeMap::new();
for (name, language) in &self.inner {
@ -307,18 +356,22 @@ impl Languages {
/// Converts `Languages` to CBOR.
///
/// ```no_run
/// extern crate tokei;
/// # use tokei::*;
/// extern crate rustc_serialize;
/// use rustc_serialize::ToHex;
/// use rustc_serialize::hex::ToHex;
///
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\
/// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\
/// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\
/// 3116b746f74616c5f66696c657301";
///
/// let languages = Languages::new();
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
///
/// assert_eq!(cbor, languages.to_cbor().to_hex());
/// assert_eq!(cbor, languages.to_cbor().unwrap().to_hex());
/// # }
/// ```
pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
serde_cbor::to_vec(&self.remove_empty())
@ -327,20 +380,42 @@ impl Languages {
/// Converts `Languages` to JSON.
///
/// ```no_run
/// # use tokei::*;
///
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#;
/// let languages = Languages::new();
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
/// let json = r#"{
/// "Rust": {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "stats": [
/// {
/// "blanks": 5,
/// "code": 12,
/// "comments": 0,
/// "lines": 17,
/// "name": ".\\src\\lib\\build.rs"
/// }
/// ],
/// "lines": 17
/// }
/// }"#;
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
///
/// assert_eq!(json, languages.to_json());
/// assert_eq!(json, languages.to_json().unwrap());
/// ```
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(&self.remove_empty())
}
pub fn to_toml(&self) -> String {
toml::encode_str(&self.remove_empty())
}
/// Converts `Languages` to YAML.
///
/// ```no_run
///
/// # use tokei::*;
/// let yaml = r#"
/// ---
/// "Rust":
@ -354,12 +429,11 @@ impl Languages {
/// "code": 12
/// "comments": 0
/// "lines": 17
/// "name": ".\\src\\lib\\build.rs"
/// "total_files": 1"#;
/// let languages = Languages::new();
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
/// "name": ".\\src\\lib\\build.rs"#;
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
///
/// assert_eq!(yaml, languages.to_yaml());
/// assert_eq!(yaml, languages.to_yaml().unwrap());
pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.remove_empty())
}

View file

@ -4,9 +4,47 @@
unstable_features,
unused_import_braces)]
//! # Tokei: Code Analysis Library([For the binary](https://github.com/Aaronepower/tokei/))
//! # Tokei: Code Analysis Library
//!
//! Tokei is a code analysis library powering the application of the same name.
//! A simple, effcient library for analysing code in directories.[_For the binary_](https://github.com/Aaronepower/tokei/)
//!
//! ## How to use
//!
//! Tokei provides both `Languages` a map of existing programming languages and `Language` for creating custom languages.
//!
//! ### Example(Get total lines of code from all rust files in current directory, and all subdirectories)
//!
//! ```no_run
//! extern crate tokei;
//!
//! use std::collections::BTreeMap;
//! use std::fs::File;
//! use std::io::Read;
//!
//! use tokei::{Languages, LanguageType};
//!
//! fn main() {
//! // The paths to search. Accepts absolute, relative, and glob paths.
//! let paths = vec!["**/*.rs"];
//! // Exclude any path that contains any of these strings.
//! let excluded = vec!["target", ".git"];
//!
//! // Create new Languages
//! let mut languages = Languages::new();
//!
//! // Get statistics
//! languages.get_statistics(&*paths, &*excluded);
//!
//! // Remove empty languages
//! let language_map = languages.remove_empty();
//!
//! // Get Rust from statistics
//! let rust = language_map.get(&LanguageType::Rust).unwrap();
//!
//! // Print the number of lines that were code.
//! println!("Lines of code: {}", rust.code);
//! }
//! ```
#[macro_use]
extern crate maplit;

View file

@ -1,10 +1,9 @@
#[macro_use]
#![allow(missing_docs)]
mod utils;
mod language;
mod stats;
mod sort;
pub use language::{LanguageType, Languages, Language};
pub use stats::Stats;
pub use utils::consts;
pub use sort::Sort;

28
src/lib/sort.rs Normal file
View file

@ -0,0 +1,28 @@
use std::borrow::Cow;
/// Used for sorting languages.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Sort {
/// Sort by number blank lines.
Blanks,
/// Sort by number comments lines.
Comments,
/// Sort by number code lines.
Code,
/// Sort by number files lines.
Files,
/// Sort by number of lines.
Lines,
}
impl<'a> From<Sort> for Cow<'a, Sort> {
fn from(from: Sort) -> Self {
Cow::Owned(from)
}
}
impl<'a> From<&'a Sort> for Cow<'a, Sort> {
fn from(from: &'a Sort) -> Self {
Cow::Borrowed(from)
}
}

View file

@ -20,6 +20,7 @@ impl Stats {
/// Create a new `Stats` from a file path.
///
/// ```
/// # use tokei::*;
/// let stats = Stats::new("src/main.rs");
/// ```
pub fn new<S: Into<String>>(name: S) -> Self {

View file

@ -1,6 +0,0 @@
pub const BLANKS: &'static str = "blanks";
pub const COMMENTS: &'static str = "comments";
pub const CODE: &'static str = "code";
pub const FILES: &'static str = "files";
pub const LINES: &'static str = "lines";

View file

@ -1,7 +1,5 @@
#[macro_use]
mod macros;
pub mod consts;
pub mod fs;
pub use self::consts::*;
pub use self::fs::*;

View file

@ -7,6 +7,7 @@ extern crate clap;
extern crate serde_cbor;
extern crate serde_json;
extern crate serde_yaml;
extern crate toml;
extern crate rustc_serialize;
extern crate tokei;
@ -23,10 +24,15 @@ use clap::App;
use rustc_serialize::hex::FromHex;
use tokei::{Languages, Language, LanguageType};
use tokei::consts::*;
use tokei::Sort::*;
pub const ROW: &'static str = "-------------------------------------------------------------------\
------------";
pub const BLANKS: &'static str = "blanks";
pub const COMMENTS: &'static str = "comments";
pub const CODE: &'static str = "code";
pub const FILES: &'static str = "files";
pub const LINES: &'static str = "lines";
fn main() {
// Get options at the beginning, so the program doesn't have to make any extra calls to get the
@ -163,11 +169,7 @@ fn main() {
}
}
"json" => print!("{}", languages.to_json().unwrap()),
// "toml" => print!("{}", {
// let encoder = toml::Encoder::new();
// lang_map.encode(&mut encoder).unwrap();
// encoder.toml
// }),
"toml" => print!("{}", languages.to_toml()),
"yaml" => print!("{}", languages.to_yaml().unwrap()),
_ => unreachable!(),
}
@ -175,11 +177,11 @@ fn main() {
for (_, ref mut language) in &mut languages {
match &*sort_category {
BLANKS => language.sort_by(BLANKS),
COMMENTS => language.sort_by(COMMENTS),
CODE => language.sort_by(CODE),
FILES => language.sort_by(FILES),
LINES => language.sort_by(LINES),
BLANKS => language.sort_by(Blanks),
COMMENTS => language.sort_by(Comments),
CODE => language.sort_by(Code),
FILES => language.sort_by(Files),
LINES => language.sort_by(Lines),
_ => unreachable!(),
}
}
@ -215,7 +217,13 @@ fn main() {
if !files_option {
println!("{}", ROW);
}
print_language(&total, LanguageType::__Total);
println!(" {: <18} {: >6} {:>12} {:>12} {:>12} {:>12}",
"Total",
total.stats.len(),
total.lines,
total.code,
total.comments,
total.blanks);
println!("{}", ROW);
}
}
@ -231,7 +239,13 @@ pub fn convert_input(contents: String) -> Option<BTreeMap<LanguageType, Language
Some(result)
} else if let Ok(result) = serde_yaml::from_str(&*contents) {
Some(result)
} else if let Ok(result) = serde_cbor::from_slice(&*contents.from_hex().unwrap()) {
} else if let Ok(hex) = contents.from_hex() {
if let Ok(result) = serde_cbor::from_slice(&*hex) {
Some(result)
} else {
None
}
} else if let Some(result) = toml::decode_str(&*contents) {
Some(result)
} else {
None
@ -243,7 +257,7 @@ fn print_language<'a, C>(language: &'a Language, name: C)
{
println!(" {: <18} {: >6} {:>12} {:>12} {:>12} {:>12}",
name.into().name(),
language.total_files,
language.stats.len(),
language.lines,
language.code,
language.comments,