Add web modules (crypto, wiki, define, urban, tinyurl, down) + shared http/json
All checks were successful
ci / check (push) Successful in 48s
All checks were successful
ci / check (push) Successful in 48s
This commit is contained in:
parent
7846f91afe
commit
c3139b4e4d
14 changed files with 794 additions and 130 deletions
38
tests/json.rs
Normal file
38
tests/json.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use rustbot::json::Json;
|
||||
|
||||
#[test]
|
||||
fn parses_object_and_navigates() {
|
||||
let j = Json::parse(r#"{"a":"x","b":{"c":42}}"#).unwrap();
|
||||
assert_eq!(j.get("a").and_then(Json::as_str), Some("x"));
|
||||
assert_eq!(
|
||||
j.get("b").and_then(|v| v.get("c")).and_then(Json::as_f64),
|
||||
Some(42.0)
|
||||
);
|
||||
assert!(j.get("missing").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_nested_arrays() {
|
||||
let j = Json::parse(r#"["q",["t1","t2"],[""],["u1"]]"#).unwrap();
|
||||
assert_eq!(j.idx(0).and_then(Json::as_str), Some("q"));
|
||||
assert_eq!(
|
||||
j.idx(1).and_then(|v| v.idx(1)).and_then(Json::as_str),
|
||||
Some("t2")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_scalars_and_escapes() {
|
||||
assert_eq!(Json::parse("true"), Some(Json::Bool(true)));
|
||||
assert_eq!(Json::parse("null"), Some(Json::Null));
|
||||
assert_eq!(Json::parse("-3.5e2").and_then(|j| j.as_f64()), Some(-350.0));
|
||||
let s = Json::parse(r#""a\"bé""#).unwrap();
|
||||
assert_eq!(s.as_str(), Some("a\"bé"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_garbage() {
|
||||
assert!(Json::parse("{oops").is_none());
|
||||
assert!(Json::parse("").is_none());
|
||||
assert!(Json::parse("[1,2").is_none());
|
||||
}
|
||||
50
tests/tier4.rs
Normal file
50
tests/tier4.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use rustbot::bot::modules::crypto::format_ticker;
|
||||
use rustbot::bot::modules::define::format_define;
|
||||
use rustbot::bot::modules::down::host_of;
|
||||
use rustbot::bot::modules::urban::format_urban;
|
||||
use rustbot::bot::modules::wiki::{format_summary, pick_title};
|
||||
|
||||
#[test]
|
||||
fn crypto_formats_ticker() {
|
||||
let body = r#"{"last":"63836.11","high":"64658.00","low":"63477.46","volume":"1032.2"}"#;
|
||||
let out = format_ticker("btc", body);
|
||||
assert!(out.contains("BTC/USD: $63836.11"), "{out}");
|
||||
assert!(out.contains("high $64658.00"), "{out}");
|
||||
assert!(out.contains("low $63477.46"), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wiki_picks_title_and_formats_summary() {
|
||||
let os = r#"["Rust programming",["Rust (programming language)"],[""],["https://en.wikipedia.org/wiki/Rust_(programming_language)"]]"#;
|
||||
assert_eq!(
|
||||
pick_title(os).as_deref(),
|
||||
Some("Rust (programming language)")
|
||||
);
|
||||
let sum = r#"{"title":"Rust (programming language)","extract":"Rust is a language."}"#;
|
||||
assert_eq!(
|
||||
format_summary(sum),
|
||||
"Rust (programming language) — Rust is a language."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn define_formats_first_meaning() {
|
||||
let body = r#"[{"word":"hello","meanings":[{"partOfSpeech":"noun","definitions":[{"definition":"a greeting"}]}]}]"#;
|
||||
assert_eq!(format_define("hello", body), "hello (noun): a greeting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn urban_formats_and_strips_brackets() {
|
||||
let body = "{\"list\":[{\"word\":\"irc\",\"definition\":\"[Internet] Relay\\r\\nChat\"}]}";
|
||||
let out = format_urban("irc", body);
|
||||
assert!(out.starts_with("irc: Internet Relay"), "{out}");
|
||||
assert!(!out.contains('['), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn down_parses_host() {
|
||||
assert_eq!(host_of("https://example.com/path?x=1"), "example.com");
|
||||
assert_eq!(host_of("example.com:8080"), "example.com");
|
||||
assert_eq!(host_of("user@host.net"), "host.net");
|
||||
assert_eq!(host_of("//foo.org/"), "foo.org");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue