Cotton code
142 lines · 5.5 KB · RustRaw
//! The Swift twin, held to the Rust declaration it is rendered from.//!//! There is no Swift compiler here, so these assert the two things a compiler//! would not catch anyway: that every kind and every field of the declaration//! reached the file, and that the tag is spelled the way the sealed bytes spell//! it. `pnpm ios:test` compiles the result.use super::*;use cotton_core::vault::payload::SecretKind;#[test]fn every_kind_arrives_as_a_case_a_struct_and_both_halves_of_the_coding() {    let swift = render_payload();    for kind in SecretKind::ALL {        let case = camel(kind.as_str());        let name = format!("Secret{}", pascal(kind.as_str()));        assert!(            swift.contains(&format!("    case {case} = \"{}\"", kind.as_str())),            "{kind} is missing from SecretKind"        );        assert!(swift.contains(&format!("public struct {name}:")), "{kind} has no struct");        assert!(swift.contains(&format!("    case {case}({name})")), "{kind} is not a payload");        assert!(            swift.contains(&format!("case .{case}: self = .{case}(try {name}(from: decoder))")),            "{kind} cannot be decoded"        );        assert!(            swift.contains(&format!("case .{case}(let payload): try payload.encode(to: encoder)")),            "{kind} cannot be encoded"        );    }}#[test]fn every_field_arrives_with_the_optionality_the_declaration_gives_it() {    let swift = render_payload();    for spec in SECRET_KINDS {        for field in spec.fields {            let member = camel(field.name);            let wanted = if field.required {                format!("    public var {member}: String\n")            } else {                format!("    public var {member}: String?\n")            };            assert!(                swift.contains(&wanted),                "{}.{} is missing, or its optionality does not match the declaration",                spec.kind,                field.name            );        }    }}#[test]fn a_field_whose_swift_name_is_not_its_json_key_carries_a_coding_row() {    // `id_type` is the one today. The assertion is over the DECLARATION rather    // than over that one field, so a kind that gains another snake_case field    // is covered the day it lands.    let swift = render_payload();    for spec in SECRET_KINDS {        for field in spec.fields {            if camel(field.name) == field.name {                continue;            }            assert!(                swift.contains(&format!("        case {} = \"{}\"", camel(field.name), field.name)),                "{}.{} would decode under the wrong JSON key",                spec.kind,                field.name            );        }    }    assert!(        swift.contains("case idType = \"id_type\""),        "the one field that needs a row today is gone — if that is deliberate, this line goes too"    );}#[test]fn the_primary_field_is_read_back_by_the_name_the_declaration_gives_it() {    let swift = render_payload();    for spec in SECRET_KINDS {        assert!(            swift.contains(&format!(                "    public var primaryField: String {{ {} }}",                camel(spec.primary)            )),            "{}: the primary field is not the one SECRET_KINDS names",            spec.kind        );    }}#[test]fn the_file_says_where_it_came_from_and_never_declares_a_kind_of_its_own() {    let swift = render_payload();    assert!(swift.starts_with("// GENERATED by `cotton-gen`"));    assert!(swift.contains("crates/cotton-core/src/vault/payload.rs"));    // The hint length is the Rust constant's, not a four spelled here.    assert!(swift.contains(&format!("String(value.suffix({}))", HINT_CHARS)));}#[test]fn a_name_becomes_swift_the_same_way_every_time() {    assert_eq!(pascal("api_key"), "ApiKey");    assert_eq!(pascal("note"), "Note");    assert_eq!(camel("api_key"), "apiKey");    assert_eq!(camel("id_type"), "idType");    assert_eq!(camel("notes"), "notes");}#[test]fn kotlin_payload_preserves_every_declared_kind_field_and_primary() {    let kotlin = render_kotlin_payload();    for spec in SECRET_KINDS {        let case = pascal(spec.kind.as_str());        assert!(kotlin.contains(&format!("{case}(\"{}\")", spec.kind.as_str())));        assert!(kotlin.contains(&format!("data class {case}(")));        assert!(kotlin.contains(&format!("SecretKind.{case} -> {case}(")));        assert!(kotlin.contains(&format!("primaryField: String get() = {}", camel(spec.primary))));        assert!(kotlin.contains(&format!("override fun toString(): String = \"SecretPayload.{case}(<redacted>)\"")));        for field in spec.fields {            let member = camel(field.name);            let key = field.name;            let (suffix, reader) = if field.required { ("", "requiredString") } else { ("? = null", "optionalString") };            assert!(kotlin.contains(&format!("val {member}: String{suffix},")));            assert!(kotlin.contains(&format!("{member} = value.{reader}(\"{key}\")")));            let encoding = if field.required {                format!("put(\"{key}\", {member})")            } else {                format!("{member}?.let {{ put(\"{key}\", it) }}")            };            assert!(kotlin.contains(&encoding));        }    }    assert!(kotlin.contains("put(\"kind\", kind.wireName)"));    assert!(kotlin.contains("fieldsJson().forEach { (key, value) -> put(key, value) }"));    assert!(kotlin.contains("package com.cottondb.android.generated"));    assert!(!kotlin.contains("import android"));}