diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 894dc5d..8c76986 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,3 +25,9 @@ jobs: with: command: fmt args: --all -- --check + + - name: Run tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --all diff --git a/rustico/tests/basic_syntax.rs b/rustico/tests/basic_syntax.rs new file mode 100644 index 0000000..08d765b --- /dev/null +++ b/rustico/tests/basic_syntax.rs @@ -0,0 +1,107 @@ +// Test basic syntax translations + +#[test] +fn test_basic_function() { + rustico::rustico! { + función prueba() -> e32 { + retorna 42; + } + } + + assert_eq!(prueba(), 42); +} + +#[test] +fn test_let_and_mut() { + rustico::rustico! { + función prueba_variables() -> e32 { + sea x = 5; + sea mutable y = 10; + y = y + x; + retorna y; + } + } + + assert_eq!(prueba_variables(), 15); +} + +#[test] +fn test_if_else() { + rustico::rustico! { + función prueba_condicional(val: e32) -> e32 { + si val > 10 { + retorna 1; + } sino { + retorna 0; + } + } + } + + assert_eq!(prueba_condicional(15), 1); + assert_eq!(prueba_condicional(5), 0); +} + +#[test] +fn test_match() { + rustico::rustico! { + función prueba_macheo(val: e32) -> e32 { + machea val { + 1 => 10, + 2 => 20, + _ => 0 + } + } + } + + assert_eq!(prueba_macheo(1), 10); + assert_eq!(prueba_macheo(2), 20); + assert_eq!(prueba_macheo(99), 0); +} + +#[test] +fn test_for_loop() { + rustico::rustico! { + función prueba_bucle_para() -> e32 { + sea mutable suma = 0; + para i de 0..5 { + suma = suma + i; + } + retorna suma; + } + } + + assert_eq!(prueba_bucle_para(), 10); +} + +#[test] +fn test_while_loop() { + rustico::rustico! { + función prueba_mientras() -> e32 { + sea mutable contador = 0; + mientras contador < 5 { + contador = contador + 1; + } + retorna contador; + } + } + + assert_eq!(prueba_mientras(), 5); +} + +#[test] +fn test_loop_and_break() { + rustico::rustico! { + función prueba_bucle_infinito() -> e32 { + sea mutable x = 0; + bucle { + x = x + 1; + si x == 10 { + rompe; + } + } + retorna x; + } + } + + assert_eq!(prueba_bucle_infinito(), 10); +} diff --git a/rustico/tests/keyword_alternatives.rs b/rustico/tests/keyword_alternatives.rs new file mode 100644 index 0000000..cb34177 --- /dev/null +++ b/rustico/tests/keyword_alternatives.rs @@ -0,0 +1,203 @@ +// Test various keyword alternatives + +#[test] +fn test_function_alternatives() { + rustico::rustico! { + // función with tilde + función con_tilde() -> e32 { 1 } + + // funcion without tilde + funcion sin_tilde() -> e32 { 2 } + } + + assert_eq!(con_tilde(), 1); + assert_eq!(sin_tilde(), 2); +} + +#[test] +fn test_let_alternatives() { + rustico::rustico! { + función prueba_deja() -> e32 { + deja x = 10; + x + } + + función prueba_sea() -> e32 { + sea y = 20; + y + } + } + + assert_eq!(prueba_deja(), 10); + assert_eq!(prueba_sea(), 20); +} + +#[test] +fn test_return_alternatives() { + rustico::rustico! { + función usa_retorna() -> e32 { + retorna 1; + } + + función usa_devuelve() -> e32 { + devuelve 2; + } + } + + assert_eq!(usa_retorna(), 1); + assert_eq!(usa_devuelve(), 2); +} + +#[test] +fn test_loop_alternatives() { + rustico::rustico! { + función usa_bucle() -> e32 { + sea mutable x = 0; + bucle { + x = x + 1; + si x == 5 { + rompe; + } + } + x + } + + función usa_ciclo() -> e32 { + sea mutable y = 0; + ciclo { + y = y + 1; + si y == 3 { + romper; + } + } + y + } + } + + assert_eq!(usa_bucle(), 5); + assert_eq!(usa_ciclo(), 3); +} + +#[test] +fn test_unwrap_alternatives() { + rustico::rustico! { + función usa_pelar() -> e32 { + Alguno(10).pelar() + } + + función usa_desenvolver() -> e32 { + Alguno(20).desenvolver() + } + + función usa_destapar() -> e32 { + Alguno(30).destapar() + } + } + + assert_eq!(usa_pelar(), 10); + assert_eq!(usa_desenvolver(), 20); + assert_eq!(usa_destapar(), 30); +} + +#[test] +fn test_self_alternatives() { + rustico::rustico! { + estructura UsaYo { + valor: e32, + } + + implementa UsaYo { + función con_yo(&yo) -> e32 { + yo.valor + } + } + + estructura UsaMismo { + valor: e32, + } + + implementa UsaMismo { + función con_mismo(&mismo) -> e32 { + mismo.valor + } + } + } + + let obj1 = UsaYo { valor: 100 }; + let obj2 = UsaMismo { valor: 200 }; + assert_eq!(obj1.con_yo(), 100); + assert_eq!(obj2.con_mismo(), 200); +} + +#[test] +fn test_pub_alternatives() { + rustico::rustico! { + púb función con_tilde() -> e32 { 1 } + publico función sin_tilde() -> e32 { 2 } + } + + assert_eq!(con_tilde(), 1); + assert_eq!(sin_tilde(), 2); +} + +#[test] +fn test_match_alternatives() { + rustico::rustico! { + función usa_machea(x: e32) -> e32 { + machea x { + 1 => 10, + _ => 0, + } + } + + función usa_encaja(x: e32) -> e32 { + encaja x { + 2 => 20, + _ => 0, + } + } + } + + assert_eq!(usa_machea(1), 10); + assert_eq!(usa_encaja(2), 20); +} + +#[test] +fn test_for_alternatives() { + rustico::rustico! { + función usa_para() -> e32 { + sea mutable suma = 0; + para i de 0..3 { + suma = suma + i; + } + suma + } + + función usa_por() -> e32 { + sea mutable suma = 0; + por i de 0..3 { + suma = suma + i; + } + suma + } + } + + assert_eq!(usa_para(), 3); + assert_eq!(usa_por(), 3); +} + +#[test] +fn test_expect_alternatives() { + rustico::rustico! { + función usa_confia() -> e32 { + Alguno(42).confia("debe existir") + } + + función usa_asume() -> e32 { + Alguno(43).asume("debe existir") + } + } + + assert_eq!(usa_confia(), 42); + assert_eq!(usa_asume(), 43); +} diff --git a/rustico/tests/option_result.rs b/rustico/tests/option_result.rs new file mode 100644 index 0000000..1a37b82 --- /dev/null +++ b/rustico/tests/option_result.rs @@ -0,0 +1,123 @@ +// Test Option and Result types + +#[test] +fn test_option_some() { + rustico::rustico! { + función retornar_alguno() -> Opcion { + Alguno(42) + } + } + + assert_eq!(retornar_alguno(), Some(42)); +} + +#[test] +fn test_option_none() { + rustico::rustico! { + función retornar_ninguno() -> Opcion { + Ninguno + } + } + + assert_eq!(retornar_ninguno(), None); +} + +#[test] +fn test_option_unwrap() { + rustico::rustico! { + función pelar_opcion() -> e32 { + sea opt = Alguno(100); + opt.pelar() + } + } + + assert_eq!(pelar_opcion(), 100); +} + +#[test] +fn test_option_unwrap_or() { + rustico::rustico! { + función pelar_o_defecto() -> e32 { + sea opt: Opcion = Ninguno; + opt.pelar_o(50) + } + } + + assert_eq!(pelar_o_defecto(), 50); +} + +#[test] +fn test_result_ok() { + rustico::rustico! { + función retornar_bien() -> Resultado { + Bien(42) + } + } + + assert_eq!(retornar_bien(), Ok(42)); +} + +#[test] +fn test_result_err() { + rustico::rustico! { + función retornar_error() -> Resultado { + Error(Cadena::desde("falló")) + } + } + + assert_eq!(retornar_error(), Err(String::from("falló"))); +} + +#[test] +fn test_result_unwrap() { + rustico::rustico! { + función pelar_resultado() -> e32 { + sea res: Resultado = Bien(200); + res.pelar() + } + } + + assert_eq!(pelar_resultado(), 200); +} + +#[test] +fn test_result_expect() { + rustico::rustico! { + función confiar_resultado() -> e32 { + sea res: Resultado = Bien(300); + res.confia("debería funcionar") + } + } + + assert_eq!(confiar_resultado(), 300); +} + +#[test] +fn test_option_match() { + rustico::rustico! { + función procesar_opcion(opt: Opcion) -> e32 { + machea opt { + Alguno(val) => val * 2, + Ninguno => 0 + } + } + } + + assert_eq!(procesar_opcion(Some(10)), 20); + assert_eq!(procesar_opcion(None), 0); +} + +#[test] +fn test_result_match() { + rustico::rustico! { + función procesar_resultado(res: Resultado) -> e32 { + machea res { + Bien(val) => val, + Error(_) => -1 + } + } + } + + assert_eq!(procesar_resultado(Ok(42)), 42); + assert_eq!(procesar_resultado(Err(String::from("error"))), -1); +} diff --git a/rustico/tests/stdlib.rs b/rustico/tests/stdlib.rs new file mode 100644 index 0000000..41e6302 --- /dev/null +++ b/rustico/tests/stdlib.rs @@ -0,0 +1,71 @@ +// Test standard library imports + +#[test] +fn test_std_imports() { + rustico::rustico! { + usar estd::colecciones::Diccionario; + + función crear_diccionario() -> Diccionario { + sea mutable mapa = Diccionario::nuevo(); + mapa.insertar(Cadena::desde("uno"), 1); + mapa.insertar(Cadena::desde("dos"), 2); + mapa + } + } + + let mapa = crear_diccionario(); + assert_eq!(mapa.get("uno"), Some(&1)); + assert_eq!(mapa.get("dos"), Some(&2)); +} + +#[test] +fn test_hashset_imports() { + rustico::rustico! { + usar estd::colecciones::Conjunto; + + función crear_conjunto() -> Conjunto { + sea mutable set = Conjunto::nuevo(); + set.insertar(1); + set.insertar(2); + set.insertar(3); + set + } + } + + let set = crear_conjunto(); + assert!(set.contains(&1)); + assert!(set.contains(&2)); + assert!(set.contains(&3)); + assert!(!set.contains(&4)); +} + +#[test] +fn test_vec_type() { + rustico::rustico! { + función crear_vector() -> Vec { + sea mutable v = Vec::nuevo(); + v.push(1); + v.push(2); + v.push(3); + v + } + } + + let v = crear_vector(); + assert_eq!(v.len(), 3); + assert_eq!(v[0], 1); + assert_eq!(v[1], 2); + assert_eq!(v[2], 3); +} + +#[test] +fn test_box_type() { + rustico::rustico! { + función crear_caja() -> Caja { + Caja::nuevo(42) + } + } + + let boxed = crear_caja(); + assert_eq!(*boxed, 42); +} diff --git a/rustico/tests/struct_impl.rs b/rustico/tests/struct_impl.rs new file mode 100644 index 0000000..a005413 --- /dev/null +++ b/rustico/tests/struct_impl.rs @@ -0,0 +1,95 @@ +// Test struct and impl + +#[test] +fn test_struct_definition() { + rustico::rustico! { + estructura Punto { + x: e32, + y: e32, + } + + función crear_punto() -> Punto { + Punto { x: 10, y: 20 } + } + } + + let p = crear_punto(); + assert_eq!(p.x, 10); + assert_eq!(p.y, 20); +} + +#[test] +fn test_impl_methods() { + rustico::rustico! { + estructura Rectangulo { + ancho: e32, + alto: e32, + } + + implementa Rectangulo { + función nuevo(ancho: e32, alto: e32) -> Rectangulo { + Rectangulo { ancho, alto } + } + + función area(&yo) -> e32 { + yo.ancho * yo.alto + } + } + } + + let rect = Rectangulo::new(5, 10); + assert_eq!(rect.area(), 50); +} + +#[test] +fn test_pub_struct() { + rustico::rustico! { + púb estructura Publico { + púb campo: e32, + } + + función crear_publico() -> Publico { + Publico { campo: 42 } + } + } + + let p = crear_publico(); + assert_eq!(p.campo, 42); +} + +#[test] +fn test_enum_definition() { + rustico::rustico! { + enumerado Color { + Rojo, + Verde, + Azul, + } + + función obtener_color() -> Color { + Color::Rojo + } + } + + let _ = obtener_color(); +} + +#[test] +fn test_enum_with_values() { + rustico::rustico! { + enumerado Mensaje { + Texto(Cadena), + Numero(e32), + } + + función procesar_mensaje(msg: Mensaje) -> e32 { + machea msg { + Mensaje::Numero(n) => n, + Mensaje::Texto(_) => 0, + } + } + } + + assert_eq!(procesar_mensaje(Mensaje::Numero(42)), 42); + assert_eq!(procesar_mensaje(Mensaje::Texto(String::from("hola"))), 0); +} diff --git a/rustico/tests/traits.rs b/rustico/tests/traits.rs new file mode 100644 index 0000000..71dd8ae --- /dev/null +++ b/rustico/tests/traits.rs @@ -0,0 +1,81 @@ +// Test trait definitions and implementations + +#[test] +fn test_trait_definition() { + rustico::rustico! { + rasgo Saludador { + función saludar(&yo) -> Cadena; + } + + estructura Persona { + nombre: Cadena, + } + + implementa Saludador para Persona { + función saludar(&yo) -> Cadena { + Cadena::desde("¡Hola!") + } + } + } + + let persona = Persona { + nombre: String::from("Juan"), + }; + assert_eq!(persona.saludar(), "¡Hola!"); +} + +#[test] +fn test_trait_with_default() { + rustico::rustico! { + rasgo Calculador { + función calcular(&yo) -> e32 { + 42 + } + } + + estructura MiCalculador; + + implementa Calculador para MiCalculador {} + } + + let calc = MiCalculador; + assert_eq!(calc.calcular(), 42); +} + +#[test] +fn test_self_type() { + rustico::rustico! { + estructura Constructor; + + implementa Constructor { + función nuevo() -> Mismo { + Constructor + } + } + } + + let _ = Constructor::new(); +} + +#[test] +fn test_self_reference() { + rustico::rustico! { + estructura Contador { + valor: e32, + } + + implementa Contador { + función incrementar(&mutable yo) { + yo.valor = yo.valor + 1; + } + + función obtener_valor(&mismo) -> e32 { + mismo.valor + } + } + } + + let mut contador = Contador { valor: 0 }; + contador.incrementar(); + assert_eq!(contador.obtener_valor(), 1); +} diff --git a/rustico/tests/types.rs b/rustico/tests/types.rs new file mode 100644 index 0000000..467677f --- /dev/null +++ b/rustico/tests/types.rs @@ -0,0 +1,58 @@ +// Test type translations + +#[test] +fn test_signed_integers() { + rustico::rustico! { + función prueba_e8() -> e8 { 127 } + función prueba_e16() -> e16 { 32767 } + función prueba_e32() -> e32 { 2147483647 } + función prueba_e64() -> e64 { 9223372036854775807 } + } + + assert_eq!(prueba_e8(), 127i8); + assert_eq!(prueba_e16(), 32767i16); + assert_eq!(prueba_e32(), 2147483647i32); + assert_eq!(prueba_e64(), 9223372036854775807i64); +} + +#[test] +fn test_unsigned_integers() { + rustico::rustico! { + función prueba_n8() -> n8 { 255 } + función prueba_n16() -> n16 { 65535 } + función prueba_n32() -> n32 { 4294967295 } + función prueba_n64() -> n64 { 18446744073709551615 } + } + + assert_eq!(prueba_n8(), 255u8); + assert_eq!(prueba_n16(), 65535u16); + assert_eq!(prueba_n32(), 4294967295u32); + assert_eq!(prueba_n64(), 18446744073709551615u64); +} + +#[test] +fn test_bool_type() { + rustico::rustico! { + función es_verdadero() -> bul { + verdad + } + + función es_falso() -> bul { + falso + } + } + + assert_eq!(es_verdadero(), true); + assert_eq!(es_falso(), false); +} + +#[test] +fn test_string_type() { + rustico::rustico! { + función crear_cadena() -> Cadena { + Cadena::desde("hola") + } + } + + assert_eq!(crear_cadena(), "hola"); +}