1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright (c) 2021 Soni L.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Documentation and comments licensed under CC BY-SA 4.0.
// because we wanna use double underscore (__) for test names
#![allow(non_snake_case)]
use ::std::io::Cursor;
use ::iosonism::args::ArgumentType;
use ::iosonism::args::BoolArgumentType;
use ::iosonism::args::bounded_float;
use ::iosonism::args::bounded_integer;
use ::iosonism::args::float;
use ::iosonism::args::greedy_string;
use ::iosonism::args::integer;
use ::iosonism::args::string;
use ::iosonism::args::word;
use ::iosonism::strcursor::StringReader;
mod common;
use self::common::ErrorCall;
use self::common::ErrorFunc;
use self::common::ErrorPanic;
use self::common::ErrorType;
#[test]
fn test_bool__parse() {
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(
&BoolArgumentType,
&mut Cursor::new("true"),
),
Ok(true),
);
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(
&BoolArgumentType,
&mut Cursor::new("false"),
),
Ok(false),
);
}
#[test]
fn test_i32__parse() {
let mut reader = Cursor::new("15");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&integer::<i32>(), &mut reader),
Ok(15),
);
assert!(!reader.can_read());
}
#[test]
fn test_i32__parse__range() {
let mut reader = Cursor::new("-5");
assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
&bounded_integer(0..100i32),
&mut reader,
).is_err());
struct ErrFn;
impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
fn call(context: &Cursor<&'a str>, ty: ErrorType) {
assert!(matches!(ty, ErrorType::RangeErrori32(..)));
assert_eq!(context.position(), 0);
}
}
}
#[test]
fn test_i64__parse() {
let mut reader = Cursor::new("15");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&integer::<i64>(), &mut reader),
Ok(15),
);
assert!(!reader.can_read());
}
#[test]
fn test_i64__parse__range() {
let mut reader = Cursor::new("-5");
assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
&bounded_integer(0..100i64),
&mut reader,
).is_err());
struct ErrFn;
impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
fn call(context: &Cursor<&'a str>, ty: ErrorType) {
assert!(matches!(ty, ErrorType::RangeErrori64(..)));
assert_eq!(context.position(), 0);
}
}
}
#[test]
fn test_f32__parse() {
let mut reader = Cursor::new("15");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&float::<f32>(), &mut reader),
Ok(15.0),
);
assert!(!reader.can_read());
}
#[test]
fn test_f32__parse__range() {
let mut reader = Cursor::new("-5");
assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
&bounded_float(0.0..100f32),
&mut reader,
).is_err());
struct ErrFn;
impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
fn call(context: &Cursor<&'a str>, ty: ErrorType) {
assert!(matches!(ty, ErrorType::RangeErrorf32(..)));
assert_eq!(context.position(), 0);
}
}
}
#[test]
fn test_f64__parse() {
let mut reader = Cursor::new("15");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(
&float::<f64>(),
&mut reader,
),
Ok(15.0),
);
assert!(!reader.can_read());
}
#[test]
fn test_f64__parse__range() {
let mut reader = Cursor::new("-5");
assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
&bounded_float(0.0..100f64),
&mut reader,
).is_err());
struct ErrFn;
impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
fn call(context: &Cursor<&'a str>, ty: ErrorType) {
assert!(matches!(ty, ErrorType::RangeErrorf64(..)));
assert_eq!(context.position(), 0);
}
}
}
#[test]
fn test_string__parse__word() {
let mut reader = Cursor::new("hello");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&word(), &mut reader),
Ok("hello".into()),
);
}
#[test]
fn test_string__parse__string() {
let mut reader = Cursor::new("\"hello world\"");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&string(), &mut reader),
Ok("hello world".into()),
);
}
#[test]
fn test_string__parse__greedy_string() {
let mut reader = Cursor::new("Hello world! This is a test.");
assert_eq!(
ArgumentType::<(), ErrorPanic>::parse(&greedy_string(), &mut reader),
Ok("Hello world! This is a test.".into()),
);
assert!(!reader.can_read());
}
|