Files
plak/Tests/PlakCoreTests/LoremTextTests.swift
T

165 lines
6.9 KiB
Swift

import Foundation
import PlakCore
import Testing
/// A repeatable generator, so a failing seed can be run again.
struct SeededGenerator: RandomNumberGenerator {
var state: UInt64
mutating func next() -> UInt64 {
state &+= 0x9E37_79B9_7F4A_7C15
var z = state
z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9
z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB
return z ^ (z >> 31)
}
}
@Suite struct LoremTextTests {
private func picks(_ count: Int = 200) throws -> [LoremText] {
let sentences = LoremText.sentences
return try (0..<UInt64(count)).map { seed in
var generator = SeededGenerator(state: seed)
return try #require(LoremText.pick(from: sentences, using: &generator), "seed: \(seed)")
}
}
@Test("lorem.txt loads from the resource bundle with at least 200 lines, some of exactly 16 words")
func fileLoads() {
#expect(LoremText.fileURL() != nil)
#expect(LoremText.sentences.count >= 200)
#expect(LoremText.sentences.contains { LoremText.wordCount($0) == 16 })
}
@Test("the opening sentence has exactly 16 words")
func openingWords() throws {
for lorem in try picks() {
#expect(LoremText.wordCount(lorem.opening) == 16)
}
}
@Test("there are two paragraphs of 3 to 5 sentences each")
func paragraphSizes() throws {
for lorem in try picks() {
#expect(lorem.paragraphs.count == 2)
for paragraph in lorem.paragraphs {
#expect((3...5).contains(paragraph.count))
}
}
}
@Test("no line of the file shows twice in one insertion")
func noRepeats() throws {
for lorem in try picks() {
let all = [lorem.opening] + lorem.paragraphs.flatMap { $0 }
#expect(Set(all).count == all.count)
#expect(all.allSatisfy(LoremText.sentences.contains))
}
}
@Test("the text is six blocks: sentence, two paragraphs, phones, addresses, e-mails, each after one blank line")
func layout() throws {
for lorem in try picks(50) {
let parts = lorem.text.components(separatedBy: "\n\n")
#expect(parts.count == 6)
#expect(!lorem.text.contains("\n\n\n"))
#expect(parts[0] == lorem.opening)
#expect(parts[1] == lorem.paragraphs[0].joined(separator: " "))
#expect(parts[2] == lorem.paragraphs[1].joined(separator: " "))
#expect(!parts[1].contains("\n") && !parts[2].contains("\n"))
#expect(parts[3] == lorem.contacts.phones.joined(separator: "\n"))
#expect(parts[4] == lorem.contacts.addresses.joined(separator: "\n"))
#expect(parts[5] == lorem.contacts.emails.joined(separator: "\n"))
}
}
@Test("contacts.json loads with streets, cities, first names and last names")
func namesLoad() {
let names = ContactNames.bundled
#expect(names.streets.count >= 50)
#expect(names.cities.count >= 30)
#expect(names.firstNames.count >= 50)
#expect(names.lastNames.count >= 50)
}
@Test("each contact group has 4 items and no duplicates")
func contactCounts() throws {
for lorem in try picks() {
for group in [lorem.contacts.phones, lorem.contacts.addresses, lorem.contacts.emails] {
#expect(group.count == 4)
#expect(Set(group).count == group.count)
}
}
}
@Test("phones are Dutch numbers, mobile and landline mixed")
func phones() throws {
let mobile = /^(06-[1-9]\d{7}|\+31 6 [1-9]\d{7})$/
let landline = /^(0[1-9]\d-[1-9]\d{6}|0[1-9]\d{2}-[1-9]\d{5})$/
for lorem in try picks() {
for phone in lorem.contacts.phones {
#expect(phone.wholeMatch(of: mobile) != nil || phone.wholeMatch(of: landline) != nil, "\(phone)")
}
#expect(lorem.contacts.phones.contains { $0.wholeMatch(of: mobile) != nil })
#expect(lorem.contacts.phones.contains { $0.wholeMatch(of: landline) != nil })
}
}
@Test("addresses are one line: street, number, postcode without SS, SD or SA, city")
func addresses() throws {
let pattern = /^.+ (\d{1,3})[a-z]?, (\d{4}) ([A-Z]{2}) .+$/
for lorem in try picks() {
for address in lorem.contacts.addresses {
let match = try #require(address.wholeMatch(of: pattern), "\(address)")
#expect((1...250).contains(Int(match.output.1) ?? 0))
#expect((1000...9999).contains(Int(match.output.2) ?? 0))
#expect(!["SS", "SD", "SA"].contains(String(match.output.3)))
}
}
}
@Test("e-mails are lower case ASCII on a reserved example domain")
func emails() throws {
let pattern = /^[a-z]+\.[a-z]+@example\.(nl|com|org)$/
for lorem in try picks() {
for email in lorem.contacts.emails {
#expect(email.wholeMatch(of: pattern) != nil, "\(email)")
}
}
}
@Test("a name with a tussenvoegsel or an accent becomes plain letters in an e-mail")
func mailPart() throws {
let one = ContactNames(streets: ["Kerkstraat"], cities: ["Gouda"], firstNames: ["Zoë"], lastNames: ["van den Berg"])
var generator = SeededGenerator(state: 3)
#expect(ContactSample.pick(from: one, using: &generator) == nil)
let two = ContactNames(streets: ["Kerkstraat"], cities: ["Gouda"], firstNames: ["Zoë", "René"], lastNames: ["van den Berg", "de Wit"])
let sample = try #require(ContactSample.pick(from: two, using: &generator))
let expected: Set = ["zoe.vandenberg", "zoe.dewit", "rene.vandenberg", "rene.dewit"]
#expect(sample.emails.allSatisfy { expected.contains($0.components(separatedBy: "@")[0]) })
}
@Test("the same seed gives the same pick; another seed gives another one")
func injectedGenerator() {
var first = SeededGenerator(state: 7)
var again = SeededGenerator(state: 7)
let sentences = LoremText.sentences
#expect(LoremText.pick(from: sentences, using: &first) == LoremText.pick(from: sentences, using: &again))
let others = (100..<110).map { seed -> LoremText? in
var generator = SeededGenerator(state: UInt64(seed))
return LoremText.pick(from: sentences, using: &generator)
}
#expect(Set(others.compactMap { $0?.text }).count > 1)
}
@Test("without a line of 16 words, or with too few lines, there is no pick")
func notEnough() {
let short = (1...20).map { "w\($0) two three." }
let words16 = Array(repeating: "w", count: 16).joined(separator: " ")
var generator = SeededGenerator(state: 1)
#expect(LoremText.pick(from: short, using: &generator) == nil)
#expect(LoremText.pick(from: [words16, "a.", "b."], using: &generator) == nil)
#expect(LoremText.pick(from: [words16] + short, using: &generator) != nil)
}
}