facet_generate 0.16.0

Generate Swift, Kotlin and TypeScript from types annotated with `#[derive(Facet)]`
Documentation
#![cfg(feature = "java")]
#![expect(deprecated, reason = "tests for the deprecated Java generator")]
// Copyright (c) Facebook, Inc. and its affiliates
// SPDX-License-Identifier: MIT OR Apache-2.0

pub mod common;

use facet_generate::generation::{CodeGeneratorConfig, Encoding, java};
use std::{collections::BTreeMap, process::Command};
use tempfile::{TempDir, tempdir};

fn test_that_java_code_compiles_with_config(
    config: &CodeGeneratorConfig,
) -> (TempDir, std::path::PathBuf) {
    let registry = common::get_registry();
    let dir = tempdir().unwrap();

    let generator = java::CodeGenerator::new(config);
    generator
        .write_source_files(dir.path().to_path_buf(), &registry)
        .unwrap();

    let paths = std::iter::empty()
        .chain(std::fs::read_dir("runtime/java/com/novi/serde").unwrap())
        .chain(std::fs::read_dir("runtime/java/com/novi/bincode").unwrap())
        .chain(std::fs::read_dir(dir.path().join("testing")).unwrap())
        .map(|e| e.unwrap().path());
    let status = Command::new("javac")
        .arg("-Xlint")
        .arg("-d")
        .arg(dir.path())
        .args(paths)
        .status()
        .unwrap();
    assert!(status.success());

    let path = dir.path().join("testing");
    (dir, path)
}

#[test]
fn test_that_java_code_compiles() {
    let config = CodeGeneratorConfig::new("testing".to_string());
    test_that_java_code_compiles_with_config(&config);
}

#[test]
fn test_that_java_code_compiles_without_serialization() {
    let config = CodeGeneratorConfig::new("testing".to_string());
    test_that_java_code_compiles_with_config(&config);
}

#[test]
fn test_that_java_code_compiles_with_bincode() {
    let config = CodeGeneratorConfig::new("testing".to_string()).with_encoding(Encoding::Bincode);
    test_that_java_code_compiles_with_config(&config);
}

#[test]
fn test_that_java_code_compiles_with_comments() {
    let comments = vec![(
        vec!["testing".to_string(), "SerdeData".to_string()],
        "Some\ncomments".to_string(),
    )]
    .into_iter()
    .collect();
    let config = CodeGeneratorConfig::new("testing".to_string()).with_comments(comments);

    let (_dir, path) = test_that_java_code_compiles_with_config(&config);

    // Comment was correctly generated.
    let content = std::fs::read_to_string(path.join("SerdeData.java")).unwrap();
    assert!(content.contains(
        r"
/**
 * Some
 * comments
 */
"
    ));
}

#[test]
fn test_java_code_with_external_definitions() {
    let registry = common::get_registry();
    let dir = tempdir().unwrap();

    // (wrongly) Declare TraitHelpers as external.
    let mut definitions = BTreeMap::new();
    definitions.insert("foo".to_string(), vec!["TraitHelpers".to_string()]);
    let config = CodeGeneratorConfig::new("testing".to_string())
        .with_external_definitions(definitions)
        .with_encoding(Encoding::Bincode);
    let generator = java::CodeGenerator::new(&config);

    generator
        .write_source_files(dir.path().to_path_buf(), &registry)
        .unwrap();

    // References were updated.
    let content = std::fs::read_to_string(dir.path().join("testing/SerdeData.java")).unwrap();
    assert!(content.contains("foo.TraitHelpers."));
}

#[test]
fn test_that_java_code_compiles_with_custom_code() {
    let comments = vec![(
        vec!["testing".to_string(), "SerdeData".to_string()],
        "SerdeData me() {{ return this; }}".to_string(),
    )]
    .into_iter()
    .collect();
    let config = CodeGeneratorConfig::new("testing".to_string()).with_comments(comments);

    let (_dir, path) = test_that_java_code_compiles_with_config(&config);

    // Comment was correctly generated.
    let content = std::fs::read_to_string(path.join("SerdeData.java")).unwrap();
    assert!(content.contains("me()"));
}