File: test.rs

package info (click to toggle)
rust-goldenfile 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: makefile: 4
file content (80 lines) | stat: -rw-r--r-- 2,202 bytes parent folder | download
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
extern crate goldenfile;

use std::fs;
use std::io::Write;

use goldenfile::Mint;

#[test]
fn binary_match() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file1 = mint.new_goldenfile("binary_match1.bin").unwrap();
    let mut file2 = mint.new_goldenfile("binary_match2.bin").unwrap();

    file1.write_all(b"").unwrap();
    file2.write_all(b"\x00\x01\x02").unwrap();
}

#[test]
fn subdir() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file1 = mint.new_goldenfile("subdir/file1.txt").unwrap();

    write!(file1, "File in subdir").unwrap();
}

#[test]
#[should_panic(expected = "File sizes differ: Old file is 2 bytes, new file is 3 bytes")]
fn binary_size_diff() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file = mint.new_goldenfile("binary_size_diff.bin").unwrap();

    file.write_all(b"\x00\x01\x02").unwrap();
}

#[test]
#[should_panic(expected = "Files differ at byte 3")]
fn binary_content_diff() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file = mint.new_goldenfile("binary_content_diff.bin").unwrap();

    file.write_all(b"\x00\x01\x02").unwrap();
}

#[test]
fn text_match() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file1 = mint.new_goldenfile("match1.txt").unwrap();
    let mut file2 = mint.new_goldenfile("match2.txt").unwrap();

    write!(file1, "Hello world!").unwrap();
    write!(file2, "foobar").unwrap();
}

#[test]
#[should_panic(expected = "foobar")]
fn text_diff() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file1 = mint.new_goldenfile("text_diff1.txt").unwrap();
    let mut file2 = mint.new_goldenfile("text_diff2.txt").unwrap();

    write!(file1, "Hello world!").unwrap();
    write!(file2, "monkeybrains").unwrap();
}

#[test]
#[should_panic(expected = "Path must be relative")]
fn absolute_path() {
    let mut mint = Mint::new("tests/goldenfiles");
    mint.new_goldenfile("/bar").unwrap();
}

#[test]
#[should_panic(expected = "assertion failed")]
fn external_panic() {
    let mut mint = Mint::new("tests/goldenfiles");
    let mut file1 = mint.new_goldenfile("panic.txt").unwrap();

    write!(file1, "new").unwrap();
    assert!(false);
}