Skip to content

Commit b56b95b

Browse files
committed
Add support of Arbitrary
1 parent 199fa59 commit b56b95b

File tree

11 files changed

+35
-12
lines changed

11 files changed

+35
-12
lines changed

.github/workflows/ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ jobs:
3636
command: test
3737
args: --features dev
3838

39+
- name: cargo test --features arbitrary
40+
uses: actions-rs/cargo@v1
41+
with:
42+
command: test
43+
args: --features arbitrary
44+
3945
rustfmt:
4046
name: Rustfmt
4147
runs-on: ubuntu-latest

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ include = [
2222
hashbrown = "0.12.0"
2323
once_cell = "1.10.0"
2424
enum-map = { version = "2", optional = true }
25+
arbitrary = { version = "1", optional = true, features = ["derive"] }
2526

2627
[dev-dependencies]
2728
serde_json = "1.0.39"
2829
bencher = "0.1.5"
2930
proptest = "0.9.1"
31+
arbtest = "0.1"
3032

3133
[features]
3234
dev = []

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ bench:
88
cargo bench --all-features
99
test:
1010
cargo test --all-features
11+
test-fuzz:
12+
ARBTEST_BUDGET_MS=60000 cargo test --all-features --release

misc/lang.rs.erb

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ use std::str::FromStr;
77

88
use crate::error::ParseError;
99

10-
#[cfg(feature = "enum-map")]
11-
use enum_map::Enum;
12-
1310
/// Represents a language following [ISO 639-3](https://en.wikipedia.org/wiki/ISO_639-3) standard.
14-
#[cfg_attr(feature = "enum-map", derive(Enum))]
11+
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
12+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
1513
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
1614
pub enum Lang {
1715
<% langs.each_with_index do |lang, index| %>

src/core/detector.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::Lang;
2828
/// let lang = detector.detect_lang("Jen la trinkejo fermitis, ni iras tra mallumo kaj pluvo.");
2929
/// assert_eq!(lang, Some(Lang::Epo));
3030
/// ```
31+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
3132
#[derive(Debug, Clone, Default)]
3233
pub struct Detector {
3334
options: Options,

src/core/filter_list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::Lang;
22

3+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
34
#[derive(Debug, Clone)]
45
pub enum FilterList {
56
All,

src/core/method.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::error::ParseError;
22
use std::fmt;
33
use std::str::FromStr;
44

5+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
56
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67
pub enum Method {
78
Trigram,

src/core/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{FilterList, Method};
22

3+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
34
#[derive(Debug, Clone)]
45
pub struct Options {
56
pub(crate) filter_list: FilterList,

src/lang.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ use std::str::FromStr;
77

88
use crate::error::ParseError;
99

10-
#[cfg(feature = "enum-map")]
11-
use enum_map::Enum;
12-
1310
/// Represents a language following [ISO 639-3](https://en.wikipedia.org/wiki/ISO_639-3) standard.
14-
#[cfg_attr(feature = "enum-map", derive(Enum))]
11+
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
12+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
1513
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
1614
pub enum Lang {
1715
/// Esperanto (Esperanto)

src/scripts/script.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ use super::lang_mapping;
55
use crate::error::ParseError;
66
use crate::Lang;
77

8-
#[cfg(feature = "enum-map")]
9-
use enum_map::Enum;
10-
118
/// Represents a writing system (Latin, Cyrillic, Arabic, etc).
12-
#[cfg_attr(feature = "enum-map", derive(Enum))]
9+
#[cfg_attr(feature = "enum-map", derive(::enum_map::Enum))]
10+
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
1311
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
1412
pub enum Script {
1513
// Keep this in alphabetic order (for C bindings)

tests/fuzzing.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[cfg(feature = "arbitrary")]
2+
#[test]
3+
fn test_fuzzing() {
4+
use whatlang::Detector;
5+
use ::arbitrary::{Unstructured, Arbitrary};
6+
7+
fn prop(u: &mut Unstructured) -> ::arbitrary::Result<()> {
8+
let detector = Detector::arbitrary(u)?;
9+
let input = String::arbitrary(u)?;
10+
detector.detect(&input);
11+
Ok(())
12+
}
13+
14+
arbtest::builder().run(prop)
15+
}

0 commit comments

Comments
 (0)