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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::setup;
use pretty_assertions::assert_eq;
use std::borrow::Cow;
use std::fmt::Debug;
use std::io::Cursor;
use std::ops::Deref;

/// Assert that they share the same AsRef somewhere.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///   let my_string = "abc";
///
///   // When there is more than one AsRef possible, say which one.
///   assert::ref_equal::<str>(&my_string, &"abc");
///   assert::ref_equal::<str>(&my_string.to_string(), &"abc".to_string());
///
///   // When there is only one AsRef possible, the types are inferred.
///   assert::ref_equal(&my_string, &b"abc");
/// }
/// ```
#[track_caller]
pub fn ref_equal<A>(a: &(impl AsRef<A> + Debug), b: &(impl AsRef<A> + Debug))
where
  A: Debug + PartialEq + ?Sized,
{
  assert_eq!(a.as_ref(), b.as_ref());
}

/// Asserts two values are equal using PartialEq, allowing for different
/// types to be compared.
///
/// Error message will show the values that were compared using
/// `pretty_assertions` crate.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///   let result = "abc";
///   equal(result, "abc");
///   equal(&result, &"abc");
///   equal(5, 5);
///   equal(&5, &5);
/// }
/// ```
#[track_caller]
pub fn equal<E, R>(a: E, b: R)
where
  E: Debug + PartialEq + PartialEq<R>,
  R: Debug + PartialEq,
{
  assert_eq!(a, b, "Expected {:?} to equal {:?}.", a, b);
}

/// Asserts two values are not equal using PartialEq, allowing for
/// different types to be compared.
///
/// Error message will show the values that were compared using
/// `pretty_assertions` crate.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///   let result = "abc";
///   not_equal(result, "def");
///   not_equal(result.as_bytes(), b"bcd");
/// }
#[track_caller]
pub fn not_equal<E, R>(a: E, b: R)
where
  E: Debug + PartialEq + PartialEq<R>,
  R: Debug + PartialEq,
{
  assert_ne!(a, b, "Expected {:?} to not equal {:?}.", a, b);
}

/// More specific than assert::equal, must be for AsRef<[u8]>. On failure,
/// the output message will show the hex values of the bytes for easier
/// debugging of longer byte arrays.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = vec![0x01, 0x0E, 0xF3];
///  assert::equal_bytes(&result, &[0x01, 0x0E, 0xF3]);
/// }
#[track_caller]
pub fn equal_bytes<R, E>(a: &R, b: &E)
where
  R: AsRef<[u8]> + ?Sized,
  E: AsRef<[u8]> + ?Sized,
{
  assert_eq!(
    a.as_ref(),
    b.as_ref(),
    "Expected {:02x?} to equal {:02x?}.",
    a.as_ref(),
    b.as_ref()
  );
}

/// Asserts that the value is equal to the contents of the file. Works
/// for anything that implements AsRef<[u8]>. This is useful for testing
/// against large fixtures. The file is read into memory and compared
/// against the value.
///
/// The file is not read until the assertion is run, preventing side
/// effects from reading the file during test setup or teardown, or from
/// affecting assertions earlier in the test.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result1 = "some file contents";
///  assert::equal_file_contents(&result, "./fixtures/test_file1");
///
///  // Works for anything that implements AsRef<[u8]>
///  let result2 = vec![0x01, 0x0E, 0xF3];
///  assert::equal_file_contents(&result, "./fixtures/test_file2");
/// }
/// ```
///
#[track_caller]
pub fn equal_file_contents<R>(a: &R, path: &str)
where
  R: AsRef<[u8]> + ?Sized,
{
  let expected = setup::get_file_contents(path).unwrap();
  ref_equal(&a.as_ref(), &expected);
}

/// More specific than assert::equal, must be for AsRef<[u8]>.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = vec![0x01, 0x0E, 0xF3];
///  assert::equal_hex_bytes(&result, "010EF3");
///  // or
///  assert::equal_hex_bytes(&result, "010ef3");
/// }
/// ```
#[track_caller]
pub fn equal_hex_bytes<R>(a: &R, b: &str)
where
  R: AsRef<[u8]> + ?Sized,
{
  let value = hex::encode(a.as_ref());
  assert_eq!(value, b, "Expected {} to equal {}.", value, b);
}

/// Assert that the value is some.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Some("abc");
///  assert::some(&result);
/// }
/// ```
#[track_caller]
pub fn some<T>(a: &Option<T>)
where
  T: Debug,
{
  assert!(a.is_some(), "Expected to be some: {:?}", a);
}

/// Asserts that the value is some and returns the value.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Some("abc");
///  let some = assert::some_into(result);
///  assert::equal(some, "abc");
/// }
/// ```
#[track_caller]
pub fn some_into<T>(a: Option<T>) -> T
where
  T: Debug,
{
  assert!(a.is_some(), "Expected to be some: {:?}", a);
  a.unwrap()
}

/// Assert that the value is none.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = None::<&str>;
///  assert::none(&result);
/// }
/// ```
#[track_caller]
pub fn none<T>(a: &Option<T>)
where
  T: Debug,
{
  assert!(a.is_none(), "Expected to be none: {:?}", a);
}

/// Assert that the value is ok.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Ok("abc");
///  assert::ok(&result);
/// }
/// ```
#[track_caller]
pub fn ok<T, E>(a: &Result<T, E>)
where
  T: Debug,
  E: Debug,
{
  assert!(a.is_ok(), "Expected to be ok: {:?}", a);
}

/// Asserts that the value is ok and returns the value.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Ok("abc");
///  let ok = assert::ok_into(a);
///  assert::equal(ok, "abc");
/// }
///
#[track_caller]
pub fn ok_into<T, E>(a: Result<T, E>) -> T
where
  T: Debug,
  E: Debug,
{
  assert!(a.is_ok(), "Expected to be ok: {:?}", a);
  a.unwrap()
}

/// Assert that the value is err.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Err("abc");
///  assert::err(&result);
/// }
/// ```
#[track_caller]
pub fn err<T, E>(a: &Result<T, E>)
where
  T: Debug,
  E: Debug,
{
  assert!(a.is_err(), "Expected to be err: {:?}", a);
}

/// Asserts that the value is err and returns the value.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let result = Err("abc");
///  let err = assert::err_into(a);
///  assert::equal(err, "abc");
/// }
#[track_caller]
pub fn err_into<T, E>(a: Result<T, E>) -> E
where
  T: Debug,
  E: Debug,
{
  assert!(a.is_err(), "Expected to be err: {:?}", a);
  a.unwrap_err()
}

/// Asserts that the value is default.
///
/// # Example
///
/// ```
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let a = 0;
///  assert::default(&a);
/// }
/// ```
#[track_caller]
pub fn default<R>(a: &R)
where
  R: Default + Debug + PartialEq + ?Sized,
{
  assert_eq!(a, &R::default());
}

/// Asserts that the value implements Cow and is borrowed.
/// This is useful for testing that a Cow is not cloned.
///
/// # Example
///
/// ```
/// use std::borrow::Cow;
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let a = Cow::Borrowed("abc");
///  assert::borrowed(&a);
/// }
/// ```
#[allow(clippy::ptr_arg)]
#[track_caller]
pub fn borrowed<R>(a: &Cow<'_, R>)
where
  R: Debug + PartialEq + ToOwned + ?Sized,
{
  assert!(
    match a {
      Cow::Borrowed(_) => true,
      Cow::Owned(_) => false,
    },
    "Expected {:?} to be borrowed",
    a.deref(),
  );
}

/// Asserts that the value implements Cow and is owned.
/// This is useful for testing that a Cow is cloned.
///
/// # Example
///
/// ```
/// use std::borrow::Cow;
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let a = Cow::Owned("abc".to_string());
///  assert::owned(&a);
/// }
/// ```
#[allow(clippy::ptr_arg)]
#[track_caller]
pub fn owned<R>(a: &Cow<'_, R>)
where
  R: Debug + PartialEq + ToOwned + ?Sized,
{
  assert!(
    match a {
      Cow::Borrowed(_) => false,
      Cow::Owned(_) => true,
    },
    "Expected {:?} to be owned",
    a.deref(),
  );
}

/// Asserts cursor position has reached the end. This is useful for testing
/// that a cursor has been completely consumed.
///
/// # Example
///
/// ```
/// use std::io::Cursor;
/// use common_testing::assert;
///
/// #[test]
/// fn test_1() {
///  let cursor = Cursor::new("abc");
///  assert::cursor_completely_consumed(&cursor);
/// }
/// ```
#[track_caller]
pub fn cursor_completely_consumed<T>(cursor: &Cursor<T>)
where
  T: AsRef<[u8]>,
{
  assert_eq!(
    cursor.position(),
    cursor.get_ref().as_ref().len() as u64,
    "Cursor was not completely consumed"
  );
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::io::Result;

  #[test]
  fn test_ref_equal() {
    let my_string = "abc";

    // When there is more than one AsRef possible, say which one.
    ref_equal::<str>(&my_string, &"abc");
    ref_equal::<str>(&my_string.to_string(), &"abc".to_string());

    // When there is only one AsRef possible, the types are inferred.
    ref_equal(&my_string, &b"abc");
  }

  #[test]
  fn test_equal() {
    let result = "abc";
    equal(result, "abc");
    equal(&result, &"abc");
    equal(5, 5);
    equal(&5, &5);
  }

  #[test]
  fn test_not_equal() {
    let result = "abc";
    not_equal(result, "def");
    not_equal(result.as_bytes(), b"bcd");
  }

  #[test]
  fn test_equal_bytes() {
    let result = vec![0x01, 0x0E, 0xF3];
    equal_bytes(&result, &[0x01, 0x0E, 0xF3]);
  }

  #[test]
  fn test_equal_file_contents() {
    let result1 = "some file content\n";
    equal_file_contents(&result1, "./fixtures/test.txt");
  }

  #[test]
  fn test_equal_hex_bytes() {
    let result = vec![0x01, 0x0E, 0xF3];
    equal_hex_bytes(&result, "010ef3");
  }

  #[test]
  fn test_some() {
    let result = Some("abc");
    some(&result);
  }

  #[test]
  fn test_some_into() {
    let result = Some("abc");
    let some = some_into(result);
    equal(some, "abc");
  }

  #[test]
  fn test_none() {
    let result = None::<&str>;
    none(&result);
  }

  #[test]
  fn test_ok() {
    let result = Result::Ok("abc");
    ok(&result);
  }

  #[test]
  fn test_ok_into() {
    let result = Result::Ok("abc");
    let ok = ok_into(result);
    equal(ok, "abc");
  }
}