1
use forms::{FormValues, REPORT_BALANCE};
2
use nomiscript::{Value, format_value};
3
use rpc::chart::{ChartFormat, ChartLook};
4

            
5
use super::{ChartRequest, chart_call, png_from_literal};
6

            
7
#[test]
8
1
fn a_bytes_literal_decodes_to_its_bytes() {
9
1
    let literal = format_value(&Value::Bytes(vec![0x89, b'P', b'N', b'G', 0, 255]));
10
1
    assert_eq!(
11
1
        png_from_literal(&literal).expect("bytes"),
12
1
        vec![0x89, b'P', b'N', b'G', 0, 255]
13
    );
14
1
}
15

            
16
#[test]
17
1
fn a_reply_that_is_not_bytes_is_an_error() {
18
1
    let err = png_from_literal("\"text\"").expect_err("not bytes");
19
1
    assert!(err.to_string().contains("not a PNG"), "{err}");
20
1
    let err = png_from_literal("(((").expect_err("unparsable");
21
1
    assert!(err.to_string().contains("not a PNG"), "{err}");
22
1
}
23

            
24
#[test]
25
1
fn the_chart_call_carries_form_kind_format_and_size() {
26
1
    let request = ChartRequest {
27
1
        kind: "bar",
28
1
        format: ChartFormat::Png,
29
1
        size: (640, 480),
30
1
        look: ChartLook::default(),
31
1
    };
32
1
    assert_eq!(
33
1
        chart_call(&REPORT_BALANCE, &FormValues::default(), &request),
34
1
        r#"(report-chart "report-balance" "()" :kind "bar" :format "png" :width 640 :height 480 :text-px 9 :background -1)"#
35
    );
36
1
}
37

            
38
#[test]
39
1
fn the_chart_call_carries_the_look_as_text_height_and_rgb_integer() {
40
1
    let request = ChartRequest {
41
1
        kind: "bar",
42
1
        format: ChartFormat::Png,
43
1
        size: (640, 480),
44
1
        look: ChartLook {
45
1
            text_px: 18,
46
1
            background: Some([0x00, 0x00, 0xaa]),
47
1
        },
48
1
    };
49
1
    let call = chart_call(&REPORT_BALANCE, &FormValues::default(), &request);
50
1
    assert!(call.ends_with(":text-px 18 :background 170)"), "{call}");
51
1
    let white = ChartRequest {
52
1
        look: ChartLook {
53
1
            text_px: 18,
54
1
            background: Some([255; 3]),
55
1
        },
56
1
        ..request
57
1
    };
58
1
    let call = chart_call(&REPORT_BALANCE, &FormValues::default(), &white);
59
1
    assert!(call.ends_with(":background 16777215)"), "{call}");
60
1
}