WOW. I finish read the doc about #:final That is so beatiful thing. Thank you to you three
Em dom, 17 de fev de 2019 às 22:18, Joao Pedro Abreu De Souza < [email protected]> escreveu: > Thanks for the feedback! > I will study this code and make more functional > > Em dom, 17 de fev de 2019 22:13, Philip McGrath <[email protected] > escreveu: > >> Well, if you call `(exit 0)`, your Racket program really will exit with >> code 0 without doing anything else: the function `exit` never returns, so, >> in your case, `execute` never returns, `refine-main` never returns, and >> Rackunit never gets a chance to look at any output it may or may not have >> written. >> >> In my opinion, the best thing to do is not really to call `(exit 0)` from >> `execute`. Here's one way to do it: >> #lang racket >> >> (module+ test >> (require rackunit)) >> >> ;; execute : string? -> (or/c #f (integer-in 0 255)) >> ;; Interprets the given string, perhaps causing side-effects. >> ;; Returns either: >> ;; - an integer between 0 and 255, inclusive, >> ;; meaning the program should exit immediately with that code >> ;; - #f, meaning the program can accept more commands >> (define (execute command) >> (match command >> [(pregexp #px"^exit\\s+(\\d+)$" >> (list _ (app string->number code))) >> code] >> [command >> (printf "command: ~v\n" command) >> #f])) >> >> ;; refine-main : -> (integer-in 0 255) >> ;; Interprets commands from (current-input-port), >> ;; perhaps causing side-effects. >> ;; Returns an exit code. >> (define (refine-main) >> (for/last ([command (in-lines)]) >> (define code >> (execute command)) >> #:final code >> (or code 0))) >> >> (module+ test >> (check-equal? >> (with-output-to-string >> (λ () >> (with-input-from-string "exit 0" >> refine-main))) >> "" >> "Consigo sair antes de comecar o tutorial")) >> >> When you really do want to exit as soon as `refine-main` returns, you can >> do that by just writing `(exit (refine-main))`. >> >> Technically this is not your only option. In fact, you may be wondering, >> if `exit` does what I say it does, why does your program not quit DrRacket? >> In fact, `exit` really calls the current exit handler >> <https://docs.racket-lang.org/reference/Exiting.html>, and DrRacket sets >> the exit handler, among other things, to prevent the programs it runs from >> interfering with each other or itself. You could likewise parameterize the >> exit handler around your call to define-main for testing purposes. >> >> However, I think this is a good example of why side-effects (like >> exiting) are less good for testing than returning values. >> >> -Philip >> >> >> On Sun, Feb 17, 2019 at 6:44 PM Joao Pedro Abreu De Souza < >> [email protected]> wrote: >> >>> (define (refine-main) >>> (display next-string) ;;feito >>> (set! next-command (shell-parser (my-read-line))) ;;TODO my-read-line >>> (execute next-command) ;;TODO execute >>> (refine-main)) ;;feito >>> >>> This is refine-main >>> >>> "shell-parser" is just a PEG-based parser, that return a struct >>> >>> "execute" in the case of reading a "exit 0" just call (exit 0) >>> >>> >>> Em dom, 17 de fev de 2019 às 16:07, Joao Pedro Abreu De Souza < >>> [email protected]> escreveu: >>> >>>> Well, there's no imediate return, i need to type exit 0 to finally the >>>> test works >>>> >>>> I am using read-line to read input. Today later I will post more >>>> code(of refine-main). >>>> >>>> Thanks >>>> >>>> Em dom, 17 de fev de 2019 10:37, Greg Hendershott < >>>> [email protected] escreveu: >>>> >>>>> What does it do? Does `check-equal?` show a failure message? If so, >>>>> what does it say? >>>>> >>>>> >>>>> I don't know what `refine-main` does, but this simple version works >>>>> for me: >>>>> >>>>> #lang racket >>>>> >>>>> (require rackunit) >>>>> >>>>> (define (refine-main) >>>>> (display (read-line))) ;echo >>>>> >>>>> (check-equal? >>>>> (with-output-to-string >>>>> (lambda () >>>>> (with-input-from-string "input" >>>>> (lambda () >>>>> (refine-main))))) >>>>> "input") >>>>> >>>>> On Sun, Feb 17, 2019 at 12:35 AM Joao Pedro Abreu De Souza >>>>> <[email protected]> wrote: >>>>> > >>>>> > Hi everyone. I have a test of a user-related function that I don't >>>>> can see why don't work : >>>>> > >>>>> > #lang racket >>>>> > >>>>> > (require rackunit) >>>>> > (require "../../main.rkt") >>>>> > >>>>> > >>>>> > >>>>> > >>>>> > (check-equal? >>>>> > (with-output-to-string (lambda () >>>>> > (with-input-from-string >>>>> > "exit 0" >>>>> > (lambda () (refine-main))))) >>>>> > "" >>>>> > "Consigo sair antes de comecar o tutorial") >>>>> > >>>>> > >>>>> > >>>>> > >>>>> > I think that this will call refine-main and, when refine-main do a >>>>> readline, will receive "exit 0" as string, and when display something, >>>>> will >>>>> appear as return of with-output-to-string, but this code don't work :( >>>>> > >>>>> > -- >>>>> > You received this message because you are subscribed to the Google >>>>> Groups "Racket Users" group. >>>>> > To unsubscribe from this group and stop receiving emails from it, >>>>> send an email to [email protected]. >>>>> > For more options, visit https://groups.google.com/d/optout. >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Racket Users" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to [email protected]. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Racket Users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

