Skip to content

declarative.class_ #30

@Jelleas

Description

@Jelleas

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???

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions