/// Fake data for tests: words, sentences, names, emails and URLs, drawn /// from small built-in tables through `gen`, so a seed gives the same data /// and shrinking makes it shorter and earlier in the tables. import gen import std/list import std/random.{type random} import std/string fn word_table :: -> List { [ "note" , "milk" , "river" , "paper" , "green" , "quiet" , "stone" , "bread" , "light" , "window" , "garden" , "letter" , "market" , "winter" , "orange" , "pencil" , "cloud" , "table" , "music" , "forest" , "harbor" , "candle" , "bridge" , "summer" ] } fn first_names :: -> List { [ "Ada" , "Grace" , "Alan" , "Edsger" , "Barbara" , "Donald" , "Frances" , "John" , "Margaret" , "Ken" ] } fn last_names :: -> List { [ "Lovelace" , "Hopper" , "Turing" , "Dijkstra" , "Liskov" , "Knuth" , "Allen" , "McCarthy" , "Hamilton" , "Thompson" ] } /// One lowercase word. pub fn word :: -> String / { gen.pick (word_table ()) } /// `min` to `max` words, separated by spaces. pub fn words :: ~min: Int ~max: Int -> String / { string.join (gen.list ~min ~max word) " " } /// Three to ten words, the first capitalised, ending with a period. pub fn sentence :: -> String / { capitalised (words ~min: 3 ~max: 10) <> "." } /// Two to five sentences. pub fn paragraph :: -> String / { string.join (gen.list ~min: 2 ~max: 5 sentence) " " } pub fn first_name :: -> String / { gen.pick (first_names ()) } pub fn last_name :: -> String / { gen.pick (last_names ()) } pub fn full_name :: -> String / { first_name () <> " " <> last_name () } /// `first.last@example.com`, lowercase. pub fn email :: -> String / { let name = string.to_lower (first_name ()) <> "." <> string.to_lower (last_name ()) name <> show (gen.int ~low: 0 ~high: 99) <> "@example.com" } /// One to four words joined by hyphens. pub fn slug :: -> String / { string.join (gen.list ~min: 1 ~max: 4 word) "-" } /// `https://example.com/`. pub fn url :: -> String / { "https://example.com/" <> slug () } fn capitalised :: text: String -> String { match string.chars text { [c, ..rest] -> upper c <> string.concat rest, [] -> text } } fn upper :: c: String -> String { let lower = string.chars (gen.lower ()) let upper = string.chars "ABCDEFGHIJKLMNOPQRSTUVWXYZ" match list.find (list.zip lower upper) fn pair { pair.0 == c } { Some(pair) -> pair.1, None -> c } }