@@ -18,7 +18,7 @@ use crate::parser::parser_common::{
1818 RIGHT_PAREN , SEMICOLON_CHAR , VAL_KEYWORD , VAR_KEYWORD , WHILE_KEYWORD ,
1919} ;
2020use crate :: parser:: parser_expr:: parse_expression;
21- use crate :: parser:: parser_type:: parse_type; // TODO: Check if needed
21+ use crate :: parser:: parser_type:: parse_type;
2222
2323pub fn parse_statement ( input : & str ) -> IResult < & str , Statement > {
2424 alt ( (
@@ -489,146 +489,155 @@ mod tests {
489489 assert_eq ! ( parsed, expected) ;
490490 }
491491
492- #[ test]
493- fn test_parse_test_function_definition_statement_valid ( ) {
494- let input = "test test_example(): x = 1; end" ;
495- let expected = Statement :: TestDef ( Function {
496- name : "test_example" . to_string ( ) ,
497- kind : Type :: TVoid ,
498- params : vec ! [ ] ,
499- body : Some ( Box :: new ( Statement :: Block ( vec ! [ Statement :: Assignment (
500- "x" . to_string( ) ,
501- Box :: new( Expression :: CInt ( 1 ) ) ,
502- ) ] ) ) ) ,
503- } ) ;
504- let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
505- assert_eq ! ( parsed, expected) ;
506- }
507-
508- #[ test]
509- fn test_parse_test_function_definition_statement_valid_multiple_statements ( ) {
510- let input = r#"test test_example():
511- x = 1;
512- y = 2;
513- assert(x == 1, "x deveria ser 1");
514- end"# ;
515- let expected = Statement :: TestDef ( Function {
516- name : "test_example" . to_string ( ) ,
517- kind : Type :: TVoid ,
518- params : vec ! [ ] ,
519- body : Some ( Box :: new ( Statement :: Block ( vec ! [
520- Statement :: Assignment ( "x" . to_string( ) , Box :: new( Expression :: CInt ( 1 ) ) ) ,
521- Statement :: Assignment ( "y" . to_string( ) , Box :: new( Expression :: CInt ( 2 ) ) ) ,
522- Statement :: Assert (
523- Box :: new( Expression :: EQ (
524- Box :: new( Expression :: Var ( "x" . to_string( ) ) ) ,
525- Box :: new( Expression :: CInt ( 1 ) ) ,
526- ) ) ,
527- Box :: new( Expression :: CString ( "x deveria ser 1" . to_string( ) ) ) ,
528- ) ,
529- ] ) ) ) ,
530- } ) ;
531- let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
532- assert_eq ! ( parsed, expected) ;
492+ //TODO: Apresentar Parser de TestDef (Testes)
493+ mod testdef_tests {
494+ use super :: * ;
495+
496+ #[ test]
497+ fn test_parse_test_function_definition_statement_valid ( ) {
498+ let input = "test test_example(): x = 1; end" ;
499+ let expected = Statement :: TestDef ( Function {
500+ name : "test_example" . to_string ( ) ,
501+ kind : Type :: TVoid ,
502+ params : vec ! [ ] ,
503+ body : Some ( Box :: new ( Statement :: Block ( vec ! [ Statement :: Assignment (
504+ "x" . to_string( ) ,
505+ Box :: new( Expression :: CInt ( 1 ) ) ,
506+ ) ] ) ) ) ,
507+ } ) ;
508+ let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
509+ assert_eq ! ( parsed, expected) ;
510+ }
511+
512+ #[ test]
513+ fn test_parse_test_function_definition_statement_valid_multiple_statements ( ) {
514+ let input = r#"test test_example():
515+ x = 1;
516+ y = 2;
517+ assert(x == 1, "x deveria ser 1");
518+ end"# ;
519+ let expected = Statement :: TestDef ( Function {
520+ name : "test_example" . to_string ( ) ,
521+ kind : Type :: TVoid ,
522+ params : vec ! [ ] ,
523+ body : Some ( Box :: new ( Statement :: Block ( vec ! [
524+ Statement :: Assignment ( "x" . to_string( ) , Box :: new( Expression :: CInt ( 1 ) ) ) ,
525+ Statement :: Assignment ( "y" . to_string( ) , Box :: new( Expression :: CInt ( 2 ) ) ) ,
526+ Statement :: Assert (
527+ Box :: new( Expression :: EQ (
528+ Box :: new( Expression :: Var ( "x" . to_string( ) ) ) ,
529+ Box :: new( Expression :: CInt ( 1 ) ) ,
530+ ) ) ,
531+ Box :: new( Expression :: CString ( "x deveria ser 1" . to_string( ) ) ) ,
532+ ) ,
533+ ] ) ) ) ,
534+ } ) ;
535+ let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
536+ assert_eq ! ( parsed, expected) ;
537+ }
538+
539+ #[ test]
540+ fn test_parse_test_function_definition_statement_with_spaces ( ) {
541+ let input = "test test_spaces( ): x = 2; end" ;
542+ let expected = Statement :: TestDef ( Function {
543+ name : "test_spaces" . to_string ( ) ,
544+ kind : Type :: TVoid ,
545+ params : vec ! [ ] ,
546+ body : Some ( Box :: new ( Statement :: Block ( vec ! [ Statement :: Assignment (
547+ "x" . to_string( ) ,
548+ Box :: new( Expression :: CInt ( 2 ) ) ,
549+ ) ] ) ) ) ,
550+ } ) ;
551+ let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
552+ assert_eq ! ( parsed, expected) ;
553+ }
554+
555+ #[ test]
556+ fn test_parse_test_function_definition_statement_args ( ) {
557+ let input = "test test_with_args(x: Int, y: Int): x = y; end" ;
558+
559+ // O parser deve falhar, pois funções de teste não podem ter argumentos.
560+ let parsed = parse_test_function_definition_statement ( input) ;
561+
562+ assert ! (
563+ parsed. is_err( ) ,
564+ "Funções de teste com argumentos devem ser rejeitadas"
565+ ) ;
566+ }
567+
568+ #[ test]
569+ fn test_parse_test_function_definition_statement_invalid_return ( ) {
570+ let input = "test test_with_invalid_return() -> Int: x = 2; end" ;
571+
572+ // O parser deve falhar, pois funções de teste não podem ter argumentos.
573+ let parsed = parse_test_function_definition_statement ( input) ;
574+
575+ assert ! (
576+ parsed. is_err( ) ,
577+ "Funções de teste não devem especificar tipo de retorno"
578+ ) ;
579+ }
580+
581+ #[ test]
582+ fn test_parse_test_function_definition_statement_valid_return_type ( ) {
583+ let input = "test test_with_valid_return() -> Boolean: x = 2; end" ;
584+
585+ let parsed = parse_test_function_definition_statement ( input) ;
586+ assert ! (
587+ parsed. is_err( ) ,
588+ "Funções de teste não devem especificar tipo de retorno"
589+ ) ;
590+ }
533591 }
534-
535- # [ test ]
536- fn test_parse_test_function_definition_statement_with_spaces ( ) {
537- let input = "test test_spaces( ): x = 2; end" ;
538- let expected = Statement :: TestDef ( Function {
539- name : "test_spaces" . to_string ( ) ,
540- kind : Type :: TVoid ,
541- params : vec ! [ ] ,
542- body : Some ( Box :: new ( Statement :: Block ( vec ! [ Statement :: Assignment (
543- "x" . to_string ( ) ,
592+
593+ //TODO: Apresentar Parser de Asserts (Testes)
594+ mod assignment_tests {
595+ use super :: * ;
596+
597+ # [ test ]
598+ fn test_parse_asserteq_statement ( ) {
599+ let input = "asserteq(1, 2, \" msg \" )" ;
600+ let expected = Statement :: AssertEQ (
601+ Box :: new ( Expression :: CInt ( 1 ) ) ,
544602 Box :: new ( Expression :: CInt ( 2 ) ) ,
545- ) ] ) ) ) ,
546- } ) ;
547- let parsed = parse_test_function_definition_statement ( input) . unwrap ( ) . 1 ;
548- assert_eq ! ( parsed, expected) ;
549- }
550-
551- #[ test]
552- fn test_parse_test_function_definition_statement_args ( ) {
553- let input = "test test_with_args(x: Int, y: Int): x = y; end" ;
554-
555- // O parser deve falhar, pois funções de teste não podem ter argumentos.
556- let parsed = parse_test_function_definition_statement ( input) ;
557-
558- assert ! (
559- parsed. is_err( ) ,
560- "Funções de teste com argumentos devem ser rejeitadas"
561- ) ;
603+ Box :: new ( Expression :: CString ( "msg" . to_string ( ) ) ) ,
604+ ) ;
605+ let parsed = parse_asserteq_statement ( input) . unwrap ( ) . 1 ;
606+ assert_eq ! ( parsed, expected) ;
607+ }
608+
609+ #[ test]
610+ fn test_parse_assertneq_statement ( ) {
611+ let input = "assertneq(3, 4, \" fail\" )" ;
612+ let expected = Statement :: AssertNEQ (
613+ Box :: new ( Expression :: CInt ( 3 ) ) ,
614+ Box :: new ( Expression :: CInt ( 4 ) ) ,
615+ Box :: new ( Expression :: CString ( "fail" . to_string ( ) ) ) ,
616+ ) ;
617+ let parsed = parse_assertneq_statement ( input) . unwrap ( ) . 1 ;
618+ assert_eq ! ( parsed, expected) ;
619+ }
620+
621+ #[ test]
622+ fn test_parse_asserttrue_statement ( ) {
623+ let input = "asserttrue(True, \" should be true\" )" ;
624+ let expected = Statement :: AssertTrue (
625+ Box :: new ( Expression :: CTrue ) ,
626+ Box :: new ( Expression :: CString ( "should be true" . to_string ( ) ) ) ,
627+ ) ;
628+ let parsed = parse_asserttrue_statement ( input) . unwrap ( ) . 1 ;
629+ assert_eq ! ( parsed, expected) ;
630+ }
631+
632+ #[ test]
633+ fn test_parse_assertfalse_statement ( ) {
634+ let input = "assertfalse(False, \" should be false\" )" ;
635+ let expected = Statement :: AssertFalse (
636+ Box :: new ( Expression :: CFalse ) ,
637+ Box :: new ( Expression :: CString ( "should be false" . to_string ( ) ) ) ,
638+ ) ;
639+ let parsed = parse_assertfalse_statement ( input) . unwrap ( ) . 1 ;
640+ assert_eq ! ( parsed, expected) ;
641+ }
562642 }
563-
564- #[ test]
565- fn test_parse_test_function_definition_statement_invalid_return ( ) {
566- let input = "test test_with_invalid_return() -> Int: x = 2; end" ;
567-
568- // O parser deve falhar, pois funções de teste não podem ter argumentos.
569- let parsed = parse_test_function_definition_statement ( input) ;
570-
571- assert ! (
572- parsed. is_err( ) ,
573- "Funções de teste não devem especificar tipo de retorno"
574- ) ;
575- }
576-
577- #[ test]
578- fn test_parse_test_function_definition_statement_valid_return_type ( ) {
579- let input = "test test_with_valid_return() -> Boolean: x = 2; end" ;
580-
581- let parsed = parse_test_function_definition_statement ( input) ;
582- assert ! (
583- parsed. is_err( ) ,
584- "Funções de teste não devem especificar tipo de retorno"
585- ) ;
586- }
587- //TODO: Implementar testes para os ASSERTS
588-
589- #[ test]
590- fn test_parse_asserteq_statement ( ) {
591- let input = "asserteq(1, 2, \" msg\" )" ;
592- let expected = Statement :: AssertEQ (
593- Box :: new ( Expression :: CInt ( 1 ) ) ,
594- Box :: new ( Expression :: CInt ( 2 ) ) ,
595- Box :: new ( Expression :: CString ( "msg" . to_string ( ) ) ) ,
596- ) ;
597- let parsed = parse_asserteq_statement ( input) . unwrap ( ) . 1 ;
598- assert_eq ! ( parsed, expected) ;
599- }
600-
601- #[ test]
602- fn test_parse_assertneq_statement ( ) {
603- let input = "assertneq(3, 4, \" fail\" )" ;
604- let expected = Statement :: AssertNEQ (
605- Box :: new ( Expression :: CInt ( 3 ) ) ,
606- Box :: new ( Expression :: CInt ( 4 ) ) ,
607- Box :: new ( Expression :: CString ( "fail" . to_string ( ) ) ) ,
608- ) ;
609- let parsed = parse_assertneq_statement ( input) . unwrap ( ) . 1 ;
610- assert_eq ! ( parsed, expected) ;
611- }
612-
613- #[ test]
614- fn test_parse_asserttrue_statement ( ) {
615- let input = "asserttrue(True, \" should be true\" )" ;
616- let expected = Statement :: AssertTrue (
617- Box :: new ( Expression :: CTrue ) ,
618- Box :: new ( Expression :: CString ( "should be true" . to_string ( ) ) ) ,
619- ) ;
620- let parsed = parse_asserttrue_statement ( input) . unwrap ( ) . 1 ;
621- assert_eq ! ( parsed, expected) ;
622- }
623-
624- #[ test]
625- fn test_parse_assertfalse_statement ( ) {
626- let input = "assertfalse(False, \" should be false\" )" ;
627- let expected = Statement :: AssertFalse (
628- Box :: new ( Expression :: CFalse ) ,
629- Box :: new ( Expression :: CString ( "should be false" . to_string ( ) ) ) ,
630- ) ;
631- let parsed = parse_assertfalse_statement ( input) . unwrap ( ) . 1 ;
632- assert_eq ! ( parsed, expected) ;
633- }
634- }
643+ }
0 commit comments