goldforge 0.8.1

Library for handling file formats used by GoldSrc and related engines.
Documentation
// Copyright 2025-2026 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.

//! Tests for [`Builder`].

#![cfg(all(test, feature = "alloc"))]

use goldforge::wad::{Builder, LumpDescriptor};

#[test]
fn test_wad_encoder_flatten() {
	fn count_patches(b: &Builder, name: &str) -> usize {
		let wad = b.build();
		wad.lumps().count_patches(name)
	}

	let mut b = Builder::new_pwad();

	assert_eq!(count_patches(&b, "FLATME"), 0);

	b.flatten();

	assert_eq!(count_patches(&b, "FLATME"), 0);

	b.with_lump(LumpDescriptor { name: "FLATME", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "FLATME", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "FLATME", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "FLATME", ..Default::default() }).unwrap();

	assert_eq!(count_patches(&b, "FLATME"), 4);

	b.flatten();

	assert_eq!(count_patches(&b, "FLATME"), 1);

	b.flatten();

	assert_eq!(count_patches(&b, "FLATME"), 1);
}

#[test]
fn test_wad_encoder_long_lump_names() {
	let mut b = Builder::new_pwad();

	b.with_lump(LumpDescriptor { name: "01234567", ..Default::default() }).unwrap();

	b.with_lump(LumpDescriptor { name: "012345678", ..Default::default() }).unwrap_err();

	let mut b = Builder::new_wad2();

	b.with_lump(LumpDescriptor { name: "0123456789012345", ..Default::default() }).unwrap();

	b.with_lump(LumpDescriptor { name: "01234567890123456", ..Default::default() }).unwrap_err();
}

#[test]
fn test_wad_encoder_test0() {
	let mut b = Builder::new_pwad();

	b.with_lump(LumpDescriptor { name: "F_START", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "F_END",   ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "S_START", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "S_END",   ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "P_START", ..Default::default() }).unwrap();
	b.with_lump(LumpDescriptor { name: "P_END",   ..Default::default() }).unwrap();

	let wad = b.build();

	assert_eq!(wad.as_bytes(), TEST0);
}

pub(in crate::wad) static TEST0: &[u8] = &[
	0x50, 0x57, 0x41, 0x44, 0x06, 0x00, 0x00, 0x00,
	0x0C, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x46, 0x5F, 0x53, 0x54,
	0x41, 0x52, 0x54, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x46, 0x5F, 0x45, 0x4E,
	0x44, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x53, 0x54,
	0x41, 0x52, 0x54, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x53, 0x5F, 0x45, 0x4E,
	0x44, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x50, 0x5F, 0x53, 0x54,
	0x41, 0x52, 0x54, 0x00, 0x0C, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x50, 0x5F, 0x45, 0x4E,
	0x44, 0x00, 0x00, 0x00,
];