Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declarative.class_ #30

Open
Jelleas opened this issue Dec 19, 2023 · 0 comments
Open

declarative.class_ #30

Jelleas opened this issue Dec 19, 2023 · 0 comments

Comments

@Jelleas
Copy link
Owner

Jelleas commented Dec 19, 2023

Test classes:

from checkpy import * 

Lexicon = (declarative.class_("Lexicon")
    .params("word_length")
    .method("get_word").params().returnType(str)
)

Hangman = (declarative.class_("Hangman")
    .params("word", "number_of_guesses")
    .method("guess").params("letter").returnType(bool)
    .method("number_of_guesses").params().returnType(int)
    .method("won").params().returnType(bool)
)

Then for checks:

testGuess = test()(Hangman
    .init("hello", 5)
    .method("number_of_guesses").call().returns(5)
    .method("guess").call("a").returns(False)
    .method("number_of_guesses").call().returns(4)
    .method("guess").call("e").returns(True)
    .method("number_of_guesses").call().returns(4)    
)

testWon = passed(testGuess)(Hangman
    .init("a", 1)
    .method("won").call().returns(False)
    .method("guess").call("a").returns(True)
    .method("won").call().returns(True)
)

Mixing with @literal ?

globals_ = {"Hangman": Hangman, "Lexicon": Lexicon}


@literal(globals=globals_)
def testGuess():
     """number of guesses decreases after incorrect guess"""
     hangman = Hangman("hello", 5)
     assert hangman.number_of_guesses() == 5
     assert not hangman.guess("a")
     assert hangman.number_of_guesses() == 4
     assert hangman.guess("e")
     assert hangman.number_of_guesses() == 4

@passed(testGuess)
@literal(globals=globals_)
def testWon():
     """won() returns True after guessing the word"""
     hangman = Hangman("a", 1)
     assert not hangman.won()
     assert hangman.guess("a")
     assert hangman.won()
  • isinstance(Hangman, declarative.class_("Hangman")) should return True.

  • any attribute access on declarative.class_("Hangman") should be passed on to Hangman

  • globals should be inserted before import *, then overwritten after to ensure globals are set on import and not overwritten by the student. Like so:

    globalsCopy = deepcopy(globals)
    exec("from Hangman import *", globals=globals)
    globals.update(globalsCopy)
  • name mangling for methods defined on declarative.class_("Hangman") to prevent accidental calls from the student's code???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant